os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/tests/proc.test
First public contribution.
1 # This file contains tests for the tclProc.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 includes only new tests, in particular
4 # tests for code changed for the addition of Tcl namespaces. Other
5 # procedure-related tests appear in other test files such as proc-old.test.
7 # Sourcing this file into Tcl runs the tests and generates output for
8 # errors. 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
14 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
16 # RCS: @(#) $Id: proc.test,v 1.11.2.1 2004/05/02 21:07:16 msofer Exp $
18 if {[lsearch [namespace children] ::tcltest] == -1} {
19 package require tcltest
20 namespace import -force ::tcltest::*
23 catch {eval namespace delete [namespace children :: test_ns_*]}
28 test proc-1.1 {Tcl_ProcObjCmd, put proc in namespace specified in name, if any} {
29 catch {eval namespace delete [namespace children :: test_ns_*]}
30 namespace eval test_ns_1 {
33 proc test_ns_1::baz::p {} {
34 return "p in [namespace current]"
36 list [test_ns_1::baz::p] \
37 [namespace eval test_ns_1 {baz::p}] \
38 [info commands test_ns_1::baz::*]
39 } {{p in ::test_ns_1::baz} {p in ::test_ns_1::baz} ::test_ns_1::baz::p}
40 test proc-1.2 {Tcl_ProcObjCmd, namespace specified in proc name must exist} {
41 catch {eval namespace delete [namespace children :: test_ns_*]}
42 list [catch {proc test_ns_1::baz::p {} {}} msg] $msg
43 } {1 {can't create procedure "test_ns_1::baz::p": unknown namespace}}
44 test proc-1.3 {Tcl_ProcObjCmd, empty proc name} {
45 catch {eval namespace delete [namespace children :: test_ns_*]}
54 test proc-1.4 {Tcl_ProcObjCmd, simple proc name and proc defined in namespace} {
55 catch {eval namespace delete [namespace children :: test_ns_*]}
56 namespace eval test_ns_1 {
59 return "p in [namespace current]"
63 list [test_ns_1::baz::p] \
64 [info commands test_ns_1::baz::*]
65 } {{p in ::test_ns_1::baz} ::test_ns_1::baz::p}
66 test proc-1.5 {Tcl_ProcObjCmd, qualified proc name and proc defined in namespace} {
67 catch {eval namespace delete [namespace children :: test_ns_*]}
68 namespace eval test_ns_1::baz {}
69 namespace eval test_ns_1 {
71 return "p in [namespace current]"
74 list [test_ns_1::baz::p] \
75 [info commands test_ns_1::baz::*] \
76 [namespace eval test_ns_1::baz {namespace which p}]
77 } {{p in ::test_ns_1::baz} ::test_ns_1::baz::p ::test_ns_1::baz::p}
78 test proc-1.6 {Tcl_ProcObjCmd, namespace code ignores single ":"s in middle or end of command names} {
79 catch {eval namespace delete [namespace children :: test_ns_*]}
80 namespace eval test_ns_1 {
81 proc q: {} {return "q:"}
82 proc value:at: {} {return "value:at:"}
84 list [namespace eval test_ns_1 {q:}] \
85 [namespace eval test_ns_1 {value:at:}] \
87 [test_ns_1::value:at:] \
88 [lsort [info commands test_ns_1::*]] \
89 [namespace eval test_ns_1 {namespace which q:}] \
90 [namespace eval test_ns_1 {namespace which value:at:}]
91 } {q: value:at: q: value:at: {::test_ns_1::q: ::test_ns_1::value:at:} ::test_ns_1::q: ::test_ns_1::value:at:}
92 test proc-1.7 {Tcl_ProcObjCmd, check that formal parameter names are not array elements} {
94 list [catch {proc p {a(1) a(2)} {
95 set z [expr $a(1)+$a(2)]
96 puts "$z=z, $a(1)=$a(1)"
98 } {1 {procedure "p" has formal parameter "a(1)" that is an array element}}
99 test proc-1.8 {Tcl_ProcObjCmd, check that formal parameter names are simple names} {
101 list [catch {proc p {b:a b::a} {
103 } {1 {procedure "p" has formal parameter "b::a" that is not a simple name}}
105 test proc-2.1 {TclFindProc, simple proc name and proc not in namespace} {
106 catch {eval namespace delete [namespace children :: test_ns_*]}
108 proc p {} {return "p in [namespace current]"}
110 } {return "p in [namespace current]"}
111 test proc-2.2 {TclFindProc, simple proc name and proc defined in namespace} {
112 catch {eval namespace delete [namespace children :: test_ns_*]}
113 namespace eval test_ns_1 {
115 proc p {} {return "p in [namespace current]"}
118 namespace eval test_ns_1::baz {info body p}
119 } {return "p in [namespace current]"}
120 test proc-2.3 {TclFindProc, qualified proc name and proc defined in namespace} {
121 catch {eval namespace delete [namespace children :: test_ns_*]}
122 namespace eval test_ns_1::baz {}
123 namespace eval test_ns_1 {
124 proc baz::p {} {return "p in [namespace current]"}
126 namespace eval test_ns_1 {info body baz::p}
127 } {return "p in [namespace current]"}
128 test proc-2.4 {TclFindProc, global proc and executing in namespace} {
129 catch {eval namespace delete [namespace children :: test_ns_*]}
131 proc p {} {return "global p"}
132 namespace eval test_ns_1::baz {info body p}
133 } {return "global p"}
135 test proc-3.1 {TclObjInterpProc, proc defined and executing in same namespace} {
136 catch {eval namespace delete [namespace children :: test_ns_*]}
137 proc p {} {return "p in [namespace current]"}
140 test proc-3.2 {TclObjInterpProc, proc defined and executing in same namespace} {
141 catch {eval namespace delete [namespace children :: test_ns_*]}
142 namespace eval test_ns_1::baz {
143 proc p {} {return "p in [namespace current]"}
146 } {p in ::test_ns_1::baz}
147 test proc-3.3 {TclObjInterpProc, proc defined and executing in different namespaces} {
148 catch {eval namespace delete [namespace children :: test_ns_*]}
150 proc p {} {return "p in [namespace current]"}
151 namespace eval test_ns_1::baz {
155 test proc-3.4 {TclObjInterpProc, procs execute in the namespace in which they were defined unless renamed into new namespace} {
156 catch {eval namespace delete [namespace children :: test_ns_*]}
158 namespace eval test_ns_1::baz {
159 proc p {} {return "p in [namespace current]"}
160 rename ::test_ns_1::baz::p ::p
161 list [p] [namespace which p]
164 test proc-3.5 {TclObjInterpProc, any old result is reset before appending error msg about missing arguments} {
165 proc p {x} {info commands 3m}
166 list [catch {p} msg] $msg
167 } {1 {wrong # args: should be "p x"}}
169 test proc-3.6 {TclObjInterpProc, proper quoting of proc name, Bug 942757} {
170 proc {a b c} {x} {info commands 3m}
171 list [catch {{a b c}} msg] $msg
172 } {1 {wrong # args: should be "{a b c} x"}}
174 catch {eval namespace delete [namespace children :: test_ns_*]}
177 catch {rename {a b c} {}}
180 if {[catch {package require procbodytest}]} {
181 puts "This application couldn't load the \"procbodytest\" package, so I"
182 puts "can't test creation of procs whose bodies have type \"procbody\"."
183 ::tcltest::cleanupTests
190 # Note that the test require that procedures whose body is used to create
191 # procbody objects must be executed before the procbodytest::proc command
192 # is executed, so that the Proc struct is populated correctly (CompiledLocals
193 # are added at compile time).
195 test proc-4.1 {TclCreateProc, procbody obj} {
197 proc p x {return "$x:$x"}
199 procbodytest::proc t x p
208 test proc-4.2 {TclCreateProc, procbody obj, use compiled locals} {
211 set y [string tolower $x]
215 procbodytest::proc t x p
224 test proc-4.3 {TclCreateProc, procbody obj, too many args} {
227 set y [string tolower $x]
231 procbodytest::proc t {x x1 x2} p
238 } {procedure "t": arg list contains 3 entries, precompiled header expects 1}
240 test proc-4.4 {TclCreateProc, procbody obj, inconsitent arg name} {
243 set v [join [list $x $y $z]]
244 set w [string tolower $v]
248 procbodytest::proc t {x x1 z} p
255 } {procedure "t": formal parameter 1 is inconsistent with precompiled body}
257 test proc-4.5 {TclCreateProc, procbody obj, inconsitent arg default type} {
260 set v [join [list $x $y $z]]
261 set w [string tolower $v]
265 procbodytest::proc t {x y z} p
272 } {procedure "t": formal parameter 2 is inconsistent with precompiled body}
274 test proc-4.6 {TclCreateProc, procbody obj, inconsitent arg default type} {
277 set v [join [list $x $y $z]]
278 set w [string tolower $v]
282 procbodytest::proc t {x y {z Z}} p
289 } {procedure "t": formal parameter 2 is inconsistent with precompiled body}
291 test proc-4.7 {TclCreateProc, procbody obj, inconsitent arg default value} {
294 set v [join [list $x $y $z]]
295 set w [string tolower $v]
299 procbodytest::proc t {x y {z ZZ}} p
306 } {procedure "t": formal parameter "z" has default value inconsistent with precompiled body}
308 test proc-5.1 {Bytecompiling noop; test for correct argument substitution} {
309 proc p args {} ; # this will be bytecompiled into t
314 trace add variable a read {append res a ;#}
315 trace add variable b write {append res b ;#}
316 p $a ccccccw {bfe} {$a} [incr b] [incr a] {[incr b]} {$a} hello
325 test proc-6.1 {ProcessProcResultCode: Bug 647307 (negative return code)} {
326 proc a {} {return -code -5}
337 ::tcltest::cleanupTests