os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/tests/var.test
Update contrib.
1 # This file contains tests for the tclVar.c source file. Tests appear in
2 # the same order as the C code that they test. The set of tests is
3 # currently incomplete since it currently includes only new tests for
4 # code changed for the addition of Tcl namespaces. Other variable-
5 # related tests appear in several other test files including
6 # namespace.test, set.test, trace.test, and upvar.test.
8 # Sourcing this file into Tcl runs the tests and generates output for
9 # errors. No output means no errors were found.
11 # Copyright (c) 1997 Sun Microsystems, Inc.
12 # Copyright (c) 1998-1999 by Scriptics Corporation.
14 # See the file "license.terms" for information on usage and redistribution
15 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
17 # RCS: @(#) $Id: var.test,v 1.20.2.4 2007/03/13 15:59:53 dgp Exp $
20 if {[lsearch [namespace children] ::tcltest] == -1} {
21 package require tcltest 2.2
22 namespace import -force ::tcltest::*
26 catch {namespace delete test_ns_var}
34 test var-1.1 {TclLookupVar, Array handling} {
36 set x "incr" ;# force no compilation and runtime call to Tcl_IncrCmd
39 list [$x i] $i [$x arr(foo)] $arr(foo)
41 test var-1.2 {TclLookupVar, TCL_GLOBAL_ONLY implies global namespace var} {
43 namespace eval test_ns_var {
44 variable x "namespace value"
46 global x ;# specifies TCL_GLOBAL_ONLY to get global x
52 test var-1.3 {TclLookupVar, TCL_NAMESPACE_ONLY implies namespace var} {
53 namespace eval test_ns_var {
55 variable x ;# specifies TCL_NAMESPACE_ONLY to get namespace x
61 test var-1.4 {TclLookupVar, no active call frame implies global namespace var} {
64 test var-1.5 {TclLookupVar, active call frame pushed for namespace eval implies namespace var} {
65 namespace eval test_ns_var {set x}
67 test var-1.6 {TclLookupVar, name starts with :: implies some namespace var} {
68 namespace eval test_ns_var {set ::x}
70 test var-1.7 {TclLookupVar, error finding namespace var} {
71 list [catch {set a:::b} msg] $msg
72 } {1 {can't read "a:::b": no such variable}}
73 test var-1.8 {TclLookupVar, error finding namespace var} {
74 list [catch {set ::foobarfoo} msg] $msg
75 } {1 {can't read "::foobarfoo": no such variable}}
76 test var-1.9 {TclLookupVar, create new namespace var} {
77 namespace eval test_ns_var {
81 test var-1.10 {TclLookupVar, create new namespace var} {
83 namespace eval test_ns_var {
88 test var-1.11 {TclLookupVar, error creating new namespace var} {
89 namespace eval test_ns_var {
90 list [catch {set ::test_ns_var::foo::bar 314159} msg] $msg
92 } {1 {can't set "::test_ns_var::foo::bar": parent namespace doesn't exist}}
93 test var-1.12 {TclLookupVar, error creating new namespace var} {
94 namespace eval test_ns_var {
95 list [catch {set ::test_ns_var::foo:: 1997} msg] $msg
97 } {1 {can't set "::test_ns_var::foo::": parent namespace doesn't exist}}
98 test var-1.13 {TclLookupVar, new namespace var is created in a particular namespace} {
99 catch {unset aNeWnAmEiNnS}
100 namespace eval test_ns_var {
101 namespace eval test_ns_var2::test_ns_var3 {
102 set aNeWnAmEiNnS 77777
104 # namespace which builds a name by traversing nsPtr chain to ::
105 namespace which -variable test_ns_var2::test_ns_var3::aNeWnAmEiNnS
107 } {::test_ns_var::test_ns_var2::test_ns_var3::aNeWnAmEiNnS}
108 test var-1.14 {TclLookupVar, namespace code ignores ":"s in middle and end of var names} {
109 namespace eval test_ns_var {
113 list [set :] [set v:] [set x:y:] \
115 [expr {[lsearch [info vars] :] != -1}] \
116 [expr {[lsearch [info vars] v:] != -1}] \
117 [expr {[lsearch [info vars] x:y:] != -1}]
119 } {123 456 789 123 456 789 1 1 1}
120 test var-1.15 {TclLookupVar, resurrect variable via upvar to deleted namespace: compiled code path} {
121 namespace eval test_ns_var {
125 variable ::test_ns_var::foo
126 lappend result [catch {set foo} msg] $msg
127 namespace delete ::test_ns_var
128 lappend result [catch {set foo 3} msg] $msg
129 lappend result [catch {set foo(3) 3} msg] $msg
132 } {0 2 1 {can't set "foo": upvar refers to variable in deleted namespace} 1 {can't set "foo(3)": upvar refers to variable in deleted namespace}}
133 test var-1.16 {TclLookupVar, resurrect variable via upvar to deleted namespace: uncompiled code path} {
134 namespace eval test_ns_var {
136 namespace eval subns {
139 upvar 0 subns::foo foo
140 lappend result [catch {set foo} msg] $msg
141 namespace delete subns
142 lappend result [catch {set foo 3} msg] $msg
143 lappend result [catch {set foo(3) 3} msg] $msg
144 namespace delete [namespace current]
147 } {0 2 1 {can't set "foo": upvar refers to variable in deleted namespace} 1 {can't set "foo(3)": upvar refers to variable in deleted namespace}}
148 test var-1.17 {TclLookupVar, resurrect array element via upvar to deleted array: compiled code path} {
149 namespace eval test_ns_var {
152 array set x {1 2 3 4}
154 lappend result [catch {set foo} msg] $msg
156 lappend result [catch {set foo 3} msg] $msg
159 namespace delete [namespace current]
162 } {0 2 1 {can't set "foo": upvar refers to element in deleted array}}
163 test var-1.18 {TclLookupVar, resurrect array element via upvar to deleted array: uncompiled code path} {
164 namespace eval test_ns_var {
167 array set x {1 2 3 4}
169 lappend result [catch {set foo} msg] $msg
171 lappend result [catch {set foo 3} msg] $msg
172 namespace delete [namespace current]
175 } {0 2 1 {can't set "foo": upvar refers to element in deleted array}}
176 test var-1.19 {TclLookupVar, right error message when parsing variable name} {
177 list [catch {[format set] thisvar(doesntexist)} msg] $msg
178 } {1 {can't read "thisvar(doesntexist)": no such variable}}
180 test var-2.1 {Tcl_LappendObjCmd, create var if new} {
185 test var-3.1 {MakeUpvar, TCL_NAMESPACE_ONLY not specified for other var} {
189 global x ;# calls MakeUpvar with TCL_NAMESPACE_ONLY for other var x
194 test var-3.2 {MakeUpvar, other var has TCL_NAMESPACE_ONLY specified} {
195 namespace eval test_ns_var {
199 variable v ;# TCL_NAMESPACE_ONLY specified for other var x
205 if {[info commands testupvar] != {}} {
206 test var-3.3 {MakeUpvar, my var has TCL_GLOBAL_ONLY specified} {
210 # create global xx linked to global a
211 testupvar 1 a {} xx global
213 list [p] $xx [set xx 789] $a
214 } {{} 123321 789 789}
215 test var-3.4 {MakeUpvar, my var has TCL_NAMESPACE_ONLY specified} {
218 namespace eval test_ns_var {
219 catch {unset ::test_ns_var::vv}
221 # create namespace var vv linked to global a
222 testupvar 1 a {} vv namespace
226 list $test_ns_var::vv [set test_ns_var::vv 123] $a
229 test var-3.5 {MakeUpvar, no call frame so my var will be in global :: ns} {
234 list [set xxxxx] [set aaaaa]
236 test var-3.6 {MakeUpvar, active call frame pushed for namespace eval} {
239 namespace eval test_ns_var {
244 test var-3.7 {MakeUpvar, my var has ::s} {
247 upvar #0 a test_ns_var::lnk
248 namespace eval test_ns_var {
252 test var-3.8 {MakeUpvar, my var already exists in global ns} {
260 test var-3.9 {MakeUpvar, my var has invalid ns name} {
263 list [catch {upvar #0 aaaaa test_ns_fred::lnk} msg] $msg
264 } {1 {can't create "test_ns_fred::lnk": parent namespace doesn't exist}}
265 test var-3.10 {MakeUpvar, } {
268 namespace eval foo upvar bar bar
270 catch {list $bar $foo::bar} msg
276 if {[info commands testgetvarfullname] != {}} {
277 test var-4.1 {Tcl_GetVariableName, global variable} {
280 testgetvarfullname a global
282 test var-4.2 {Tcl_GetVariableName, namespace variable} {
283 namespace eval test_ns_var {
285 testgetvarfullname george namespace
287 } ::test_ns_var::george
288 test var-4.3 {Tcl_GetVariableName, variable can't be array element} {
291 list [catch {testgetvarfullname a(1) global} msg] $msg
292 } {1 {unknown variable "a(1)"}}
295 test var-5.1 {Tcl_GetVariableFullName, global variable} {
298 namespace which -variable a
300 test var-5.2 {Tcl_GetVariableFullName, namespace variable} {
301 namespace eval test_ns_var {
303 namespace which -variable martha
305 } {::test_ns_var::martha}
306 test var-5.3 {Tcl_GetVariableFullName, namespace variable} {
307 namespace which -variable test_ns_var::martha
308 } {::test_ns_var::martha}
310 test var-6.1 {Tcl_GlobalObjCmd, variable is qualified by a namespace name} {
311 namespace eval test_ns_var {
315 global ::test_ns_var::boeing
320 test var-6.2 {Tcl_GlobalObjCmd, variable is qualified by a namespace name} {
321 namespace eval test_ns_var {
322 namespace eval test_ns_nested {
326 global ::test_ns_var::test_ns_nested::java
332 test var-6.3 {Tcl_GlobalObjCmd, variable named {} qualified by a namespace name} {
333 set ::test_ns_var::test_ns_nested:: 24
335 global ::test_ns_var::test_ns_nested::
340 test var-6.4 {Tcl_GlobalObjCmd, variable name matching :*} {
341 # Test for Tcl Bug 480176
351 test var-7.1 {Tcl_VariableObjCmd, create and initialize one new ns variable} {
352 catch {namespace delete test_ns_var}
353 namespace eval test_ns_var {
356 list [info vars test_ns_var::*] [set test_ns_var::one]
357 } {::test_ns_var::one 1}
358 test var-7.2 {Tcl_VariableObjCmd, if new and no value, leave undefined} {
360 namespace eval test_ns_var {
363 list [info exists test_ns_var::two] [catch {set test_ns_var::two} msg] $msg
364 } {0 1 {can't read "test_ns_var::two": no such variable}}
365 test var-7.3 {Tcl_VariableObjCmd, "define" var already created above} {
366 namespace eval test_ns_var {
369 list [lsort [info vars test_ns_var::*]] \
370 [namespace eval test_ns_var {set two}]
371 } [list [lsort {::test_ns_var::two ::test_ns_var::one}] 2]
372 test var-7.4 {Tcl_VariableObjCmd, list of vars} {
373 namespace eval test_ns_var {
374 variable three 3 four 4
376 list [lsort [info vars test_ns_var::*]] \
377 [namespace eval test_ns_var {expr $three+$four}]
378 } [list [lsort {::test_ns_var::four ::test_ns_var::three ::test_ns_var::two ::test_ns_var::one}] 7]
379 test var-7.5 {Tcl_VariableObjCmd, value for last var is optional} {
386 namespace eval test_ns_var {
390 lappend a $test_ns_var::five \
391 [set test_ns_var::six 6] [set test_ns_var::six] $six
397 test var-7.6 {Tcl_VariableObjCmd, variable name can be qualified} {
398 namespace eval test_ns_var {
399 variable ::newvar cheers!
404 test var-7.7 {Tcl_VariableObjCmd, bad var name} {
405 namespace eval test_ns_var {
406 list [catch {variable sev:::en 7} msg] $msg
408 } {1 {can't define "sev:::en": parent namespace doesn't exist}}
409 test var-7.8 {Tcl_VariableObjCmd, if var already exists and no value is given, leave value unchanged} {
411 namespace eval test_ns_var {
419 test var-7.9 {Tcl_VariableObjCmd, mark as namespace var so var persists until namespace is destroyed or var is unset} {
420 catch {namespace delete test_ns_var2}
422 namespace eval test_ns_var2 {
427 lappend a [lsort [info vars test_ns_var2::*]]
428 lappend a [info exists test_ns_var2::x] [info exists test_ns_var2::y] \
429 [info exists test_ns_var2::z]
430 lappend a [list [catch {set test_ns_var2::y} msg] $msg]
431 lappend a [lsort [info vars test_ns_var2::*]]
432 lappend a [info exists test_ns_var2::y] [info exists test_ns_var2::z]
433 lappend a [set test_ns_var2::y hello]
434 lappend a [info exists test_ns_var2::y] [info exists test_ns_var2::z]
435 lappend a [list [catch {unset test_ns_var2::y} msg] $msg]
436 lappend a [lsort [info vars test_ns_var2::*]]
437 lappend a [info exists test_ns_var2::y] [info exists test_ns_var2::z]
438 lappend a [list [catch {unset test_ns_var2::z} msg] $msg]
439 lappend a [namespace delete test_ns_var2]
441 } [list [lsort {::test_ns_var2::x ::test_ns_var2::y ::test_ns_var2::z}] 1 0 0\
442 {1 {can't read "test_ns_var2::y": no such variable}}\
443 [lsort {::test_ns_var2::x ::test_ns_var2::y ::test_ns_var2::z}] 0 0\
446 [lsort {::test_ns_var2::x ::test_ns_var2::z}] 0 0\
447 {1 {can't unset "test_ns_var2::z": no such variable}}\
449 test var-7.10 {Tcl_VariableObjCmd, variable cmd inside proc creates local link var} {
450 namespace eval test_ns_var {
453 list [set eight] [info vars]
458 test var-7.11 {Tcl_VariableObjCmd, variable cmd inside proc creates local link var} {
459 proc p {} { ;# note this proc is at global :: scope
460 variable test_ns_var::eight
461 list [set eight] [info vars]
465 test var-7.12 {Tcl_VariableObjCmd, variable cmd inside proc creates local link var} {
466 namespace eval test_ns_var {
467 variable {} {My name is empty}
469 proc p {} { ;# note this proc is at global :: scope
470 variable test_ns_var::
471 list [set {}] [info vars]
474 } {{My name is empty} {{}}}
475 test var-7.13 {Tcl_VariableObjCmd, variable named ":"} {
476 namespace eval test_ns_var {
477 variable : {My name is ":"}
480 list [set :] [info vars]
484 } {{My name is ":"} :}
485 test var-7.14 {Tcl_VariableObjCmd, array element parameter} {
486 catch {namespace eval test_ns_var { variable arrayvar(1) }} res
488 } "can't define \"arrayvar(1)\": name refers to an element in an array"
489 test var-7.15 {Tcl_VariableObjCmd, array element parameter} {
491 namespace eval test_ns_var {
494 variable arrayvar(1) y
498 } "can't define \"arrayvar(1)\": name refers to an element in an array"
499 test var-7.16 {Tcl_VariableObjCmd, no args} {
500 list [catch {variable} msg] $msg
501 } {1 {wrong # args: should be "variable ?name value...? name ?value?"}}
502 test var-7.17 {Tcl_VariableObjCmd, no args} {
503 namespace eval test_ns_var {
504 list [catch {variable} msg] $msg
506 } {1 {wrong # args: should be "variable ?name value...? name ?value?"}}
508 test var-8.1 {TclDeleteVars, "unset" traces are called with fully-qualified var names} {
509 catch {namespace delete test_ns_var}
511 namespace eval test_ns_var {
515 proc traceUnset {name1 name2 op} {
517 set info [concat $info [list $name1 $name2 $op]]
520 trace var v u [namespace code traceUnset]
522 list [unset test_ns_var::v] $test_ns_var::info
523 } {{} {test_ns_var::v {} u}}
525 if {[info commands testsetnoerr] == {}} {
526 puts "This application hasn't been compiled with the \"testsetnoerr\""
527 puts "command, so I can't test TclSetVar etc."
529 test var-9.1 {behaviour of TclGet/SetVar simple get/set} {
530 catch {unset u}; catch {unset v}
532 [set u a; testsetnoerr u] \
535 [unset v; testseterr v b]
536 } [list {before get a} {before set b} {before get a} {before set b}]
537 test var-9.2 {behaviour of TclGet/SetVar namespace get/set} {
538 catch {namespace delete ns}
539 namespace eval ns {variable u a; variable v}
541 [testsetnoerr ns::u] \
542 [testsetnoerr ns::v b] \
544 [unset ns::v; testseterr ns::v b]
545 } [list {before get a} {before set b} {before get a} {before set b}]
546 test var-9.3 {behaviour of TclGetVar no variable} {
549 [catch {testsetnoerr u} res] $res \
550 [catch {testseterr u} res] $res
551 } {1 {before get} 1 {can't read "u": no such variable}}
552 test var-9.4 {behaviour of TclGetVar no namespace variable} {
553 catch {namespace delete ns}
556 [catch {testsetnoerr ns::w} res] $res \
557 [catch {testseterr ns::w} res] $res
558 } {1 {before get} 1 {can't read "ns::w": no such variable}}
559 test var-9.5 {behaviour of TclGetVar no namespace} {
560 catch {namespace delete ns}
562 [catch {testsetnoerr ns::u} res] $res \
563 [catch {testseterr ns::v} res] $res
564 } {1 {before get} 1 {can't read "ns::v": no such variable}}
565 test var-9.6 {behaviour of TclSetVar no namespace} {
566 catch {namespace delete ns}
568 [catch {testsetnoerr ns::v 1} res] $res \
569 [catch {testseterr ns::v 1} res] $res
570 } {1 {before set} 1 {can't set "ns::v": parent namespace doesn't exist}}
571 test var-9.7 {behaviour of TclGetVar array variable} {
575 [catch {testsetnoerr arr} res] $res \
576 [catch {testseterr arr} res] $res
577 } {1 {before get} 1 {can't read "arr": variable is array}}
578 test var-9.8 {behaviour of TclSetVar array variable} {
582 [catch {testsetnoerr arr 2} res] $res \
583 [catch {testseterr arr 2} res] $res
584 } {1 {before set} 1 {can't set "arr": variable is array}}
585 test var-9.9 {behaviour of TclGetVar read trace success} {
586 proc resetvar {val name elem op} {upvar 1 $name v; set v $val}
587 catch {unset u}; catch {unset v}
589 trace var u r [list resetvar 1]
590 trace var v r [list resetvar 2]
594 } {{before get 1} {before get 2}}
595 test var-9.10 {behaviour of TclGetVar read trace error} {
596 proc writeonly args {error "write-only"}
598 trace var v r writeonly
600 [catch {testsetnoerr v} msg] $msg \
601 [catch {testseterr v} msg] $msg
602 } {1 {before get} 1 {can't read "v": write-only}}
603 test var-9.11 {behaviour of TclSetVar write trace success} {
604 proc doubleval {name elem op} {upvar 1 $name v; set v [expr {2 * $v}]}
605 catch {unset u}; catch {unset v}
607 trace var v w doubleval
608 trace var u w doubleval
612 } {{before set 4} {before set 6}}
613 test var-9.12 {behaviour of TclSetVar write trace error} {
614 proc readonly args {error "read-only"}
616 trace var v w readonly
618 [catch {testsetnoerr v 2} msg] $msg $v \
619 [catch {testseterr v 3} msg] $msg $v
620 } {1 {before set} 2 1 {can't set "v": read-only} 3}
622 test var-10.1 {can't nest arrays with array set} {
624 list [catch {array set arr(x) {a 1 b 2}} res] $res
625 } {1 {can't set "arr(x)": variable isn't array}}
627 test var-10.2 {can't nest arrays with array set} {
629 list [catch {array set arr(x) {}} res] $res
630 } {1 {can't set "arr(x)": variable isn't array}}
632 test var-11.1 {array unset} {
634 array set a { 1,1 a 1,2 b 2,1 c 2,3 d }
636 lsort -dict [array names a]
638 test var-11.2 {array unset} {
640 array set a { 1,1 a 1,2 b }
644 test var-11.3 {array unset errors} {
646 array set a { 1,1 a 1,2 b }
647 list [catch {array unset a pattern too} msg] $msg
648 } {1 {wrong # args: should be "array unset arrayName ?pattern?"}}
650 test var-12.1 {TclFindCompiledLocals, {} array name} {
661 lsort -dictionary [array names {}]
665 test var-13.1 {Tcl_UnsetVar2, unset array with trace set on element} {
667 proc foo {var ind op} {
673 trace variable t(1) u foo
676 set x "If you see this, it worked"
677 } "If you see this, it worked"
679 test var-14.1 {array names syntax} -body {
680 array names foo bar baz snafu
681 } -returnCodes 1 -match glob -result *
683 test var-15.1 {segfault in [unset], [Bug 735335]} {
689 # Note that the variable name has to be
690 # unused previously for the segfault to
693 namespace eval test A useSomeUnlikelyNameHere
694 namespace eval test unset useSomeUnlikelyNameHere
697 test var-16.1 {CallVarTraces: save/restore interp error state: 1038021} {
698 trace add variable errorCode write { ;#}
699 catch {error foo bar baz}
700 trace remove variable errorCode write { ;#}
704 test var-17.1 {TclArraySet [Bug 1669489]} -setup {
705 unset -nocomplain ::a
708 set elements {1 2 3 4}
709 trace add variable a write {string length $elements ;#}
710 array set a $elements
713 unset -nocomplain ::a ::elements
716 catch {namespace delete ns}
721 catch {namespace delete test_ns_var}
722 catch {namespace delete test_ns_var2}
732 ::tcltest::cleanupTests