]> gitweb.factorcode.org Git - factor.git/blob - basis/json/json-tests.factor
0f8225d109191172e2a136861b2b13b7836ef556
[factor.git] / basis / json / json-tests.factor
1 USING: hashtables io.encodings.utf8 io.files io.files.unique
2 io.streams.string json json.private kernel linked-assocs
3 literals math namespaces sequences strings tools.test ;
4 IN: json.tests
5
6 ! !!!!!!!!!!!!
7 ! READER TESTS
8 ! !!!!!!!!!!!!
9
10 { f } [ "false" json> ] unit-test
11 { t } [ "true" json> ] unit-test
12 { json-null } [ "null" json> ] unit-test
13 { 0 } [ "0" json> ] unit-test
14 { 0 } [ "-0" json> ] unit-test
15 { 102 } [ "102" json> ] unit-test
16 { -102 } [ "-102" json> ] unit-test
17 { 102 } [ "+102" json> ] unit-test
18 { 1000.0 } [ "1.0e3" json> ] unit-test
19 { 1000.0 } [ "10e2" json> ] unit-test
20 { 102.0 } [ "102.0" json> ] unit-test
21 { 102.5 } [ "102.5" json> ] unit-test
22 { 102.5 } [ "102.50" json> ] unit-test
23 { -10250.0 } [ "-102.5e2" json> ] unit-test
24 { -10250.0 } [ "-102.5E+2" json> ] unit-test
25 { -1.025 } [ "-102.5E-2" json> ] unit-test
26 { 10.25 } [ "1025e-2" json> ] unit-test
27 { 0.125 } [ "0.125" json> ] unit-test
28 { -0.125 } [ "-0.125" json> ] unit-test
29 { -0.00125 } [ "-0.125e-2" json> ] unit-test
30 { -012.5 } [ "-0.125e+2" json> ] unit-test
31 { 0.0 } [ "123e-10000000" json> ] unit-test
32
33 ! not widely supported by javascript, but allowed in the grammar, and a nice
34 ! feature to get
35 { -0.0 } [ "-0.0" json> ] unit-test
36
37 { " fuzzy  pickles " } [ "  \" fuzzy  pickles \" "  json> ] unit-test
38 { "while 1:\n\tpass" } [ "  \"while 1:\n\tpass\" "  json> ] unit-test
39 ! unicode is allowed in json
40 { "ß∂¬ƒ˚∆" } [ "  \"ß∂¬ƒ˚∆\""  json> ] unit-test
41 ${ { 8 9 10 12 13 34 47 92 } >string } [ " \"\\b\\t\\n\\f\\r\\\"\\/\\\\\" " json> ] unit-test
42 ${ { 0xabcd } >string } [ " \"\\uaBCd\" " json> ] unit-test
43 { "𝄞" } [ "\"\\ud834\\udd1e\"" json> ] unit-test
44
45 { LH{ { "a" { } } { "b" 123 } } } [ "{\"a\":[],\"b\":123}" json> ] unit-test
46 { { } } [ "[]" json> ] unit-test
47 { { 1 "two" 3.0 } } [ " [1, \"two\", 3.0] " json> ] unit-test
48 { LH{ } } [ "{}" json> ] unit-test
49
50 ! the returned hashtable should be different every time
51 { LH{ } } [ "key" "value" "{}" json> ?set-at "{}" json> nip ] unit-test
52
53 { LH{ { "US$" 1.0 } { "EU€" 1.5 } } } [ " { \"US$\":1.00, \"EU\\u20AC\":1.50 } " json> ] unit-test
54 { LH{
55     { "fib" { 1 1 2 3 5 8 LH{ { "etc" "etc" } } } }
56     { "prime" { 2 3 5 7 11 13 } }
57 } } [ " {
58     \"fib\": [1, 1,  2,   3,     5,         8,
59         { \"etc\":\"etc\" } ],
60     \"prime\":
61     [ 2,3,     5,7,
62 11,
63 13
64 ]      }
65 " json> ] unit-test
66
67 { 0 } [ "      0" json> ] unit-test
68 { 0 } [ "0      " json> ] unit-test
69 { 0 } [ "   0   " json> ] unit-test
70
71 { V{ LH{ { "a" "b" } } LH{ { "c" "d" } } } }
72 [ "{\"a\": \"b\"} {\"c\": \"d\"}" [ read-json ] with-string-reader ] unit-test
73
74 ! empty objects are allowed as values in objects
75 { LH{ { "foo" LH{ } } } } [ "{ \"foo\" : {}}" json> ] unit-test
76 ! And arrays
77 { { LH{ } } } [ "[{}]" json> ] unit-test
78
79 {
80     "\0\x01\x02\x03\x04\x05\x06\a\b\t\n\v\f\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\e\x1c\x1d\x1e\x1f"
81 } [
82     "\"\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f\""
83     json>
84 ] unit-test
85
86 { 1/0. } [ "Infinity" json> ] unit-test
87 { -1/0. } [ "-Infinity" json> ] unit-test
88 { t } [ "NaN" json> fp-nan? ] unit-test
89
90 [ "<!doctype html>\n<html>\n<head>\n   " json> ]
91 [ not-a-json-number? ] must-fail-with
92
93 ! unclosed objects and mismatched brackets are not allowed
94 [ "[\"a\",
95 4
96 ,1," json> ] must-fail
97
98 [ "[]]]" json> ]  must-fail
99
100 [ "{[: \"x\"}" json> ] must-fail
101
102 ! !!!!!!!!!!!!
103 ! WRITER TESTS
104 ! !!!!!!!!!!!!
105
106 { "false" } [ f >json ] unit-test
107 { "true" } [ t >json ] unit-test
108 { "null" } [ json-null >json ] unit-test
109 { "0" } [ 0 >json ] unit-test
110 { "102" } [ 102 >json ] unit-test
111 { "-102" } [ -102 >json ] unit-test
112 { "102.0" } [ 102.0 >json ] unit-test
113 { "102.5" } [ 102.5 >json ] unit-test
114 { "0.5" } [ 1/2 >json ] unit-test
115 { "\"hello world\"" } [ "hello world" >json ] unit-test
116
117 { "[1,\"two\",3.0]" } [ { 1 "two" 3.0 } >json ] unit-test
118 { "{\"US$\":1.0,\"EU€\":1.5}" } [ LH{ { "US$" 1.0 } { "EU€" 1.5 } } >json ] unit-test
119
120 { "\">json\"" } [ \ >json >json ] unit-test
121
122 { { 0.5 } } [ { 1/2 } >json json> ] unit-test
123
124 TUPLE: person first-name age ;
125
126 { "{\"first-name\":\"David\",\"age\":32}" }
127 [
128     f json-friendly-keys?
129     [ "David" 32 person boa >json ]
130     with-variable
131 ] unit-test
132
133 { "{\"first_name\":\"David\",\"age\":32}" }
134 [
135     t json-friendly-keys?
136     [ "David" 32 person boa >json ]
137     with-variable
138 ] unit-test
139
140 { "{\"1\":2,\"3\":4}" }
141 [ LH{ { "1" 2 } { "3" 4 } } >json ] unit-test
142
143 { "{\"1\":2,\"3\":4}" }
144 [ LH{ { 1 2 } { 3 4 } } >json ] unit-test
145
146 { "{\"\":4}" }
147 [ LH{ { "" 2 } { "" 4 } } >json ] unit-test
148
149 { "{\"false\":2,\"true\":4,\"\":5}" }
150 [ LH{ { f 2 } { t 4 } { "" 5 } } >json ] unit-test
151
152 { "{\"3.1\":3}" }
153 [ LH{ { 3.1 3 } } >json ] unit-test
154
155 { "{\"null\":1}" }
156 [ LH{ { json-null 1 } } >json ] unit-test
157
158 { "{\"Infinity\":1}" }
159 [ t json-allow-fp-special? [ LH{ { 1/0. 1 } } >json ] with-variable ] unit-test
160
161 { "{\"-Infinity\":1}" }
162 [ t json-allow-fp-special? [ LH{ { -1/0. 1 } } >json ] with-variable ] unit-test
163
164 { "{\"NaN\":1}" }
165 [ t json-allow-fp-special? [ LH{ { NAN: 333 1 } } >json ] with-variable ] unit-test
166
167 {
168     "\"\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f\""
169 } [
170     "\0\x01\x02\x03\x04\x05\x06\a\b\t\n\v\f\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\e\x1c\x1d\x1e\x1f"
171     >json
172 ] unit-test
173
174 { "\"\\ud834\\udd1e\"" }
175 [ t json-escape-unicode? [ "𝄞" >json ] with-variable ] unit-test
176
177 { "\"\\ud800\\udc01\"" }
178 [ t json-escape-unicode? [ "𐀁" >json ] with-variable ] unit-test
179
180
181 { t } [
182     {
183         LH{ { "foo" 1 } { "bar" 2 } }
184         LH{ { "baz" 3 } { "qux" 4 } }
185     } dup >jsonlines jsonlines> =
186 ] unit-test
187
188 { "6" } [ "[1,2,3]" [ sum ] rewrite-json-string ] unit-test
189 { "9\n81" } [ "3\n9" [ [ sq ] map ] rewrite-jsons-string ] unit-test
190
191 { "[1,2]" } [
192     [
193         "[1]" "test-json"
194         [ utf8 set-file-contents ]
195         [ [ { 2 } append ] rewrite-json-path ]
196         [ utf8 file-contents ] tri
197     ] cleanup-unique-directory
198 ] unit-test
199
200 { "121\n144" } [
201     [
202         "11\n12" "test-jsons"
203         [ utf8 set-file-contents ]
204         [ [ [ sq ] map ] rewrite-jsons-path ]
205         [ utf8 file-contents ] tri
206     ] cleanup-unique-directory
207 ] unit-test