RegressionTests__JSONTests.st
author Claus Gittinger <cg@exept.de>
Tue, 25 Feb 2020 17:18:51 +0100
changeset 2579 bec2c103be07
parent 2578 0a1480d503cc
permissions -rw-r--r--
#OTHER by cg s
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2578
0a1480d503cc #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 2476
diff changeset
     1
"{ Encoding: utf8 }"
0a1480d503cc #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 2476
diff changeset
     2
2356
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     3
"{ Package: 'stx:goodies/regression' }"
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
"{ NameSpace: RegressionTests }"
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
TestCase subclass:#JSONTests
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
	instanceVariableNames:''
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
	classVariableNames:''
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
	poolDictionaries:''
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
	category:'tests-Regression-Files and Encodings'
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
!
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
!JSONTests class methodsFor:'documentation'!
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
documentation
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
"
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
    Notice that there are frameworks for JSON:
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
        goodies/json
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
    and: 
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
        JSONReader/JSONPrinter from goodies/communications.
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
    This tests the second one.
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
"
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
! !
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
!JSONTests class methodsFor:'queries'!
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
coveredClassNames
2578
0a1480d503cc #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 2476
diff changeset
    30
    "These classes can be instrumented for coverage analysis,
2356
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
     before running the suite to provide coverage analysis/report"
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
    ^ #( JSONReader JSONPrinter )
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
    "Created: / 06-07-2011 / 21:27:03 / cg"
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
    "Modified: / 21-03-2019 / 22:28:33 / Claus Gittinger"
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
! !
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
2476
e77b3f22bcf0 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 2403
diff changeset
    39
!JSONTests methodsFor:'setup'!
e77b3f22bcf0 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 2403
diff changeset
    40
e77b3f22bcf0 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 2403
diff changeset
    41
setUp
e77b3f22bcf0 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 2403
diff changeset
    42
    JSONPrinter isNil ifTrue:[
e77b3f22bcf0 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 2403
diff changeset
    43
        Smalltalk loadPackage:'stx:goodies/communication'
e77b3f22bcf0 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 2403
diff changeset
    44
    ].
e77b3f22bcf0 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 2403
diff changeset
    45
! !
e77b3f22bcf0 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 2403
diff changeset
    46
2356
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
!JSONTests methodsFor:'tests'!
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
test01_simple
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
    
2403
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
    51
    self assert:(JSONPrinter toJSON:true) = 'true'.
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
    52
    self assert:(JSONPrinter toJSON:false) = 'false'.
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
    53
    self assert:(JSONPrinter toJSON:nil) = 'null'.
2356
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
2403
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
    55
    self assert:(JSONPrinter toJSON:123) = '123'.
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
    56
    self assert:(JSONPrinter toJSON:0) = '0'.
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
    57
    self assert:(JSONPrinter toJSON:-0) = '0'.
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
    58
    self assert:(JSONPrinter toJSON:-1) = '-1'.
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
    59
    self assert:(JSONPrinter toJSON:-123) = '-123'.
2356
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
2403
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
    61
    self assert:(JSONPrinter toJSON:123.0) = '123.0'.
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
    62
    self assert:(JSONPrinter toJSON:1e3) = '1000.0'.
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
    63
    self assert:(JSONPrinter toJSON:10e3) = '10000.0'.
2356
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
2403
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
    65
    self assert:(JSONPrinter toJSON:'hello') = '"hello"'.
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
    66
    self assert:(JSONPrinter toJSON:'') = '""'.
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
    67
    self assert:(JSONPrinter toJSON:'"a"') = '"\"a\""'.
2356
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
!
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
test02_arrays
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
    
2403
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
    72
    self assert:(JSONPrinter toJSON:#()) = '[]'.
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
    73
    self assert:(JSONPrinter toJSON:#(1 2 3)) = '[1,2,3]'.
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
    74
    self assert:(JSONPrinter toJSON:#(true false -1 'a')) = '[true,false,-1,"a"]'.
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
    75
    self assert:(JSONPrinter toJSON:#( (1) ('a') (nil))) = '[[1],["a"],[null]]'.
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
    76
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
    77
    self assert:(JSONReader fromJSON:'[]') = #().
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
    78
    self assert:(JSONReader fromJSON:'[1,2,3]') = #(1 2 3).
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
    79
    self assert:(JSONReader fromJSON:'[true,false,-1,"a"]') = #(true false -1 'a').
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
    80
    self assert:(JSONReader fromJSON:'[[1],["a"],[null]]') = #( (1) ('a') (nil)).
2356
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
!
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
test03_encodeDecode
2403
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
    84
    "/ self assert:(JSONReader fromJSON:'"\uD83C\uDFBC"') = (Character value:0x1F3BC) asString.
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
    85
2356
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
    #(
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
        true false nil
2400
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
    88
        123 0 -1 -123 
2356
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
        123.0 10e4 1e5
2400
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
    90
        -123.0 -10e4 -1e5
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
    91
        -123.0e+5 -123.0e-5
2356
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
        ''
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
        'a'
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
        'hello'
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
        c'hello\nworld'
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
        c'hello\tworld'
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
        '"'
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
        '""'
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
        'a"b"c'
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
        ()
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
        (1)
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
        (1 2 3)
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
        ('a' 2.0 (true false nil) 3)
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   104
    ) do:[:each |
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
        |encoding decoded|
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   107
        encoding := JSONPrinter encode:each.
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
        decoded := JSONReader decode:encoding.
2400
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   109
        self assert:((decoded = each) or:[decoded isFloat and:[decoded isAlmostEqualTo:each nEpsilon:2]])
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   110
    ].
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   111
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   112
    self should:[ (JSONReader fromJSON:'[1,2,]') ] raise:JSONConversionError.
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   113
    self should:[ (JSONReader fromJSON:'[,]') ] raise:JSONConversionError.
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   114
    self should:[ (JSONReader fromJSON:'{ "foo": "bla", }') ] raise:JSONConversionError.
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   115
    self should:[ (JSONReader fromJSON:'0x123') ] raise:JSONConversionError.
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   116
!
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   117
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   118
test04_encodeDecodeJSON5
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   119
    #(
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   120
        '123' 123
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   121
        '0'   0 
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   122
        '-1'  -1
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   123
        '-123' -123 
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   124
        '0x123' 0x123
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   125
        '0x0'   0
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   126
        '-0x1'  -1
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   127
        '-0x123' -0x123 
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   128
        '123.0' 123.0
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   129
        '10e4'  10e4
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   130
        '1e5'   1e5
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   131
        '-123.0' -123.0 
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   132
        '-10e4'  -10e4 
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   133
        '-1e5'   -1e5
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   134
        '-123.0e+5' -123.0e+5 
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   135
        '-123.0e-5' -123.0e-5
2403
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   136
        '.123' 0.123
2400
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   137
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   138
        '[1,2,]' #(1 2)
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   139
    ) pairWiseDo:[:json :expected |
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   140
        |decoded|
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   141
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   142
        decoded := JSONReader fromJSON5:json.
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   143
        self assert:((decoded = expected) or:[decoded isFloat and:[decoded isAlmostEqualTo:expected nEpsilon:2]])
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   144
    ].
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   145
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   146
    self should:[ (JSONReader fromJSON5:'[,]') ] raise:JSONConversionError.
ee60cc84fecf #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2387
diff changeset
   147
    self shouldnt:[ (JSONReader fromJSON5:'{ "foo": "bla", }') ] raise:JSONConversionError.
2356
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   148
!
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   149
2403
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   150
test05_JSON5
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   151
    |j o|
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   152
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   153
    "/ self assert:(JSONReader fromJSON5:'''\uD83C\uDFBC''') = (Character value:0x1F3BC) asString.
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   154
    "/ self assert:(JSONReader fromJSON5:'"\uD83C\uDFBC"') = (Character value:0x1F3BC) asString.
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   155
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   156
    j := '
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   157
{
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   158
  // comments
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   159
  unquoted: ''and you can quote me on that'',
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   160
  singleQuotes: ''I can use "double quotes" here'',
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   161
  lineBreaks: "Look, Mom!! \
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   162
No \\n''s!!",
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   163
  hexadecimal: 0xdecaf,
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   164
  leadingDecimalPoint: .8675309, andTrailing: 8675309.,
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   165
  positiveSign: +1,
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   166
  trailingComma: ''in objects'', andIn: [''arrays'',],
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   167
  "backwardsCompatible": "with JSON",
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   168
}
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   169
'.
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   170
    self should:[ (JSONReader fromJSON:j) ] raise:JSONConversionError.
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   171
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   172
    o := JSONReader fromJSON5:j.
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   173
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   174
    #(
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   175
        unquoted 'and you can quote me on that' 
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   176
        singleQuotes 'I can use "double quotes" here' 
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   177
        lineBreaks 'Look, Mom!! No \n''s!!'
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   178
        hexadecimal 0xdecaf
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   179
        leadingDecimalPoint 0.8675309
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   180
        andTrailing 8675309.0
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   181
        positiveSign 1
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   182
        trailingComma 'in objects'
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   183
        andIn #('arrays')
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   184
        backwardsCompatible 'with JSON'
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   185
    ) pairWiseDo:[:key :expected |
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   186
        |got|
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   187
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   188
        got := o at:key.
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   189
        self assert:((got = expected) or:[got isFloat and:[got isAlmostEqualTo:expected nEpsilon:2]])
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   190
    ].
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   191
!
adff42ba7996 #UI_ENHANCEMENT by exept
Claus Gittinger <cg@exept.de>
parents: 2400
diff changeset
   192
2356
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   193
test10_typed
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   194
    
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   195
    self assert:((JSONPrinter new typeInfoFormat:#stx) 
2387
977c661d3399 #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2356
diff changeset
   196
                    encode:(10@20)) = '{"@__type__":"Point","@__value__":{"x":10,"y":20}}'.
2356
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   197
    self assert:((JSONReader new typeInfoFormat:#stx) 
2387
977c661d3399 #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2356
diff changeset
   198
                    decode:'{"@__type__":"Point","@__value__":{"x":10,"y":20}}')
2356
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   199
                = (10@20).
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   200
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   201
"/    self assert:((JSONPrinter new typeInfoFormat:#stx) 
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   202
"/                    encode:(Color red)) = '{"@type":"Point","@value":{"x":10,"y":20}}'.
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   203
"/    self assert:((JSONReader new typeInfoFormat:#stx) 
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   204
"/                    decode:'{"@type":"Point","@value":{"x":10,"y":20}}')
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   205
"/                = (10@20).
2387
977c661d3399 #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2356
diff changeset
   206
!
2356
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   207
2387
977c661d3399 #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2356
diff changeset
   208
test11_typedV2
977c661d3399 #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2356
diff changeset
   209
    
977c661d3399 #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2356
diff changeset
   210
    self assert:((JSONPrinter new typeInfoFormat:#stx2) 
977c661d3399 #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2356
diff changeset
   211
                    encode:(10@20)) = '{"@__type__":"Point","x":10,"y":20}'.
977c661d3399 #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2356
diff changeset
   212
    self assert:((JSONReader new typeInfoFormat:#stx2) 
977c661d3399 #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2356
diff changeset
   213
                    decode:'{"@__type__":"Point","x":10,"y":20}')
977c661d3399 #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 2356
diff changeset
   214
                = (10@20).
2356
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   215
! !
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   216
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   217
!JSONTests class methodsFor:'documentation'!
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   218
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   219
version_CVS
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   220
    ^ '$Header$'
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   221
! !
78d2069ea228 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   222