1 # This file contains tests for the tclProc.c source file. Tests appear in the
2 # same order as the C code that they test. The set of tests is currently
3 # incomplete since it includes only new tests, in particular tests for code
4 # changed for the addition of Tcl namespaces. Other procedure-related tests
5 # appear in other test files such as proc-old.test.
7 # Sourcing this file into Tcl runs the tests and generates output for errors.
8 # No output means no errors were found.
10 # Copyright (c) 1997 Sun Microsystems, Inc.
11 # Copyright (c) 1998-1999 by Scriptics Corporation.
13 # See the file "license.terms" for information on usage and redistribution of
14 # this file, and for a DISCLAIMER OF ALL WARRANTIES.
16 if {"::tcltest" ni [namespace children]} {
17 package require tcltest 2.5
18 namespace import -force ::tcltest::*
21 testConstraint procbodytest [expr {![catch {package require procbodytest}]}]
22 testConstraint memory [llength [info commands memory]]
24 catch {namespace delete {*}[namespace children :: test_ns_*]}
29 test proc-1.1 {Tcl_ProcObjCmd, put proc in namespace specified in name, if any} -setup {
30 catch {namespace delete {*}[namespace children :: test_ns_*]}
32 namespace eval test_ns_1 {
35 proc test_ns_1::baz::p {} {
36 return "p in [namespace current]"
38 list [test_ns_1::baz::p] \
39 [namespace eval test_ns_1 {baz::p}] \
40 [info commands test_ns_1::baz::*]
41 } -result {{p in ::test_ns_1::baz} {p in ::test_ns_1::baz} ::test_ns_1::baz::p}
42 test proc-1.2 {Tcl_ProcObjCmd, namespace specified in proc name must exist} -setup {
43 catch {namespace delete {*}[namespace children :: test_ns_*]}
44 } -returnCodes error -body {
45 proc test_ns_1::baz::p {} {}
46 } -result {can't create procedure "test_ns_1::baz::p": unknown namespace}
47 test proc-1.3 {Tcl_ProcObjCmd, empty proc name} -setup {
48 catch {namespace delete {*}[namespace children :: test_ns_*]}
55 } -result {{empty called} {
58 test proc-1.4 {Tcl_ProcObjCmd, simple proc name and proc defined in namespace} -setup {
59 catch {namespace delete {*}[namespace children :: test_ns_*]}
61 namespace eval test_ns_1 {
64 return "p in [namespace current]"
68 list [test_ns_1::baz::p] \
69 [info commands test_ns_1::baz::*]
70 } -result {{p in ::test_ns_1::baz} ::test_ns_1::baz::p}
71 test proc-1.5 {Tcl_ProcObjCmd, qualified proc name and proc defined in namespace} -setup {
72 catch {namespace delete {*}[namespace children :: test_ns_*]}
74 namespace eval test_ns_1::baz {}
75 namespace eval test_ns_1 {
77 return "p in [namespace current]"
80 list [test_ns_1::baz::p] \
81 [info commands test_ns_1::baz::*] \
82 [namespace eval test_ns_1::baz {namespace which p}]
83 } -result {{p in ::test_ns_1::baz} ::test_ns_1::baz::p ::test_ns_1::baz::p}
84 test proc-1.6 {Tcl_ProcObjCmd, namespace code ignores single ":"s in middle or end of command names} -setup {
85 catch {namespace delete {*}[namespace children :: test_ns_*]}
87 namespace eval test_ns_1 {
88 proc q: {} {return "q:"}
89 proc value:at: {} {return "value:at:"}
91 list [namespace eval test_ns_1 {q:}] \
92 [namespace eval test_ns_1 {value:at:}] \
94 [test_ns_1::value:at:] \
95 [lsort [info commands test_ns_1::*]] \
96 [namespace eval test_ns_1 {namespace which q:}] \
97 [namespace eval test_ns_1 {namespace which value:at:}]
98 } -result {q: value:at: q: value:at: {::test_ns_1::q: ::test_ns_1::value:at:} ::test_ns_1::q: ::test_ns_1::value:at:}
99 test proc-1.7 {Tcl_ProcObjCmd, check that formal parameter names are not array elements} -setup {
101 } -returnCodes error -body {
103 set z [expr {$a(1)+$a(2)}]
104 puts "$z=z, $a(1)=$a(1)"
106 } -result {formal parameter "a(1)" is an array element}
107 test proc-1.8 {Tcl_ProcObjCmd, check that formal parameter names are simple names} -setup {
112 } -returnCodes error -result {formal parameter "b::a" is not a simple name}
113 test proc-1.9 {Tcl_ProcObjCmd, arguments via canonical list (string-representation bug [631b4c45df])} -body {
115 binary scan AB cc a b
116 proc p [list [list a $a] [list b $b] [list v [expr {$v + 2}]]] {expr {$a + $b + $v}}
118 } -result [expr {65+66+4}] -cleanup {
122 test proc-2.1 {TclFindProc, simple proc name and proc not in namespace} -setup {
123 catch {namespace delete {*}[namespace children :: test_ns_*]}
126 proc p {} {return "p in [namespace current]"}
128 } -result {return "p in [namespace current]"}
129 test proc-2.2 {TclFindProc, simple proc name and proc defined in namespace} -setup {
130 catch {namespace delete {*}[namespace children :: test_ns_*]}
132 namespace eval test_ns_1 {
134 proc p {} {return "p in [namespace current]"}
137 namespace eval test_ns_1::baz {info body p}
138 } -result {return "p in [namespace current]"}
139 test proc-2.3 {TclFindProc, qualified proc name and proc defined in namespace} -setup {
140 catch {namespace delete {*}[namespace children :: test_ns_*]}
142 namespace eval test_ns_1::baz {}
143 namespace eval test_ns_1 {
144 proc baz::p {} {return "p in [namespace current]"}
146 namespace eval test_ns_1 {info body baz::p}
147 } -result {return "p in [namespace current]"}
148 test proc-2.4 {TclFindProc, global proc and executing in namespace} -setup {
149 catch {namespace delete {*}[namespace children :: test_ns_*]}
152 proc p {} {return "global p"}
153 namespace eval test_ns_1::baz {info body p}
154 } -result {return "global p"}
156 test proc-3.1 {TclObjInterpProc, proc defined and executing in same namespace} -setup {
157 catch {namespace delete {*}[namespace children :: test_ns_*]}
159 proc p {} {return "p in [namespace current]"}
162 test proc-3.2 {TclObjInterpProc, proc defined and executing in same namespace} -setup {
163 catch {namespace delete {*}[namespace children :: test_ns_*]}
165 namespace eval test_ns_1::baz {
166 proc p {} {return "p in [namespace current]"}
169 } -result {p in ::test_ns_1::baz}
170 test proc-3.3 {TclObjInterpProc, proc defined and executing in different namespaces} -setup {
171 catch {namespace delete {*}[namespace children :: test_ns_*]}
174 proc p {} {return "p in [namespace current]"}
175 namespace eval test_ns_1::baz {
179 test proc-3.4 {TclObjInterpProc, procs execute in the namespace in which they were defined unless renamed into new namespace} -setup {
180 catch {namespace delete {*}[namespace children :: test_ns_*]}
183 namespace eval test_ns_1::baz {
184 proc p {} {return "p in [namespace current]"}
185 rename ::test_ns_1::baz::p ::p
186 list [p] [namespace which p]
188 } -result {{p in ::} ::p}
189 test proc-3.5 {TclObjInterpProc, any old result is reset before appending error msg about missing arguments} -body {
190 proc p {x} {info commands 3m}
192 } -returnCodes error -result {wrong # args: should be "p x"}
193 test proc-3.6 {TclObjInterpProc, proper quoting of proc name, Bug 942757} -body {
194 proc {a b c} {x} {info commands 3m}
196 } -returnCodes error -result {wrong # args: should be "{a b c} x"}
198 test proc-3.7 {TclObjInterpProc, wrong num args, Bug 3366265} {
200 list [catch {{}} msg] $msg
201 } {1 {wrong # args: should be "{} x"}}
203 catch {namespace delete {*}[namespace children :: test_ns_*]}
206 catch {rename {a b c} {}}
212 # Note that the test require that procedures whose body is used to create
213 # procbody objects must be executed before the procbodytest::proc command is
214 # executed, so that the Proc struct is populated correctly (CompiledLocals are
215 # added at compile time).
217 test proc-4.1 {TclCreateProc, procbody obj} -constraints procbodytest -body {
218 proc p x {return "$x:$x"}
220 procbodytest::proc t x p
226 test proc-4.2 {TclCreateProc, procbody obj, use compiled locals} -body {
228 set y [string tolower $x]
232 procbodytest::proc t x p
234 } -constraints procbodytest -cleanup {
238 test proc-4.3 {TclCreateProc, procbody obj, too many args} -body {
240 set y [string tolower $x]
244 procbodytest::proc t {x x1 x2} p
246 } -constraints procbodytest -returnCodes error -cleanup {
249 } -result {procedure "t": arg list contains 3 entries, precompiled header expects 1}
250 test proc-4.4 {TclCreateProc, procbody obj, inconsistent arg name} -body {
252 set v [join [list $x $y $z]]
253 set w [string tolower $v]
257 procbodytest::proc t {x x1 z} p
259 } -constraints procbodytest -returnCodes error -cleanup {
262 } -result {procedure "t": formal parameter 1 is inconsistent with precompiled body}
263 test proc-4.5 {TclCreateProc, procbody obj, inconsistent arg default type} -body {
265 set v [join [list $x $y $z]]
266 set w [string tolower $v]
270 procbodytest::proc t {x y z} p
272 } -constraints procbodytest -returnCodes error -cleanup {
275 } -result {procedure "t": formal parameter 2 is inconsistent with precompiled body}
276 test proc-4.6 {TclCreateProc, procbody obj, inconsistent arg default type} -body {
278 set v [join [list $x $y $z]]
279 set w [string tolower $v]
283 procbodytest::proc t {x y {z Z}} p
285 } -returnCodes error -constraints procbodytest -cleanup {
288 } -result {procedure "t": formal parameter 2 is inconsistent with precompiled body}
289 test proc-4.7 {TclCreateProc, procbody obj, inconsistent arg default value} -body {
291 set v [join [list $x $y $z]]
292 set w [string tolower $v]
296 procbodytest::proc t {x y {z ZZ}} p
298 } -constraints procbodytest -returnCodes error -cleanup {
301 } -result {procedure "t": formal parameter "z" has default value inconsistent with precompiled body}
302 test proc-4.8 {TclCreateProc, procbody obj, no leak on multiple iterations} -setup {
304 set lines [split [memory info] "\n"]
308 set y [string tolower $x]
312 } -constraints {procbodytest memory} -body {
314 for {set i 0} {$i < 5} {incr i} {
315 procbodytest::proc tx x px
319 set leakedBytes [expr {$end - $tmp}]
322 unset -nocomplain end i tmp leakedBytes
324 test proc-4.9 {[39fed4dae5] Valid Tcl_PkgPresent return} procbodytest {
328 TclCreateProc, issue a8579d906a28, argument with no name
331 proc p1 [list [list [expr {1 + 2}] default]] {}
337 test proc-5.1 {Bytecompiling noop; test for correct argument substitution} -body {
338 proc p args {} ; # this will be bytecompiled into t
343 trace add variable a read {append res a ;#}
344 trace add variable b write {append res b ;#}
345 p $a ccccccw {bfe} {$a} [incr b] [incr a] {[incr b]} {$a} hello
354 test proc-6.1 {ProcessProcResultCode: Bug 647307 (negative return code)} -body {
355 proc a {} {return -code -5}
363 test proc-7.1 {Redefining a compiled cmd: Bug 729692} {
366 proc bar args {return bar}
371 test proc-7.2 {Shadowing a compiled cmd: Bug 729692} -body {
372 namespace eval ugly {}
374 proc set args {return bar}
379 namespace delete ugly
381 test proc-7.3 {Returning loop exception from redefined cmd: Bug 729692} -body {
382 namespace eval ugly {}
386 if { [incr i] > 3 } {
387 proc continue {} {return -code break}
395 namespace delete ugly
398 test proc-7.4 {Proc struct outlives its interp: Bug 3532959} {
400 lappend lambda {set a 1}
402 child eval [list apply $lambda foo]
407 test proc-7.5 {[631b4c45df] Crash in argument processing} {
409 proc foo [list [list from $val]] {}
411 unset -nocomplain val
418 ::tcltest::cleanupTests