sl@0
|
1 |
# This file contains tests for the tclProc.c source file. Tests appear in
|
sl@0
|
2 |
# the same order as the C code that they test. The set of tests is
|
sl@0
|
3 |
# currently incomplete since it includes only new tests, in particular
|
sl@0
|
4 |
# tests for code changed for the addition of Tcl namespaces. Other
|
sl@0
|
5 |
# procedure-related tests appear in other test files such as proc-old.test.
|
sl@0
|
6 |
#
|
sl@0
|
7 |
# Sourcing this file into Tcl runs the tests and generates output for
|
sl@0
|
8 |
# errors. No output means no errors were found.
|
sl@0
|
9 |
#
|
sl@0
|
10 |
# Copyright (c) 1997 Sun Microsystems, Inc.
|
sl@0
|
11 |
# Copyright (c) 1998-1999 by Scriptics Corporation.
|
sl@0
|
12 |
#
|
sl@0
|
13 |
# See the file "license.terms" for information on usage and redistribution
|
sl@0
|
14 |
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
sl@0
|
15 |
#
|
sl@0
|
16 |
# RCS: @(#) $Id: proc.test,v 1.11.2.1 2004/05/02 21:07:16 msofer Exp $
|
sl@0
|
17 |
|
sl@0
|
18 |
if {[lsearch [namespace children] ::tcltest] == -1} {
|
sl@0
|
19 |
package require tcltest
|
sl@0
|
20 |
namespace import -force ::tcltest::*
|
sl@0
|
21 |
}
|
sl@0
|
22 |
|
sl@0
|
23 |
catch {eval namespace delete [namespace children :: test_ns_*]}
|
sl@0
|
24 |
catch {rename p ""}
|
sl@0
|
25 |
catch {rename {} ""}
|
sl@0
|
26 |
catch {unset msg}
|
sl@0
|
27 |
|
sl@0
|
28 |
test proc-1.1 {Tcl_ProcObjCmd, put proc in namespace specified in name, if any} {
|
sl@0
|
29 |
catch {eval namespace delete [namespace children :: test_ns_*]}
|
sl@0
|
30 |
namespace eval test_ns_1 {
|
sl@0
|
31 |
namespace eval baz {}
|
sl@0
|
32 |
}
|
sl@0
|
33 |
proc test_ns_1::baz::p {} {
|
sl@0
|
34 |
return "p in [namespace current]"
|
sl@0
|
35 |
}
|
sl@0
|
36 |
list [test_ns_1::baz::p] \
|
sl@0
|
37 |
[namespace eval test_ns_1 {baz::p}] \
|
sl@0
|
38 |
[info commands test_ns_1::baz::*]
|
sl@0
|
39 |
} {{p in ::test_ns_1::baz} {p in ::test_ns_1::baz} ::test_ns_1::baz::p}
|
sl@0
|
40 |
test proc-1.2 {Tcl_ProcObjCmd, namespace specified in proc name must exist} {
|
sl@0
|
41 |
catch {eval namespace delete [namespace children :: test_ns_*]}
|
sl@0
|
42 |
list [catch {proc test_ns_1::baz::p {} {}} msg] $msg
|
sl@0
|
43 |
} {1 {can't create procedure "test_ns_1::baz::p": unknown namespace}}
|
sl@0
|
44 |
test proc-1.3 {Tcl_ProcObjCmd, empty proc name} {
|
sl@0
|
45 |
catch {eval namespace delete [namespace children :: test_ns_*]}
|
sl@0
|
46 |
proc :: {} {
|
sl@0
|
47 |
return "empty called"
|
sl@0
|
48 |
}
|
sl@0
|
49 |
list [::] \
|
sl@0
|
50 |
[info body {}]
|
sl@0
|
51 |
} {{empty called} {
|
sl@0
|
52 |
return "empty called"
|
sl@0
|
53 |
}}
|
sl@0
|
54 |
test proc-1.4 {Tcl_ProcObjCmd, simple proc name and proc defined in namespace} {
|
sl@0
|
55 |
catch {eval namespace delete [namespace children :: test_ns_*]}
|
sl@0
|
56 |
namespace eval test_ns_1 {
|
sl@0
|
57 |
namespace eval baz {
|
sl@0
|
58 |
proc p {} {
|
sl@0
|
59 |
return "p in [namespace current]"
|
sl@0
|
60 |
}
|
sl@0
|
61 |
}
|
sl@0
|
62 |
}
|
sl@0
|
63 |
list [test_ns_1::baz::p] \
|
sl@0
|
64 |
[info commands test_ns_1::baz::*]
|
sl@0
|
65 |
} {{p in ::test_ns_1::baz} ::test_ns_1::baz::p}
|
sl@0
|
66 |
test proc-1.5 {Tcl_ProcObjCmd, qualified proc name and proc defined in namespace} {
|
sl@0
|
67 |
catch {eval namespace delete [namespace children :: test_ns_*]}
|
sl@0
|
68 |
namespace eval test_ns_1::baz {}
|
sl@0
|
69 |
namespace eval test_ns_1 {
|
sl@0
|
70 |
proc baz::p {} {
|
sl@0
|
71 |
return "p in [namespace current]"
|
sl@0
|
72 |
}
|
sl@0
|
73 |
}
|
sl@0
|
74 |
list [test_ns_1::baz::p] \
|
sl@0
|
75 |
[info commands test_ns_1::baz::*] \
|
sl@0
|
76 |
[namespace eval test_ns_1::baz {namespace which p}]
|
sl@0
|
77 |
} {{p in ::test_ns_1::baz} ::test_ns_1::baz::p ::test_ns_1::baz::p}
|
sl@0
|
78 |
test proc-1.6 {Tcl_ProcObjCmd, namespace code ignores single ":"s in middle or end of command names} {
|
sl@0
|
79 |
catch {eval namespace delete [namespace children :: test_ns_*]}
|
sl@0
|
80 |
namespace eval test_ns_1 {
|
sl@0
|
81 |
proc q: {} {return "q:"}
|
sl@0
|
82 |
proc value:at: {} {return "value:at:"}
|
sl@0
|
83 |
}
|
sl@0
|
84 |
list [namespace eval test_ns_1 {q:}] \
|
sl@0
|
85 |
[namespace eval test_ns_1 {value:at:}] \
|
sl@0
|
86 |
[test_ns_1::q:] \
|
sl@0
|
87 |
[test_ns_1::value:at:] \
|
sl@0
|
88 |
[lsort [info commands test_ns_1::*]] \
|
sl@0
|
89 |
[namespace eval test_ns_1 {namespace which q:}] \
|
sl@0
|
90 |
[namespace eval test_ns_1 {namespace which value:at:}]
|
sl@0
|
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:}
|
sl@0
|
92 |
test proc-1.7 {Tcl_ProcObjCmd, check that formal parameter names are not array elements} {
|
sl@0
|
93 |
catch {rename p ""}
|
sl@0
|
94 |
list [catch {proc p {a(1) a(2)} {
|
sl@0
|
95 |
set z [expr $a(1)+$a(2)]
|
sl@0
|
96 |
puts "$z=z, $a(1)=$a(1)"
|
sl@0
|
97 |
}} msg] $msg
|
sl@0
|
98 |
} {1 {procedure "p" has formal parameter "a(1)" that is an array element}}
|
sl@0
|
99 |
test proc-1.8 {Tcl_ProcObjCmd, check that formal parameter names are simple names} {
|
sl@0
|
100 |
catch {rename p ""}
|
sl@0
|
101 |
list [catch {proc p {b:a b::a} {
|
sl@0
|
102 |
}} msg] $msg
|
sl@0
|
103 |
} {1 {procedure "p" has formal parameter "b::a" that is not a simple name}}
|
sl@0
|
104 |
|
sl@0
|
105 |
test proc-2.1 {TclFindProc, simple proc name and proc not in namespace} {
|
sl@0
|
106 |
catch {eval namespace delete [namespace children :: test_ns_*]}
|
sl@0
|
107 |
catch {rename p ""}
|
sl@0
|
108 |
proc p {} {return "p in [namespace current]"}
|
sl@0
|
109 |
info body p
|
sl@0
|
110 |
} {return "p in [namespace current]"}
|
sl@0
|
111 |
test proc-2.2 {TclFindProc, simple proc name and proc defined in namespace} {
|
sl@0
|
112 |
catch {eval namespace delete [namespace children :: test_ns_*]}
|
sl@0
|
113 |
namespace eval test_ns_1 {
|
sl@0
|
114 |
namespace eval baz {
|
sl@0
|
115 |
proc p {} {return "p in [namespace current]"}
|
sl@0
|
116 |
}
|
sl@0
|
117 |
}
|
sl@0
|
118 |
namespace eval test_ns_1::baz {info body p}
|
sl@0
|
119 |
} {return "p in [namespace current]"}
|
sl@0
|
120 |
test proc-2.3 {TclFindProc, qualified proc name and proc defined in namespace} {
|
sl@0
|
121 |
catch {eval namespace delete [namespace children :: test_ns_*]}
|
sl@0
|
122 |
namespace eval test_ns_1::baz {}
|
sl@0
|
123 |
namespace eval test_ns_1 {
|
sl@0
|
124 |
proc baz::p {} {return "p in [namespace current]"}
|
sl@0
|
125 |
}
|
sl@0
|
126 |
namespace eval test_ns_1 {info body baz::p}
|
sl@0
|
127 |
} {return "p in [namespace current]"}
|
sl@0
|
128 |
test proc-2.4 {TclFindProc, global proc and executing in namespace} {
|
sl@0
|
129 |
catch {eval namespace delete [namespace children :: test_ns_*]}
|
sl@0
|
130 |
catch {rename p ""}
|
sl@0
|
131 |
proc p {} {return "global p"}
|
sl@0
|
132 |
namespace eval test_ns_1::baz {info body p}
|
sl@0
|
133 |
} {return "global p"}
|
sl@0
|
134 |
|
sl@0
|
135 |
test proc-3.1 {TclObjInterpProc, proc defined and executing in same namespace} {
|
sl@0
|
136 |
catch {eval namespace delete [namespace children :: test_ns_*]}
|
sl@0
|
137 |
proc p {} {return "p in [namespace current]"}
|
sl@0
|
138 |
p
|
sl@0
|
139 |
} {p in ::}
|
sl@0
|
140 |
test proc-3.2 {TclObjInterpProc, proc defined and executing in same namespace} {
|
sl@0
|
141 |
catch {eval namespace delete [namespace children :: test_ns_*]}
|
sl@0
|
142 |
namespace eval test_ns_1::baz {
|
sl@0
|
143 |
proc p {} {return "p in [namespace current]"}
|
sl@0
|
144 |
p
|
sl@0
|
145 |
}
|
sl@0
|
146 |
} {p in ::test_ns_1::baz}
|
sl@0
|
147 |
test proc-3.3 {TclObjInterpProc, proc defined and executing in different namespaces} {
|
sl@0
|
148 |
catch {eval namespace delete [namespace children :: test_ns_*]}
|
sl@0
|
149 |
catch {rename p ""}
|
sl@0
|
150 |
proc p {} {return "p in [namespace current]"}
|
sl@0
|
151 |
namespace eval test_ns_1::baz {
|
sl@0
|
152 |
p
|
sl@0
|
153 |
}
|
sl@0
|
154 |
} {p in ::}
|
sl@0
|
155 |
test proc-3.4 {TclObjInterpProc, procs execute in the namespace in which they were defined unless renamed into new namespace} {
|
sl@0
|
156 |
catch {eval namespace delete [namespace children :: test_ns_*]}
|
sl@0
|
157 |
catch {rename p ""}
|
sl@0
|
158 |
namespace eval test_ns_1::baz {
|
sl@0
|
159 |
proc p {} {return "p in [namespace current]"}
|
sl@0
|
160 |
rename ::test_ns_1::baz::p ::p
|
sl@0
|
161 |
list [p] [namespace which p]
|
sl@0
|
162 |
}
|
sl@0
|
163 |
} {{p in ::} ::p}
|
sl@0
|
164 |
test proc-3.5 {TclObjInterpProc, any old result is reset before appending error msg about missing arguments} {
|
sl@0
|
165 |
proc p {x} {info commands 3m}
|
sl@0
|
166 |
list [catch {p} msg] $msg
|
sl@0
|
167 |
} {1 {wrong # args: should be "p x"}}
|
sl@0
|
168 |
|
sl@0
|
169 |
test proc-3.6 {TclObjInterpProc, proper quoting of proc name, Bug 942757} {
|
sl@0
|
170 |
proc {a b c} {x} {info commands 3m}
|
sl@0
|
171 |
list [catch {{a b c}} msg] $msg
|
sl@0
|
172 |
} {1 {wrong # args: should be "{a b c} x"}}
|
sl@0
|
173 |
|
sl@0
|
174 |
catch {eval namespace delete [namespace children :: test_ns_*]}
|
sl@0
|
175 |
catch {rename p ""}
|
sl@0
|
176 |
catch {rename {} ""}
|
sl@0
|
177 |
catch {rename {a b c} {}}
|
sl@0
|
178 |
catch {unset msg}
|
sl@0
|
179 |
|
sl@0
|
180 |
if {[catch {package require procbodytest}]} {
|
sl@0
|
181 |
puts "This application couldn't load the \"procbodytest\" package, so I"
|
sl@0
|
182 |
puts "can't test creation of procs whose bodies have type \"procbody\"."
|
sl@0
|
183 |
::tcltest::cleanupTests
|
sl@0
|
184 |
return
|
sl@0
|
185 |
}
|
sl@0
|
186 |
|
sl@0
|
187 |
catch {rename p ""}
|
sl@0
|
188 |
catch {rename t ""}
|
sl@0
|
189 |
|
sl@0
|
190 |
# Note that the test require that procedures whose body is used to create
|
sl@0
|
191 |
# procbody objects must be executed before the procbodytest::proc command
|
sl@0
|
192 |
# is executed, so that the Proc struct is populated correctly (CompiledLocals
|
sl@0
|
193 |
# are added at compile time).
|
sl@0
|
194 |
|
sl@0
|
195 |
test proc-4.1 {TclCreateProc, procbody obj} {
|
sl@0
|
196 |
catch {
|
sl@0
|
197 |
proc p x {return "$x:$x"}
|
sl@0
|
198 |
set rv [p P]
|
sl@0
|
199 |
procbodytest::proc t x p
|
sl@0
|
200 |
lappend rv [t T]
|
sl@0
|
201 |
set rv
|
sl@0
|
202 |
} result
|
sl@0
|
203 |
catch {rename p ""}
|
sl@0
|
204 |
catch {rename t ""}
|
sl@0
|
205 |
set result
|
sl@0
|
206 |
} {P:P T:T}
|
sl@0
|
207 |
|
sl@0
|
208 |
test proc-4.2 {TclCreateProc, procbody obj, use compiled locals} {
|
sl@0
|
209 |
catch {
|
sl@0
|
210 |
proc p x {
|
sl@0
|
211 |
set y [string tolower $x]
|
sl@0
|
212 |
return "$x:$y"
|
sl@0
|
213 |
}
|
sl@0
|
214 |
set rv [p P]
|
sl@0
|
215 |
procbodytest::proc t x p
|
sl@0
|
216 |
lappend rv [t T]
|
sl@0
|
217 |
set rv
|
sl@0
|
218 |
} result
|
sl@0
|
219 |
catch {rename p ""}
|
sl@0
|
220 |
catch {rename t ""}
|
sl@0
|
221 |
set result
|
sl@0
|
222 |
} {P:p T:t}
|
sl@0
|
223 |
|
sl@0
|
224 |
test proc-4.3 {TclCreateProc, procbody obj, too many args} {
|
sl@0
|
225 |
catch {
|
sl@0
|
226 |
proc p x {
|
sl@0
|
227 |
set y [string tolower $x]
|
sl@0
|
228 |
return "$x:$y"
|
sl@0
|
229 |
}
|
sl@0
|
230 |
set rv [p P]
|
sl@0
|
231 |
procbodytest::proc t {x x1 x2} p
|
sl@0
|
232 |
lappend rv [t T]
|
sl@0
|
233 |
set rv
|
sl@0
|
234 |
} result
|
sl@0
|
235 |
catch {rename p ""}
|
sl@0
|
236 |
catch {rename t ""}
|
sl@0
|
237 |
set result
|
sl@0
|
238 |
} {procedure "t": arg list contains 3 entries, precompiled header expects 1}
|
sl@0
|
239 |
|
sl@0
|
240 |
test proc-4.4 {TclCreateProc, procbody obj, inconsitent arg name} {
|
sl@0
|
241 |
catch {
|
sl@0
|
242 |
proc p {x y z} {
|
sl@0
|
243 |
set v [join [list $x $y $z]]
|
sl@0
|
244 |
set w [string tolower $v]
|
sl@0
|
245 |
return "$v:$w"
|
sl@0
|
246 |
}
|
sl@0
|
247 |
set rv [p P Q R]
|
sl@0
|
248 |
procbodytest::proc t {x x1 z} p
|
sl@0
|
249 |
lappend rv [t S T U]
|
sl@0
|
250 |
set rv
|
sl@0
|
251 |
} result
|
sl@0
|
252 |
catch {rename p ""}
|
sl@0
|
253 |
catch {rename t ""}
|
sl@0
|
254 |
set result
|
sl@0
|
255 |
} {procedure "t": formal parameter 1 is inconsistent with precompiled body}
|
sl@0
|
256 |
|
sl@0
|
257 |
test proc-4.5 {TclCreateProc, procbody obj, inconsitent arg default type} {
|
sl@0
|
258 |
catch {
|
sl@0
|
259 |
proc p {x y {z Z}} {
|
sl@0
|
260 |
set v [join [list $x $y $z]]
|
sl@0
|
261 |
set w [string tolower $v]
|
sl@0
|
262 |
return "$v:$w"
|
sl@0
|
263 |
}
|
sl@0
|
264 |
set rv [p P Q R]
|
sl@0
|
265 |
procbodytest::proc t {x y z} p
|
sl@0
|
266 |
lappend rv [t S T U]
|
sl@0
|
267 |
set rv
|
sl@0
|
268 |
} result
|
sl@0
|
269 |
catch {rename p ""}
|
sl@0
|
270 |
catch {rename t ""}
|
sl@0
|
271 |
set result
|
sl@0
|
272 |
} {procedure "t": formal parameter 2 is inconsistent with precompiled body}
|
sl@0
|
273 |
|
sl@0
|
274 |
test proc-4.6 {TclCreateProc, procbody obj, inconsitent arg default type} {
|
sl@0
|
275 |
catch {
|
sl@0
|
276 |
proc p {x y z} {
|
sl@0
|
277 |
set v [join [list $x $y $z]]
|
sl@0
|
278 |
set w [string tolower $v]
|
sl@0
|
279 |
return "$v:$w"
|
sl@0
|
280 |
}
|
sl@0
|
281 |
set rv [p P Q R]
|
sl@0
|
282 |
procbodytest::proc t {x y {z Z}} p
|
sl@0
|
283 |
lappend rv [t S T U]
|
sl@0
|
284 |
set rv
|
sl@0
|
285 |
} result
|
sl@0
|
286 |
catch {rename p ""}
|
sl@0
|
287 |
catch {rename t ""}
|
sl@0
|
288 |
set result
|
sl@0
|
289 |
} {procedure "t": formal parameter 2 is inconsistent with precompiled body}
|
sl@0
|
290 |
|
sl@0
|
291 |
test proc-4.7 {TclCreateProc, procbody obj, inconsitent arg default value} {
|
sl@0
|
292 |
catch {
|
sl@0
|
293 |
proc p {x y {z Z}} {
|
sl@0
|
294 |
set v [join [list $x $y $z]]
|
sl@0
|
295 |
set w [string tolower $v]
|
sl@0
|
296 |
return "$v:$w"
|
sl@0
|
297 |
}
|
sl@0
|
298 |
set rv [p P Q R]
|
sl@0
|
299 |
procbodytest::proc t {x y {z ZZ}} p
|
sl@0
|
300 |
lappend rv [t S T U]
|
sl@0
|
301 |
set rv
|
sl@0
|
302 |
} result
|
sl@0
|
303 |
catch {rename p ""}
|
sl@0
|
304 |
catch {rename t ""}
|
sl@0
|
305 |
set result
|
sl@0
|
306 |
} {procedure "t": formal parameter "z" has default value inconsistent with precompiled body}
|
sl@0
|
307 |
|
sl@0
|
308 |
test proc-5.1 {Bytecompiling noop; test for correct argument substitution} {
|
sl@0
|
309 |
proc p args {} ; # this will be bytecompiled into t
|
sl@0
|
310 |
proc t {} {
|
sl@0
|
311 |
set res {}
|
sl@0
|
312 |
set a 0
|
sl@0
|
313 |
set b 0
|
sl@0
|
314 |
trace add variable a read {append res a ;#}
|
sl@0
|
315 |
trace add variable b write {append res b ;#}
|
sl@0
|
316 |
p $a ccccccw {bfe} {$a} [incr b] [incr a] {[incr b]} {$a} hello
|
sl@0
|
317 |
set res
|
sl@0
|
318 |
}
|
sl@0
|
319 |
set result [t]
|
sl@0
|
320 |
catch {rename p ""}
|
sl@0
|
321 |
catch {rename t ""}
|
sl@0
|
322 |
set result
|
sl@0
|
323 |
} {aba}
|
sl@0
|
324 |
|
sl@0
|
325 |
test proc-6.1 {ProcessProcResultCode: Bug 647307 (negative return code)} {
|
sl@0
|
326 |
proc a {} {return -code -5}
|
sl@0
|
327 |
proc b {} a
|
sl@0
|
328 |
set result [catch b]
|
sl@0
|
329 |
rename a {}
|
sl@0
|
330 |
rename b {}
|
sl@0
|
331 |
set result
|
sl@0
|
332 |
} -5
|
sl@0
|
333 |
|
sl@0
|
334 |
# cleanup
|
sl@0
|
335 |
catch {rename p ""}
|
sl@0
|
336 |
catch {rename t ""}
|
sl@0
|
337 |
::tcltest::cleanupTests
|
sl@0
|
338 |
return
|
sl@0
|
339 |
|
sl@0
|
340 |
|
sl@0
|
341 |
|
sl@0
|
342 |
|
sl@0
|
343 |
|
sl@0
|
344 |
|
sl@0
|
345 |
|
sl@0
|
346 |
|
sl@0
|
347 |
|
sl@0
|
348 |
|
sl@0
|
349 |
|
sl@0
|
350 |
|