-
Notifications
You must be signed in to change notification settings - Fork 12
/
foreach.test
303 lines (298 loc) · 7.8 KB
/
foreach.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
if {"::tcltest" ni [namespace children]} {
package require tcltest
namespace import ::tcltest::*
}
package require rl_json
namespace path {::rl_json}
test foreach-1.0 {Single iterator, no vars, over array} -body { #<<<
json foreach {} {["a", "b", "other var", null, true, false, {"x": 42}]} {}
} -returnCodes error -result "foreach varlist is empty"
#>>>
test foreach-1.1 {Single iterator, single var, over array} -setup { #<<<
set i 0
set res {}
} -body {
json foreach elem {["a", "b", "other var", null, true, false, {"x": 42}]} {
lappend res "[incr i]($elem)([json get $elem])"
}
set res
} -cleanup {
unset -nocomplain i res elem
} -result [list \
"1(\"a\")(a)" \
"2(\"b\")(b)" \
"3(\"other var\")(other var)" \
"4(null)()" \
"5(true)(1)" \
"6(false)(0)" \
"7({\"x\":42})(x 42)" \
]
#>>>
test foreach-2.1 {Single iterator, multi vars, over array} -setup { #<<<
set i 0
set res {}
} -body {
json foreach {x y} {["a", "b", "other var", null, true, false, {"x": 42}, 42.1, "last"]} {
lappend res [string cat [incr i] \
($x)([json get $x]) \
($y)([json get $y])]
}
set res
} -cleanup {
unset -nocomplain i res x y
} -result [list \
"1(\"a\")(a)(\"b\")(b)" \
"2(\"other var\")(other var)(null)()" \
"3(true)(1)(false)(0)" \
"4({\"x\":42})(x 42)(42.1)(42.1)" \
"5(\"last\")(last)(null)()" \
]
#>>>
test foreach-3.0 {Multiple iterators, no vars, over array} -body { #<<<
json foreach \
elem {["a", "b", "other var", null, true, false, {"x": 42}]} \
{} {["A", "B", "OTHER VAR", null, true, {"X": 42}]} \
{}
} -cleanup {
unset -nocomplain elem
} -returnCodes error -result "foreach varlist is empty"
#>>>
test foreach-3.1 {Multiple iterators, single var, over array} -setup { #<<<
set i 0
set res {}
} -body {
json foreach \
elem {["a", "b", "other var", null, true, false, {"x": 42}]} \
elem2 {["A", "B", "OTHER VAR", null, true, {"X": 42}]} \
{
lappend res [string cat [incr i]($elem)([json get $elem])/($elem2)([json get $elem2])]
}
set res
} -cleanup {
unset -nocomplain i res elem elem2
} -result [list \
1("a")(a)/("A")(A) \
2("b")(b)/("B")(B) \
{3("other var")(other var)/("OTHER VAR")(OTHER VAR)} \
4(null)()/(null)() \
5(true)(1)/(true)(1) \
"6(false)(0)/({\"X\":42})(X 42)" \
"7({\"x\":42})(x 42)/(null)()" \
]
#>>>
test foreach-4.1 {Multiple iterators, multi vars, over array} -setup { #<<<
set i 0
set res {}
} -body {
json foreach \
{x y} {["a", "b", "other var", null, true, false, {"x": 42}, 42.1]} \
{a b} {["A", "B", "OTHER VAR", null, true, {"X": 42}, 42.1]} \
{
lappend res [string cat [incr i] \
($x)([json get $x]) \
($y)([json get $y]) \
/ \
($a)([json get $a]) \
($b)([json get $b])]
}
set res
} -cleanup {
unset -nocomplain i res x y a b
} -result [list \
"1(\"a\")(a)(\"b\")(b)/(\"A\")(A)(\"B\")(B)" \
"2(\"other var\")(other var)(null)()/(\"OTHER VAR\")(OTHER VAR)(null)()" \
"3(true)(1)(false)(0)/(true)(1)({\"X\":42})(X 42)" \
"4({\"x\":42})(x 42)(42.1)(42.1)/(42.1)(42.1)(null)()" \
]
#>>>
test foreach-5.1 {Single iterator, single var, over array, continue} -setup { #<<<
set i 0
set res {}
} -body {
json foreach elem {["a", "b", "other var", null, true, false, {"x": 42}]} {
if {[json get $elem] eq ""} continue
lappend res "[incr i]($elem)([json get $elem])"
}
set res
} -cleanup {
unset -nocomplain i res elem
} -result [list \
"1(\"a\")(a)" \
"2(\"b\")(b)" \
"3(\"other var\")(other var)" \
"4(true)(1)" \
"5(false)(0)" \
"6({\"x\":42})(x 42)" \
]
#>>>
test foreach-5.2 {Single iterator, single var, over array, continue on final iteration} -setup { #<<<
set i 0
set res {}
} -body {
json foreach elem {["a", "b", "other var", null]} {
if {[json get $elem] eq ""} continue
lappend res "[incr i]($elem)([json get $elem])"
}
set res
} -cleanup {
unset -nocomplain i res elem
} -result [list \
"1(\"a\")(a)" \
"2(\"b\")(b)" \
"3(\"other var\")(other var)" \
]
#>>>
test foreach-6.1 {Single iterator, single var, over array, break} -setup { #<<<
set i 0
set res {}
} -body {
json foreach elem {["a", "b", "other var", null, true, false, {"x": 42}]} {
if {[json get $elem] eq ""} break
lappend res "[incr i]($elem)([json get $elem])"
}
set res
} -cleanup {
unset -nocomplain i res elem
} -result [list \
"1(\"a\")(a)" \
"2(\"b\")(b)" \
"3(\"other var\")(other var)" \
]
#>>>
test foreach-6.2 {Single iterator, single var, over array, return} -setup { #<<<
set i 0
set res {}
} -body {
json foreach elem {["a", "b", "other var", null, true, false, {"x": 42}]} {
if {[json get $elem] eq ""} {return returned}
lappend res "[incr i]($elem)([json get $elem])"
}
set res
} -cleanup {
unset -nocomplain i res elem
} -result returned -returnCodes 2
#>>>
test foreach-7.1 {Single iterator, single var, over null} -setup { #<<<
set i 0
} -body {
json foreach elem null { incr i }
set i
} -cleanup {
unset -nocomplain i elem
} -result 0
#>>>
test foreach-7.2 {Single iterator, single var, over empty array} -setup { #<<<
set i 0
} -body {
json foreach elem {[]} { incr i }
set i
} -cleanup {
unset -nocomplain i elem
} -result 0
#>>>
test foreach-7.3 {Single iterator, multi vars, over null} -setup { #<<<
set i 0
} -body {
json foreach {x y} null { incr i }
set i
} -cleanup {
unset -nocomplain i x y
} -result 0
#>>>
test foreach-8.1 {Multiple iterators, single var, over null} -setup { #<<<
set i 0
set res {}
} -body {
json foreach \
elem {["a", "b", "other var", null, true, false, {"x": 42}]} \
elem2 {null} \
{
lappend res [string cat [incr i]($elem)([json get $elem])/($elem2)([json get $elem2])]
}
set res
} -cleanup {
unset -nocomplain i res elem elem2
} -result [list \
1("a")(a)/(null)() \
2("b")(b)/(null)() \
{3("other var")(other var)/(null)()} \
4(null)()/(null)() \
5(true)(1)/(null)() \
"6(false)(0)/(null)()" \
"7({\"x\":42})(x 42)/(null)()" \
]
#>>>
test foreach-8.2 {Multiple iterators, single var, over empty array} -setup { #<<<
set i 0
set res {}
} -body {
json foreach \
elem {["a", "b", "other var", null, true, false, {"x": 42}]} \
elem2 {[]} \
{
lappend res [string cat [incr i]($elem)([json get $elem])/($elem2)([json get $elem2])]
}
set res
} -cleanup {
unset -nocomplain i res elem elem2
} -result [list \
1("a")(a)/(null)() \
2("b")(b)/(null)() \
{3("other var")(other var)/(null)()} \
4(null)()/(null)() \
5(true)(1)/(null)() \
"6(false)(0)/(null)()" \
"7({\"x\":42})(x 42)/(null)()" \
]
#>>>
test foreach-20.1 {over string} -body { # TODO: perhaps allow this, and iterate over characters? <<<
json foreach x {"foo"} {
lappend res $x
}
set res
} -cleanup {
unset -nocomplain x res
} -returnCodes error -result {Cannot iterate over JSON type string}
#>>>
test foreach-20.2 {over number} -body { #<<<
json foreach x {123} {
lappend res $x
}
set res
} -cleanup {
unset -nocomplain x res
} -returnCodes error -result {Cannot iterate over JSON type number}
#>>>
test foreach-20.3 {over true} -body { #<<<
json foreach x {true} {
lappend res $x
}
set res
} -cleanup {
unset -nocomplain x res
} -returnCodes error -result {Cannot iterate over JSON type boolean}
#>>>
test foreach-20.4 {over false} -body { #<<<
json foreach x {false} {
lappend res $x
}
set res
} -cleanup {
unset -nocomplain x res
} -returnCodes error -result {Cannot iterate over JSON type boolean}
#>>>
test foreach-20.5 {too few args} -body { #<<<
list [catch {json foreach x {[]}} r o] $r [dict get $o -errorcode]
} -cleanup {
unset -nocomplain x y r o
} -result {1 {wrong # args: should be "*foreach ?varlist datalist ...? script"} {TCL WRONGARGS}} -match glob
#>>>
test foreach-20.6 {too many args} -body { #<<<
list [catch {json foreach x {[]} y {}} r o] $r [dict get $o -errorcode]
} -cleanup {
unset -nocomplain x y r o
} -result {1 {wrong # args: should be "*foreach ?varlist datalist ...? script"} {TCL WRONGARGS}} -match glob
#>>>
::tcltest::cleanupTests
return
# vim: ft=tcl foldmethod=marker foldmarker=<<<,>>> ts=4 shiftwidth=4