RegressionTests__JSONTests.st
author Claus Gittinger <cg@exept.de>
Wed, 18 Sep 2019 15:24:23 +0200
changeset 2400 ee60cc84fecf
parent 2387 977c661d3399
child 2403 adff42ba7996
permissions -rw-r--r--
#QUALITY by exept class: RegressionTests::JSONTests added: #test04_encodeDecodeJSON5 changed: #test01_simple #test03_encodeDecode

"{ 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
    "should be redefined to return a collection of classes which are tested by
     this suite/case.
     If not redefined, coveredPackageNames should be.

     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:'tests'!

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

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

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

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

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

test03_encodeDecode
    #(
        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

        '[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.
!

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$'
! !