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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
source [file dirname [info script]]/testing.tcl
needs cmd json::decode json
needs cmd json::encode json
# Create a json string as though it was read from data.json
set json [info source {
{
"fossil":"9c65b5432e4aeecf3556e5550c338ce93fd861cc",
"timestamp":1435827337,
"command":"timeline/checkin",
"procTimeUs":3333,
"procTimeMs":3,
"homepage":null,
"payload":{
"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"]
}]
}
}} data.json 1]
test json-decode-001 {top level keys} {
lsort [dict keys [json::decode $json]]
} {command fossil homepage payload procTimeMs procTimeUs timestamp}
# Note that the decode will return the keys/values in order
test json-decode-002 {object value} {
dict get [json::decode $json] payload
} {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}}}
test json-decode-003 {object nested value} {
dict get [json::decode $json] payload timeline
} {{type checkin uuid f8b17edee7ff4f16517601c40eb713602aed7a52 isLeaf true timestamp 1435318826 user juef comment {adwaita-icon-theme: update to 3.17.3} parents de628be645cc62429d630f9234c56d1fddfdc2a3 tags trunk}}
test json-decode-004 {array entry from nested value} {
lindex [dict get [json::decode $json] payload timeline] 0
} {type checkin uuid f8b17edee7ff4f16517601c40eb713602aed7a52 isLeaf true timestamp 1435318826 user juef comment {adwaita-icon-theme: update to 3.17.3} parents de628be645cc62429d630f9234c56d1fddfdc2a3 tags trunk}
test json-decode-005 {object value from child array entry} {
dict get [lindex [dict get [json::decode $json] payload timeline] 0] comment
} {adwaita-icon-theme: update to 3.17.3}
test json-decode-006 {unicode escape} {
dict get [json::decode {{"key":"\u2022"}}] key
} \u2022
test json-decode-011 {null subsitution} {
dict get [json::decode -null NULL $json] homepage
} {NULL}
test json-decode-012 {default null value} {
dict get [json::decode $json] homepage
} {null}
test json-decode-1.1 {Number forms} {
# Note that this is not strictly correct JSON, but is usable in practice
json::decode {[ 1, 2, 3.0, 4, Infinity, NaN, -Infinity, -0.0, 1e5, -1e-5 ]}
} {1 2 3.0 4 Inf NaN -Inf -0.0 1e5 -1e-5}
test json-2.1 {schema tests} {
lindex [json::decode -schema {[]}] 1
} {list}
test json-2.2 {schema tests} {
lindex [json::decode -schema {[1, 2]}] 1
} {list num}
test json-2.3 {schema tests} {
lindex [json::decode -schema {[1, 2, [3, 4], 4, 6]}] 1
} {mixed num num {list num} num num}
test json-2.4 {schema tests} {
lindex [json::decode -schema {{"a":1, "b":2}}] 1
} {obj a num b num}
test json-2.5 {schema tests} {
lindex [json::decode -schema {[1, 2, {"a":"b", "c":false}, "hello"]}] 1
} {mixed num num {obj a str c bool} str}
test json-2.6 {schema tests} {
lindex [json::decode -schema {[1, 2, {"a":["b", 1, true, Infinity]}]}] 1
} {mixed num num {obj a {mixed str num bool num}}}
test json-2.7 {schema tests} {
lindex [json::decode -schema {[1, 2, {"a":["b", 1, true, ["d", "e", "f"]]}]}] 1
} {mixed num num {obj a {mixed str num bool {list str}}}}
test json-2.8 {schema tests} {
lindex [json::decode -schema {[1, 2, true, false]}] 1
} {mixed num num bool bool}
test json-2.9 {schema tests} {
lindex [json::decode -schema {[{"a":1},{"b":2}]}] 1
} {mixed {obj a num} {obj b num}}
test json-3.1 {-index array} {
json::decode -index \
{[null, 1, 2, true, false, "hello"]}
} {0 null 1 1 2 2 3 true 4 false 5 hello}
test json-3.2 {-index array and object} {
json::decode -index \
{{"outer": [{"key": "value"}, {"key2": "value2"}]}}
} {outer {0 {key value} 1 {key2 value2}}}
test json-3.3 {-index array with -schema} {
json::decode -index -schema \
{[null, 1, 2, true, false, "hello"]}
} "{0 null 1 1 2 2 3 true 4 false 5 hello}\
{mixed num num num bool bool str}"
test json-3.4 {-index array with -schema 2} {
json::decode -index -schema \
{{"outer": [{"key": "value"}, {"key2": "value2"}]}}
} "{outer {0 {key value} 1 {key2 value2}}}\
{obj outer {mixed {obj key str} {obj key2 str}}}"
test json-4.1 {source info preserved} -body {
info source [dict get [json::decode $json] fossil]
} -result {data.json 3}
test json-4.2 {source info preserved} -body {
info source [dict get [json::decode $json] procTimeUs]
} -result {data.json 6}
test json-4.3 {source info preserved} -body {
info source [dict get [lindex [dict get [json::decode $json] payload timeline] 0] comment]
} -result {data.json 17}
unset -nocomplain json
test json-encode-1.1 {String with backslashes} {
json::encode {A "quoted string containing \backslashes\"}
} {"A \"quoted string containing \\backslashes\\\""}
test json-encode-1.2 {String with special chars} {
json::encode "Various \n special \b characters \t and /slash/ \r too"
} {"Various \n special \b characters \t and \/slash\/ \r too"}
test json-encode-1.3 {Array of numbers} {
json::encode {1 2 3.0 4 Inf NaN -Inf -0.0 1e5 -1e-5} {list num}
} {[ 1, 2, 3.0, 4, Infinity, NaN, -Infinity, -0.0, 1e5, -1e-5 ]}
test json-encode-1.4 {Array of strings} {
json::encode {1 2 3.0 4} list
} {[ "1", "2", "3.0", "4" ]}
test json-encode-1.5 {Array of objects} {
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}
} {[ { "city":"New York", "postalCode":10021, "state":"NY", "streetAddress":"21 2nd Street" }, { "city":"Los Angeles", "postalCode":10345, "state":"CA", "streetAddress":"15 Hale St" } ]}
test json-encode-1.6 {Simple typeless object} {
json::encode {home {212 555-1234} fax {646 555-4567}} obj
} {{ "fax":"646 555-4567", "home":"212 555-1234" }}
test json-encode-1.7 {Primitives as num} {
json::encode {a false b null c true} {obj a num b num c num}
} {{ "a":false, "b":null, "c":true }}
test json-encode-1.8 {Complex schema} {
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}}}
} {{ "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 ] } }}
test json-encode-1.9 {Array of mixed types} {
json::encode {{a b c d} 44} {mixed list num}
} {[ [ "a", "b", "c", "d" ], 44 ]}
test json-encode-1.10 {Array of objects} {
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}
} {[ { "city":"New York", "postalCode":10021, "state":"NY", "streetAddress":"21 2nd Street" }, { "city":"Los Angeles", "postalCode":10345, "state":"CA", "streetAddress":"15 Hale St" } ]}
test json-encode-1.11 {Forms of boolean} {
json::encode {-5 4 1 0 yes no true false} {list bool}
} {[ true, true, true, false, true, false, true, false ]}
testreport
|