📄 namespace-old.test
字号:
return [uplevel $num {info vars}] } proc test_uplevel {num} { set a 0 set b 1 namespace eval ::test_ns_uplevel " return \[show_vars $num\] " } }} {}test namespace-old-7.2 {uplevel can access namespace call frame} { list [expr {[lsearch -exact [test_ns_uplevel::test_uplevel 1] x]>=0}] \ [expr {[lsearch -exact [test_ns_uplevel::test_uplevel 1] y]>=0}]} {1 1}test namespace-old-7.3 {uplevel can go beyond namespace call frame} { lsort [test_ns_uplevel::test_uplevel 2]} {a b num}test namespace-old-7.4 {uplevel can go up to global context} { expr {[test_ns_uplevel::test_uplevel 3] == [info globals]}} {1}test namespace-old-7.5 {absolute call frame references work too} { list [expr {[lsearch -exact [test_ns_uplevel::test_uplevel #2] x]>=0}] \ [expr {[lsearch -exact [test_ns_uplevel::test_uplevel #2] y]>=0}]} {1 1}test namespace-old-7.6 {absolute call frame references work too} { lsort [test_ns_uplevel::test_uplevel #1]} {a b num}test namespace-old-7.7 {absolute call frame references work too} { expr {[test_ns_uplevel::test_uplevel #0] == [info globals]}} {1}test namespace-old-7.8 {namespaces are included in the call stack} { namespace eval test_ns_upvar { variable scope "test_ns_upvar" proc show_val {var num} { upvar $num $var x return $x } proc test_upvar {num} { set scope "test_ns_upvar::test_upvar" namespace eval ::test_ns_upvar " return \[show_val scope $num\] " } }} {}test namespace-old-7.9 {upvar can access namespace call frame} { test_ns_upvar::test_upvar 1} {test_ns_upvar}test namespace-old-7.10 {upvar can go beyond namespace call frame} { test_ns_upvar::test_upvar 2} {test_ns_upvar::test_upvar}test namespace-old-7.11 {absolute call frame references work too} { test_ns_upvar::test_upvar #2} {test_ns_upvar}test namespace-old-7.12 {absolute call frame references work too} { test_ns_upvar::test_upvar #1} {test_ns_upvar::test_upvar}# -----------------------------------------------------------------------# TEST: variable traces across namespace boundaries# -----------------------------------------------------------------------test namespace-old-8.1 {traces work across namespace boundaries} { namespace eval test_ns_trace { namespace eval foo { variable x "" } variable status "" proc monitor {name1 name2 op} { variable status lappend status "$op: $name1" } trace variable foo::x rwu [namespace code monitor] } set test_ns_trace::foo::x "yes!" set test_ns_trace::foo::x unset test_ns_trace::foo::x namespace eval test_ns_trace { set status }} {{w: test_ns_trace::foo::x} {r: test_ns_trace::foo::x} {u: test_ns_trace::foo::x}}# -----------------------------------------------------------------------# TEST: imported commands# -----------------------------------------------------------------------test namespace-old-9.1 {empty "namespace export" list} { list [catch "namespace export" msg] $msg} {0 {}}test namespace-old-9.2 {usage for "namespace export" command} { list [catch "namespace export test_ns_trace::zzz" msg] $msg} {1 {invalid export pattern "test_ns_trace::zzz": pattern can't specify a namespace}}test namespace-old-9.3 {define test namespaces for import} { namespace eval test_ns_export { namespace export cmd1 cmd2 cmd3 proc cmd1 {args} {return "cmd1: $args"} proc cmd2 {args} {return "cmd2: $args"} proc cmd3 {args} {return "cmd3: $args"} proc cmd4 {args} {return "cmd4: $args"} proc cmd5 {args} {return "cmd5: $args"} proc cmd6 {args} {return "cmd6: $args"} } lsort [info commands test_ns_export::*]} {::test_ns_export::cmd1 ::test_ns_export::cmd2 ::test_ns_export::cmd3 ::test_ns_export::cmd4 ::test_ns_export::cmd5 ::test_ns_export::cmd6}test namespace-old-9.4 {check export status} { set x "" namespace eval test_ns_import { namespace export cmd1 cmd2 namespace import ::test_ns_export::* } foreach cmd [lsort [info commands test_ns_import::*]] { lappend x $cmd } set x} {::test_ns_import::cmd1 ::test_ns_import::cmd2 ::test_ns_import::cmd3}test namespace-old-9.5 {empty import list in "namespace import" command} { namespace import} {}test namespace-old-9.6 {empty import list for "namespace import" command} { namespace import} {}test namespace-old-9.7 {empty forget list for "namespace forget" command} { namespace forget} {}catch {rename cmd1 {}}catch {rename cmd2 {}}catch {rename ncmd {}}catch {rename ncmd1 {}}catch {rename ncmd2 {}}test namespace-old-9.8 {only exported commands are imported} { namespace import test_ns_import::cmd* set x [lsort [info commands cmd*]]} {cmd1 cmd2}test namespace-old-9.9 {imported commands work just the same as original} { list [cmd1 test 1 2 3] [test_ns_import::cmd1 test 4 5 6]} {{cmd1: test 1 2 3} {cmd1: test 4 5 6}}test namespace-old-9.10 {commands can be imported from many namespaces} { namespace eval test_ns_import2 { namespace export ncmd ncmd1 ncmd2 proc ncmd {args} {return "ncmd: $args"} proc ncmd1 {args} {return "ncmd1: $args"} proc ncmd2 {args} {return "ncmd2: $args"} proc ncmd3 {args} {return "ncmd3: $args"} } namespace import test_ns_import2::* lsort [concat [info commands cmd*] [info commands ncmd*]]} {cmd1 cmd2 ncmd ncmd1 ncmd2}test namespace-old-9.11 {imported commands can be removed by deleting them} { rename cmd1 "" lsort [concat [info commands cmd*] [info commands ncmd*]]} {cmd2 ncmd ncmd1 ncmd2}test namespace-old-9.12 {command "namespace forget" checks for valid namespaces} { list [catch {namespace forget xyzzy::*} msg] $msg} {1 {unknown namespace in namespace forget pattern "xyzzy::*"}}test namespace-old-9.13 {command "namespace forget" ignores patterns that don't match} { list [catch {namespace forget test_ns_import::xy*zzy} msg] $msg \ [lsort [info commands cmd?]]} {0 {} cmd2}test namespace-old-9.14 {imported commands can be removed} { namespace forget test_ns_import::cmd? list [lsort [info commands cmd?]] \ [catch {cmd1 another test} msg] $msg} {{} 1 {invalid command name "cmd1"}}test namespace-old-9.15 {existing commands can't be overwritten} { proc cmd1 {x y} { return [expr $x+$y] } list [catch {namespace import test_ns_import::cmd?} msg] $msg \ [cmd1 3 5]} {1 {can't import command "cmd1": already exists} 8}test namespace-old-9.16 {use "-force" option to override existing commands} { list [cmd1 3 5] \ [namespace import -force test_ns_import::cmd?] \ [cmd1 3 5]} {8 {} {cmd1: 3 5}}test namespace-old-9.17 {commands can be imported into many namespaces} { namespace eval test_ns_import_use { namespace import ::test_ns_import::* ::test_ns_import2::ncmd? lsort [concat [info commands ::test_ns_import_use::cmd*] \ [info commands ::test_ns_import_use::ncmd*]] }} {::test_ns_import_use::cmd1 ::test_ns_import_use::cmd2 ::test_ns_import_use::ncmd1 ::test_ns_import_use::ncmd2}test namespace-old-9.18 {when command is deleted, imported commands go away} { namespace eval test_ns_import { rename cmd1 "" } list [info commands cmd1] \ [namespace eval test_ns_import_use {info commands cmd1}]} {{} {}}test namespace-old-9.19 {when namesp is deleted, all imported commands go away} { namespace delete test_ns_import test_ns_import2 list [info commands cmd*] \ [info commands ncmd*] \ [namespace eval test_ns_import_use {info commands cmd*}] \ [namespace eval test_ns_import_use {info commands ncmd*}] \} {{} {} {} {}}# -----------------------------------------------------------------------# TEST: scoped values# -----------------------------------------------------------------------test namespace-old-10.1 {define namespace for scope test} { namespace eval test_ns_inscope { variable x "x-value" proc show {args} { return "show: $args" } proc do {args} { return [eval $args] } list [set x] [show test] }} {x-value {show: test}}test namespace-old-10.2 {command "namespace code" requires one argument} { list [catch {namespace code} msg] $msg} {1 {wrong # args: should be "namespace code arg"}}test namespace-old-10.3 {command "namespace code" requires one argument} { list [catch {namespace code first "second arg" third} msg] $msg} {1 {wrong # args: should be "namespace code arg"}}test namespace-old-10.4 {command "namespace code" gets current namesp context} { namespace eval test_ns_inscope { namespace code {"1 2 3" "4 5" 6} }} {namespace inscope ::test_ns_inscope {"1 2 3" "4 5" 6}}test namespace-old-10.5 {with one arg, first "scope" sticks} { set sval [namespace eval test_ns_inscope {namespace code {one two}}] namespace code $sval} {namespace inscope ::test_ns_inscope {one two}}test namespace-old-10.6 {with many args, each "scope" adds new args} { set sval [namespace eval test_ns_inscope {namespace code {one two}}] namespace code "$sval three"} {namespace inscope ::test_ns_inscope {one two} three}test namespace-old-10.7 {scoped commands work with eval} { set cref [namespace eval test_ns_inscope {namespace code show}] list [eval $cref "a" "b c" "d e f"]} {{show: a b c d e f}}test namespace-old-10.8 {scoped commands execute in namespace context} { set cref [namespace eval test_ns_inscope { namespace code {set x "some new value"} }] list [set test_ns_inscope::x] [eval $cref] [set test_ns_inscope::x]} {x-value {some new value} {some new value}}foreach cmd [info commands test_ns_*] { rename $cmd ""}catch {rename cmd {}}catch {rename cmd1 {}}catch {rename cmd2 {}}catch {rename ncmd {}}catch {rename ncmd1 {}}catch {rename ncmd2 {}}catch {unset cref}catch {unset trigger}catch {unset trigger2}catch {unset sval}catch {unset msg}catch {unset x}catch {unset test_ns_var_global}catch {unset cmd}eval namespace delete [namespace children :: test_ns_*]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -