-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathset.test
More file actions
496 lines (483 loc) · 10.1 KB
/
set.test
File metadata and controls
496 lines (483 loc) · 10.1 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
if {"::tcltest" ni [namespace children]} {
package require tcltest
namespace import ::tcltest::*
}
package require rl_json
namespace path {::rl_json}
source [file join [file dirname [info script]] helpers.tcl]
test set-1.1 {Update a key in an object with a string} -setup { #<<<
set json {
{
"foo": "Foo",
"baz": "Baz"
}
}
} -body {
json set json bar {"Bar"}
compare_json {
{
"foo": "Foo",
"bar": "Bar",
"baz": "Baz"
}
} $json
} -cleanup {
unset -nocomplain json
} -result match
#>>>
test set-1.2 {Update a key in an object with a number} -setup { #<<<
set json {
{
"foo": "Foo",
"bar": "Bar",
"baz": "Baz"
}
}
} -body {
json set json bar 1.6
compare_json {
{
"foo": "Foo",
"bar": 1.6,
"baz": "Baz"
}
} $json
} -cleanup {
unset -nocomplain json
} -result match
#>>>
test set-1.3 {Update a key in an object with an object} -setup { #<<<
set json {
{
"foo": "Foo",
"bar": "Bar",
"baz": "Baz"
}
}
} -body {
json set json bar {{"x":true}}
compare_json {
{
"foo": "Foo",
"bar": {
"x": true
},
"baz": "Baz"
}
} $json
} -cleanup {
unset -nocomplain json
} -result match
#>>>
test set-1.4 {Update a key in an object with an array} -setup { #<<<
set json {
{
"foo": "Foo",
"bar": "Bar",
"baz": "Baz"
}
}
} -body {
json set json bar {["x", 42, null, true, false]}
compare_json {
{
"foo": "Foo",
"bar": ["x", 42, null, true, false],
"baz": "Baz"
}
} $json
} -cleanup {
unset -nocomplain json
} -result match
#>>>
test set-1.5 {Update a key in an object with a null} -setup { #<<<
set json {
{
"foo": "Foo",
"bar": "Bar",
"baz": "Baz"
}
}
} -body {
json set json bar null
compare_json {
{
"foo": "Foo",
"bar": null,
"baz": "Baz"
}
} $json
} -cleanup {
unset -nocomplain json
} -result match
#>>>
test set-1.6 {Update a key in an object with a boolean: true} -setup { #<<<
set json {
{
"foo": "Foo",
"bar": "Bar",
"baz": "Baz"
}
}
} -body {
json set json bar true
compare_json {
{
"foo": "Foo",
"bar": true,
"baz": "Baz"
}
} $json
} -cleanup {
unset -nocomplain json
} -result match
#>>>
test set-1.7 {Update a key in an object with a boolean: false} -setup { #<<<
set json {
{
"foo": "Foo",
"bar": "Bar",
"baz": "Baz"
}
}
} -body {
json set json bar false
compare_json {
{
"foo": "Foo",
"bar": false,
"baz": "Baz"
}
} $json
} -cleanup {
unset -nocomplain json
} -result match
#>>>
test set-2.1 {Do Nothing Gracefully (sort of - don't blow up if an empty path is supplied} -setup { #<<<
set json {
{
"foo": "Foo",
"baz": "Baz"
}
}
} -body {
json set json {"Bar"}
compare_json {
"Bar"
} $json
} -cleanup {
unset -nocomplain json
} -result match
#>>>
test set-3.1 {Create missing objects in path} -setup { #<<<
set json {
{
"foo": "Foo",
"baz": "Baz"
}
}
} -body {
json set json bar Bar FooBar test {"x"}
json set json bar Bar FooBar test2 {"y"}
json set json bar Bar FooBar test3 {"z"}
set json
} -cleanup {
unset -nocomplain json
} -result {{"foo":"Foo","baz":"Baz","bar":{"Bar":{"FooBar":{"test":"x","test2":"y","test3":"z"}}}}}
#>>>
test set-3.2 {Return value of set is the new variable's value} -setup { #<<<
set json {
{
"foo": "Foo",
"baz": "Baz"
}
}
} -body {
json set json foo {"Bar"}
} -cleanup {
unset -nocomplain json
} -result {{"foo":"Bar","baz":"Baz"}}
#>>>
test set-4.1 {Create a variable if it didn't exist} -body { #<<<
json set newvar x y z {
{
"foo": "Foo",
"bar": [1, 2, 3]
}
}
set newvar
} -cleanup {
unset -nocomplain newvar
} -result {{"x":{"y":{"z":{"foo":"Foo","bar":[1,2,3]}}}}}
#>>>
test set-4.2 {Create a variable if it didn't exist with atomic type - string} -body { #<<<
json set newvar {"foo"}
set newvar
} -cleanup {
unset -nocomplain newvar
} -result {"foo"}
#>>>
test set-4.3 {Create a variable if it didn't exist with atomic type - number} -body { #<<<
json set newvar 42
set newvar
} -cleanup {
unset -nocomplain newvar
} -result 42
#>>>
test set-4.4 {Create a variable if it didn't exist with atomic type - null} -body { #<<<
json set newvar null
set newvar
} -cleanup {
unset -nocomplain newvar
} -result null
#>>>
test set-4.5 {Create a variable if it didn't exist with atomic type - boolean: true} -body { #<<<
json set newvar true
set newvar
} -cleanup {
unset -nocomplain newvar
} -result true
#>>>
test set-4.6 {Create a variable if it didn't exist with atomic type - boolean: false} -body { #<<<
json set newvar false
set newvar
} -cleanup {
unset -nocomplain newvar
} -result false
#>>>
test set-4.7 {Create a variable if it didn't exist with array} -body { #<<<
json set newvar {[1, 2, 3]}
set newvar
} -cleanup {
unset -nocomplain newvar
} -result {[1,2,3]}
#>>>
test set-4.8 {Create a variable if it didn't exist with object} -body { #<<<
json set newvar {{"foo": null}}
set newvar
} -cleanup {
unset -nocomplain newvar
} -result {{"foo":null}}
#>>>
test set-5.1 {Set an existing element in an array, numbered} -setup { #<<<
set json {
{
"foo": ["a", "b", "c"]
}
}
} -body {
json set json foo 1 {"updated"}
set json
} -cleanup {
unset -nocomplain json
} -result {{"foo":["a","updated","c"]}}
#>>>
test set-5.2 {Prepend an element in an array, numbered} -setup { #<<<
set json {
{
"foo": ["a", "b", "c"]
}
}
} -body {
json set json foo -1 {"updated"}
set json
} -cleanup {
unset -nocomplain json
} -result {{"foo":["updated","a","b","c"]}}
#>>>
test set-5.3 {Append an element in an array, numbered, just beyond end} -setup { #<<<
set json {
{
"foo": ["a", "b", "c"]
}
}
} -body {
json set json foo 3 {"updated"}
set json
} -cleanup {
unset -nocomplain json
} -result {{"foo":["a","b","c","updated"]}}
#>>>
test set-5.4 {Append an element in an array, numbered, 2 beyond end} -setup { #<<<
set json {
{
"foo": ["a", "b", "c"]
}
}
} -body {
json set json foo 5 {"updated"}
set json
} -cleanup {
unset -nocomplain json
} -result {{"foo":["a","b","c",null,null,"updated"]}}
#>>>
test set-6.1 {Set an existing element in an array, end} -setup { #<<<
set json {
{
"foo": ["a", "b", "c"]
}
}
} -body {
json set json foo end {"updated"}
set json
} -cleanup {
unset -nocomplain json
} -result {{"foo":["a","b","updated"]}}
#>>>
test set-6.1.2 {Set a new element in an empty array, end} -body { #<<<
set json [json new array]
json set json end [json new string asdf]
set json
} -cleanup {
unset -nocomplain json
} -result {["asdf"]}
#>>>
test set-6.2 {Prepend an element in an array, end-relative} -setup { #<<<
set json {
{
"foo": ["a", "b", "c"]
}
}
} -body {
json set json foo end-3 {"updated"}
set json
} -cleanup {
unset -nocomplain json
} -result {{"foo":["updated","a","b","c"]}}
#>>>
test set-6.3 {Append an element in an array, end-relative, just beyond end} -setup { #<<<
set json {
{
"foo": ["a", "b", "c"]
}
}
} -body {
json set json foo end+1 {"updated"}
set json
} -cleanup {
unset -nocomplain json
} -result {{"foo":["a","b","c","updated"]}}
#>>>
test set-6.4 {Append an element in an array, end-relative, 2 beyond end} -setup { #<<<
set json {
{
"foo": ["a", "b", "c"]
}
}
} -body {
json set json foo end+3 {"updated"}
set json
} -cleanup {
unset -nocomplain json
} -result {{"foo":["a","b","c",null,null,"updated"]}}
#>>>
test set-7.1 {Be lenient about the value - if it can't be interpreted as a JSON native type, convert to a JSON string} -body { #<<<
set j {{}}
foreach {k v} {
num 42
bool true
null null
string string
arr { [1,2,3]}
notarr " \[1,2,3"
} {
json set j $k $v
}
set j
} -cleanup {
unset -nocomplain j k v
} -result [json normalize {
{
"num": 42,
"bool": true,
"null": null,
"string": "string",
"arr": [1,2,3],
"notarr": " [1,2,3" // Fix vim syntax highlighting confusion: ]"
}
}]
#>>>
test set-8.1.1 {json set into a template} -body { #<<<
set foo fooval
set bar barval
set template {
{
"foo": "~S:foo"
}
}
json set template bar "~S:bar"
json template $template
} -cleanup {
unset -nocomplain foo bar template
} -result {{"foo":"fooval","bar":"barval"}}
#>>>
test set-10.2 {JSON set on shared value} -setup { #<<<
set object {["a", "b"]}
proc t {object v} {
#puts stderr "-----------------------------------------"
#puts stderr "t, object rep: [tcl::unsupported::representation $object]"
#puts stderr "t before: [json debug $object]"
json set object 0 [json string $v]
#puts stderr "t after: [json debug $object]"
json get $object 0
}
} -body {
list [t $object us-west-2] [t $object us-west-1] [json get $object 0]
} -cleanup {
catch {rename t {}}
unset -nocomplain object
} -result {us-west-2 us-west-1 a}
#>>>
test set-11.1 {Interaction with var traces: value is always shared} -setup { #<<<
set json {
{
"foo": ["~S:a", ["1","2","3"], "c"]
}
}
set saved $json
set g_ref {}
trace add variable json write [list apply {{n1 n2 op} {
upvar 1 $n1 v
lappend ::g_ref $v ;# Take another ref to the value in the variable
}}]
} -body {
list [json set json foo 1 end-1 {"5"}] $g_ref [json normalize $saved]
} -cleanup {
unset -nocomplain json g_ref saved
} -result [list {{"foo":["~S:a",["1","5","3"],"c"]}} {{{"foo":["~S:a",["1","5","3"],"c"]}}} {{"foo":["~S:a",["1","2","3"],"c"]}}]
#>>>
test set-11.2 {Interaction with var traces: traces must fire even if value is unshared} -setup { #<<<
set json [string trim {
{
"foo": ["~S:a", ["1","2","3"], "c"]
}
}]
set g_ref {}
trace add variable json write [list apply {{n1 n2 op} {
set ::g_ref fired
}}]
} -body {
list [json set json foo 1 end-1 {"5"}] $g_ref
} -cleanup {
unset -nocomplain json g_ref
} -result [list {{"foo":["~S:a",["1","5","3"],"c"]}} fired]
#>>>
test set-11.3 {Interaction with var traces: result of an unset is the value of the variable, after the trace} -setup { #<<<
set json [string trim {
{
"foo": ["~S:a", ["1","2","3"], "c"]
}
}]
trace add variable json write [list apply {{n1 n2 op} {
upvar 1 $n1 v
json set v foo 1 end+1 {"4"}
}}]
} -body {
json set json foo 1 end-1 {"5"}
} -cleanup {
unset -nocomplain json
} -result {{"foo":["~S:a",["1","5","3","4"],"c"]}}
#>>>
::tcltest::cleanupTests
return
# vim: ft=tcl foldmethod=marker foldmarker=<<<,>>> ts=4 shiftwidth=4