-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_test.qd
More file actions
298 lines (259 loc) · 8.84 KB
/
json_test.qd
File metadata and controls
298 lines (259 loc) · 8.84 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Unit tests for the json module
use json
use str
use strconv
use testing
// Helper to build JSON strings (since Quadrate doesn't support \" escape)
// q = 34 (double quote character)
fn make_json_string(key:str val:str -- json:str) {
-> val -> key
34 -> q
"{" q str::from_char str::concat key str::concat q str::from_char str::concat ": " str::concat
q str::from_char str::concat val str::concat q str::from_char str::concat "}" str::concat
}
fn make_json_int(key:str val:i64 -- json:str) {
-> val -> key
34 -> q
"{" q str::from_char str::concat key str::concat q str::from_char str::concat ": " str::concat
val strconv::itoa str::concat "}" str::concat
}
fn make_json_bool(key:str val:i64 -- json:str) {
-> val -> key
34 -> q
"{" q str::from_char str::concat key str::concat q str::from_char str::concat ": " str::concat
val if { "true" } else { "false" } str::concat "}" str::concat
}
test "json::get_string" {
"name" "Alice" make_json_string "name" json::get_string -> found -> val
found testing::assert_true
val "Alice" testing::assert_eq
}
test "json::get_string missing key" {
"name" "Alice" make_json_string "missing" json::get_string -> found -> val
found testing::assert_false
}
test "json::get_int" {
"count" 42 make_json_int "count" json::get_int -> found -> val
found testing::assert_true
val 42 testing::assert_eq
}
test "json::get_int missing key" {
"count" 42 make_json_int "missing" json::get_int -> found -> val
found testing::assert_false
}
test "json::get_bool true" {
"active" 1 make_json_bool "active" json::get_bool -> found -> val
found testing::assert_true
val testing::assert_true
}
test "json::get_bool false" {
"active" 0 make_json_bool "active" json::get_bool -> found -> val
found testing::assert_true
val testing::assert_false
}
test "json::has_key" {
34 -> q
"{" q str::from_char str::concat "name" str::concat q str::from_char str::concat ": " str::concat
q str::from_char str::concat "test" str::concat q str::from_char str::concat ", " str::concat
q str::from_char str::concat "value" str::concat q str::from_char str::concat ": 123}" str::concat
-> j
j "name" json::has_key testing::assert_true
j "value" json::has_key testing::assert_true
j "missing" json::has_key testing::assert_false
}
test "json::array_len" {
"[1, 2, 3]" json::array_len 3 testing::assert_eq
"[]" json::array_len 0 testing::assert_eq
"[1]" json::array_len 1 testing::assert_eq
"[1, 2, 3, 4, 5]" json::array_len 5 testing::assert_eq
}
test "json::array_get_int" {
"[10, 20, 30]" 0 json::array_get_int -> found -> val
found testing::assert_true
val 10 testing::assert_eq
"[10, 20, 30]" 1 json::array_get_int -> found -> val
found testing::assert_true
val 20 testing::assert_eq
"[10, 20, 30]" 2 json::array_get_int -> found -> val
found testing::assert_true
val 30 testing::assert_eq
}
test "json::array_get_int out of bounds" {
"[10, 20, 30]" 5 json::array_get_int -> found -> val
found testing::assert_false
}
test "json::array_get_int negative" {
"[-5, 0, 5]" 0 json::array_get_int -> found -> val
found testing::assert_true
val -5 testing::assert_eq
}
test "json::array_get_string" {
34 -> q
"[" q str::from_char str::concat "a" str::concat q str::from_char str::concat ", " str::concat
q str::from_char str::concat "b" str::concat q str::from_char str::concat ", " str::concat
q str::from_char str::concat "c" str::concat q str::from_char str::concat "]" str::concat
-> json_arr
json_arr 0 json::array_get_string -> found -> val
found testing::assert_true
val "a" testing::assert_eq
json_arr 1 json::array_get_string -> found -> val
found testing::assert_true
val "b" testing::assert_eq
}
test "json type constants" {
json::Null 0 testing::assert_eq
json::Bool 1 testing::assert_eq
json::Number 2 testing::assert_eq
json::String 3 testing::assert_eq
json::Array 4 testing::assert_eq
json::Object 5 testing::assert_eq
}
test "json::get_float" {
34 -> q
"{" q str::from_char str::concat "value" str::concat q str::from_char str::concat ": 3.14}" str::concat
-> j
j "value" json::get_float -> found -> val
found testing::assert_true
val 3.14 - 0.001 < testing::assert_true
}
test "json::get_float negative" {
34 -> q
"{" q str::from_char str::concat "temp" str::concat q str::from_char str::concat ": -273.15}" str::concat
-> j
j "temp" json::get_float -> found -> val
found testing::assert_true
val -273.15 - 0.001 < testing::assert_true
}
test "json::array_get_float" {
"[1.1, 2.2, 3.3]" 0 json::array_get_float -> found -> val
found testing::assert_true
val 1.1 - 0.001 < testing::assert_true
"[1.1, 2.2, 3.3]" 1 json::array_get_float -> found -> val
found testing::assert_true
val 2.2 - 0.001 < testing::assert_true
}
test "json::get_object" {
34 -> q
// {"outer": {"inner": 42}}
"{" q str::from_char str::concat "outer" str::concat q str::from_char str::concat ": {" str::concat
q str::from_char str::concat "inner" str::concat q str::from_char str::concat ": 42}}" str::concat
-> j
j "outer" json::get_object -> found -> json_obj
found testing::assert_true
json_obj "inner" json::get_int -> found -> val
found testing::assert_true
val 42 testing::assert_eq
}
test "json::get_array" {
34 -> q
// {"items": [1, 2, 3]}
"{" q str::from_char str::concat "items" str::concat q str::from_char str::concat ": [1, 2, 3]}" str::concat
-> j
j "items" json::get_array -> found -> json_arr
found testing::assert_true
json_arr json::array_len 3 testing::assert_eq
json_arr 0 json::array_get_int -> found -> val
found testing::assert_true
val 1 testing::assert_eq
}
// ============================================================
// JSON Building Tests
// ============================================================
test "json::str quotes string" {
34 -> q
"hello" json::str -> result
q str::from_char "hello" str::concat q str::from_char str::concat -> expected
result expected testing::assert_eq
}
test "json::kv builds key-value" {
34 -> q
"name" "Bob" json::kv -> result
// Expected: "name":"Bob"
q str::from_char "name" str::concat q str::from_char str::concat ":" str::concat
q str::from_char str::concat "Bob" str::concat q str::from_char str::concat -> expected
result expected testing::assert_eq
}
test "json::kv_int builds key-int" {
34 -> q
"age" 42 json::kv_int -> result
// Expected: "age":42
q str::from_char "age" str::concat q str::from_char str::concat ":42" str::concat -> expected
result expected testing::assert_eq
}
test "json::kv_int negative" {
34 -> q
"temp" -10 json::kv_int -> result
// Expected: "temp":-10
q str::from_char "temp" str::concat q str::from_char str::concat ":-10" str::concat -> expected
result expected testing::assert_eq
}
test "json::kv_int zero" {
34 -> q
"count" 0 json::kv_int -> result
// Expected: "count":0
q str::from_char "count" str::concat q str::from_char str::concat ":0" str::concat -> expected
result expected testing::assert_eq
}
test "json::kv_bool true" {
34 -> q
"active" 1 json::kv_bool -> result
// Expected: "active":true
q str::from_char "active" str::concat q str::from_char str::concat ":true" str::concat -> expected
result expected testing::assert_eq
}
test "json::kv_bool false" {
34 -> q
"active" 0 json::kv_bool -> result
// Expected: "active":false
q str::from_char "active" str::concat q str::from_char str::concat ":false" str::concat -> expected
result expected testing::assert_eq
}
test "json::kv_raw builds key-raw" {
34 -> q
"data" "{}" json::kv_raw -> result
// Expected: "data":{}
q str::from_char "data" str::concat q str::from_char str::concat ":{}" str::concat -> expected
result expected testing::assert_eq
}
test "json::obj builds object" {
34 -> q
"a" "1" json::kv -> kv1
kv1 json::obj -> result
// Expected: {"a":"1"}
"{" q str::from_char str::concat "a" str::concat q str::from_char str::concat ":" str::concat
q str::from_char str::concat "1" str::concat q str::from_char str::concat "}" str::concat -> expected
result expected testing::assert_eq
}
test "json::empty_obj" {
json::empty_obj "{}" testing::assert_eq
}
test "json::arr builds array" {
"1,2,3" json::arr -> result
result "[1,2,3]" testing::assert_eq
}
test "json::empty_arr" {
json::empty_arr "[]" testing::assert_eq
}
test "json::join" {
34 -> q
"a" 1 json::kv_int -> kv1
"b" 2 json::kv_int -> kv2
kv1 kv2 json::join -> result
// Expected: "a":1,"b":2
q str::from_char "a" str::concat q str::from_char str::concat ":1," str::concat
q str::from_char str::concat "b" str::concat q str::from_char str::concat ":2" str::concat -> expected
result expected testing::assert_eq
}
test "json build full object" {
// Build {"name":"Alice","age":30}
"name" "Alice" json::kv -> kv1
"age" 30 json::kv_int -> kv2
kv1 kv2 json::join json::obj -> result
// Parse it back to verify
result "name" json::get_string -> found -> val
found testing::assert_true
val "Alice" testing::assert_eq
result "age" json::get_int -> found -> val
found testing::assert_true
val 30 testing::assert_eq
}