blob: d88d2c965f3a39d4ed442df122ab4c9faa4fc3cd (
plain)
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
|
# Commands covered: 'upvar', 'namespace upvar'
#
# This file contains a collection of tests for one or more of the Tcl
# built-in commands. Sourcing this file into Tcl runs the tests and
# generates output for errors. No output means no errors were found.
#
# Copyright (c) 1991-1993 The Regents of the University of California.
# Copyright (c) 1994 Sun Microsystems, Inc.
# Copyright (c) 1998-1999 by Scriptics Corporation.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
if {[lsearch [namespace children] ::tcltest] == -1} {
package require tcltest 2
namespace import -force ::tcltest::*
}
testConstraint testupvar [llength [info commands testupvar]]
test upvar-1.1 {reading variables with upvar} {
proc p1 {a b} {set c 22; set d 33; p2}
proc p2 {} {upvar a x1 b x2 c x3 d x4; set a abc; list $x1 $x2 $x3 $x4 $a}
p1 foo bar
} {foo bar 22 33 abc}
test upvar-1.2 {reading variables with upvar} {
proc p1 {a b} {set c 22; set d 33; p2}
proc p2 {} {p3}
proc p3 {} {upvar 2 a x1 b x2 c x3 d x4; set a abc; list $x1 $x2 $x3 $x4 $a}
p1 foo bar
} {foo bar 22 33 abc}
test upvar-1.3 {reading variables with upvar} {
proc p1 {a b} {set c 22; set d 33; p2}
proc p2 {} {p3}
proc p3 {} {
upvar #1 a x1 b x2 c x3 d x4
set a abc
list $x1 $x2 $x3 $x4 $a
}
p1 foo bar
} {foo bar 22 33 abc}
test upvar-1.4 {reading variables with upvar} {
set x1 44
set x2 55
proc p1 {} {p2}
proc p2 {} {
upvar 2 x1 x1 x2 a
upvar #0 x1 b
set c $b
incr b 3
list $x1 $a $b
}
p1
} {47 55 47}
test upvar-1.5 {reading array elements with upvar} {
proc p1 {} {set a(0) zeroth; set a(1) first; p2}
proc p2 {} {upvar a(0) x; set x}
p1
} {zeroth}
test upvar-2.1 {writing variables with upvar} {
proc p1 {a b} {set c 22; set d 33; p2; list $a $b $c $d}
proc p2 {} {
upvar a x1 b x2 c x3 d x4
set x1 14
set x4 88
}
p1 foo bar
} {14 bar 22 88}
test upvar-2.2 {writing variables with upvar} {
set x1 44
set x2 55
proc p1 {x1 x2} {
upvar #0 x1 a
upvar x2 b
set a $x1
set b $x2
}
p1 newbits morebits
list $x1 $x2
} {newbits morebits}
test upvar-2.3 {writing variables with upvar} {
catch {unset x1}
catch {unset x2}
proc p1 {x1 x2} {
upvar #0 x1 a
upvar x2 b
set a $x1
set b $x2
}
p1 newbits morebits
list [catch {set x1} msg] $msg [catch {set x2} msg] $msg
} {0 newbits 0 morebits}
test upvar-2.4 {writing array elements with upvar} {
proc p1 {} {set a(0) zeroth; set a(1) first; list [p2] $a(0)}
proc p2 {} {upvar a(0) x; set x xyzzy}
p1
} {xyzzy xyzzy}
test upvar-3.1 {unsetting variables with upvar} {
proc p1 {a b} {set c 22; set d 33; p2; lsort [info vars]}
proc p2 {} {
upvar 1 a x1 d x2
unset x1 x2
}
p1 foo bar
} {b c}
test upvar-3.2 {unsetting variables with upvar} {
proc p1 {a b} {set c 22; set d 33; p2; lsort [info vars]}
proc p2 {} {
upvar 1 a x1 d x2
unset x1 x2
set x2 28
}
p1 foo bar
} {b c d}
test upvar-3.3 {unsetting variables with upvar} {
set x1 44
set x2 55
proc p1 {} {p2}
proc p2 {} {
upvar 2 x1 a
upvar #0 x2 b
unset a b
}
p1
list [info exists x1] [info exists x2]
} {0 0}
test upvar-3.4 {unsetting variables with upvar} {
set x1 44
set x2 55
proc p1 {} {
upvar x1 a x2 b
unset a b
set b 118
}
p1
list [info exists x1] [catch {set x2} msg] $msg
} {0 0 118}
test upvar-3.5 {unsetting array elements with upvar} {
proc p1 {} {
set a(0) zeroth
set a(1) first
set a(2) second
p2
array names a
}
proc p2 {} {upvar a(0) x; unset x}
p1
} {1 2}
test upvar-3.6 {unsetting then resetting array elements with upvar} {
proc p1 {} {
set a(0) zeroth
set a(1) first
set a(2) second
p2
list [array names a] [catch {set a(0)} msg] $msg
}
proc p2 {} {upvar a(0) x; unset x; set x 12345}
p1
} {{0 1 2} 0 12345}
test upvar-4.1 {nested upvars} {
set x1 88
proc p1 {a b} {set c 22; set d 33; p2}
proc p2 {} {global x1; upvar c x2; p3}
proc p3 {} {
upvar x1 a x2 b
list $a $b
}
p1 14 15
} {88 22}
test upvar-4.2 {nested upvars} {
set x1 88
proc p1 {a b} {set c 22; set d 33; p2; list $a $b $c $d}
proc p2 {} {global x1; upvar c x2; p3}
proc p3 {} {
upvar x1 a x2 b
set a foo
set b bar
}
list [p1 14 15] $x1
} {{14 15 bar 33} foo}
proc tproc {args} {global x; set x [list $args [uplevel info vars]]}
test upvar-5.1 {traces involving upvars} {
proc p1 {a b} {set c 22; set d 33; trace var c rw tproc; p2}
proc p2 {} {upvar c x1; set x1 22}
set x ---
p1 foo bar
set x
} {{x1 {} w} x1}
test upvar-5.2 {traces involving upvars} {
proc p1 {a b} {set c 22; set d 33; trace var c rw tproc; p2}
proc p2 {} {upvar c x1; set x1}
set x ---
p1 foo bar
set x
} {{x1 {} r} x1}
test upvar-5.3 {traces involving upvars} {
proc p1 {a b} {set c 22; set d 33; trace var c rwu tproc; p2}
proc p2 {} {upvar c x1; unset x1}
set x ---
p1 foo bar
set x
} {{x1 {} u} x1}
test upvar-6.1 {retargeting an upvar} {
proc p1 {} {
set a(0) zeroth
set a(1) first
set a(2) second
p2
}
proc p2 {} {
upvar a x
set result {}
foreach i [array names x] {
upvar a($i) x
lappend result $x
}
lsort $result
}
p1
} {first second zeroth}
test upvar-6.2 {retargeting an upvar} {
set x 44
set y abcde
proc p1 {} {
global x
set result $x
upvar y x
lappend result $x
}
p1
} {44 abcde}
test upvar-6.3 {retargeting an upvar} {
set x 44
set y abcde
proc p1 {} {
upvar y x
lappend result $x
global x
lappend result $x
}
p1
} {abcde 44}
test upvar-7.1 {upvar to same level} {
set x 44
set y 55
catch {unset uv}
upvar #0 x uv
set uv abc
upvar 0 y uv
set uv xyzzy
list $x $y
} {abc xyzzy}
test upvar-7.2 {upvar to same level} {
set x 1234
set y 4567
proc p1 {x y} {
upvar 0 x uv
set uv $y
return "$x $y"
}
p1 44 89
} {89 89}
test upvar-7.3 {upvar to same level} {
set x 1234
set y 4567
proc p1 {x y} {
upvar #1 x uv
set uv $y
return "$x $y"
}
p1 xyz abc
} {abc abc}
test upvar-7.4 {upvar to same level: tricky problems when deleting variable table} {
proc tt {} {upvar #1 toto loc; return $loc}
list [catch tt msg] $msg
} {1 {can't read "loc": no such variable}}
test upvar-7.5 {potential memory leak when deleting variable table} {
proc leak {} {
array set foo {1 2 3 4}
upvar 0 foo(1) bar
}
leak
} {}
test upvar-8.1 {errors in upvar command} {
list [catch upvar msg] $msg
} {1 {wrong # args: should be "upvar ?level? otherVar localVar ?otherVar localVar ...?"}}
test upvar-8.2 {errors in upvar command} {
list [catch {upvar 1} msg] $msg
} {1 {wrong # args: should be "upvar ?level? otherVar localVar ?otherVar localVar ...?"}}
test upvar-8.3 {errors in upvar command} {
proc p1 {} {upvar a b c}
list [catch p1 msg] $msg
} {1 {wrong # args: should be "upvar ?level? otherVar localVar ?otherVar localVar ...?"}}
test upvar-8.4 {errors in upvar command} {
proc p1 {} {upvar 0 b b}
list [catch p1 msg] $msg
} {1 {can't upvar from variable to itself}}
test upvar-8.5 {errors in upvar command} {
proc p1 {} {upvar 0 a b; upvar 0 b a}
list [catch p1 msg] $msg
} {1 {can't upvar from variable to itself}}
test upvar-8.6 {errors in upvar command} {
proc p1 {} {set a 33; upvar b a}
list [catch p1 msg] $msg
} {1 {variable "a" already exists}}
test upvar-8.7 {errors in upvar command} {
proc p1 {} {trace variable a w foo; upvar b a}
list [catch p1 msg] $msg
} {1 {variable "a" has traces: can't use for upvar}}
test upvar-8.8 {create nested array with upvar} -body {
proc p1 {} {upvar x(a) b; set b(2) 44}
catch {unset x}
list [catch p1 msg] $msg
} -cleanup {
unset x
} -result {1 {can't set "b(2)": variable isn't array}}
test upvar-8.9 {upvar won't create namespace variable that refers to procedure variable} {
catch {namespace delete {*}[namespace children :: test_ns_*]}
catch {rename MakeLink ""}
namespace eval ::test_ns_1 {}
proc MakeLink {a} {
namespace eval ::test_ns_1 {
upvar a a
}
unset ::test_ns_1::a
}
list [catch {MakeLink 1} msg] $msg
} {1 {bad variable name "a": upvar won't create namespace variable that refers to procedure variable}}
test upvar-8.10 {upvar will create element alias for new array element} {
catch {unset upvarArray}
array set upvarArray {}
catch {upvar 0 upvarArray(elem) upvarArrayElemAlias}
} {0}
test upvar-8.11 {upvar will not create a variable that looks like an array} -body {
catch {unset upvarArray}
array set upvarArray {}
upvar 0 upvarArray(elem) upvarArrayElemAlias(elem)
} -returnCodes 1 -match glob -result *
test upvar-9.1 {Tcl_UpVar2 procedure} testupvar {
list [catch {testupvar xyz a {} x global} msg] $msg
} {1 {bad level "xyz"}}
test upvar-9.2 {Tcl_UpVar2 procedure} testupvar {
catch {unset a}
catch {unset x}
set a 44
list [catch "testupvar #0 a 1 x global" msg] $msg
} {1 {can't access "a(1)": variable isn't array}}
test upvar-9.3 {Tcl_UpVar2 procedure} testupvar {
proc foo {} {
testupvar 1 a {} x local
set x
}
catch {unset a}
catch {unset x}
set a 44
foo
} {44}
test upvar-9.4 {Tcl_UpVar2 procedure} testupvar {
proc foo {} {
testupvar 1 a {} _up_ global
list [catch {set x} msg] $msg
}
catch {unset a}
catch {unset _up_}
set a 44
concat [foo] $_up_
} {1 {can't read "x": no such variable} 44}
test upvar-9.5 {Tcl_UpVar2 procedure} testupvar {
proc foo {} {
testupvar 1 a b x local
set x
}
catch {unset a}
catch {unset x}
set a(b) 1234
foo
} {1234}
test upvar-9.6 {Tcl_UpVar procedure} testupvar {
proc foo {} {
testupvar 1 a x local
set x
}
catch {unset a}
catch {unset x}
set a xyzzy
foo
} {xyzzy}
test upvar-9.7 {Tcl_UpVar procedure} testupvar {
proc foo {} {
testupvar #0 a(b) x local
set x
}
catch {unset a}
catch {unset x}
set a(b) 1234
foo
} {1234}
catch {unset a}
test upvar-10.1 {CompileWord OBOE} -setup {
proc linenumber {} {dict get [info frame -1] line}
} -body {
apply {n {
upvar 1 {*}{
} [return [incr n -[linenumber]]] x
}} [linenumber]
} -cleanup {
rename linenumber {}
} -result 1
#
# Tests for 'namespace upvar'. As the implementation is essentially the same as
# for 'upvar', we only test that the variables are linked correctly. Ie, we
# assume that the behaviour of variables once the link is established has
# already been tested above.
#
#
# Clear out any namespaces called test_ns_*
catch {namespace delete {*}[namespace children :: test_ns_*]}
namespace eval test_ns_0 {
variable x test_ns_0
}
set x test_global
test upvar-NS-1.1 {nsupvar links to correct variable} \
-body {
namespace eval test_ns_1 {
namespace upvar ::test_ns_0 x w
set w
}
} \
-result {test_ns_0} \
-cleanup {namespace delete test_ns_1}
test upvar-NS-1.2 {nsupvar links to correct variable} \
-body {
namespace eval test_ns_1 {
proc a {} {
namespace upvar ::test_ns_0 x w
set w
}
return [a]
}
} \
-result {test_ns_0} \
-cleanup {namespace delete test_ns_1}
test upvar-NS-1.3 {nsupvar links to correct variable} \
-body {
namespace eval test_ns_1 {
namespace upvar test_ns_0 x w
set w
}
} \
-result {namespace "test_ns_0" not found in "::test_ns_1"} \
-returnCodes error \
-cleanup {namespace delete test_ns_1}
test upvar-NS-1.4 {nsupvar links to correct variable} \
-body {
namespace eval test_ns_1 {
proc a {} {
namespace upvar test_ns_0 x w
set w
}
return [a]
}
} \
-result {namespace "test_ns_0" not found in "::test_ns_1"} \
-returnCodes error \
-cleanup {namespace delete test_ns_1}
test upvar-NS-1.5 {nsupvar links to correct variable} \
-body {
namespace eval test_ns_1 {
namespace eval test_ns_0 {}
namespace upvar test_ns_0 x w
set w
}
} \
-result {can't read "w": no such variable} \
-returnCodes error \
-cleanup {namespace delete test_ns_1}
test upvar-NS-1.6 {nsupvar links to correct variable} \
-body {
namespace eval test_ns_1 {
namespace eval test_ns_0 {}
proc a {} {
namespace upvar test_ns_0 x w
set w
}
return [a]
}
} \
-result {can't read "w": no such variable} \
-returnCodes error \
-cleanup {namespace delete test_ns_1}
test upvar-NS-1.7 {nsupvar links to correct variable} \
-body {
namespace eval test_ns_1 {
namespace eval test_ns_0 {
variable x test_ns_1::test_ns_0
}
namespace upvar test_ns_0 x w
set w
}
} \
-result {test_ns_1::test_ns_0} \
-cleanup {namespace delete test_ns_1}
test upvar-NS-1.8 {nsupvar links to correct variable} \
-body {
namespace eval test_ns_1 {
namespace eval test_ns_0 {
variable x test_ns_1::test_ns_0
}
proc a {} {
namespace upvar test_ns_0 x w
set w
}
return [a]
}
} \
-result {test_ns_1::test_ns_0} \
-cleanup {namespace delete test_ns_1}
test upvar-NS-1.9 {nsupvar links to correct variable} \
-body {
namespace eval test_ns_1 {
variable x test_ns_1
proc a {} {
namespace upvar test_ns_0 x w
set w
}
return [a]
}
} \
-result {namespace "test_ns_0" not found in "::test_ns_1"} \
-returnCodes error \
-cleanup {namespace delete test_ns_1}
# cleanup
::tcltest::cleanupTests
return
|