RegressionTests__JSONTests.st
author Claus Gittinger <cg@exept.de>
Tue, 25 Feb 2020 17:19:18 +0100
changeset 2581 e889c17eef8f
parent 2578 0a1480d503cc
permissions -rw-r--r--
s

"{ Encoding: utf8 }"

"{ Package: 'stx:goodies/regression' }"

"{ NameSpace: RegressionTests }"

TestCase subclass:#JSONTests
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'tests-Regression-Files and Encodings'
!

!JSONTests class methodsFor:'documentation'!

documentation
"
    Notice that there are frameworks for JSON:
        goodies/json
    and: 
        JSONReader/JSONPrinter from goodies/communications.

    This tests the second one.
"
! !

!JSONTests class methodsFor:'queries'!

coveredClassNames
    "These classes can be instrumented for coverage analysis,
     before running the suite to provide coverage analysis/report"

    ^ #( JSONReader JSONPrinter )

    "Created: / 06-07-2011 / 21:27:03 / cg"
    "Modified: / 21-03-2019 / 22:28:33 / Claus Gittinger"
! !

!JSONTests methodsFor:'setup'!

setUp
    JSONPrinter isNil ifTrue:[
        Smalltalk loadPackage:'stx:goodies/communication'
    ].
! !

!JSONTests methodsFor:'tests'!

test01_simple
    
    self assert:(JSONPrinter toJSON:true) = 'true'.
    self assert:(JSONPrinter toJSON:false) = 'false'.
    self assert:(JSONPrinter toJSON:nil) = 'null'.

    self assert:(JSONPrinter toJSON:123) = '123'.
    self assert:(JSONPrinter toJSON:0) = '0'.
    self assert:(JSONPrinter toJSON:-0) = '0'.
    self assert:(JSONPrinter toJSON:-1) = '-1'.
    self assert:(JSONPrinter toJSON:-123) = '-123'.

    self assert:(JSONPrinter toJSON:123.0) = '123.0'.
    self assert:(JSONPrinter toJSON:1e3) = '1000.0'.
    self assert:(JSONPrinter toJSON:10e3) = '10000.0'.

    self assert:(JSONPrinter toJSON:'hello') = '"hello"'.
    self assert:(JSONPrinter toJSON:'') = '""'.
    self assert:(JSONPrinter toJSON:'"a"') = '"\"a\""'.
!

test02_arrays
    
    self assert:(JSONPrinter toJSON:#()) = '[]'.
    self assert:(JSONPrinter toJSON:#(1 2 3)) = '[1,2,3]'.
    self assert:(JSONPrinter toJSON:#(true false -1 'a')) = '[true,false,-1,"a"]'.
    self assert:(JSONPrinter toJSON:#( (1) ('a') (nil))) = '[[1],["a"],[null]]'.

    self assert:(JSONReader fromJSON:'[]') = #().
    self assert:(JSONReader fromJSON:'[1,2,3]') = #(1 2 3).
    self assert:(JSONReader fromJSON:'[true,false,-1,"a"]') = #(true false -1 'a').
    self assert:(JSONReader fromJSON:'[[1],["a"],[null]]') = #( (1) ('a') (nil)).
!

test03_encodeDecode
    "/ self assert:(JSONReader fromJSON:'"\uD83C\uDFBC"') = (Character value:0x1F3BC) asString.

    #(
        true false nil
        123 0 -1 -123 
        123.0 10e4 1e5
        -123.0 -10e4 -1e5
        -123.0e+5 -123.0e-5
        ''
        'a'
        'hello'
        c'hello\nworld'
        c'hello\tworld'
        '"'
        '""'
        'a"b"c'
        ()
        (1)
        (1 2 3)
        ('a' 2.0 (true false nil) 3)
    ) do:[:each |
        |encoding decoded|

        encoding := JSONPrinter encode:each.
        decoded := JSONReader decode:encoding.
        self assert:((decoded = each) or:[decoded isFloat and:[decoded isAlmostEqualTo:each nEpsilon:2]])
    ].

    self should:[ (JSONReader fromJSON:'[1,2,]') ] raise:JSONConversionError.
    self should:[ (JSONReader fromJSON:'[,]') ] raise:JSONConversionError.
    self should:[ (JSONReader fromJSON:'{ "foo": "bla", }') ] raise:JSONConversionError.
    self should:[ (JSONReader fromJSON:'0x123') ] raise:JSONConversionError.
!

test04_encodeDecodeJSON5
    #(
        '123' 123
        '0'   0 
        '-1'  -1
        '-123' -123 
        '0x123' 0x123
        '0x0'   0
        '-0x1'  -1
        '-0x123' -0x123 
        '123.0' 123.0
        '10e4'  10e4
        '1e5'   1e5
        '-123.0' -123.0 
        '-10e4'  -10e4 
        '-1e5'   -1e5
        '-123.0e+5' -123.0e+5 
        '-123.0e-5' -123.0e-5
        '.123' 0.123

        '[1,2,]' #(1 2)
    ) pairWiseDo:[:json :expected |
        |decoded|

        decoded := JSONReader fromJSON5:json.
        self assert:((decoded = expected) or:[decoded isFloat and:[decoded isAlmostEqualTo:expected nEpsilon:2]])
    ].

    self should:[ (JSONReader fromJSON5:'[,]') ] raise:JSONConversionError.
    self shouldnt:[ (JSONReader fromJSON5:'{ "foo": "bla", }') ] raise:JSONConversionError.
!

test05_JSON5
    |j o|

    "/ self assert:(JSONReader fromJSON5:'''\uD83C\uDFBC''') = (Character value:0x1F3BC) asString.
    "/ self assert:(JSONReader fromJSON5:'"\uD83C\uDFBC"') = (Character value:0x1F3BC) asString.

    j := '
{
  // comments
  unquoted: ''and you can quote me on that'',
  singleQuotes: ''I can use "double quotes" here'',
  lineBreaks: "Look, Mom!! \
No \\n''s!!",
  hexadecimal: 0xdecaf,
  leadingDecimalPoint: .8675309, andTrailing: 8675309.,
  positiveSign: +1,
  trailingComma: ''in objects'', andIn: [''arrays'',],
  "backwardsCompatible": "with JSON",
}
'.
    self should:[ (JSONReader fromJSON:j) ] raise:JSONConversionError.

    o := JSONReader fromJSON5:j.

    #(
        unquoted 'and you can quote me on that' 
        singleQuotes 'I can use "double quotes" here' 
        lineBreaks 'Look, Mom!! No \n''s!!'
        hexadecimal 0xdecaf
        leadingDecimalPoint 0.8675309
        andTrailing 8675309.0
        positiveSign 1
        trailingComma 'in objects'
        andIn #('arrays')
        backwardsCompatible 'with JSON'
    ) pairWiseDo:[:key :expected |
        |got|

        got := o at:key.
        self assert:((got = expected) or:[got isFloat and:[got isAlmostEqualTo:expected nEpsilon:2]])
    ].
!

test10_typed
    
    self assert:((JSONPrinter new typeInfoFormat:#stx) 
                    encode:(10@20)) = '{"@__type__":"Point","@__value__":{"x":10,"y":20}}'.
    self assert:((JSONReader new typeInfoFormat:#stx) 
                    decode:'{"@__type__":"Point","@__value__":{"x":10,"y":20}}')
                = (10@20).

"/    self assert:((JSONPrinter new typeInfoFormat:#stx) 
"/                    encode:(Color red)) = '{"@type":"Point","@value":{"x":10,"y":20}}'.
"/    self assert:((JSONReader new typeInfoFormat:#stx) 
"/                    decode:'{"@type":"Point","@value":{"x":10,"y":20}}')
"/                = (10@20).
!

test11_typedV2
    
    self assert:((JSONPrinter new typeInfoFormat:#stx2) 
                    encode:(10@20)) = '{"@__type__":"Point","x":10,"y":20}'.
    self assert:((JSONReader new typeInfoFormat:#stx2) 
                    decode:'{"@__type__":"Point","x":10,"y":20}')
                = (10@20).
! !

!JSONTests class methodsFor:'documentation'!

version_CVS
    ^ '$Header$'
! !