-
Notifications
You must be signed in to change notification settings - Fork 12
/
unset.test
350 lines (345 loc) · 7.29 KB
/
unset.test
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
if {"::tcltest" ni [namespace children]} {
package require tcltest
namespace import ::tcltest::*
}
package require rl_json
package require parse_args
namespace path {::rl_json ::parse_args}
source [file join [file dirname [info script]] helpers.tcl]
test unset-1.1 {Unset a key in an object} -setup { #<<<
set json {
{
"foo": "Foo",
"bar": "Bar",
"baz": "Baz"
}
}
} -body {
json unset json bar
compare_json {
{
"foo": "Foo",
"baz": "Baz"
}
} $json
} -cleanup {
unset -nocomplain json
} -result match
#>>>
test unset-1.2 {No args} -body { #<<<
set json "{}"
json unset json
} -cleanup {
unset -nocomplain json r o
} -result "{}"
#>>>
test unset-1.3 {No args, invalid varname} -setup { #<<<
unset -nocomplain notset
} -body {
list [catch {json unset notset} r o] [dict get $o -errorcode]
} -cleanup {
unset -nocomplain r o
} -result {1 {TCL LOOKUP VARNAME notset}}
#>>>
test unset-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 unset json
compare_json {
{
"foo": "Foo",
"baz": "Baz"
}
} $json
} -cleanup {
unset -nocomplain json
} -result match
#>>>
test unset-2.2 {Return value of unset is the new variable's value} -setup { #<<<
set json {
{
"foo": "Foo",
"bar": "Bar",
"baz": "Baz"
}
}
} -body {
json unset json bar
} -cleanup {
unset -nocomplain json
} -result {{"foo":"Foo","baz":"Baz"}}
#>>>
test unset-3.1 {Throw error for missing objects in path} -setup { #<<<
set json {
{
"foo": "Foo",
"baz": "Baz"
}
}
} -body {
list [catch {json unset json bar Bar} r o] $r [dict get $o -errorcode]
} -cleanup {
unset -nocomplain json r o
} -result {1 {Path element "bar" doesn't exist} {RL JSON BAD_PATH bar}}
#>>>
test unset-3.1 {Throw error for dereferencing atomic objects in path} -setup { #<<<
set json {
{
"foo": "Foo",
"baz": "Baz"
}
}
} -body {
list [catch {json unset json foo Bar} r o] [dict get $o -errorcode]
} -cleanup {
unset -nocomplain json r o
} -result {1 {RL JSON BAD_PATH {foo Bar}}}
#>>>
test unset-4.1 {Named variable doesn't exist} -body { #<<<
list [catch {json unset newvar x} r o] [dict get $o -errorcode]
} -cleanup {
unset -nocomplain newvar r o
} -result {1 {TCL LOOKUP VARNAME newvar}}
#>>>
test unset-5.1 {Unset an existing element in an array, numbered} -setup { #<<<
set json {
{
"foo": ["a", "b", "c"]
}
}
} -body {
json unset json foo 1
set json
} -cleanup {
unset -nocomplain json
} -result {{"foo":["a","c"]}}
#>>>
test unset-5.2 {Unset an element in an array, negative index} -setup { #<<<
set json {
{
"foo": ["a", "b", "c"]
}
}
} -body {
json unset json foo -1
set json
} -cleanup {
unset -nocomplain json
} -result {{"foo":["a","b","c"]}}
#>>>
test unset-5.3 {Unset an element in an array, numbered, just beyond end} -setup { #<<<
set json {
{
"foo": ["a", "b", "c"]
}
}
} -body {
json unset json foo 3
set json
} -cleanup {
unset -nocomplain json
} -result {{"foo":["a","b","c"]}}
#>>>
test unset-5.4 {Unset an element in an array, numbered, 2 beyond end} -setup { #<<<
set json {
{
"foo": ["a", "b", "c"]
}
}
} -body {
json unset json foo 5
set json
} -cleanup {
unset -nocomplain json
} -result {{"foo":["a","b","c"]}}
#>>>
test unset-6.1 {Unset an existing element in an array, end} -setup { #<<<
set json {
{
"foo": ["a", "b", "c"]
}
}
} -body {
json unset json foo end
set json
} -cleanup {
unset -nocomplain json
} -result {{"foo":["a","b"]}}
#>>>
test unset-6.2 {Unset an element in an array, end-relative} -setup { #<<<
set json {
{
"foo": ["a", "b", "c"]
}
}
} -body {
json unset json foo end-1
set json
} -cleanup {
unset -nocomplain json
} -result {{"foo":["a","c"]}}
#>>>
test unset-6.3 {Unset an element in an array, end-relative, just beyond end} -setup { #<<<
set json {
{
"foo": ["a", "b", "c"]
}
}
} -body {
json unset json foo end+1
set json
} -cleanup {
unset -nocomplain json
} -result {{"foo":["a","b","c"]}}
#>>>
test unset-6.4 {Unset an element in an array, end-relative, 2 beyond end} -setup { #<<<
set json {
{
"foo": ["a", "b", "c"]
}
}
} -body {
json unset json foo end+3
set json
} -cleanup {
unset -nocomplain json
} -result {{"foo":["a","b","c"]}}
#>>>
test unset-7.1 {Unset an element in an array, end-relative} -setup { #<<<
set json {
{
"foo": ["a", ["1","2","3"], "c"]
}
}
} -body {
json unset json foo end-1 1
set json
} -cleanup {
unset -nocomplain json
} -result {{"foo":["a",["1","3"],"c"]}}
#>>>
test unset-7.2 {Unset an element in an array, end-relative, invalid} -setup { #<<<
set json {
{
"foo": ["a", ["1","2","3"], "c"]
}
}
} -body {
json unset json foo end/1 1
set json
} -cleanup {
unset -nocomplain json
} -returnCodes error -result {Expected an integer index or end(+/-integer)?, got end/1}
#>>>
test unset-7.3 {Unset an element in an array, end-relative, invalid} -setup { #<<<
set json {
{
"foo": ["a", ["1","2","3"], "c"]
}
}
} -body {
json unset json foo en 1
set json
} -cleanup {
unset -nocomplain json
} -returnCodes error -result {Expected an integer index or end(+/-integer)?, got en}
#>>>
test unset-7.4 {Unset an element in an array, end-relative, invalid} -setup { #<<<
set json {
{
"foo": ["a", ["1","2","3"], "c"]
}
}
} -body {
json unset json foo end-1x 1
set json
} -cleanup {
unset -nocomplain json
} -returnCodes error -result {Expected an integer index or end(+/-integer)?, got end-1x}
#>>>
test unset-7.5 {Unset an element in an array, invalid} -setup { #<<<
set json {
{
"foo": ["a", ["1","2","3"], "c"]
}
}
} -body {
json unset json foo -1 1
set json
} -cleanup {
unset -nocomplain json
} -returnCodes error -result {Path element "foo -1" doesn't exist}
#>>>
test unset-7.6 {Unset an element in an array, invalid} -setup { #<<<
set json {
{
"foo": ["a", ["1","2","3"], "c"]
}
}
} -body {
json unset json foo 4 1
set json
} -cleanup {
unset -nocomplain json
} -returnCodes error -result {Path element "foo 4" doesn't exist}
#>>>
test unset-7.7 {Unset an element in an array, invalid} -setup { #<<<
set json {
{
"foo": ["~S:a", ["1","2","3"], "c"]
}
}
} -body {
json unset json foo 0 1
set json
} -cleanup {
unset -nocomplain json
} -returnCodes error -result {Attempt to index into atomic type string at path "foo 0 1"}
#>>>
test unset-7.8 {Unset an element in an array, invalid} -setup { #<<<
set json {
{
"foo": ["~S:a", ["1","2","3"], "c"]
}
}
} -body {
json unset json foo 1 end/1
set json
} -cleanup {
unset -nocomplain json
} -returnCodes error -result {Expected an integer index or end(+/-integer)?, got end/1}
#>>>
test unset-7.9 {Unset an element in an array, invalid} -setup { #<<<
set json {
{
"foo": ["~S:a", ["1","2","3"], "c"]
}
}
} -body {
json unset json foo 1 en
set json
} -cleanup {
unset -nocomplain json
} -returnCodes error -result {Expected an integer index or end(+/-integer)?, got en}
#>>>
test unset-7.10 {Unset an element in an array, invalid} -setup { #<<<
set json {
{
"foo": ["~S:a", ["1","2","3"], "c"]
}
}
} -body {
json unset json foo 1 end-1x
set json
} -cleanup {
unset -nocomplain json
} -returnCodes error -result {Expected an integer index or end(+/-integer)?, got end-1x}
#>>>
::tcltest::cleanupTests
return
# vim: ft=tcl foldmethod=marker foldmarker=<<<,>>> ts=4 shiftwidth=4