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