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