1 # Commands covered: upvar
3 # This file contains a collection of tests for one or more of the Tcl
4 # built-in commands. Sourcing this file into Tcl runs the tests and
5 # generates output for errors. No output means no errors were found.
7 # Copyright (c) 1991-1993 The Regents of the University of California.
8 # Copyright (c) 1994 Sun Microsystems, Inc.
9 # Copyright (c) 1998-1999 by Scriptics Corporation.
11 # See the file "license.terms" for information on usage and redistribution
12 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 # RCS: @(#) $Id: upvar.test,v 1.7 2000/04/10 17:19:05 ericm Exp $
16 source [file dirname [info script]]/testing.tcl
20 test upvar-1.1 {reading variables with upvar} {
21 proc p1 {a b} {set c 22; set d 33; p2}
22 proc p2 {} {upvar a x1 b x2 c x3 d x4; set a abc; list $x1 $x2 $x3 $x4 $a}
25 test upvar-1.2 {reading variables with upvar} {
26 proc p1 {a b} {set c 22; set d 33; p2}
28 proc p3 {} {upvar 2 a x1 b x2 c x3 d x4; set a abc; list $x1 $x2 $x3 $x4 $a}
31 test upvar-1.3 {reading variables with upvar} {
32 proc p1 {a b} {set c 22; set d 33; p2}
35 upvar #1 a x1 b x2 c x3 d x4
37 list $x1 $x2 $x3 $x4 $a
41 test upvar-1.4 {reading variables with upvar} {
54 test upvar-1.5 {reading array elements with upvar} {
55 proc p1 {} {set a(0) zeroth; set a(1) first; p2}
56 proc p2 {} {upvar a(0) x; set x}
60 test upvar-2.1 {writing variables with upvar} {
61 proc p1 {a b} {set c 22; set d 33; p2; list $a $b $c $d}
63 upvar a x1 b x2 c x3 d x4
69 test upvar-2.2 {writing variables with upvar} {
81 test upvar-2.3 {writing variables with upvar} {
91 list [catch {set x1} msg] $msg [catch {set x2} msg] $msg
92 } {0 newbits 0 morebits}
93 test upvar-2.4 {writing array elements with upvar} {
94 proc p1 {} {set a(0) zeroth; set a(1) first; list [p2] $a(0)}
95 proc p2 {} {upvar a(0) x; set x xyzzy}
99 test upvar-3.1 {unsetting variables with upvar} {
100 proc p1 {a b} {set c 22; set d 33; p2; lsort [info vars]}
107 test upvar-3.2 {unsetting variables with upvar} {
108 proc p1 {a b} {set c 22; set d 33; p2; lsort [info vars]}
116 test upvar-3.3 {unsetting variables with upvar} {
126 list [info exists x1] [info exists x2]
128 test upvar-3.4 {unsetting variables with upvar} {
137 list [info exists x1] [catch {set x2} msg] $msg
139 test upvar-3.5 {unsetting array elements with upvar} {
145 lsort [array names a]
147 proc p2 {} {upvar a(0) x; unset x}
150 test upvar-3.6 {unsetting then resetting array elements with upvar} {
156 list [lsort [array names a]] [catch {set a(0)} msg] $msg
158 proc p2 {} {upvar a(0) x; unset x; set x 12345}
162 test upvar-4.1 {nested upvars} {
164 proc p1 {a b} {set c 22; set d 33; p2}
165 proc p2 {} {global x1; upvar c x2; p3}
172 test upvar-4.2 {nested upvars} {
174 proc p1 {a b} {set c 22; set d 33; p2; list $a $b $c $d}
175 proc p2 {} {global x1; upvar c x2; p3}
182 } {{14 15 bar 33} foo}
184 proc tproc {args} {global x; set x [list $args [uplevel info vars]]}
186 test upvar-6.1 {retargeting an upvar} {
196 foreach i [array names x] {
203 } {first second zeroth}
204 test upvar-6.2 {retargeting an upvar} {
215 test upvar-6.3 {retargeting an upvar} {
227 test upvar-7.1 {upvar to same level} {
237 test upvar-7.2 {upvar to same level} {
247 test upvar-7.3 {upvar to same level} {
257 test upvar-7.4 {upvar to same level: tricky problems when deleting variable table} {
258 proc tt {} {upvar #1 toto loc; return $loc}
259 list [catch tt msg] $msg
260 } {1 {can't read "loc": no such variable}}
261 test upvar-7.5 {potential memory leak when deleting variable table} {
263 array set foo {1 2 3 4}
269 test upvar-8.1 {errors in upvar command} {
272 test upvar-8.2 {errors in upvar command} {
275 test upvar-8.3 {errors in upvar command} {
276 proc p1 {} {upvar a b c}
279 test upvar-8.4 {errors in upvar command} {
280 proc p1 {} {upvar 0 b b}
281 list [catch p1 msg] $msg
282 } {1 {can't upvar from variable to itself}}
283 test upvar-8.5 {errors in upvar command} {
284 proc p1 {} {upvar 0 a b; upvar 0 b a}
285 list [catch p1 msg] $msg
286 } {1 {can't upvar from variable to itself}}
287 test upvar-8.6 {errors in upvar command} {
288 proc p1 {} {set a 33; upvar b a}
289 list [catch p1 msg] $msg
290 } {1 {variable "a" already exists}}
291 # Jim allows dicts within dicts. Tcl can't do this.
292 test upvar-8.8 {create nested array with upvar} jim {
293 proc p1 {} {upvar x(a) b; set b(2) 44}
298 test upvar-8.10 {upvar will create element alias for new array element} {
299 catch {unset upvarArray}
300 array set upvarArray {}
301 catch {upvar 0 upvarArray(elem) upvarArrayElemAlias}
303 test upvar-8.11 {error upvar array element} {
304 proc a {} { upvar a b(1) }
305 list [catch {a} msg] $msg
306 } {1 {bad variable name "b(1)": upvar won't create a scalar variable that looks like an array element}}
307 test upvar-9.1 {global redefine} {
308 proc p1 {} { global x; global x }
311 test upvar-9.2 {upvar redefine} {
314 proc p1 {} { upvar a x; upvar b x; return $x }
317 test upvar-9.3 {upvar redefine static} jim {
318 proc p1 {} {{a 3}} { upvar b a; return $b }
319 list [catch p1 msg] $msg
320 } {1 {variable "a" already exists}}
321 test upvar-9.4 {upvar links to static} jim {
322 proc p1 {} {} { upvar a x; incr x; return $x }
323 proc p2 {} {{a 3}} { list [p1] $a }
326 test upvar-9.5 {upvar via global namespace} {
330 proc p1 {} { upvar x ::y; incr ::y -1 }
335 test upvar-9.6 {upvar via global namespace} {
339 proc p1 {} { upvar x ::x; incr ::x }
340 list [catch p1 msg] $msg
341 } {1 {can't upvar from variable to itself}}
343 test upvar-9.7 {upvar to higher level} {
344 proc p1 {} { upvar 0 x ::globx }
345 list [catch p1 msg] $msg
346 } {1 {bad variable name "::globx": upvar won't create namespace variable that refers to procedure variable}}