sl@0: # Commands covered:  proc, return, global
sl@0: #
sl@0: # This file, proc-old.test, includes the original set of tests for Tcl's
sl@0: # proc, return, and global commands. There is now a new file proc.test
sl@0: # that contains tests for the tclProc.c source file.
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) 1991-1993 The Regents of the University of California.
sl@0: # Copyright (c) 1994-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-old.test,v 1.9.2.1 2003/03/27 21:46:32 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 {rename t1 ""}
sl@0: catch {rename foo ""}
sl@0: 
sl@0: proc tproc {} {return a; return b}
sl@0: test proc-old-1.1 {simple procedure call and return} {tproc} a
sl@0: proc tproc x {
sl@0:     set x [expr $x+1]
sl@0:     return $x
sl@0: }
sl@0: test proc-old-1.2 {simple procedure call and return} {tproc 2} 3
sl@0: test proc-old-1.3 {simple procedure call and return} {
sl@0:     proc tproc {} {return foo}
sl@0: } {}
sl@0: test proc-old-1.4 {simple procedure call and return} {
sl@0:     proc tproc {} {return}
sl@0:     tproc
sl@0: } {}
sl@0: proc tproc1 {a}   {incr a; return $a}
sl@0: proc tproc2 {a b} {incr a; return $a}
sl@0: test proc-old-1.5 {simple procedure call and return (2 procs with same body but different parameters)} {
sl@0:     list [tproc1 123] [tproc2 456 789]
sl@0: } {124 457}
sl@0: test proc-old-1.6 {simple procedure call and return (shared proc body string)} {
sl@0:     set x {}
sl@0:     proc tproc {} {}   ;# body is shared with x
sl@0:     list [tproc] [append x foo]
sl@0: } {{} foo}
sl@0: 
sl@0: test proc-old-2.1 {local and global variables} {
sl@0:     proc tproc x {
sl@0: 	set x [expr $x+1]
sl@0: 	return $x
sl@0:     }
sl@0:     set x 42
sl@0:     list [tproc 6] $x
sl@0: } {7 42}
sl@0: test proc-old-2.2 {local and global variables} {
sl@0:     proc tproc x {
sl@0: 	set y [expr $x+1]
sl@0: 	return $y
sl@0:     }
sl@0:     set y 18
sl@0:     list [tproc 6] $y
sl@0: } {7 18}
sl@0: test proc-old-2.3 {local and global variables} {
sl@0:     proc tproc x {
sl@0: 	global y
sl@0: 	set y [expr $x+1]
sl@0: 	return $y
sl@0:     }
sl@0:     set y 189
sl@0:     list [tproc 6] $y
sl@0: } {7 7}
sl@0: test proc-old-2.4 {local and global variables} {
sl@0:     proc tproc x {
sl@0: 	global y
sl@0: 	return [expr $x+$y]
sl@0:     }
sl@0:     set y 189
sl@0:     list [tproc 6] $y
sl@0: } {195 189}
sl@0: catch {unset _undefined_}
sl@0: test proc-old-2.5 {local and global variables} {
sl@0:     proc tproc x {
sl@0: 	global _undefined_
sl@0: 	return $_undefined_
sl@0:     }
sl@0:     list [catch {tproc xxx} msg] $msg
sl@0: } {1 {can't read "_undefined_": no such variable}}
sl@0: test proc-old-2.6 {local and global variables} {
sl@0:     set a 114
sl@0:     set b 115
sl@0:     global a b
sl@0:     list $a $b
sl@0: } {114 115}
sl@0: 
sl@0: proc do {cmd} {eval $cmd}
sl@0: test proc-old-3.1 {local and global arrays} {
sl@0:     catch {unset a}
sl@0:     set a(0) 22
sl@0:     list [catch {do {global a; set a(0)}} msg] $msg
sl@0: } {0 22}
sl@0: test proc-old-3.2 {local and global arrays} {
sl@0:     catch {unset a}
sl@0:     set a(x) 22
sl@0:     list [catch {do {global a; set a(x) newValue}} msg] $msg $a(x)
sl@0: } {0 newValue newValue}
sl@0: test proc-old-3.3 {local and global arrays} {
sl@0:     catch {unset a}
sl@0:     set a(x) 22
sl@0:     set a(y) 33
sl@0:     list [catch {do {global a; unset a(y)}; array names a} msg] $msg
sl@0: } {0 x}
sl@0: test proc-old-3.4 {local and global arrays} {
sl@0:     catch {unset a}
sl@0:     set a(x) 22
sl@0:     set a(y) 33
sl@0:     list [catch {do {global a; unset a; info exists a}} msg] $msg \
sl@0: 	    [info exists a]
sl@0: } {0 0 0}
sl@0: test proc-old-3.5 {local and global arrays} {
sl@0:     catch {unset a}
sl@0:     set a(x) 22
sl@0:     set a(y) 33
sl@0:     list [catch {do {global a; unset a(y); array names a}} msg] $msg
sl@0: } {0 x}
sl@0: catch {unset a}
sl@0: test proc-old-3.6 {local and global arrays} {
sl@0:     catch {unset a}
sl@0:     set a(x) 22
sl@0:     set a(y) 33
sl@0:     do {global a; do {global a; unset a}; set a(z) 22}
sl@0:     list [catch {array names a} msg] $msg
sl@0: } {0 z}
sl@0: test proc-old-3.7 {local and global arrays} {
sl@0:     proc t1 {args} {global info; set info 1}
sl@0:     catch {unset a}
sl@0:     set info {}
sl@0:     do {global a; trace var a(1) w t1}
sl@0:     set a(1) 44
sl@0:     set info
sl@0: } 1
sl@0: test proc-old-3.8 {local and global arrays} {
sl@0:     proc t1 {args} {global info; set info 1}
sl@0:     catch {unset a}
sl@0:     trace var a(1) w t1
sl@0:     set info {}
sl@0:     do {global a; trace vdelete a(1) w t1}
sl@0:     set a(1) 44
sl@0:     set info
sl@0: } {}
sl@0: test proc-old-3.9 {local and global arrays} {
sl@0:     proc t1 {args} {global info; set info 1}
sl@0:     catch {unset a}
sl@0:     trace var a(1) w t1
sl@0:     do {global a; trace vinfo a(1)}
sl@0: } {{w t1}}
sl@0: catch {unset a}
sl@0: 
sl@0: test proc-old-30.1 {arguments and defaults} {
sl@0:     proc tproc {x y z} {
sl@0: 	return [list $x $y $z]
sl@0:     }
sl@0:     tproc 11 12 13
sl@0: } {11 12 13}
sl@0: test proc-old-30.2 {arguments and defaults} {
sl@0:     proc tproc {x y z} {
sl@0: 	return [list $x $y $z]
sl@0:     }
sl@0:     list [catch {tproc 11 12} msg] $msg
sl@0: } {1 {wrong # args: should be "tproc x y z"}}
sl@0: test proc-old-30.3 {arguments and defaults} {
sl@0:     proc tproc {x y z} {
sl@0: 	return [list $x $y $z]
sl@0:     }
sl@0:     list [catch {tproc 11 12 13 14} msg] $msg
sl@0: } {1 {wrong # args: should be "tproc x y z"}}
sl@0: test proc-old-30.4 {arguments and defaults} {
sl@0:     proc tproc {x {y y-default} {z z-default}} {
sl@0: 	return [list $x $y $z]
sl@0:     }
sl@0:     tproc 11 12 13
sl@0: } {11 12 13}
sl@0: test proc-old-30.5 {arguments and defaults} {
sl@0:     proc tproc {x {y y-default} {z z-default}} {
sl@0: 	return [list $x $y $z]
sl@0:     }
sl@0:     tproc 11 12
sl@0: } {11 12 z-default}
sl@0: test proc-old-30.6 {arguments and defaults} {
sl@0:     proc tproc {x {y y-default} {z z-default}} {
sl@0: 	return [list $x $y $z]
sl@0:     }
sl@0:     tproc 11
sl@0: } {11 y-default z-default}
sl@0: test proc-old-30.7 {arguments and defaults} {
sl@0:     proc tproc {x {y y-default} {z z-default}} {
sl@0: 	return [list $x $y $z]
sl@0:     }
sl@0:     list [catch {tproc} msg] $msg
sl@0: } {1 {wrong # args: should be "tproc x ?y? ?z?"}}
sl@0: test proc-old-30.8 {arguments and defaults} {
sl@0:     list [catch {
sl@0: 	proc tproc {x {y y-default} z} {
sl@0: 	    return [list $x $y $z]
sl@0: 	}
sl@0: 	tproc 2 3
sl@0:     } msg] $msg
sl@0: } {1 {wrong # args: should be "tproc x ?y? z"}}
sl@0: test proc-old-30.9 {arguments and defaults} {
sl@0:     proc tproc {x {y y-default} args} {
sl@0: 	return [list $x $y $args]
sl@0:     }
sl@0:     tproc 2 3 4 5
sl@0: } {2 3 {4 5}}
sl@0: test proc-old-30.10 {arguments and defaults} {
sl@0:     proc tproc {x {y y-default} args} {
sl@0: 	return [list $x $y $args]
sl@0:     }
sl@0:     tproc 2 3
sl@0: } {2 3 {}}
sl@0: test proc-old-30.11 {arguments and defaults} {
sl@0:     proc tproc {x {y y-default} args} {
sl@0: 	return [list $x $y $args]
sl@0:     }
sl@0:     tproc 2
sl@0: } {2 y-default {}}
sl@0: test proc-old-30.12 {arguments and defaults} {
sl@0:     proc tproc {x {y y-default} args} {
sl@0: 	return [list $x $y $args]
sl@0:     }
sl@0:     list [catch {tproc} msg] $msg
sl@0: } {1 {wrong # args: should be "tproc x ?y? args"}}
sl@0: 
sl@0: test proc-old-4.1 {variable numbers of arguments} {
sl@0:     proc tproc args {return $args}
sl@0:     tproc
sl@0: } {}
sl@0: test proc-old-4.2 {variable numbers of arguments} {
sl@0:     proc tproc args {return $args}
sl@0:     tproc 1 2 3 4 5 6 7 8
sl@0: } {1 2 3 4 5 6 7 8}
sl@0: test proc-old-4.3 {variable numbers of arguments} {
sl@0:     proc tproc args {return $args}
sl@0:     tproc 1 {2 3} {4 {5 6} {{{7}}}} 8
sl@0: } {1 {2 3} {4 {5 6} {{{7}}}} 8}
sl@0: test proc-old-4.4 {variable numbers of arguments} {
sl@0:     proc tproc {x y args} {return $args}
sl@0:     tproc 1 2 3 4 5 6 7
sl@0: } {3 4 5 6 7}
sl@0: test proc-old-4.5 {variable numbers of arguments} {
sl@0:     proc tproc {x y args} {return $args}
sl@0:     tproc 1 2
sl@0: } {}
sl@0: test proc-old-4.6 {variable numbers of arguments} {
sl@0:     proc tproc {x missing args} {return $args}
sl@0:     list [catch {tproc 1} msg] $msg
sl@0: } {1 {wrong # args: should be "tproc x missing args"}}
sl@0: 
sl@0: test proc-old-5.1 {error conditions} {
sl@0:     list [catch {proc} msg] $msg
sl@0: } {1 {wrong # args: should be "proc name args body"}}
sl@0: test proc-old-5.2 {error conditions} {
sl@0:     list [catch {proc tproc b} msg] $msg
sl@0: } {1 {wrong # args: should be "proc name args body"}}
sl@0: test proc-old-5.3 {error conditions} {
sl@0:     list [catch {proc tproc b c d e} msg] $msg
sl@0: } {1 {wrong # args: should be "proc name args body"}}
sl@0: test proc-old-5.4 {error conditions} {
sl@0:     list [catch {proc tproc \{xyz {return foo}} msg] $msg
sl@0: } {1 {unmatched open brace in list}}
sl@0: test proc-old-5.5 {error conditions} {
sl@0:     list [catch {proc tproc {{} y} {return foo}} msg] $msg
sl@0: } {1 {procedure "tproc" has argument with no name}}
sl@0: test proc-old-5.6 {error conditions} {
sl@0:     list [catch {proc tproc {{} y} {return foo}} msg] $msg
sl@0: } {1 {procedure "tproc" has argument with no name}}
sl@0: test proc-old-5.7 {error conditions} {
sl@0:     list [catch {proc tproc {{x 1 2} y} {return foo}} msg] $msg
sl@0: } {1 {too many fields in argument specifier "x 1 2"}}
sl@0: test proc-old-5.8 {error conditions} {
sl@0:     catch {return}
sl@0: } 2
sl@0: test proc-old-5.9 {error conditions} {
sl@0:     list [catch {global} msg] $msg
sl@0: } {1 {wrong # args: should be "global varName ?varName ...?"}}
sl@0: proc tproc {} {
sl@0:     set a 22
sl@0:     global a
sl@0: }
sl@0: test proc-old-5.10 {error conditions} {
sl@0:     list [catch {tproc} msg] $msg
sl@0: } {1 {variable "a" already exists}}
sl@0: test proc-old-5.11 {error conditions} {
sl@0:     catch {rename tproc {}}
sl@0:     catch {
sl@0: 	proc tproc {x {} z} {return foo}
sl@0:     }
sl@0:     list [catch {tproc 1} msg] $msg
sl@0: } {1 {invalid command name "tproc"}}
sl@0: test proc-old-5.12 {error conditions} {
sl@0:     proc tproc {} {
sl@0: 	set a 22
sl@0: 	error "error in procedure"
sl@0: 	return
sl@0:     }
sl@0:     list [catch tproc msg] $msg
sl@0: } {1 {error in procedure}}
sl@0: test proc-old-5.13 {error conditions} {
sl@0:     proc tproc {} {
sl@0: 	set a 22
sl@0: 	error "error in procedure"
sl@0: 	return
sl@0:     }
sl@0:     catch tproc msg
sl@0:     set errorInfo
sl@0: } {error in procedure
sl@0:     while executing
sl@0: "error "error in procedure""
sl@0:     (procedure "tproc" line 3)
sl@0:     invoked from within
sl@0: "tproc"}
sl@0: test proc-old-5.14 {error conditions} {
sl@0:     proc tproc {} {
sl@0: 	set a 22
sl@0: 	break
sl@0: 	return
sl@0:     }
sl@0:     catch tproc msg
sl@0:     set errorInfo
sl@0: } {invoked "break" outside of a loop
sl@0:     (procedure "tproc" line 1)
sl@0:     invoked from within
sl@0: "tproc"}
sl@0: test proc-old-5.15 {error conditions} {
sl@0:     proc tproc {} {
sl@0: 	set a 22
sl@0: 	continue
sl@0: 	return
sl@0:     }
sl@0:     catch tproc msg
sl@0:     set errorInfo
sl@0: } {invoked "continue" outside of a loop
sl@0:     (procedure "tproc" line 1)
sl@0:     invoked from within
sl@0: "tproc"}
sl@0: test proc-old-5.16 {error conditions} {
sl@0:     proc foo args {
sl@0: 	global fooMsg
sl@0: 	set fooMsg "foo was called: $args"
sl@0:     }
sl@0:     proc tproc {} {
sl@0: 	set x 44
sl@0: 	trace var x u foo
sl@0: 	while {$x < 100} {
sl@0: 	    error "Nested error"
sl@0: 	}
sl@0:     }
sl@0:     set fooMsg "foo not called"
sl@0:     list [catch tproc msg] $msg $errorInfo $fooMsg
sl@0: } {1 {Nested error} {Nested error
sl@0:     while executing
sl@0: "error "Nested error""
sl@0:     (procedure "tproc" line 5)
sl@0:     invoked from within
sl@0: "tproc"} {foo was called: x {} u}}
sl@0: 
sl@0: # The tests below will really only be useful when run under Purify or
sl@0: # some other system that can detect accesses to freed memory...
sl@0: 
sl@0: test proc-old-6.1 {procedure that redefines itself} {
sl@0:     proc tproc {} {
sl@0: 	proc tproc {} {
sl@0: 	    return 44
sl@0: 	}
sl@0: 	return 45
sl@0:     }
sl@0:     tproc
sl@0: } 45
sl@0: test proc-old-6.2 {procedure that deletes itself} {
sl@0:     proc tproc {} {
sl@0: 	rename tproc {}
sl@0: 	return 45
sl@0:     }
sl@0:     tproc
sl@0: } 45
sl@0: 
sl@0: proc tproc code {
sl@0:     return -code $code abc
sl@0: }
sl@0: test proc-old-7.1 {return with special completion code} {
sl@0:     list [catch {tproc ok} msg] $msg
sl@0: } {0 abc}
sl@0: test proc-old-7.2 {return with special completion code} {
sl@0:     list [catch {tproc error} msg] $msg $errorInfo $errorCode
sl@0: } {1 abc {abc
sl@0:     while executing
sl@0: "tproc error"} NONE}
sl@0: test proc-old-7.3 {return with special completion code} {
sl@0:     list [catch {tproc return} msg] $msg
sl@0: } {2 abc}
sl@0: test proc-old-7.4 {return with special completion code} {
sl@0:     list [catch {tproc break} msg] $msg
sl@0: } {3 abc}
sl@0: test proc-old-7.5 {return with special completion code} {
sl@0:     list [catch {tproc continue} msg] $msg
sl@0: } {4 abc}
sl@0: test proc-old-7.6 {return with special completion code} {
sl@0:     list [catch {tproc -14} msg] $msg
sl@0: } {-14 abc}
sl@0: test proc-old-7.7 {return with special completion code} {
sl@0:     list [catch {tproc gorp} msg] $msg
sl@0: } {1 {bad completion code "gorp": must be ok, error, return, break, continue, or an integer}}
sl@0: test proc-old-7.8 {return with special completion code} {
sl@0:     list [catch {tproc 10b} msg] $msg
sl@0: } {1 {bad completion code "10b": must be ok, error, return, break, continue, or an integer}}
sl@0: test proc-old-7.9 {return with special completion code} {
sl@0:     proc tproc2 {} {
sl@0: 	tproc return
sl@0:     }
sl@0:     list [catch tproc2 msg] $msg
sl@0: } {0 abc}
sl@0: test proc-old-7.10 {return with special completion code} {
sl@0:     proc tproc2 {} {
sl@0: 	return -code error
sl@0:     }
sl@0:     list [catch tproc2 msg] $msg
sl@0: } {1 {}}
sl@0: test proc-old-7.11 {return with special completion code} {
sl@0:     proc tproc2 {} {
sl@0: 	global errorCode errorInfo
sl@0: 	catch {open _bad_file_name r} msg
sl@0: 	return -code error -errorinfo $errorInfo -errorcode $errorCode $msg
sl@0:     }
sl@0:     set msg [list [catch tproc2 msg] $msg $errorInfo $errorCode]
sl@0:     regsub -all [file join {} _bad_file_name] $msg "_bad_file_name" msg
sl@0:     normalizeMsg $msg
sl@0: } {1 {couldn't open "_bad_file_name": no such file or directory} {couldn't open "_bad_file_name": no such file or directory
sl@0:     while executing
sl@0: "open _bad_file_name r"
sl@0:     invoked from within
sl@0: "tproc2"} {posix enoent {no such file or directory}}}
sl@0: test proc-old-7.12 {return with special completion code} {
sl@0:     proc tproc2 {} {
sl@0: 	global errorCode errorInfo
sl@0: 	catch {open _bad_file_name r} msg
sl@0: 	return -code error -errorcode $errorCode $msg
sl@0:     }
sl@0:     set msg [list [catch tproc2 msg] $msg $errorInfo $errorCode]
sl@0:     regsub -all [file join {} _bad_file_name] $msg "_bad_file_name" msg
sl@0:     normalizeMsg $msg
sl@0: } {1 {couldn't open "_bad_file_name": no such file or directory} {couldn't open "_bad_file_name": no such file or directory
sl@0:     while executing
sl@0: "tproc2"} {posix enoent {no such file or directory}}}
sl@0: test proc-old-7.13 {return with special completion code} {
sl@0:     proc tproc2 {} {
sl@0: 	global errorCode errorInfo
sl@0: 	catch {open _bad_file_name r} msg
sl@0: 	return -code error -errorinfo $errorInfo $msg
sl@0:     }
sl@0:     set msg [list [catch tproc2 msg] $msg $errorInfo $errorCode]
sl@0:     regsub -all [file join {} _bad_file_name] $msg "_bad_file_name" msg
sl@0:     normalizeMsg $msg
sl@0: } {1 {couldn't open "_bad_file_name": no such file or directory} {couldn't open "_bad_file_name": no such file or directory
sl@0:     while executing
sl@0: "open _bad_file_name r"
sl@0:     invoked from within
sl@0: "tproc2"} none}
sl@0: test proc-old-7.14 {return with special completion code} {
sl@0:     proc tproc2 {} {
sl@0: 	global errorCode errorInfo
sl@0: 	catch {open _bad_file_name r} msg
sl@0: 	return -code error $msg
sl@0:     }
sl@0:     set msg [list [catch tproc2 msg] $msg $errorInfo $errorCode]
sl@0:     regsub -all [file join {} _bad_file_name] $msg "_bad_file_name" msg
sl@0:     normalizeMsg $msg
sl@0: } {1 {couldn't open "_bad_file_name": no such file or directory} {couldn't open "_bad_file_name": no such file or directory
sl@0:     while executing
sl@0: "tproc2"} none}
sl@0: test proc-old-7.15 {return with special completion code} {
sl@0:     list [catch {return -badOption foo message} msg] $msg
sl@0: } {1 {bad option "-badOption": must be -code, -errorcode, or -errorinfo}}
sl@0: 
sl@0: test proc-old-8.1 {unset and undefined local arrays} {
sl@0:     proc t1 {} {
sl@0:         foreach v {xxx, yyy} {
sl@0:             catch {unset $v}
sl@0:         }
sl@0:         set yyy(foo) bar
sl@0:     }
sl@0:     t1
sl@0: } bar
sl@0: 
sl@0: test proc-old-9.1 {empty command name} {
sl@0:     catch {rename {} ""}
sl@0:     proc t1 {args} {
sl@0:         return
sl@0:     }
sl@0:     set v [t1]
sl@0:     catch {$v}
sl@0: } 1
sl@0: 
sl@0: test proc-old-10.1 {ByteCode epoch change during recursive proc execution} {
sl@0:     proc t1 x {
sl@0:         set y 20
sl@0:         rename expr expr.old
sl@0:         rename expr.old expr
sl@0:         if $x then {t1 0} ;# recursive call after foo's code is invalidated
sl@0:         return 20
sl@0:     }
sl@0:     t1 1
sl@0: } 20
sl@0: 
sl@0: # cleanup
sl@0: catch {rename t1 ""}
sl@0: catch {rename foo ""}
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: