]> gitweb.factorcode.org Git - factor.git/blob - basis/json/reader/reader-tests.factor
more test IN: cleanup.
[factor.git] / basis / json / reader / reader-tests.factor
1 USING: hashtables io.streams.string json json.reader
2 json.reader.private kernel literals math strings tools.test ;
3
4 { f } [ "false" json> ] unit-test
5 { t } [ "true" json> ] unit-test
6 { json-null } [ "null" json> ] unit-test
7 { 0 } [ "0" json> ] unit-test
8 { 0 } [ "-0" json> ] unit-test
9 { 102 } [ "102" json> ] unit-test
10 { -102 } [ "-102" json> ] unit-test
11 { 102 } [ "+102" json> ] unit-test
12 { 1000.0 } [ "1.0e3" json> ] unit-test
13 { 1000.0 } [ "10e2" json> ] unit-test
14 { 102.0 } [ "102.0" json> ] unit-test
15 { 102.5 } [ "102.5" json> ] unit-test
16 { 102.5 } [ "102.50" json> ] unit-test
17 { -10250.0 } [ "-102.5e2" json> ] unit-test
18 { -10250.0 } [ "-102.5E+2" json> ] unit-test
19 { -1.025 } [ "-102.5E-2" json> ] unit-test
20 { 10.25 } [ "1025e-2" json> ] unit-test
21 { 0.125 } [ "0.125" json> ] unit-test
22 { -0.125 } [ "-0.125" json> ] unit-test
23 { -0.00125 } [ "-0.125e-2" json> ] unit-test
24 { -012.5 } [ "-0.125e+2" json> ] unit-test
25 { 0.0 } [ "123e-10000000" json> ] unit-test
26
27 ! not widely supported by javascript, but allowed in the grammar, and a nice
28 ! feature to get
29 { -0.0 } [ "-0.0" json> ] unit-test
30
31 { " fuzzy  pickles " } [ "  \" fuzzy  pickles \" "  json> ] unit-test
32 { "while 1:\n\tpass" } [ "  \"while 1:\n\tpass\" "  json> ] unit-test
33 ! unicode is allowed in json
34 { "ß∂¬ƒ˚∆" } [ "  \"ß∂¬ƒ˚∆\""  json> ] unit-test
35 ${ { 8 9 10 12 13 34 47 92 } >string } [ " \"\\b\\t\\n\\f\\r\\\"\\/\\\\\" " json> ] unit-test
36 ${ { 0xabcd } >string } [ " \"\\uaBCd\" " json> ] unit-test
37 { "𝄞" } [ "\"\\ud834\\udd1e\"" json> ] unit-test
38
39 { H{ { "a" { } } { "b" 123 } } } [ "{\"a\":[],\"b\":123}" json> ] unit-test
40 { { } } [ "[]" json> ] unit-test
41 { { 1 "two" 3.0 } } [ " [1, \"two\", 3.0] " json> ] unit-test
42 { H{ } } [ "{}" json> ] unit-test
43
44 ! the returned hashtable should be different every time
45 { H{ } } [ "key" "value" "{}" json> ?set-at "{}" json> nip ] unit-test
46
47 { H{ { "US$" 1.0 } { "EU€" 1.5 } } } [ " { \"US$\":1.00, \"EU\\u20AC\":1.50 } " json> ] unit-test
48 { H{
49     { "fib" { 1 1 2 3 5 8 H{ { "etc" "etc" } } } }
50     { "prime" { 2 3 5 7 11 13 } }
51 } } [ " {
52     \"fib\": [1, 1,  2,   3,     5,         8,
53         { \"etc\":\"etc\" } ],
54     \"prime\":
55     [ 2,3,     5,7,
56 11,
57 13
58 ]      }
59 " json> ] unit-test
60
61 { 0 } [ "      0" json> ] unit-test
62 { 0 } [ "0      " json> ] unit-test
63 { 0 } [ "   0   " json> ] unit-test
64
65 { V{ H{ { "a" "b" } } H{ { "c" "d" } } } }
66 [ "{\"a\": \"b\"} {\"c\": \"d\"}" [ read-json-objects ] with-string-reader ] unit-test
67
68 ! empty objects are allowed as values in objects
69 { H{ { "foo" H{ } } } } [ "{ \"foo\" : {}}" json> ] unit-test
70 ! And arrays
71 { { H{ } } } [ "[{}]" json> ] unit-test
72
73 {
74     "\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"
75 } [
76     "\"\\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\""
77     json>
78 ] unit-test
79
80 { 1/0. } [ "Infinity" json> ] unit-test
81 { -1/0. } [ "-Infinity" json> ] unit-test
82 { t } [ "NaN" json> fp-nan? ] unit-test
83
84 [ "<!doctype html>\n<html>\n<head>\n   " json> ]
85 [ not-a-json-number? ] must-fail-with
86
87 { H{ } } [ "" json> ] unit-test