sl@0: # This file contains tests for the tclProc.c source file. Tests appear in
sl@0: # the same order as the C code that they test. The set of tests is
sl@0: # currently incomplete since it includes only new tests, in particular
sl@0: # tests for code changed for the addition of Tcl namespaces. Other
sl@0: # procedure-related tests appear in other test files such as proc-old.test.
sl@0: #
sl@0: # Sourcing this file into Tcl runs the tests and generates output for
sl@0: # errors.  No output means no errors were found.
sl@0: #
sl@0: # Copyright (c) 1997 Sun Microsystems, Inc.
sl@0: # Copyright (c) 1998-1999 by Scriptics Corporation.
sl@0: #
sl@0: # See the file "license.terms" for information on usage and redistribution
sl@0: # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
sl@0: #
sl@0: # RCS: @(#) $Id: proc.test,v 1.11.2.1 2004/05/02 21:07:16 msofer Exp $
sl@0: 
sl@0: if {[lsearch [namespace children] ::tcltest] == -1} {
sl@0:     package require tcltest
sl@0:     namespace import -force ::tcltest::*
sl@0: }
sl@0: 
sl@0: catch {eval namespace delete [namespace children :: test_ns_*]}
sl@0: catch {rename p ""}
sl@0: catch {rename {} ""}
sl@0: catch {unset msg}
sl@0: 
sl@0: test proc-1.1 {Tcl_ProcObjCmd, put proc in namespace specified in name, if any} {
sl@0:     catch {eval namespace delete [namespace children :: test_ns_*]}
sl@0:     namespace eval test_ns_1 {
sl@0:         namespace eval baz {}
sl@0:     }
sl@0:     proc test_ns_1::baz::p {} {
sl@0:         return "p in [namespace current]"
sl@0:     }
sl@0:     list [test_ns_1::baz::p] \
sl@0:          [namespace eval test_ns_1 {baz::p}] \
sl@0:          [info commands test_ns_1::baz::*]
sl@0: } {{p in ::test_ns_1::baz} {p in ::test_ns_1::baz} ::test_ns_1::baz::p}
sl@0: test proc-1.2 {Tcl_ProcObjCmd, namespace specified in proc name must exist} {
sl@0:     catch {eval namespace delete [namespace children :: test_ns_*]}
sl@0:     list [catch {proc test_ns_1::baz::p {} {}} msg] $msg
sl@0: } {1 {can't create procedure "test_ns_1::baz::p": unknown namespace}}
sl@0: test proc-1.3 {Tcl_ProcObjCmd, empty proc name} {
sl@0:     catch {eval namespace delete [namespace children :: test_ns_*]}
sl@0:     proc :: {} {
sl@0:         return "empty called"
sl@0:     }
sl@0:     list [::] \
sl@0:          [info body {}]
sl@0: } {{empty called} {
sl@0:         return "empty called"
sl@0:     }}
sl@0: test proc-1.4 {Tcl_ProcObjCmd, simple proc name and proc defined in namespace} {
sl@0:     catch {eval namespace delete [namespace children :: test_ns_*]}
sl@0:     namespace eval test_ns_1 {
sl@0:         namespace eval baz {
sl@0:             proc p {} {
sl@0:                 return "p in [namespace current]"
sl@0:             }
sl@0:         }
sl@0:     }
sl@0:     list [test_ns_1::baz::p] \
sl@0:          [info commands test_ns_1::baz::*]
sl@0: } {{p in ::test_ns_1::baz} ::test_ns_1::baz::p}
sl@0: test proc-1.5 {Tcl_ProcObjCmd, qualified proc name and proc defined in namespace} {
sl@0:     catch {eval namespace delete [namespace children :: test_ns_*]}
sl@0:     namespace eval test_ns_1::baz {}
sl@0:     namespace eval test_ns_1 {
sl@0:         proc baz::p {} {
sl@0:             return "p in [namespace current]"
sl@0:         }
sl@0:     }
sl@0:     list [test_ns_1::baz::p] \
sl@0:          [info commands test_ns_1::baz::*] \
sl@0:          [namespace eval test_ns_1::baz {namespace which p}]
sl@0: } {{p in ::test_ns_1::baz} ::test_ns_1::baz::p ::test_ns_1::baz::p}
sl@0: test proc-1.6 {Tcl_ProcObjCmd, namespace code ignores single ":"s in middle or end of command names} {
sl@0:     catch {eval namespace delete [namespace children :: test_ns_*]}
sl@0:     namespace eval test_ns_1 {
sl@0:         proc q: {} {return "q:"}
sl@0:         proc value:at: {} {return "value:at:"}
sl@0:     }
sl@0:     list [namespace eval test_ns_1 {q:}] \
sl@0:          [namespace eval test_ns_1 {value:at:}] \
sl@0:          [test_ns_1::q:] \
sl@0:          [test_ns_1::value:at:] \
sl@0:          [lsort [info commands test_ns_1::*]] \
sl@0:          [namespace eval test_ns_1 {namespace which q:}] \
sl@0:          [namespace eval test_ns_1 {namespace which value:at:}]
sl@0: } {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: test proc-1.7 {Tcl_ProcObjCmd, check that formal parameter names are not array elements} {
sl@0:     catch {rename p ""}
sl@0:     list [catch {proc p {a(1) a(2)} { 
sl@0:             set z [expr $a(1)+$a(2)]
sl@0:             puts "$z=z, $a(1)=$a(1)"
sl@0:         }} msg] $msg
sl@0: } {1 {procedure "p" has formal parameter "a(1)" that is an array element}}
sl@0: test proc-1.8 {Tcl_ProcObjCmd, check that formal parameter names are simple names} {
sl@0:     catch {rename p ""}
sl@0:     list [catch {proc p {b:a b::a} { 
sl@0:     }} msg] $msg
sl@0: } {1 {procedure "p" has formal parameter "b::a" that is not a simple name}}
sl@0: 
sl@0: test proc-2.1 {TclFindProc, simple proc name and proc not in namespace} {
sl@0:     catch {eval namespace delete [namespace children :: test_ns_*]}
sl@0:     catch {rename p ""}
sl@0:     proc p {} {return "p in [namespace current]"}
sl@0:     info body p
sl@0: } {return "p in [namespace current]"}
sl@0: test proc-2.2 {TclFindProc, simple proc name and proc defined in namespace} {
sl@0:     catch {eval namespace delete [namespace children :: test_ns_*]}
sl@0:     namespace eval test_ns_1 {
sl@0:         namespace eval baz {
sl@0:             proc p {} {return "p in [namespace current]"}
sl@0:         }
sl@0:     }
sl@0:     namespace eval test_ns_1::baz {info body p}
sl@0: } {return "p in [namespace current]"}
sl@0: test proc-2.3 {TclFindProc, qualified proc name and proc defined in namespace} {
sl@0:     catch {eval namespace delete [namespace children :: test_ns_*]}
sl@0:     namespace eval test_ns_1::baz {}
sl@0:     namespace eval test_ns_1 {
sl@0:         proc baz::p {} {return "p in [namespace current]"}
sl@0:     }
sl@0:     namespace eval test_ns_1 {info body baz::p}
sl@0: } {return "p in [namespace current]"}
sl@0: test proc-2.4 {TclFindProc, global proc and executing in namespace} {
sl@0:     catch {eval namespace delete [namespace children :: test_ns_*]}
sl@0:     catch {rename p ""}
sl@0:     proc p {} {return "global p"}
sl@0:     namespace eval test_ns_1::baz {info body p}
sl@0: } {return "global p"}
sl@0: 
sl@0: test proc-3.1 {TclObjInterpProc, proc defined and executing in same namespace} {
sl@0:     catch {eval namespace delete [namespace children :: test_ns_*]}
sl@0:     proc p {} {return "p in [namespace current]"}
sl@0:     p
sl@0: } {p in ::}
sl@0: test proc-3.2 {TclObjInterpProc, proc defined and executing in same namespace} {
sl@0:     catch {eval namespace delete [namespace children :: test_ns_*]}
sl@0:     namespace eval test_ns_1::baz {
sl@0:         proc p {} {return "p in [namespace current]"}
sl@0:         p
sl@0:     }
sl@0: } {p in ::test_ns_1::baz}
sl@0: test proc-3.3 {TclObjInterpProc, proc defined and executing in different namespaces} {
sl@0:     catch {eval namespace delete [namespace children :: test_ns_*]}
sl@0:     catch {rename p ""}
sl@0:     proc p {} {return "p in [namespace current]"}
sl@0:     namespace eval test_ns_1::baz {
sl@0:         p
sl@0:     }
sl@0: } {p in ::}
sl@0: test proc-3.4 {TclObjInterpProc, procs execute in the namespace in which they were defined unless renamed into new namespace} {
sl@0:     catch {eval namespace delete [namespace children :: test_ns_*]}
sl@0:     catch {rename p ""}
sl@0:     namespace eval test_ns_1::baz {
sl@0:         proc p {} {return "p in [namespace current]"}
sl@0:         rename ::test_ns_1::baz::p ::p
sl@0:         list [p] [namespace which p]
sl@0:     }
sl@0: } {{p in ::} ::p}
sl@0: test proc-3.5 {TclObjInterpProc, any old result is reset before appending error msg about missing arguments} {
sl@0:     proc p {x} {info commands 3m}
sl@0:     list [catch {p} msg] $msg
sl@0: } {1 {wrong # args: should be "p x"}}
sl@0: 
sl@0: test proc-3.6 {TclObjInterpProc, proper quoting of proc name, Bug 942757} {
sl@0:     proc {a b  c} {x} {info commands 3m}
sl@0:     list [catch {{a b  c}} msg] $msg
sl@0: } {1 {wrong # args: should be "{a b  c} x"}}
sl@0: 
sl@0: catch {eval namespace delete [namespace children :: test_ns_*]}
sl@0: catch {rename p ""}
sl@0: catch {rename {} ""}
sl@0: catch {rename {a b  c} {}}
sl@0: catch {unset msg}
sl@0: 
sl@0: if {[catch {package require procbodytest}]} {
sl@0:     puts "This application couldn't load the \"procbodytest\" package, so I"
sl@0:     puts "can't test creation of procs whose bodies have type \"procbody\"."
sl@0:     ::tcltest::cleanupTests
sl@0:     return
sl@0: }
sl@0: 
sl@0: catch {rename p ""}
sl@0: catch {rename t ""}
sl@0: 
sl@0: # Note that the test require that procedures whose body is used to create
sl@0: # procbody objects must be executed before the procbodytest::proc command
sl@0: # is executed, so that the Proc struct is populated correctly (CompiledLocals
sl@0: # are added at compile time).
sl@0: 
sl@0: test proc-4.1 {TclCreateProc, procbody obj} {
sl@0:     catch {
sl@0: 	proc p x {return "$x:$x"}
sl@0: 	set rv [p P]
sl@0: 	procbodytest::proc t x p
sl@0: 	lappend rv [t T]
sl@0: 	set rv
sl@0:     } result
sl@0:     catch {rename p ""}
sl@0:     catch {rename t ""}
sl@0:     set result
sl@0: } {P:P T:T}
sl@0: 
sl@0: test proc-4.2 {TclCreateProc, procbody obj, use compiled locals} {
sl@0:     catch {
sl@0: 	proc p x {
sl@0: 	    set y [string tolower $x]
sl@0: 	    return "$x:$y"
sl@0: 	}
sl@0: 	set rv [p P]
sl@0: 	procbodytest::proc t x p
sl@0: 	lappend rv [t T]
sl@0: 	set rv
sl@0:     } result
sl@0:     catch {rename p ""}
sl@0:     catch {rename t ""}
sl@0:     set result
sl@0: } {P:p T:t}
sl@0: 
sl@0: test proc-4.3 {TclCreateProc, procbody obj, too many args} {
sl@0:     catch {
sl@0: 	proc p x {
sl@0: 	    set y [string tolower $x]
sl@0: 	    return "$x:$y"
sl@0: 	}
sl@0: 	set rv [p P]
sl@0: 	procbodytest::proc t {x x1 x2} p
sl@0: 	lappend rv [t T]
sl@0: 	set rv
sl@0:     } result
sl@0:     catch {rename p ""}
sl@0:     catch {rename t ""}
sl@0:     set result
sl@0: } {procedure "t": arg list contains 3 entries, precompiled header expects 1}
sl@0: 
sl@0: test proc-4.4 {TclCreateProc, procbody obj, inconsitent arg name} {
sl@0:     catch {
sl@0: 	proc p {x y z} {
sl@0: 	    set v [join [list $x $y $z]]
sl@0: 	    set w [string tolower $v]
sl@0: 	    return "$v:$w"
sl@0: 	}
sl@0: 	set rv [p P Q R]
sl@0: 	procbodytest::proc t {x x1 z} p
sl@0: 	lappend rv [t S T U]
sl@0: 	set rv
sl@0:     } result
sl@0:     catch {rename p ""}
sl@0:     catch {rename t ""}
sl@0:     set result
sl@0: } {procedure "t": formal parameter 1 is inconsistent with precompiled body}
sl@0: 
sl@0: test proc-4.5 {TclCreateProc, procbody obj, inconsitent arg default type} {
sl@0:     catch {
sl@0: 	proc p {x y {z Z}} {
sl@0: 	    set v [join [list $x $y $z]]
sl@0: 	    set w [string tolower $v]
sl@0: 	    return "$v:$w"
sl@0: 	}
sl@0: 	set rv [p P Q R]
sl@0: 	procbodytest::proc t {x y z} p
sl@0: 	lappend rv [t S T U]
sl@0: 	set rv
sl@0:     } result
sl@0:     catch {rename p ""}
sl@0:     catch {rename t ""}
sl@0:     set result
sl@0: } {procedure "t": formal parameter 2 is inconsistent with precompiled body}
sl@0: 
sl@0: test proc-4.6 {TclCreateProc, procbody obj, inconsitent arg default type} {
sl@0:     catch {
sl@0: 	proc p {x y z} {
sl@0: 	    set v [join [list $x $y $z]]
sl@0: 	    set w [string tolower $v]
sl@0: 	    return "$v:$w"
sl@0: 	}
sl@0: 	set rv [p P Q R]
sl@0: 	procbodytest::proc t {x y {z Z}} p
sl@0: 	lappend rv [t S T U]
sl@0: 	set rv
sl@0:     } result
sl@0:     catch {rename p ""}
sl@0:     catch {rename t ""}
sl@0:     set result
sl@0: } {procedure "t": formal parameter 2 is inconsistent with precompiled body}
sl@0: 
sl@0: test proc-4.7 {TclCreateProc, procbody obj, inconsitent arg default value} {
sl@0:     catch {
sl@0: 	proc p {x y {z Z}} {
sl@0: 	    set v [join [list $x $y $z]]
sl@0: 	    set w [string tolower $v]
sl@0: 	    return "$v:$w"
sl@0: 	}
sl@0: 	set rv [p P Q R]
sl@0: 	procbodytest::proc t {x y {z ZZ}} p
sl@0: 	lappend rv [t S T U]
sl@0: 	set rv
sl@0:     } result
sl@0:     catch {rename p ""}
sl@0:     catch {rename t ""}
sl@0:     set result
sl@0: } {procedure "t": formal parameter "z" has default value inconsistent with precompiled body}
sl@0: 
sl@0: test proc-5.1 {Bytecompiling noop; test for correct argument substitution} {
sl@0:     proc p args {} ; # this will be bytecompiled into t
sl@0:     proc t {} {
sl@0: 	set res {}
sl@0: 	set a 0
sl@0: 	set b 0
sl@0: 	trace add variable a read {append res a ;#}
sl@0: 	trace add variable b write {append res b ;#}
sl@0: 	p $a ccccccw {bfe} {$a} [incr b] [incr a] {[incr b]} {$a} hello
sl@0: 	set res
sl@0:     }
sl@0:     set result [t]
sl@0:     catch {rename p ""}
sl@0:     catch {rename t ""}
sl@0:     set result
sl@0: } {aba}    
sl@0: 
sl@0: test proc-6.1 {ProcessProcResultCode: Bug 647307 (negative return code)} {
sl@0:     proc a {} {return -code -5}
sl@0:     proc b {} a
sl@0:     set result [catch b]
sl@0:     rename a {}
sl@0:     rename b {}
sl@0:     set result
sl@0: } -5
sl@0: 
sl@0: # cleanup
sl@0: catch {rename p ""}
sl@0: catch {rename t ""}
sl@0: ::tcltest::cleanupTests
sl@0: return
sl@0: 
sl@0: 
sl@0: 
sl@0: 
sl@0: 
sl@0: 
sl@0: 
sl@0: 
sl@0: 
sl@0: 
sl@0: 
sl@0: