sl@0: # Commands covered: upvar sl@0: # sl@0: # This file contains a collection of tests for one or more of the Tcl sl@0: # built-in commands. Sourcing this file into Tcl runs the tests and sl@0: # generates output for errors. No output means no errors were found. sl@0: # sl@0: # Copyright (c) 1991-1993 The Regents of the University of California. sl@0: # Copyright (c) 1994 Sun Microsystems, Inc. sl@0: # Copyright (c) 1998-1999 by Scriptics Corporation. sl@0: # sl@0: # See the file "license.terms" for information on usage and redistribution sl@0: # of this file, and for a DISCLAIMER OF ALL WARRANTIES. sl@0: # sl@0: # RCS: @(#) $Id: upvar.test,v 1.7 2000/04/10 17:19:05 ericm Exp $ sl@0: sl@0: if {[lsearch [namespace children] ::tcltest] == -1} { sl@0: package require tcltest sl@0: namespace import -force ::tcltest::* sl@0: } sl@0: sl@0: test upvar-1.1 {reading variables with upvar} { sl@0: proc p1 {a b} {set c 22; set d 33; p2} sl@0: proc p2 {} {upvar a x1 b x2 c x3 d x4; set a abc; list $x1 $x2 $x3 $x4 $a} sl@0: p1 foo bar sl@0: } {foo bar 22 33 abc} sl@0: test upvar-1.2 {reading variables with upvar} { sl@0: proc p1 {a b} {set c 22; set d 33; p2} sl@0: proc p2 {} {p3} sl@0: proc p3 {} {upvar 2 a x1 b x2 c x3 d x4; set a abc; list $x1 $x2 $x3 $x4 $a} sl@0: p1 foo bar sl@0: } {foo bar 22 33 abc} sl@0: test upvar-1.3 {reading variables with upvar} { sl@0: proc p1 {a b} {set c 22; set d 33; p2} sl@0: proc p2 {} {p3} sl@0: proc p3 {} { sl@0: upvar #1 a x1 b x2 c x3 d x4 sl@0: set a abc sl@0: list $x1 $x2 $x3 $x4 $a sl@0: } sl@0: p1 foo bar sl@0: } {foo bar 22 33 abc} sl@0: test upvar-1.4 {reading variables with upvar} { sl@0: set x1 44 sl@0: set x2 55 sl@0: proc p1 {} {p2} sl@0: proc p2 {} { sl@0: upvar 2 x1 x1 x2 a sl@0: upvar #0 x1 b sl@0: set c $b sl@0: incr b 3 sl@0: list $x1 $a $b sl@0: } sl@0: p1 sl@0: } {47 55 47} sl@0: test upvar-1.5 {reading array elements with upvar} { sl@0: proc p1 {} {set a(0) zeroth; set a(1) first; p2} sl@0: proc p2 {} {upvar a(0) x; set x} sl@0: p1 sl@0: } {zeroth} sl@0: sl@0: test upvar-2.1 {writing variables with upvar} { sl@0: proc p1 {a b} {set c 22; set d 33; p2; list $a $b $c $d} sl@0: proc p2 {} { sl@0: upvar a x1 b x2 c x3 d x4 sl@0: set x1 14 sl@0: set x4 88 sl@0: } sl@0: p1 foo bar sl@0: } {14 bar 22 88} sl@0: test upvar-2.2 {writing variables with upvar} { sl@0: set x1 44 sl@0: set x2 55 sl@0: proc p1 {x1 x2} { sl@0: upvar #0 x1 a sl@0: upvar x2 b sl@0: set a $x1 sl@0: set b $x2 sl@0: } sl@0: p1 newbits morebits sl@0: list $x1 $x2 sl@0: } {newbits morebits} sl@0: test upvar-2.3 {writing variables with upvar} { sl@0: catch {unset x1} sl@0: catch {unset x2} sl@0: proc p1 {x1 x2} { sl@0: upvar #0 x1 a sl@0: upvar x2 b sl@0: set a $x1 sl@0: set b $x2 sl@0: } sl@0: p1 newbits morebits sl@0: list [catch {set x1} msg] $msg [catch {set x2} msg] $msg sl@0: } {0 newbits 0 morebits} sl@0: test upvar-2.4 {writing array elements with upvar} { sl@0: proc p1 {} {set a(0) zeroth; set a(1) first; list [p2] $a(0)} sl@0: proc p2 {} {upvar a(0) x; set x xyzzy} sl@0: p1 sl@0: } {xyzzy xyzzy} sl@0: sl@0: test upvar-3.1 {unsetting variables with upvar} { sl@0: proc p1 {a b} {set c 22; set d 33; p2; lsort [info vars]} sl@0: proc p2 {} { sl@0: upvar 1 a x1 d x2 sl@0: unset x1 x2 sl@0: } sl@0: p1 foo bar sl@0: } {b c} sl@0: test upvar-3.2 {unsetting variables with upvar} { sl@0: proc p1 {a b} {set c 22; set d 33; p2; lsort [info vars]} sl@0: proc p2 {} { sl@0: upvar 1 a x1 d x2 sl@0: unset x1 x2 sl@0: set x2 28 sl@0: } sl@0: p1 foo bar sl@0: } {b c d} sl@0: test upvar-3.3 {unsetting variables with upvar} { sl@0: set x1 44 sl@0: set x2 55 sl@0: proc p1 {} {p2} sl@0: proc p2 {} { sl@0: upvar 2 x1 a sl@0: upvar #0 x2 b sl@0: unset a b sl@0: } sl@0: p1 sl@0: list [info exists x1] [info exists x2] sl@0: } {0 0} sl@0: test upvar-3.4 {unsetting variables with upvar} { sl@0: set x1 44 sl@0: set x2 55 sl@0: proc p1 {} { sl@0: upvar x1 a x2 b sl@0: unset a b sl@0: set b 118 sl@0: } sl@0: p1 sl@0: list [info exists x1] [catch {set x2} msg] $msg sl@0: } {0 0 118} sl@0: test upvar-3.5 {unsetting array elements with upvar} { sl@0: proc p1 {} { sl@0: set a(0) zeroth sl@0: set a(1) first sl@0: set a(2) second sl@0: p2 sl@0: array names a sl@0: } sl@0: proc p2 {} {upvar a(0) x; unset x} sl@0: p1 sl@0: } {1 2} sl@0: test upvar-3.6 {unsetting then resetting array elements with upvar} { sl@0: proc p1 {} { sl@0: set a(0) zeroth sl@0: set a(1) first sl@0: set a(2) second sl@0: p2 sl@0: list [array names a] [catch {set a(0)} msg] $msg sl@0: } sl@0: proc p2 {} {upvar a(0) x; unset x; set x 12345} sl@0: p1 sl@0: } {{0 1 2} 0 12345} sl@0: sl@0: test upvar-4.1 {nested upvars} { sl@0: set x1 88 sl@0: proc p1 {a b} {set c 22; set d 33; p2} sl@0: proc p2 {} {global x1; upvar c x2; p3} sl@0: proc p3 {} { sl@0: upvar x1 a x2 b sl@0: list $a $b sl@0: } sl@0: p1 14 15 sl@0: } {88 22} sl@0: test upvar-4.2 {nested upvars} { sl@0: set x1 88 sl@0: proc p1 {a b} {set c 22; set d 33; p2; list $a $b $c $d} sl@0: proc p2 {} {global x1; upvar c x2; p3} sl@0: proc p3 {} { sl@0: upvar x1 a x2 b sl@0: set a foo sl@0: set b bar sl@0: } sl@0: list [p1 14 15] $x1 sl@0: } {{14 15 bar 33} foo} sl@0: sl@0: proc tproc {args} {global x; set x [list $args [uplevel info vars]]} sl@0: test upvar-5.1 {traces involving upvars} { sl@0: proc p1 {a b} {set c 22; set d 33; trace var c rw tproc; p2} sl@0: proc p2 {} {upvar c x1; set x1 22} sl@0: set x --- sl@0: p1 foo bar sl@0: set x sl@0: } {{x1 {} w} x1} sl@0: test upvar-5.2 {traces involving upvars} { sl@0: proc p1 {a b} {set c 22; set d 33; trace var c rw tproc; p2} sl@0: proc p2 {} {upvar c x1; set x1} sl@0: set x --- sl@0: p1 foo bar sl@0: set x sl@0: } {{x1 {} r} x1} sl@0: test upvar-5.3 {traces involving upvars} { sl@0: proc p1 {a b} {set c 22; set d 33; trace var c rwu tproc; p2} sl@0: proc p2 {} {upvar c x1; unset x1} sl@0: set x --- sl@0: p1 foo bar sl@0: set x sl@0: } {{x1 {} u} x1} sl@0: sl@0: test upvar-6.1 {retargeting an upvar} { sl@0: proc p1 {} { sl@0: set a(0) zeroth sl@0: set a(1) first sl@0: set a(2) second sl@0: p2 sl@0: } sl@0: proc p2 {} { sl@0: upvar a x sl@0: set result {} sl@0: foreach i [array names x] { sl@0: upvar a($i) x sl@0: lappend result $x sl@0: } sl@0: lsort $result sl@0: } sl@0: p1 sl@0: } {first second zeroth} sl@0: test upvar-6.2 {retargeting an upvar} { sl@0: set x 44 sl@0: set y abcde sl@0: proc p1 {} { sl@0: global x sl@0: set result $x sl@0: upvar y x sl@0: lappend result $x sl@0: } sl@0: p1 sl@0: } {44 abcde} sl@0: test upvar-6.3 {retargeting an upvar} { sl@0: set x 44 sl@0: set y abcde sl@0: proc p1 {} { sl@0: upvar y x sl@0: lappend result $x sl@0: global x sl@0: lappend result $x sl@0: } sl@0: p1 sl@0: } {abcde 44} sl@0: sl@0: test upvar-7.1 {upvar to same level} { sl@0: set x 44 sl@0: set y 55 sl@0: catch {unset uv} sl@0: upvar #0 x uv sl@0: set uv abc sl@0: upvar 0 y uv sl@0: set uv xyzzy sl@0: list $x $y sl@0: } {abc xyzzy} sl@0: test upvar-7.2 {upvar to same level} { sl@0: set x 1234 sl@0: set y 4567 sl@0: proc p1 {x y} { sl@0: upvar 0 x uv sl@0: set uv $y sl@0: return "$x $y" sl@0: } sl@0: p1 44 89 sl@0: } {89 89} sl@0: test upvar-7.3 {upvar to same level} { sl@0: set x 1234 sl@0: set y 4567 sl@0: proc p1 {x y} { sl@0: upvar #1 x uv sl@0: set uv $y sl@0: return "$x $y" sl@0: } sl@0: p1 xyz abc sl@0: } {abc abc} sl@0: test upvar-7.4 {upvar to same level: tricky problems when deleting variable table} { sl@0: proc tt {} {upvar #1 toto loc; return $loc} sl@0: list [catch tt msg] $msg sl@0: } {1 {can't read "loc": no such variable}} sl@0: test upvar-7.5 {potential memory leak when deleting variable table} { sl@0: proc leak {} { sl@0: array set foo {1 2 3 4} sl@0: upvar 0 foo(1) bar sl@0: } sl@0: leak sl@0: } {} sl@0: sl@0: test upvar-8.1 {errors in upvar command} { sl@0: list [catch upvar msg] $msg sl@0: } {1 {wrong # args: should be "upvar ?level? otherVar localVar ?otherVar localVar ...?"}} sl@0: test upvar-8.2 {errors in upvar command} { sl@0: list [catch {upvar 1} msg] $msg sl@0: } {1 {wrong # args: should be "upvar ?level? otherVar localVar ?otherVar localVar ...?"}} sl@0: test upvar-8.3 {errors in upvar command} { sl@0: proc p1 {} {upvar a b c} sl@0: list [catch p1 msg] $msg sl@0: } {1 {wrong # args: should be "upvar ?level? otherVar localVar ?otherVar localVar ...?"}} sl@0: test upvar-8.4 {errors in upvar command} { sl@0: proc p1 {} {upvar 0 b b} sl@0: list [catch p1 msg] $msg sl@0: } {1 {can't upvar from variable to itself}} sl@0: test upvar-8.5 {errors in upvar command} { sl@0: proc p1 {} {upvar 0 a b; upvar 0 b a} sl@0: list [catch p1 msg] $msg sl@0: } {1 {can't upvar from variable to itself}} sl@0: test upvar-8.6 {errors in upvar command} { sl@0: proc p1 {} {set a 33; upvar b a} sl@0: list [catch p1 msg] $msg sl@0: } {1 {variable "a" already exists}} sl@0: test upvar-8.7 {errors in upvar command} { sl@0: proc p1 {} {trace variable a w foo; upvar b a} sl@0: list [catch p1 msg] $msg sl@0: } {1 {variable "a" has traces: can't use for upvar}} sl@0: test upvar-8.8 {create nested array with upvar} { sl@0: proc p1 {} {upvar x(a) b; set b(2) 44} sl@0: catch {unset x} sl@0: list [catch p1 msg] $msg sl@0: } {1 {can't set "b(2)": variable isn't array}} sl@0: test upvar-8.9 {upvar won't create namespace variable that refers to procedure variable} { sl@0: catch {eval namespace delete [namespace children :: test_ns_*]} sl@0: catch {rename MakeLink ""} sl@0: namespace eval ::test_ns_1 {} sl@0: proc MakeLink {a} { sl@0: namespace eval ::test_ns_1 { sl@0: upvar a a sl@0: } sl@0: unset ::test_ns_1::a sl@0: } sl@0: list [catch {MakeLink 1} msg] $msg sl@0: } {1 {bad variable name "a": upvar won't create namespace variable that refers to procedure variable}} sl@0: test upvar-8.10 {upvar will create element alias for new array element} { sl@0: catch {unset upvarArray} sl@0: array set upvarArray {} sl@0: catch {upvar 0 upvarArray(elem) upvarArrayElemAlias} sl@0: } {0} sl@0: sl@0: if {[info commands testupvar] != {}} { sl@0: test upvar-9.1 {Tcl_UpVar2 procedure} { sl@0: list [catch {testupvar xyz a {} x global} msg] $msg sl@0: } {1 {bad level "xyz"}} sl@0: test upvar-9.2 {Tcl_UpVar2 procedure} { sl@0: catch {unset a} sl@0: catch {unset x} sl@0: set a 44 sl@0: list [catch {testupvar #0 a 1 x global} msg] $msg sl@0: } {1 {can't access "a(1)": variable isn't array}} sl@0: test upvar-9.3 {Tcl_UpVar2 procedure} { sl@0: proc foo {} { sl@0: testupvar 1 a {} x local sl@0: set x sl@0: } sl@0: catch {unset a} sl@0: catch {unset x} sl@0: set a 44 sl@0: foo sl@0: } {44} sl@0: test upvar-9.4 {Tcl_UpVar2 procedure} { sl@0: proc foo {} { sl@0: testupvar 1 a {} _up_ global sl@0: list [catch {set x} msg] $msg sl@0: } sl@0: catch {unset a} sl@0: catch {unset _up_} sl@0: set a 44 sl@0: concat [foo] $_up_ sl@0: } {1 {can't read "x": no such variable} 44} sl@0: test upvar-9.5 {Tcl_UpVar2 procedure} { sl@0: proc foo {} { sl@0: testupvar 1 a b x local sl@0: set x sl@0: } sl@0: catch {unset a} sl@0: catch {unset x} sl@0: set a(b) 1234 sl@0: foo sl@0: } {1234} sl@0: test upvar-9.6 {Tcl_UpVar procedure} { sl@0: proc foo {} { sl@0: testupvar 1 a x local sl@0: set x sl@0: } sl@0: catch {unset a} sl@0: catch {unset x} sl@0: set a xyzzy sl@0: foo sl@0: } {xyzzy} sl@0: test upvar-9.7 {Tcl_UpVar procedure} { sl@0: proc foo {} { sl@0: testupvar #0 a(b) x local sl@0: set x sl@0: } sl@0: catch {unset a} sl@0: catch {unset x} sl@0: set a(b) 1234 sl@0: foo sl@0: } {1234} sl@0: } sl@0: catch {unset a} sl@0: sl@0: # cleanup sl@0: ::tcltest::cleanupTests sl@0: return sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: