build: Fix out-of-tree build with json ext
[jimtcl.git] / tests / json.test
blobed73401eddb5f31f6095c638abe977b3c57cbf18
1 source [file dirname [info script]]/testing.tcl
3 needs cmd json::decode json
4 needs cmd json::encode json
6 set json {
8         "fossil":"9c65b5432e4aeecf3556e5550c338ce93fd861cc",
9         "timestamp":1435827337,
10         "command":"timeline/checkin",
11         "procTimeUs":3333,
12         "procTimeMs":3,
13         "homepage":null,
14         "payload":{
15                 "limit":1,
16                 "timeline":[{
17                         "type":"checkin",
18                         "uuid":"f8b17edee7ff4f16517601c40eb713602aed7a52",
19                         "isLeaf":true,
20                         "timestamp":1435318826,
21                         "user":"juef",
22                         "comment":"adwaita-icon-theme: update to 3.17.3",
23                         "parents":["de628be645cc62429d630f9234c56d1fddfdc2a3"],
24                         "tags":["trunk"]
25                 }]
26         }
29 test json-decode-001 {top level keys} {
30         lsort [dict keys [json::decode $json]]
31 } {command fossil homepage payload procTimeMs procTimeUs timestamp}
33 # Note that the decode will return the keys/values in order
34 test json-decode-002 {object value} {
35         dict get [json::decode $json] payload
36 } {limit 1 timeline {{type checkin uuid f8b17edee7ff4f16517601c40eb713602aed7a52 isLeaf true timestamp 1435318826 user juef comment {adwaita-icon-theme: update to 3.17.3} parents de628be645cc62429d630f9234c56d1fddfdc2a3 tags trunk}}}
38 test json-decode-003 {object nested value} {
39         dict get [json::decode $json] payload timeline
40 } {{type checkin uuid f8b17edee7ff4f16517601c40eb713602aed7a52 isLeaf true timestamp 1435318826 user juef comment {adwaita-icon-theme: update to 3.17.3} parents de628be645cc62429d630f9234c56d1fddfdc2a3 tags trunk}}
42 test json-decode-004 {array entry from nested value} {
43         lindex [dict get [json::decode $json] payload timeline] 0
44 } {type checkin uuid f8b17edee7ff4f16517601c40eb713602aed7a52 isLeaf true timestamp 1435318826 user juef comment {adwaita-icon-theme: update to 3.17.3} parents de628be645cc62429d630f9234c56d1fddfdc2a3 tags trunk}
46 test json-decode-005 {object value from child array entry} {
47         dict get [lindex [dict get [json::decode $json] payload timeline] 0] comment
48 } {adwaita-icon-theme: update to 3.17.3}
50 test json-decode-006 {unicode escape} {
51         dict get [json::decode {{"key":"\u2022"}}] key
52 } \u2022
54 test json-decode-011 {null subsitution} {
55         dict get [json::decode -null NULL $json] homepage
56 } {NULL}
58 test json-decode-012 {default null value} {
59         dict get [json::decode $json] homepage
60 } {null}
62 test json-decode-1.1 {Number forms} {
63         json::decode {[ 1, 2, 3.0, 4, Infinity, NaN, -Infinity, -0.0, 1e5, -1e-5 ]}
64 } {1 2 3.0 4 Inf NaN -Inf -0.0 1e5 -1e-5}
66 test json-2.1 {schema tests} {
67         lindex [json::decode -schema {[]}] 1
68 } {list}
70 test json-2.2 {schema tests} {
71         lindex [json::decode -schema {[1, 2]}] 1
72 } {list num}
74 test json-2.3 {schema tests} {
75         lindex [json::decode -schema {[1, 2, [3, 4], 4, 6]}] 1
76 } {mixed num num {list num} num num}
78 test json-2.4 {schema tests} {
79         lindex [json::decode -schema {{"a":1, "b":2}}] 1
80 } {obj a num b num}
82 test json-2.5 {schema tests} {
83         lindex [json::decode -schema {[1, 2, {a:"b", c:false}, "hello"]}] 1
84 } {mixed num num {obj a str c bool} str}
86 test json-2.6 {schema tests} {
87         lindex [json::decode -schema {[1, 2, {a:["b", 1, true, Infinity]}]}] 1
88 } {mixed num num {obj a {mixed str num bool num}}}
90 test json-2.7 {schema tests} {
91         lindex [json::decode -schema {[1, 2, {a:["b", 1, true, ["d", "e", "f"]]}]}] 1
92 } {mixed num num {obj a {mixed str num bool {list str}}}}
94 test json-2.8 {schema tests} {
95         lindex [json::decode -schema {[1, 2, true, false]}] 1
96 } {mixed num num bool bool}
98 test json-2.9 {schema tests} {
99         lindex [json::decode -schema {[{a:1},{b:2}]}] 1
100 } {mixed {obj a num} {obj b num}}
103 test json-3.1 {-index array} {
104         json::decode -index \
105                 {[null, 1, 2, true, false, "hello"]}
106 } {0 null 1 1 2 2 3 true 4 false 5 hello}
108 test json-3.2 {-index array and object} {
109         json::decode -index \
110                 {{"outer": [{"key": "value"}, {"key2": "value2"}]}}
111 } {outer {0 {key value} 1 {key2 value2}}}
113 test json-3.3 {-index array with -schema} {
114         json::decode -index -schema \
115                 {[null, 1, 2, true, false, "hello"]}
116 } "{0 null 1 1 2 2 3 true 4 false 5 hello}\
117    {mixed num num num bool bool str}"
119 test json-3.4 {-index array with -schema 2} {
120         json::decode -index -schema \
121                 {{"outer": [{"key": "value"}, {"key2": "value2"}]}}
122 } "{outer {0 {key value} 1 {key2 value2}}}\
123    {obj outer {mixed {obj key str} {obj key2 str}}}"
126 unset -nocomplain json
128 test json-encode-1.1 {String with backslashes}  {
129         json::encode {A "quoted string containing \backslashes\"}
130 } {"A \"quoted string containing \\backslashes\\\""}
132 test json-encode-1.2 {String with special chars} {
133         json::encode "Various \n special \b characters \t and /slash/ \r too"
134 } {"Various \n special \b characters \t and \/slash\/ \r too"}
136 test json-encode-1.3 {Array of numbers} {
137         json::encode {1 2 3.0 4 Inf NaN -Inf -0.0 1e5 -1e-5} {list num}
138 } {[ 1, 2, 3.0, 4, Infinity, NaN, -Infinity, -0.0, 1e5, -1e-5 ]}
140 test json-encode-1.4 {Array of strings} {
141         json::encode {1 2 3.0 4} list
142 } {[ "1", "2", "3.0", "4" ]}
144 test json-encode-1.5 {Array of objects} {
145         json::encode {{state NY city {New York} postalCode 10021 streetAddress {21 2nd Street}} {state CA city {Los Angeles} postalCode 10345 streetAddress {15 Hale St}}} {list obj postalCode num}
146 } {[ { "city":"New York", "postalCode":10021, "state":"NY", "streetAddress":"21 2nd Street" }, { "city":"Los Angeles", "postalCode":10345, "state":"CA", "streetAddress":"15 Hale St" } ]}
148 test json-encode-1.6 {Simple typeless object} {
149         json::encode {home {212 555-1234} fax {646 555-4567}} obj
150 } {{ "fax":"646 555-4567", "home":"212 555-1234" }}
152 test json-encode-1.7 {Primitives as num} {
153         json::encode {a false b null c true} {obj a num b num c num}
154 } {{ "a":false, "b":null, "c":true }}
156 test json-encode-1.8 {Complex schema} {
157         json::encode {Person {firstName John age 25 lastName Smith years {1972 1980 1995 2002} PhoneNumbers {home {212 555-1234} fax {646 555-4567}} Address {state NY city {New York} postalCode 10021 streetAddress {21 2nd Street}}}} {obj Person {obj age num Address {obj postalCode num} PhoneNumbers obj years {list num}}}
158 } {{ "Person":{ "Address":{ "city":"New York", "postalCode":10021, "state":"NY", "streetAddress":"21 2nd Street" }, "PhoneNumbers":{ "fax":"646 555-4567", "home":"212 555-1234" }, "age":25, "firstName":"John", "lastName":"Smith", "years":[ 1972, 1980, 1995, 2002 ] } }}
160 test json-encode-1.9 {Array of mixed types} {
161         json::encode {{a b c d} 44} {mixed list num}
162 } {[ [ "a", "b", "c", "d" ], 44 ]}
164 test json-encode-1.10 {Array of objects} {
165         json::encode {{state NY city {New York} postalCode 10021 streetAddress {21 2nd Street}} {state CA city {Los Angeles} postalCode 10345 streetAddress {15 Hale St}}} {list obj postalCode num}
166 } {[ { "city":"New York", "postalCode":10021, "state":"NY", "streetAddress":"21 2nd Street" }, { "city":"Los Angeles", "postalCode":10345, "state":"CA", "streetAddress":"15 Hale St" } ]}
168 test json-encode-1.11 {Forms of boolean} {
169         json::encode {-5 4 1 0 yes no true false} {list bool}
170 } {[ true, true, true, false, true, false, true, false ]}
173 testreport