Debian Codegraph @

tests/upvar.test

raw
1 # Commands covered: 'upvar', 'namespace upvar'
2 #
3 # This file contains a collection of tests for one or more of the Tcl built-in
4 # commands. Sourcing this file into Tcl runs the tests and generates output
5 # for errors. No output means no errors were found.
6 #
7 # Copyright (c) 1991-1993 The Regents of the University of California.
8 # Copyright (c) 1994 Sun Microsystems, Inc.
9 # Copyright (c) 1998-1999 Scriptics Corporation.
10 #
11 # See the file "license.terms" for information on usage and redistribution of
12 # this file, and for a DISCLAIMER OF ALL WARRANTIES.
13
14 if {"::tcltest" ni [namespace children]} {
15 package require tcltest 2.5
16 namespace import -force ::tcltest::*
17 }
18
19 ::tcltest::loadTestedCommands
20 catch [list package require -exact Tcltest [info patchlevel]]
21
22 testConstraint testupvar [llength [info commands testupvar]]
23
24 test upvar-1.1 {reading variables with upvar} {
25 proc p1 {a b} {set c 22; set d 33; p2}
26 proc p2 {} {upvar a x1 b x2 c x3 d x4; set a abc; list $x1 $x2 $x3 $x4 $a}
27 p1 foo bar
28 } {foo bar 22 33 abc}
29 test upvar-1.2 {reading variables with upvar} {
30 proc p1 {a b} {set c 22; set d 33; p2}
31 proc p2 {} {p3}
32 proc p3 {} {upvar 2 a x1 b x2 c x3 d x4; set a abc; list $x1 $x2 $x3 $x4 $a}
33 p1 foo bar
34 } {foo bar 22 33 abc}
35 test upvar-1.3 {reading variables with upvar} {
36 proc p1 {a b} {set c 22; set d 33; p2}
37 proc p2 {} {p3}
38 proc p3 {} {
39 upvar #1 a x1 b x2 c x3 d x4
40 set a abc
41 list $x1 $x2 $x3 $x4 $a
42 }
43 p1 foo bar
44 } {foo bar 22 33 abc}
45 test upvar-1.4 {reading variables with upvar} {
46 set x1 44
47 set x2 55
48 proc p1 {} {p2}
49 proc p2 {} {
50 upvar 2 x1 x1 x2 a
51 upvar #0 x1 b
52 set c $b
53 incr b 3
54 list $x1 $a $b
55 }
56 p1
57 } {47 55 47}
58 test upvar-1.5 {reading array elements with upvar} {
59 proc p1 {} {set a(0) zeroth; set a(1) first; p2}
60 proc p2 {} {upvar a(0) x; set x}
61 p1
62 } {zeroth}
63
64 test upvar-2.1 {writing variables with upvar} {
65 proc p1 {a b} {set c 22; set d 33; p2; list $a $b $c $d}
66 proc p2 {} {
67 upvar a x1 b x2 c x3 d x4
68 set x1 14
69 set x4 88
70 }
71 p1 foo bar
72 } {14 bar 22 88}
73 test upvar-2.2 {writing variables with upvar} {
74 set x1 44
75 set x2 55
76 proc p1 {x1 x2} {
77 upvar #0 x1 a
78 upvar x2 b
79 set a $x1
80 set b $x2
81 }
82 p1 newbits morebits
83 list $x1 $x2
84 } {newbits morebits}
85 test upvar-2.3 {writing variables with upvar} {
86 catch {unset x1}
87 catch {unset x2}
88 proc p1 {x1 x2} {
89 upvar #0 x1 a
90 upvar x2 b
91 set a $x1
92 set b $x2
93 }
94 p1 newbits morebits
95 list [catch {set x1} msg] $msg [catch {set x2} msg] $msg
96 } {0 newbits 0 morebits}
97 test upvar-2.4 {writing array elements with upvar} {
98 proc p1 {} {set a(0) zeroth; set a(1) first; list [p2] $a(0)}
99 proc p2 {} {upvar a(0) x; set x xyzzy}
100 p1
101 } {xyzzy xyzzy}
102
103 test upvar-3.1 {unsetting variables with upvar} {
104 proc p1 {a b} {set c 22; set d 33; p2; lsort [info vars]}
105 proc p2 {} {
106 upvar 1 a x1 d x2
107 unset x1 x2
108 }
109 p1 foo bar
110 } {b c}
111 test upvar-3.2 {unsetting variables with upvar} {
112 proc p1 {a b} {set c 22; set d 33; p2; lsort [info vars]}
113 proc p2 {} {
114 upvar 1 a x1 d x2
115 unset x1 x2
116 set x2 28
117 }
118 p1 foo bar
119 } {b c d}
120 test upvar-3.3 {unsetting variables with upvar} {
121 set x1 44
122 set x2 55
123 proc p1 {} {p2}
124 proc p2 {} {
125 upvar 2 x1 a
126 upvar #0 x2 b
127 unset a b
128 }
129 p1
130 list [info exists x1] [info exists x2]
131 } {0 0}
132 test upvar-3.4 {unsetting variables with upvar} {
133 set x1 44
134 set x2 55
135 proc p1 {} {
136 upvar x1 a x2 b
137 unset a b
138 set b 118
139 }
140 p1
141 list [info exists x1] [catch {set x2} msg] $msg
142 } {0 0 118}
143 test upvar-3.5 {unsetting array elements with upvar} {
144 proc p1 {} {
145 set a(0) zeroth
146 set a(1) first
147 set a(2) second
148 p2
149 array names a
150 }
151 proc p2 {} {upvar a(0) x; unset x}
152 lsort [p1]
153 } {1 2}
154 test upvar-3.6 {unsetting then resetting array elements with upvar} {
155 proc p1 {} {
156 set a(0) zeroth
157 set a(1) first
158 set a(2) second
159 p2
160 list [lsort [array names a]] [catch {set a(0)} msg] $msg
161 }
162 proc p2 {} {upvar a(0) x; unset x; set x 12345}
163 p1
164 } {{0 1 2} 0 12345}
165
166 test upvar-4.1 {nested upvars} {
167 set x1 88
168 proc p1 {a b} {set c 22; set d 33; p2}
169 proc p2 {} {global x1; upvar c x2; p3}
170 proc p3 {} {
171 upvar x1 a x2 b
172 list $a $b
173 }
174 p1 14 15
175 } {88 22}
176 test upvar-4.2 {nested upvars} {
177 set x1 88
178 proc p1 {a b} {set c 22; set d 33; p2; list $a $b $c $d}
179 proc p2 {} {global x1; upvar c x2; p3}
180 proc p3 {} {
181 upvar x1 a x2 b
182 set a foo
183 set b bar
184 }
185 list [p1 14 15] $x1
186 } {{14 15 bar 33} foo}
187
188 proc tproc {args} {global x; set x [list $args [uplevel info vars]]}
189 test upvar-5.1 {traces involving upvars} {
190 proc p1 {a b} {set c 22; set d 33; trace add var c {read write} tproc; p2}
191 proc p2 {} {upvar c x1; set x1 22}
192 set x ---
193 p1 foo bar
194 set x
195 } {{x1 {} write} x1}
196 test upvar-5.2 {traces involving upvars} {
197 proc p1 {a b} {set c 22; set d 33; trace add var c {read write} tproc; p2}
198 proc p2 {} {upvar c x1; set x1}
199 set x ---
200 p1 foo bar
201 set x
202 } {{x1 {} read} x1}
203 test upvar-5.3 {traces involving upvars} {
204 proc p1 {a b} {set c 22; set d 33; trace add var c {read write unset} tproc; p2}
205 proc p2 {} {upvar c x1; unset x1}
206 set x ---
207 p1 foo bar
208 set x
209 } {{x1 {} unset} x1}
210
211 test upvar-6.1 {retargeting an upvar} {
212 proc p1 {} {
213 set a(0) zeroth
214 set a(1) first
215 set a(2) second
216 p2
217 }
218 proc p2 {} {
219 upvar a x
220 set result {}
221 foreach i [array names x] {
222 upvar a($i) x
223 lappend result $x
224 }
225 lsort $result
226 }
227 p1
228 } {first second zeroth}
229 test upvar-6.2 {retargeting an upvar} {
230 set x 44
231 set y abcde
232 proc p1 {} {
233 global x
234 set result $x
235 upvar y x
236 lappend result $x
237 }
238 p1
239 } {44 abcde}
240 test upvar-6.3 {retargeting an upvar} {
241 set x 44
242 set y abcde
243 proc p1 {} {
244 upvar y x
245 lappend result $x
246 global x
247 lappend result $x
248 }
249 p1
250 } {abcde 44}
251
252 test upvar-7.1 {upvar to same level} {
253 set x 44
254 set y 55
255 catch {unset uv}
256 upvar #0 x uv
257 set uv abc
258 upvar 0 y uv
259 set uv xyzzy
260 list $x $y
261 } {abc xyzzy}
262 test upvar-7.2 {upvar to same level} {
263 set x 1234
264 set y 4567
265 proc p1 {x y} {
266 upvar 0 x uv
267 set uv $y
268 return "$x $y"
269 }
270 p1 44 89
271 } {89 89}
272 test upvar-7.3 {upvar to same level} {
273 set x 1234
274 set y 4567
275 proc p1 {x y} {
276 upvar #1 x uv
277 set uv $y
278 return "$x $y"
279 }
280 p1 xyz abc
281 } {abc abc}
282 test upvar-7.4 {upvar to same level: tricky problems when deleting variable table} {
283 proc tt {} {upvar #1 toto loc; return $loc}
284 list [catch tt msg] $msg
285 } {1 {can't read "loc": no such variable}}
286 test upvar-7.5 {potential memory leak when deleting variable table} {
287 proc leak {} {
288 array set foo {1 2 3 4}
289 upvar 0 foo(1) bar
290 }
291 leak
292 } {}
293
294 test upvar-8.1 {errors in upvar command} -returnCodes error -body {
295 upvar
296 } -result {wrong # args: should be "upvar ?level? otherVar localVar ?otherVar localVar ...?"}
297 test upvar-8.2 {errors in upvar command} -returnCodes error -body {
298 upvar 1
299 } -result {wrong # args: should be "upvar ?level? otherVar localVar ?otherVar localVar ...?"}
300 test upvar-8.2.1 {upvar with numeric first argument} {
301 apply {{} {set 0 ok; apply {{} {upvar 0 x; return $x}}}}
302 } ok
303 test upvar-8.3 {errors in upvar command} -returnCodes error -body {
304 proc p1 {} {upvar a b c}
305 p1
306 } -result {bad level "a"}
307 test upvar-8.3.1 {bad level for upvar (upvar at top-level, bug [775ee88560])} -body {
308 proc p1 {} { uplevel { upvar b b; lappend b UNEXPECTED } }
309 uplevel #0 { p1 }
310 } -returnCodes error -result {bad level "1"}
311 test upvar-8.3.2 {bad level for upvar (upvar at top-level, bug [775ee88560])} -setup {
312 interp create i
313 } -body {
314 i eval { upvar b b; lappend b UNEXPECTED }
315 } -returnCodes error -result {bad level "1"} -cleanup {
316 interp delete i
317 }
318 test upvar-8.4 {errors in upvar command} -returnCodes error -body {
319 proc p1 {} {upvar 0 b b}
320 p1
321 } -result {can't upvar from variable to itself}
322 test upvar-8.5 {errors in upvar command} -returnCodes error -body {
323 proc p1 {} {upvar 0 a b; upvar 0 b a}
324 p1
325 } -result {can't upvar from variable to itself}
326 test upvar-8.6 {errors in upvar command} -returnCodes error -body {
327 proc p1 {} {set a 33; upvar b a}
328 p1
329 } -result {variable "a" already exists}
330 test upvar-8.7 {errors in upvar command} -returnCodes error -body {
331 proc p1 {} {trace add variable a write foo; upvar b a}
332 p1
333 } -result {variable "a" has traces: can't use for upvar}
334 test upvar-8.8 {create nested array with upvar} -body {
335 proc p1 {} {upvar x(a) b; set b(2) 44}
336 catch {unset x}
337 p1
338 } -returnCodes error -cleanup {
339 unset x
340 } -result {can't set "b(2)": variable isn't array}
341 test upvar-8.9 {upvar won't create namespace variable that refers to procedure variable} -setup {
342 catch {namespace delete {*}[namespace children :: test_ns_*]}
343 catch {rename MakeLink ""}
344 namespace eval ::test_ns_1 {}
345 } -returnCodes error -body {
346 proc MakeLink {a} {
347 namespace eval ::test_ns_1 {
348 upvar a a
349 }
350 unset ::test_ns_1::a
351 }
352 MakeLink 1
353 } -result {bad variable name "a": can't create namespace variable that refers to procedure variable}
354 test upvar-8.10 {upvar will create element alias for new array element} -setup {
355 catch {unset upvarArray}
356 } -body {
357 array set upvarArray {}
358 catch {upvar 0 upvarArray(elem) upvarArrayElemAlias}
359 } -result {0}
360 test upvar-8.11 {upvar will not create a variable that looks like an array} -setup {
361 catch {unset upvarArray}
362 } -body {
363 array set upvarArray {}
364 upvar 0 upvarArray(elem) upvarArrayElemAlias(elem)
365 } -returnCodes 1 -match glob -result *
366
367 test upvar-9.1 {Tcl_UpVar2 procedure} testupvar {
368 list [catch {testupvar xyz a {} x global} msg] $msg
369 } {1 {bad level "1"}}
370 test upvar-9.1.1 {TclGetFrame, via Tcl_UpVar2} testupvar {
371 apply {{} {testupvar xyz a {} x local; set x foo}}
372 set a
373 } foo
374 test upvar-9.2 {Tcl_UpVar2 procedure} testupvar {
375 catch {unset a}
376 catch {unset x}
377 set a 44
378 list [catch "testupvar #0 a 1 x global" msg] $msg
379 } {1 {can't access "a(1)": variable isn't array}}
380 test upvar-9.3 {Tcl_UpVar2 procedure} testupvar {
381 proc foo {} {
382 testupvar 1 a {} x local
383 set x
384 }
385 catch {unset a}
386 catch {unset x}
387 set a 44
388 foo
389 } {44}
390 test upvar-9.4 {Tcl_UpVar2 procedure} testupvar {
391 proc foo {} {
392 testupvar 1 a {} _up_ global
393 list [catch {set x} msg] $msg
394 }
395 catch {unset a}
396 catch {unset _up_}
397 set a 44
398 concat [foo] $_up_
399 } {1 {can't read "x": no such variable} 44}
400 test upvar-9.5 {Tcl_UpVar2 procedure} testupvar {
401 proc foo {} {
402 testupvar 1 a b x local
403 set x
404 }
405 catch {unset a}
406 catch {unset x}
407 set a(b) 1234
408 foo
409 } {1234}
410 test upvar-9.6 {Tcl_UpVar procedure} testupvar {
411 proc foo {} {
412 testupvar 1 a x local
413 set x
414 }
415 catch {unset a}
416 catch {unset x}
417 set a xyzzy
418 foo
419 } {xyzzy}
420 test upvar-9.7 {Tcl_UpVar procedure} testupvar {
421 proc foo {} {
422 testupvar #0 a(b) x local
423 set x
424 }
425 catch {unset a}
426 catch {unset x}
427 set a(b) 1234
428 foo
429 } {1234}
430 catch {unset a}
431
432 test upvar-10.1 {CompileWord OBOE} -setup {
433 proc linenumber {} {dict get [info frame -1] line}
434 } -body {
435 apply {n {
436 upvar 1 {*}{
437 } [return [incr n -[linenumber]]] x
438 }} [linenumber]
439 } -cleanup {
440 rename linenumber {}
441 } -result 1
442
443 #
444 # Tests for 'namespace upvar'. As the implementation is essentially the same as
445 # for 'upvar', we only test that the variables are linked correctly, i.e., we
446 # assume that the behaviour of variables once the link is established has
447 # already been tested above.
448 #
449
450 # Clear out any namespaces called test_ns_*
451 catch {namespace delete {*}[namespace children :: test_ns_*]}
452 namespace eval test_ns_0 {
453 variable x test_ns_0
454 }
455 set ::x test_global
456
457 test upvar-NS-1.1 {nsupvar links to correct variable} -body {
458 namespace eval test_ns_1 {
459 namespace upvar ::test_ns_0 x w
460 set w
461 }
462 } -result {test_ns_0} -cleanup {
463 namespace delete test_ns_1
464 }
465 test upvar-NS-1.2 {nsupvar links to correct variable} -body {
466 namespace eval test_ns_1 {
467 proc a {} {
468 namespace upvar ::test_ns_0 x w
469 set w
470 }
471 return [a]
472 }
473 } -result {test_ns_0} -cleanup {
474 namespace delete test_ns_1
475 }
476 test upvar-NS-1.3 {nsupvar links to correct variable} -body {
477 namespace eval test_ns_1 {
478 namespace upvar test_ns_0 x w
479 set w
480 }
481 } -returnCodes error -cleanup {
482 namespace delete test_ns_1
483 } -result {namespace "test_ns_0" not found in "::test_ns_1"}
484 test upvar-NS-1.4 {nsupvar links to correct variable} -body {
485 namespace eval test_ns_1 {
486 proc a {} {
487 namespace upvar test_ns_0 x w
488 set w
489 }
490 return [a]
491 }
492 } -returnCodes error -cleanup {
493 namespace delete test_ns_1
494 } -result {namespace "test_ns_0" not found in "::test_ns_1"}
495
496 test upvar-NS-1.5 {nsupvar links to correct variable} -body {
497 namespace eval test_ns_1 {
498 namespace eval test_ns_0 {}
499 namespace upvar test_ns_0 x w
500 set w
501 }
502 } -cleanup {
503 namespace delete test_ns_1
504 } -result {can't read "w": no such variable} -returnCodes error
505 test upvar-NS-1.6 {nsupvar links to correct variable} -body {
506 namespace eval test_ns_1 {
507 namespace eval test_ns_0 {}
508 proc a {} {
509 namespace upvar test_ns_0 x w
510 set w
511 }
512 return [a]
513 }
514 } -cleanup {
515 namespace delete test_ns_1
516 } -result {can't read "w": no such variable} -returnCodes error
517 test upvar-NS-1.7 {nsupvar links to correct variable} -body {
518 namespace eval test_ns_1 {
519 namespace eval test_ns_0 {
520 variable x test_ns_1::test_ns_0
521 }
522 namespace upvar test_ns_0 x w
523 set w
524 }
525 } -cleanup {
526 namespace delete test_ns_1
527 } -result {test_ns_1::test_ns_0}
528 test upvar-NS-1.8 {nsupvar links to correct variable} -body {
529 namespace eval test_ns_1 {
530 namespace eval test_ns_0 {
531 variable x test_ns_1::test_ns_0
532 }
533 proc a {} {
534 namespace upvar test_ns_0 x w
535 set w
536 }
537 return [a]
538 }
539 } -cleanup {
540 namespace delete test_ns_1
541 } -result {test_ns_1::test_ns_0}
542 test upvar-NS-1.9 {nsupvar links to correct variable} -body {
543 namespace eval test_ns_1 {
544 variable x test_ns_1
545 proc a {} {
546 namespace upvar test_ns_0 x w
547 set w
548 }
549 return [a]
550 }
551 } -returnCodes error -cleanup {
552 namespace delete test_ns_1
553 } -result {namespace "test_ns_0" not found in "::test_ns_1"}
554
555 test upvar-NS-2.1 {TIP 323} -returnCodes error -body {
556 namespace upvar
557 } -result {wrong # args: should be "namespace upvar ns ?otherVar myVar ...?"}
558 test upvar-NS-2.2 {TIP 323} -setup {
559 namespace eval test_ns_1 {}
560 } -body {
561 namespace upvar test_ns_1
562 } -cleanup {
563 namespace delete test_ns_1
564 } -result {}
565
566 test upvar-NS-3.1 {CompileWord OBOE} -setup {
567 proc linenumber {} {dict get [info frame -1] line}
568 } -body {
569 apply {n {
570 namespace upvar {*}{
571 } [return [incr n -[linenumber]]] x y
572 }} [linenumber]
573 } -cleanup {
574 rename linenumber {}
575 } -result 1
576 test upvar-NS-3.2 {CompileWord OBOE} -setup {
577 proc linenumber {} {dict get [info frame -1] line}
578 } -body {
579 apply {n {
580 namespace upvar :: {*}{
581 } [return [incr n -[linenumber]]] x
582 }} [linenumber]
583 } -cleanup {
584 rename linenumber {}
585 } -result 1
586 test upvar-NS-3.3 {CompileWord OBOE} -setup {
587 proc linenumber {} {dict get [info frame -1] line}
588 } -body {
589 apply {n {
590 variable x {*}{
591 } [return [incr n -[linenumber]]]
592 }} [linenumber]
593 } -cleanup {
594 rename linenumber {}
595 } -result 1
596
597 # cleanup
598 ::tcltest::cleanupTests
599 return
600
601 # Local Variables:
602 # mode: tcl
603 # End: