forked from timfel/JSON-st
/
JsonTests.st
141 lines (123 loc) · 3.26 KB
/
JsonTests.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
TestCase subclass: JsonTests [
<comment: 'I provide a number of test cases for class Json.'>
<category: 'JSON'>
json: aString equals: aValue [
| readValue |
readValue := self readFrom: aString.
self assert: readValue = aValue
]
readFrom: aString [
^(Json newWithConstructors: {JsonDummyTestObject})
readFrom: aString readStream
]
render: anObject equals: aString [
self assert: (Json render: anObject) = aString
description: 'Json render ' , anObject printString , ' = ' , aString
]
simpleDummyObject [
^(JsonDummyTestObject new)
a: 1;
b: 2;
c: 3;
yourself
]
testAtomFalse [
self json: 'false' equals: false.
self json: ' false' equals: false.
self json: 'false ' equals: false.
self json: ' false ' equals: false
]
testAtomNull [
self json: 'null' equals: nil.
self json: ' null' equals: nil.
self json: 'null ' equals: nil.
self json: ' null ' equals: nil
]
testAtomNumber [
self json: '1' equals: 1.
self json: '0123' equals: 123.
self json: '1.23e2' equals: 123.
self json: '-1' equals: -1.
self json: '-0' equals: 0.
self json: '[-1.2]' equals: {1.2 negated}
]
testAtomString [
self json: '"hi"' equals: 'hi'.
self json: '"\""' equals: '"'.
self json: '"\\"' equals: '\'.
self json: '""' equals: ''.
self json: '"a\u0004b"'
equals: (String from:
{$a.
Character value: 4.
$b}).
self json: '"a\nb"' equals: (String from:
{$a.
Character lf.
$b})
]
testAtomTrue [
self json: 'true' equals: true.
self json: ' true' equals: true.
self json: 'true ' equals: true.
self json: ' true ' equals: true
]
testCtor [
self json: '@JsonDummyTestObject {"a": 1, "b": 2, "c": 3}'
equals: self simpleDummyObject.
self json: (Json render: self simpleDummyObject)
equals: self simpleDummyObject
]
testDictionary [
self json: '{}' equals: JsonObject new.
self json: '{"a": "a"}'
equals: ((JsonObject new)
at: 'a' put: 'a';
yourself).
self json: '{"a": [[]]}'
equals: ((JsonObject new)
at: 'a' put: #(#());
yourself).
self json: '{"a":"b", "b":"a"}'
equals: ((JsonObject new)
add: 'a' -> 'b';
add: 'b' -> 'a';
yourself)
]
testMissingCtor [
self should: [self readFrom: '@Missing[]'] raise: JsonSyntaxError
]
testMissingCtorNoMap [
self should: [Json new readFrom: '@Missing[]' readStream]
raise: JsonSyntaxError
]
testStringWithUnicode [
self json: '"\u263A"'
equals: (Character codePoint: 9786) asUnicodeString asString.
self render: (Character codePoint: 9786) asUnicodeString asString
equals: '"\u263A"'
]
testWriteAssociation [
self render: 'key' -> 'value' equals: '"key": "value"'.
self render: 'key' -> 2 equals: '"key": 2'.
"keys should be strings"
self render: 42 -> 2 equals: '"42": 2'.
"try to do _something_ for more complex keys"
self render: #(42 43 44) -> 2 equals: '"#(42 43 44)": 2'
]
testWriteString [
self render: '"' equals: '"\""'.
self render: '\' equals: '"\\"'.
self render: 'hi' equals: '"hi"'.
self render: (String from:
{$a.
Character lf.
$b})
equals: '"a\nb"'.
self render: (String from:
{$a.
Character value: 4.
$b})
equals: '"a\u0004b"'
]
]