1+ from emberjson import JSON , Null, Array, Object, Value
2+ from emberjson import write_pretty
3+ from testing import *
4+
5+ def main ():
6+ test_json_object()
7+ test_json_array()
8+ test_equality()
9+ test_setter_object()
10+ test_setter_array()
11+ test_stringify_array()
12+ test_pretty_print_array()
13+ test_pretty_print_object()
14+ test_trailing_tokens()
15+ test_bool()
16+ test_string()
17+ test_null()
18+ test_integer()
19+ test_integer_leading_plus()
20+ test_integer_negative()
21+ test_float()
22+ test_eight_digits_after_dot()
23+ test_special_case_floats()
24+ test_float_leading_plus()
25+ test_float_negative()
26+ test_float_exponent()
27+ test_float_exponent_negative()
28+ test_equality_value()
29+ test_implicit_conversion()
30+ test_pretty()
31+ test_booling()
32+
33+ def test_json_object ():
34+ var s = ' {"key": 123}'
35+ var json = JSON .from_string(s)
36+ assert_true(json.is_object())
37+ assert_equal(json.object()[" key" ].int(), 123 )
38+ assert_equal(json[" key" ].int(), 123 )
39+
40+ assert_equal(str (json), ' {"key":123}' )
41+
42+ assert_equal(len (json), 1 )
43+
44+ with assert_raises():
45+ _ = json[2 ]
46+
47+ def test_json_array ():
48+ var s = ' [123, 345]'
49+ var json = JSON .from_string(s)
50+ assert_true(json.is_array())
51+ assert_equal(json.array()[0 ].int(), 123 )
52+ assert_equal(json.array()[1 ].int(), 345 )
53+ assert_equal(json[0 ].int(), 123 )
54+
55+ assert_equal(str (json), ' [123,345]' )
56+
57+ assert_equal(len (json), 2 )
58+
59+ with assert_raises():
60+ _ = json[" key" ]
61+
62+ json = JSON .from_string(" [1, 2, 3]" )
63+ assert_true(json.is_array())
64+ assert_equal(json[0 ], 1 )
65+ assert_equal(json[1 ], 2 )
66+ assert_equal(json[2 ], 3 )
67+
68+ def test_equality ():
69+
70+ var ob = JSON .from_string(' {"key": 123}' )
71+ var ob2 = JSON .from_string(' {"key": 123}' )
72+ var arr = JSON .from_string(' [123, 345]' )
73+
74+ assert_equal(ob, ob2)
75+ ob[" key" ] = 456
76+ assert_not_equal(ob, ob2)
77+ assert_not_equal(ob, arr)
78+
79+ def test_setter_object ():
80+ var ob : JSON = Object()
81+ ob[" key" ] = " foo"
82+ assert_true(" key" in ob)
83+ assert_equal(ob[" key" ], " foo" )
84+
85+ def test_setter_array ():
86+ var arr : JSON = Array(123 , " foo" )
87+ arr[0 ] = Null()
88+ assert_true(arr[0 ].isa[Null]())
89+ assert_equal(arr[1 ], " foo" )
90+
91+ def test_stringify_array ():
92+ var arr = JSON .from_string(' [123,"foo",false,null]' )
93+ assert_equal(str (arr), ' [123,"foo",false,null]' )
94+
95+ def test_pretty_print_array ():
96+ var arr = JSON .from_string(' [123,"foo",false,null]' )
97+ var expected = """ [
98+ 123,
99+ "foo",
100+ false,
101+ null
102+ ]"""
103+ assert_equal(expected, write_pretty(arr))
104+
105+ expected = """ [
106+ iamateapot123,
107+ iamateapot"foo",
108+ iamateapotfalse,
109+ iamateapotnull
110+ ]"""
111+ assert_equal(expected, write_pretty(arr, indent = String(" iamateapot" )))
112+
113+ arr = JSON .from_string(' [123,"foo",false,{"key": null}]' )
114+ expected = """ [
115+ 123,
116+ "foo",
117+ false,
118+ {
119+ "key": null
120+ }
121+ ]"""
122+
123+ assert_equal(expected, write_pretty(arr))
124+
125+
126+ def test_pretty_print_object ():
127+ var ob = JSON .from_string(' {"k1": null, "k2": 123}' )
128+ var expected = """ {
129+ "k1": null,
130+ "k2": 123
131+ }"""
132+ assert_equal(expected, write_pretty(ob))
133+
134+ ob = JSON .from_string(' {"key": 123, "k": [123, false, null]}' )
135+
136+ expected = """ {
137+ "key": 123,
138+ "k": [
139+ 123,
140+ false,
141+ null
142+ ]
143+ }"""
144+
145+ assert_equal(expected, write_pretty(ob))
146+
147+ ob = JSON .from_string(' {"key": 123, "k": [123, false, [1, 2, 3]]}' )
148+ expected = """ {
149+ "key": 123,
150+ "k": [
151+ 123,
152+ false,
153+ [
154+ 1,
155+ 2,
156+ 3
157+ ]
158+ ]
159+ }"""
160+ assert_equal(expected, write_pretty(ob))
161+
162+
163+ def test_trailing_tokens ():
164+ with assert_raises(contains = " Invalid json, expected end of input, recieved: garbage tokens" ):
165+ _ = JSON .from_string(' [1, null, false] garbage tokens' )
166+
167+ with assert_raises(contains = ' Invalid json, expected end of input, recieved: "trailing string"' ):
168+ _ = JSON .from_string(' {"key": null} "trailing string"' )
169+
170+ def test_bool ():
171+ var s = " false"
172+ var v = Value.from_string(s)
173+ assert_true(v.isa[Bool]())
174+ assert_equal(v.get[Bool](), False )
175+ assert_equal(str (v), s)
176+
177+ s = " true"
178+ v = Value.from_string(s)
179+ assert_true(v.isa[Bool]())
180+ assert_equal(v.get[Bool](), True )
181+ assert_equal(str (v), s)
182+
183+ def test_string ():
184+ var s = ' "Some String"'
185+ var v = Value.from_string(s)
186+ assert_true(v.isa[String]())
187+ assert_equal(v.get[String](), " Some String" )
188+ assert_equal(str (v), s)
189+
190+ s = " \" Escaped\" "
191+ v = Value.from_string(s)
192+ assert_true(v.isa[String]())
193+ assert_equal(v.get[String](), " Escaped" )
194+ assert_equal(str (v), s)
195+
196+ def test_null ():
197+ var s = " null"
198+ var v = Value.from_string(s)
199+ assert_true(v.isa[Null]())
200+ assert_equal(v.get[Null](), Null())
201+ assert_equal(str (v), s)
202+
203+ with assert_raises(contains = " Expected 'null'" ):
204+ _ = Value.from_string(" nil" )
205+
206+ def test_integer ():
207+ var v = Value.from_string(" 123" )
208+ assert_true(v.isa[Int]())
209+ assert_equal(v.get[Int](), 123 )
210+ assert_equal(str (v), " 123" )
211+
212+ def test_integer_leading_plus ():
213+ v = Value.from_string(" +123" )
214+ assert_true(v.isa[Int]())
215+ assert_equal(v.get[Int](), 123 )
216+
217+ def test_integer_negative ():
218+ v = Value.from_string(" -123" )
219+ assert_true(v.isa[Int]())
220+ assert_equal(v.get[Int](), - 123 )
221+ assert_equal(str (v), " -123" )
222+
223+ def test_float ():
224+ v = Value.from_string(" 43.5" )
225+ assert_true(v.isa[Float64]())
226+ assert_almost_equal(v.get[Float64](), 43.5 )
227+ assert_equal(str (v), " 43.5" )
228+
229+ def test_eight_digits_after_dot ():
230+ v = Value.from_string(" 342.12345678" )
231+ assert_true(v.isa[Float64]())
232+ assert_almost_equal(v.get[Float64](), 342.12345678 )
233+ assert_equal(str (v), " 342.12345678" )
234+
235+ def test_special_case_floats ():
236+
237+ v = Value.from_string(' 2.2250738585072013e-308' )
238+ assert_almost_equal(v.float(), 2.2250738585072013e-308 )
239+ assert_true(v.isa[Float64]())
240+
241+ v = Value.from_string(' 7.2057594037927933e+16' )
242+ assert_true(v.isa[Float64]())
243+ assert_almost_equal(v.float(), 7.2057594037927933e+16 )
244+
245+ v = Value.from_string(' 1e000000000000000000001' )
246+ assert_true(v.isa[Float64]())
247+ assert_almost_equal(v.float(), 1e000000000000000000001 )
248+
249+
250+ def test_float_leading_plus ():
251+ v = Value.from_string(" +43.5" )
252+ assert_true(v.isa[Float64]())
253+ assert_almost_equal(v.get[Float64](), 43.5 )
254+
255+ def test_float_negative ():
256+ v = Value.from_string(" -43.5" )
257+ assert_true(v.isa[Float64]())
258+ assert_almost_equal(v.get[Float64](), - 43.5 )
259+
260+ def test_float_exponent ():
261+ v = Value.from_string(" 43.5e10" )
262+ assert_true(v.isa[Float64]())
263+ assert_almost_equal(v.get[Float64](), 43.5e10 )
264+
265+ def test_float_exponent_negative ():
266+ v = Value.from_string(" -43.5e10" )
267+ assert_true(v.isa[Float64]())
268+ assert_almost_equal(v.get[Float64](), - 43.5e10 )
269+
270+ def test_equality_value ():
271+ var v1 = Value(34 )
272+ var v2 = Value(" Some string" )
273+ var v3 = Value(" Some string" )
274+ assert_equal(v2, v3)
275+ assert_not_equal(v1, v2)
276+
277+
278+ def test_implicit_conversion ():
279+ var val : Value = " a string"
280+ assert_equal(val.string(), " a string" )
281+ val = 100
282+ assert_equal(val.int(), 100 )
283+ val = False
284+ assert_false(val.bool())
285+ val = 1e10
286+ assert_almost_equal(val.float(), 1e10 )
287+ val = Null()
288+ assert_equal(val.null(), Null())
289+ val = Object()
290+ assert_equal(val.object(), Object())
291+ val = Array(1 , 2 , 3 )
292+ assert_equal(val.array(), Array(1 , 2 , 3 ))
293+ val = JSON()
294+ assert_equal(val.object(), Object())
295+
296+
297+ def test_pretty ():
298+ var v = Value.from_string(" [123, 43564, false]" )
299+ var expected = """ [
300+ 123,
301+ 43564,
302+ false
303+ ]"""
304+ assert_equal(expected, write_pretty(v))
305+
306+ v = Value.from_string(' {"key": 123, "k2": null}' )
307+ expected = """ {
308+ "key": 123,
309+ "k2": null
310+ }"""
311+
312+ assert_equal(expected, write_pretty(v))
313+
314+ def test_booling ():
315+ var a : Value = True
316+ assert_true(a)
317+ if not a:
318+ raise Error(" Implicit bool failed" )
319+
320+
321+ var trues = Array(" some string" , 123 , 3.43 )
322+ for t in trues:
323+ assert_true(t[])
324+
325+ var falsies = Array(" " , 0 , 0.0 , False , Null())
326+ for f in falsies:
327+ assert_false(f[])
0 commit comments