namespace-old.test
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:32k
源码类别:

通讯编程

开发平台:

Visual C++

  1. # Functionality covered: this file contains slightly modified versions of
  2. # the original tests written by Mike McLennan of Lucent Technologies for
  3. # the procedures in tclNamesp.c that implement Tcl's basic support for
  4. # namespaces. Other namespace-related tests appear in namespace.test
  5. # and variable.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) 1997 Lucent Technologies
  12. # Copyright (c) 1998-1999 by Scriptics Corporation.
  13. #
  14. # See the file "license.terms" for information on usage and redistribution
  15. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  16. #
  17. # RCS: @(#) $Id: namespace-old.test,v 1.6 2001/04/07 02:11:19 msofer Exp $
  18. if {[lsearch [namespace children] ::tcltest] == -1} {
  19.     package require tcltest
  20.     namespace import -force ::tcltest::*
  21. }
  22. # Clear out any namespaces called test_ns_*
  23. catch {eval namespace delete [namespace children :: test_ns_*]}
  24. test namespace-old-1.1 {usage for "namespace" command} {
  25.     list [catch {namespace} msg] $msg
  26. } {1 {wrong # args: should be "namespace subcommand ?arg ...?"}}
  27. test namespace-old-1.2 {global namespace's name is "::" or {}} {
  28.     list [namespace current] [namespace eval {} {namespace current}]
  29. } {:: ::}
  30. test namespace-old-1.3 {usage for "namespace eval"} {
  31.     list [catch {namespace eval} msg] $msg
  32. } {1 {wrong # args: should be "namespace eval name arg ?arg...?"}}
  33. test namespace-old-1.4 {create new namespaces} {
  34.     list [lsort [namespace children :: test_ns_simple*]] 
  35.  [namespace eval test_ns_simple {}] 
  36.  [namespace eval test_ns_simple2 {}] 
  37.          [lsort [namespace children :: test_ns_simple*]]
  38. } {{} {} {} {::test_ns_simple ::test_ns_simple2}}
  39. test namespace-old-1.5 {access a new namespace} {
  40.     namespace eval test_ns_simple { namespace current }
  41. } {::test_ns_simple}
  42. test namespace-old-1.6 {usage for "namespace eval"} {
  43.     list [catch {namespace eval} msg] $msg
  44. } {1 {wrong # args: should be "namespace eval name arg ?arg...?"}}
  45. test namespace-old-1.7 {usage for "namespace eval"} {
  46.     list [catch {namespace eval test_ns_xyzzy} msg] $msg
  47. } {1 {wrong # args: should be "namespace eval name arg ?arg...?"}}
  48. test namespace-old-1.8 {command "namespace eval" concatenates args} {
  49.     namespace eval test_ns_simple namespace current
  50. } {::test_ns_simple}
  51. test namespace-old-1.9 {add elements to a namespace} {
  52.     namespace eval test_ns_simple {
  53.         variable test_ns_x 0
  54.         proc test {test_ns_x} {
  55.             return "test: $test_ns_x"
  56.         }
  57.     }
  58. } {}
  59. test namespace-old-1.10 {commands in a namespace} {
  60.     namespace eval test_ns_simple { info commands [namespace current]::*}
  61. } {::test_ns_simple::test}
  62. test namespace-old-1.11 {variables in a namespace} {
  63.     namespace eval test_ns_simple { info vars [namespace current]::* }
  64. } {::test_ns_simple::test_ns_x}
  65. test namespace-old-1.12 {global vars are separate from locals vars} {
  66.     list [test_ns_simple::test 123] [set test_ns_simple::test_ns_x]
  67. } {{test: 123} 0}
  68. test namespace-old-1.13 {add to an existing namespace} {
  69.     namespace eval test_ns_simple {
  70.         variable test_ns_y 123
  71.         proc _backdoor {cmd} {
  72.             eval $cmd
  73.         }
  74.     }
  75. } ""
  76. test namespace-old-1.14 {commands in a namespace} {
  77.     lsort [namespace eval test_ns_simple {info commands [namespace current]::*}]
  78. } {::test_ns_simple::_backdoor ::test_ns_simple::test}
  79. test namespace-old-1.15 {variables in a namespace} {
  80.     lsort [namespace eval test_ns_simple {info vars [namespace current]::*}]
  81. } {::test_ns_simple::test_ns_x ::test_ns_simple::test_ns_y}
  82. test namespace-old-1.16 {variables in a namespace} {
  83.     lsort [info vars test_ns_simple::*]
  84. } {::test_ns_simple::test_ns_x ::test_ns_simple::test_ns_y}
  85. test namespace-old-1.17 {commands in a namespace are hidden} {
  86.     list [catch "_backdoor {return yes!}" msg] $msg
  87. } {1 {invalid command name "_backdoor"}}
  88. test namespace-old-1.18 {using namespace qualifiers} {
  89.     list [catch "test_ns_simple::_backdoor {return yes!}" msg] $msg
  90. } {0 yes!}
  91. test namespace-old-1.19 {using absolute namespace qualifiers} {
  92.     list [catch "::test_ns_simple::_backdoor {return yes!}" msg] $msg
  93. } {0 yes!}
  94. test namespace-old-1.20 {variables in a namespace are hidden} {
  95.     list [catch "set test_ns_x" msg] $msg [catch "set test_ns_y" msg] $msg
  96. } {1 {can't read "test_ns_x": no such variable} 1 {can't read "test_ns_y": no such variable}}
  97. test namespace-old-1.21 {using namespace qualifiers} {
  98.     list [catch "set test_ns_simple::test_ns_x" msg] $msg 
  99.          [catch "set test_ns_simple::test_ns_y" msg] $msg
  100. } {0 0 0 123}
  101. test namespace-old-1.22 {using absolute namespace qualifiers} {
  102.     list [catch "set ::test_ns_simple::test_ns_x" msg] $msg 
  103.          [catch "set ::test_ns_simple::test_ns_y" msg] $msg
  104. } {0 0 0 123}
  105. test namespace-old-1.23 {variables can be accessed within a namespace} {
  106.     test_ns_simple::_backdoor {
  107.         variable test_ns_x
  108.         variable test_ns_y
  109.         return "$test_ns_x $test_ns_y"
  110.     }
  111. } {0 123}
  112. test namespace-old-1.24 {setting global variables} {
  113.     test_ns_simple::_backdoor {variable test_ns_x;  set test_ns_x "new val"}
  114.     namespace eval test_ns_simple {set test_ns_x}
  115. } {new val}
  116. test namespace-old-1.25 {qualified variables don't need a global declaration} {
  117.     namespace eval test_ns_another { variable test_ns_x 456 }
  118.     set cmd {set ::test_ns_another::test_ns_x}
  119.     list [catch {test_ns_simple::_backdoor "$cmd some-value"} msg] $msg 
  120.          [eval $cmd]
  121. } {0 some-value some-value}
  122. test namespace-old-1.26 {namespace qualifiers are okay after $'s} {
  123.     namespace eval test_ns_simple { set test_ns_x 12; set test_ns_y 34 }
  124.     set cmd {list $::test_ns_simple::test_ns_x $::test_ns_simple::test_ns_y}
  125.     list [test_ns_simple::_backdoor $cmd] [eval $cmd]
  126. } {{12 34} {12 34}}
  127. test namespace-old-1.27 {can create commands with null names} {
  128.     proc test_ns_simple:: {args} {return $args}
  129. } {}
  130. # -----------------------------------------------------------------------
  131. # TEST: using "info" in namespace contexts
  132. # -----------------------------------------------------------------------
  133. test namespace-old-2.1 {querying:  info commands} {
  134.     lsort [test_ns_simple::_backdoor {info commands [namespace current]::*}]
  135. } {::test_ns_simple:: ::test_ns_simple::_backdoor ::test_ns_simple::test}
  136. test namespace-old-2.2 {querying:  info procs} {
  137.     lsort [test_ns_simple::_backdoor {info procs}]
  138. } {{} _backdoor test}
  139. test namespace-old-2.3 {querying:  info vars} {
  140.     lsort [info vars test_ns_simple::*]
  141. } {::test_ns_simple::test_ns_x ::test_ns_simple::test_ns_y}
  142. test namespace-old-2.4 {querying:  info vars} {
  143.     lsort [test_ns_simple::_backdoor {info vars [namespace current]::*}]
  144. } {::test_ns_simple::test_ns_x ::test_ns_simple::test_ns_y}
  145. test namespace-old-2.5 {querying:  info locals} {
  146.     lsort [test_ns_simple::_backdoor {info locals}]
  147. } {cmd}
  148. test namespace-old-2.6 {querying:  info exists} {
  149.     test_ns_simple::_backdoor {info exists test_ns_x}
  150. } {0}
  151. test namespace-old-2.7 {querying:  info exists} {
  152.     test_ns_simple::_backdoor {info exists cmd}
  153. } {1}
  154. test namespace-old-2.8 {querying:  info args} {
  155.     info args test_ns_simple::_backdoor
  156. } {cmd}
  157. test namespace-old-2.9 {querying:  info body} {
  158.     string trim [info body test_ns_simple::test]
  159. } {return "test: $test_ns_x"}
  160. # -----------------------------------------------------------------------
  161. # TEST: namespace qualifiers, namespace tail
  162. # -----------------------------------------------------------------------
  163. test namespace-old-3.1 {usage for "namespace qualifiers"} {
  164.     list [catch "namespace qualifiers" msg] $msg
  165. } {1 {wrong # args: should be "namespace qualifiers string"}}
  166. test namespace-old-3.2 {querying:  namespace qualifiers} {
  167.     list [namespace qualifiers ""] 
  168.          [namespace qualifiers ::] 
  169.          [namespace qualifiers x] 
  170.          [namespace qualifiers ::x] 
  171.          [namespace qualifiers foo::x] 
  172.          [namespace qualifiers ::foo::bar::xyz]
  173. } {{} {} {} {} foo ::foo::bar}
  174. test namespace-old-3.3 {usage for "namespace tail"} {
  175.     list [catch "namespace tail" msg] $msg
  176. } {1 {wrong # args: should be "namespace tail string"}}
  177. test namespace-old-3.4 {querying:  namespace tail} {
  178.     list [namespace tail ""] 
  179.          [namespace tail ::] 
  180.          [namespace tail x] 
  181.          [namespace tail ::x] 
  182.          [namespace tail foo::x] 
  183.          [namespace tail ::foo::bar::xyz]
  184. } {{} {} x x x xyz}
  185. # -----------------------------------------------------------------------
  186. # TEST: delete commands and namespaces
  187. # -----------------------------------------------------------------------
  188. test namespace-old-4.1 {define test namespaces} {
  189.     namespace eval test_ns_delete {
  190.         namespace eval ns1 {
  191.             variable var1 1
  192.             proc cmd1 {} {return "cmd1"}
  193.         }
  194.         namespace eval ns2 {
  195.             variable var2 2
  196.             proc cmd2 {} {return "cmd2"}
  197.         }
  198.         namespace eval another {}
  199.         lsort [namespace children]
  200.     }
  201. } {::test_ns_delete::another ::test_ns_delete::ns1 ::test_ns_delete::ns2}
  202. test namespace-old-4.2 {it's okay to invoke "namespace delete" with no args} {
  203.     list [catch {namespace delete} msg] $msg
  204. } {0 {}}
  205. test namespace-old-4.3 {command "namespace delete" doesn't support patterns} {
  206.     set cmd {
  207.         namespace eval test_ns_delete {namespace delete ns*}
  208.     }
  209.     list [catch $cmd msg] $msg
  210. } {1 {unknown namespace "ns*" in namespace delete command}}
  211. test namespace-old-4.4 {command "namespace delete" handles multiple args} {
  212.     set cmd {
  213.         namespace eval test_ns_delete {
  214.             eval namespace delete 
  215.                 [namespace children [namespace current] ns?]
  216.         }
  217.     }
  218.     list [catch $cmd msg] $msg [namespace children test_ns_delete]
  219. } {0 {} ::test_ns_delete::another}
  220. # -----------------------------------------------------------------------
  221. # TEST: namespace hierarchy
  222. # -----------------------------------------------------------------------
  223. test namespace-old-5.1 {define nested namespaces} {
  224.     set test_ns_var_global "var in ::"
  225.     proc test_ns_cmd_global {} {return "cmd in ::"}
  226.     namespace eval test_ns_hier1 {
  227.         set test_ns_var_hier1 "particular to hier1"
  228.         proc test_ns_cmd_hier1 {} {return "particular to hier1"}
  229.         set test_ns_level 1
  230.         proc test_ns_show {} {return "[namespace current]: 1"}
  231.         namespace eval test_ns_hier2 {
  232.             set test_ns_var_hier2 "particular to hier2"
  233.             proc test_ns_cmd_hier2 {} {return "particular to hier2"}
  234.             set test_ns_level 2
  235.             proc test_ns_show {} {return "[namespace current]: 2"}
  236.             namespace eval test_ns_hier3a {}
  237.             namespace eval test_ns_hier3b {}
  238.         }
  239.         namespace eval test_ns_hier2a {}
  240.         namespace eval test_ns_hier2b {}
  241.     }
  242. } {}
  243. test namespace-old-5.2 {namespaces can be nested} {
  244.     list [namespace eval test_ns_hier1 {namespace current}] 
  245.          [namespace eval test_ns_hier1 {
  246.               namespace eval test_ns_hier2 {namespace current}
  247.           }]
  248. } {::test_ns_hier1 ::test_ns_hier1::test_ns_hier2}
  249. test namespace-old-5.3 {namespace qualifiers work in namespace command} {
  250.     list [namespace eval ::test_ns_hier1 {namespace current}] 
  251.          [namespace eval test_ns_hier1::test_ns_hier2 {namespace current}] 
  252.          [namespace eval ::test_ns_hier1::test_ns_hier2 {namespace current}]
  253. } {::test_ns_hier1 ::test_ns_hier1::test_ns_hier2 ::test_ns_hier1::test_ns_hier2}
  254. test namespace-old-5.4 {nested namespaces can access global namespace} {
  255.     list [namespace eval test_ns_hier1 {set test_ns_var_global}] 
  256.          [namespace eval test_ns_hier1 {test_ns_cmd_global}] 
  257.          [namespace eval test_ns_hier1::test_ns_hier2 {set test_ns_var_global}] 
  258.          [namespace eval test_ns_hier1::test_ns_hier2 {test_ns_cmd_global}]
  259. } {{var in ::} {cmd in ::} {var in ::} {cmd in ::}}
  260. test namespace-old-5.5 {variables in different namespaces don't conflict} {
  261.     list [set test_ns_hier1::test_ns_level] 
  262.          [set test_ns_hier1::test_ns_hier2::test_ns_level]
  263. } {1 2}
  264. test namespace-old-5.6 {commands in different namespaces don't conflict} {
  265.     list [test_ns_hier1::test_ns_show] 
  266.          [test_ns_hier1::test_ns_hier2::test_ns_show]
  267. } {{::test_ns_hier1: 1} {::test_ns_hier1::test_ns_hier2: 2}}
  268. test namespace-old-5.7 {nested namespaces don't see variables in parent} {
  269.     set cmd {
  270.         namespace eval test_ns_hier1::test_ns_hier2 {set test_ns_var_hier1}
  271.     }
  272.     list [catch $cmd msg] $msg
  273. } {1 {can't read "test_ns_var_hier1": no such variable}}
  274. test namespace-old-5.8 {nested namespaces don't see commands in parent} {
  275.     set cmd {
  276.         namespace eval test_ns_hier1::test_ns_hier2 {test_ns_cmd_hier1}
  277.     }
  278.     list [catch $cmd msg] $msg
  279. } {1 {invalid command name "test_ns_cmd_hier1"}}
  280. test namespace-old-5.9 {usage for "namespace children"} {
  281.     list [catch {namespace children test_ns_hier1 y z} msg] $msg
  282. } {1 {wrong # args: should be "namespace children ?name? ?pattern?"}}
  283. test namespace-old-5.10 {command "namespace children" must get valid namespace} {
  284.     list [catch {namespace children xyzzy} msg] $msg
  285. } {1 {unknown namespace "xyzzy" in namespace children command}}
  286. test namespace-old-5.11 {querying namespace children} {
  287.     lsort [namespace children :: test_ns_hier*]
  288. } {::test_ns_hier1}
  289. test namespace-old-5.12 {querying namespace children} {
  290.     lsort [namespace children test_ns_hier1]
  291. } {::test_ns_hier1::test_ns_hier2 ::test_ns_hier1::test_ns_hier2a ::test_ns_hier1::test_ns_hier2b}
  292. test namespace-old-5.13 {querying namespace children} {
  293.     lsort [namespace eval test_ns_hier1 {namespace children}]
  294. } {::test_ns_hier1::test_ns_hier2 ::test_ns_hier1::test_ns_hier2a ::test_ns_hier1::test_ns_hier2b}
  295. test namespace-old-5.14 {querying namespace children} {
  296.     lsort [namespace children test_ns_hier1::test_ns_hier2]
  297. } {::test_ns_hier1::test_ns_hier2::test_ns_hier3a ::test_ns_hier1::test_ns_hier2::test_ns_hier3b}
  298. test namespace-old-5.15 {querying namespace children} {
  299.     lsort [namespace eval test_ns_hier1::test_ns_hier2 {namespace children}]
  300. } {::test_ns_hier1::test_ns_hier2::test_ns_hier3a ::test_ns_hier1::test_ns_hier2::test_ns_hier3b}
  301. test namespace-old-5.16 {querying namespace children with patterns} {
  302.     lsort [namespace children test_ns_hier1::test_ns_hier2 test_ns_*]
  303. } {::test_ns_hier1::test_ns_hier2::test_ns_hier3a ::test_ns_hier1::test_ns_hier2::test_ns_hier3b}
  304. test namespace-old-5.17 {querying namespace children with patterns} {
  305.     lsort [namespace children test_ns_hier1::test_ns_hier2 *b]
  306. } {::test_ns_hier1::test_ns_hier2::test_ns_hier3b}
  307. test namespace-old-5.18 {usage for "namespace parent"} {
  308.     list [catch {namespace parent x y} msg] $msg
  309. } {1 {wrong # args: should be "namespace parent ?name?"}}
  310. test namespace-old-5.19 {command "namespace parent" must get valid namespace} {
  311.     list [catch {namespace parent xyzzy} msg] $msg
  312. } {1 {unknown namespace "xyzzy" in namespace parent command}}
  313. test namespace-old-5.20 {querying namespace parent} {
  314.     list [namespace eval :: {namespace parent}] 
  315.         [namespace eval test_ns_hier1 {namespace parent}] 
  316.         [namespace eval test_ns_hier1::test_ns_hier2 {namespace parent}] 
  317.         [namespace eval test_ns_hier1::test_ns_hier2::test_ns_hier3a {namespace parent}] 
  318. } {{} :: ::test_ns_hier1 ::test_ns_hier1::test_ns_hier2}
  319. test namespace-old-5.21 {querying namespace parent for explicit namespace} {
  320.     list [namespace parent ::] 
  321.          [namespace parent test_ns_hier1] 
  322.          [namespace parent test_ns_hier1::test_ns_hier2] 
  323.          [namespace parent test_ns_hier1::test_ns_hier2::test_ns_hier3a]
  324. } {{} :: ::test_ns_hier1 ::test_ns_hier1::test_ns_hier2}
  325. # -----------------------------------------------------------------------
  326. # TEST: name resolution and caching
  327. # -----------------------------------------------------------------------
  328. test namespace-old-6.1 {relative ns names only looked up in current ns} {
  329.     namespace eval test_ns_cache1 {}
  330.     namespace eval test_ns_cache2 {}
  331.     namespace eval test_ns_cache2::test_ns_cache3 {}
  332.     set trigger {
  333.         namespace eval test_ns_cache2 {namespace current}
  334.     }
  335.     set trigger2 {
  336.         namespace eval test_ns_cache2::test_ns_cache3 {namespace current}
  337.     }
  338.     list [namespace eval test_ns_cache1 $trigger] 
  339.          [namespace eval test_ns_cache1 $trigger2]
  340. } {::test_ns_cache1::test_ns_cache2 ::test_ns_cache1::test_ns_cache2::test_ns_cache3}
  341. test namespace-old-6.2 {relative ns names only looked up in current ns} {
  342.     namespace eval test_ns_cache1::test_ns_cache2 {}
  343.     list [namespace eval test_ns_cache1 $trigger] 
  344.          [namespace eval test_ns_cache1 $trigger2]
  345. } {::test_ns_cache1::test_ns_cache2 ::test_ns_cache1::test_ns_cache2::test_ns_cache3}
  346. test namespace-old-6.3 {relative ns names only looked up in current ns} {
  347.     namespace eval test_ns_cache1::test_ns_cache2::test_ns_cache3 {}
  348.     list [namespace eval test_ns_cache1 $trigger] 
  349.          [namespace eval test_ns_cache1 $trigger2]
  350. } {::test_ns_cache1::test_ns_cache2 ::test_ns_cache1::test_ns_cache2::test_ns_cache3}
  351. test namespace-old-6.4 {relative ns names only looked up in current ns} {
  352.     namespace delete test_ns_cache1::test_ns_cache2
  353.     list [namespace eval test_ns_cache1 $trigger] 
  354.          [namespace eval test_ns_cache1 $trigger2]
  355. } {::test_ns_cache1::test_ns_cache2 ::test_ns_cache1::test_ns_cache2::test_ns_cache3}
  356. test namespace-old-6.5 {define test commands} {
  357.     proc test_ns_cache_cmd {} {
  358.         return "global version"
  359.     }
  360.     namespace eval test_ns_cache1 {
  361.         proc trigger {} {
  362.             test_ns_cache_cmd
  363.         }
  364.     }
  365.     test_ns_cache1::trigger
  366. } {global version}
  367. test namespace-old-6.6 {one-level check for command shadowing} {
  368.     proc test_ns_cache1::test_ns_cache_cmd {} {
  369.         return "cache1 version"
  370.     }
  371.     test_ns_cache1::trigger
  372. } {cache1 version}
  373. test namespace-old-6.7 {renaming commands changes command epoch} {
  374.     namespace eval test_ns_cache1 {
  375.         rename test_ns_cache_cmd test_ns_new
  376.     }
  377.     test_ns_cache1::trigger
  378. } {global version}
  379. test namespace-old-6.8 {renaming back handles shadowing} {
  380.     namespace eval test_ns_cache1 {
  381.         rename test_ns_new test_ns_cache_cmd
  382.     }
  383.     test_ns_cache1::trigger
  384. } {cache1 version}
  385. test namespace-old-6.9 {deleting commands changes command epoch} {
  386.     namespace eval test_ns_cache1 {
  387.         rename test_ns_cache_cmd ""
  388.     }
  389.     test_ns_cache1::trigger
  390. } {global version}
  391. test namespace-old-6.10 {define test namespaces} {
  392.     namespace eval test_ns_cache2 {
  393.         proc test_ns_cache_cmd {} {
  394.             return "global cache2 version"
  395.         }
  396.     }
  397.     namespace eval test_ns_cache1 {
  398.         proc trigger {} {
  399.             test_ns_cache2::test_ns_cache_cmd
  400.         }
  401.     }
  402.     namespace eval test_ns_cache1::test_ns_cache2 {
  403.         proc trigger {} {
  404.             test_ns_cache_cmd
  405.         }
  406.     }
  407.     list [test_ns_cache1::trigger] [test_ns_cache1::test_ns_cache2::trigger]
  408. } {{global cache2 version} {global version}}
  409. test namespace-old-6.11 {commands affect all parent namespaces} {
  410.     proc test_ns_cache1::test_ns_cache2::test_ns_cache_cmd {} {
  411.         return "cache2 version"
  412.     }
  413.     list [test_ns_cache1::trigger] [test_ns_cache1::test_ns_cache2::trigger]
  414. } {{cache2 version} {cache2 version}}
  415. test namespace-old-6.12 {define test variables} {
  416.     variable test_ns_cache_var "global version"
  417.     set trigger {set test_ns_cache_var}
  418.     namespace eval test_ns_cache1 $trigger
  419. } {global version}
  420. test namespace-old-6.13 {one-level check for variable shadowing} {
  421.     namespace eval test_ns_cache1 {
  422.         variable test_ns_cache_var "cache1 version"
  423.     }
  424.     namespace eval test_ns_cache1 $trigger
  425. } {cache1 version}
  426. test namespace-old-6.14 {deleting variables changes variable epoch} {
  427.     namespace eval test_ns_cache1 {
  428.         unset test_ns_cache_var
  429.     }
  430.     namespace eval test_ns_cache1 $trigger
  431. } {global version}
  432. test namespace-old-6.15 {define test namespaces} {
  433.     namespace eval test_ns_cache2 {
  434.         variable test_ns_cache_var "global cache2 version"
  435.     }
  436.     set trigger2 {set test_ns_cache2::test_ns_cache_var}
  437.     list [namespace eval test_ns_cache1 $trigger2] 
  438.          [namespace eval test_ns_cache1::test_ns_cache2 $trigger]
  439. } {{global cache2 version} {global version}}
  440. test namespace-old-6.16 {public variables affect all parent namespaces} {
  441.     variable test_ns_cache1::test_ns_cache2::test_ns_cache_var "cache2 version"
  442.     list [namespace eval test_ns_cache1 $trigger2] 
  443.          [namespace eval test_ns_cache1::test_ns_cache2 $trigger]
  444. } {{cache2 version} {cache2 version}}
  445. test namespace-old-6.17 {usage for "namespace which"} {
  446.     list [catch "namespace which -baz" msg] $msg
  447. } {1 {wrong # args: should be "namespace which ?-command? ?-variable? name"}}
  448. test namespace-old-6.18 {usage for "namespace which"} {
  449.     list [catch "namespace which -command" msg] $msg
  450. } {1 {wrong # args: should be "namespace which ?-command? ?-variable? name"}}
  451. test namespace-old-6.19 {querying:  namespace which -command} {
  452.     proc test_ns_cache1::test_ns_cache_cmd {} {
  453.         return "cache1 version"
  454.     }
  455.     list [namespace eval :: {namespace which test_ns_cache_cmd}] 
  456.          [namespace eval test_ns_cache1 {namespace which test_ns_cache_cmd}] 
  457.          [namespace eval :: {namespace which -command test_ns_cache_cmd}] 
  458.          [namespace eval test_ns_cache1 {namespace which -command test_ns_cache_cmd}]
  459. } {::test_ns_cache_cmd ::test_ns_cache1::test_ns_cache_cmd ::test_ns_cache_cmd ::test_ns_cache1::test_ns_cache_cmd}
  460. test namespace-old-6.20 {command "namespace which" may not find commands} {
  461.     namespace eval test_ns_cache1 {namespace which -command xyzzy}
  462. } {}
  463. test namespace-old-6.21 {querying:  namespace which -variable} {
  464.     namespace eval test_ns_cache1::test_ns_cache2 {
  465.         namespace which -variable test_ns_cache_var
  466.     }
  467. } {::test_ns_cache1::test_ns_cache2::test_ns_cache_var}
  468. test namespace-old-6.22 {command "namespace which" may not find variables} {
  469.     namespace eval test_ns_cache1 {namespace which -variable xyzzy}
  470. } {}
  471. # -----------------------------------------------------------------------
  472. # TEST: uplevel/upvar across namespace boundaries
  473. # -----------------------------------------------------------------------
  474. test namespace-old-7.1 {define test namespace} {
  475.     namespace eval test_ns_uplevel {
  476.         variable x 0
  477.         variable y 1
  478.         proc show_vars {num} {
  479.             return [uplevel $num {info vars}]
  480.         }
  481.         proc test_uplevel {num} {
  482.             set a 0
  483.             set b 1
  484.             namespace eval ::test_ns_uplevel " return [show_vars $num] "
  485.         }
  486.     }
  487. } {}
  488. test namespace-old-7.2 {uplevel can access namespace call frame} {
  489.     list [expr {[lsearch -exact [test_ns_uplevel::test_uplevel 1] x]>=0}] 
  490.          [expr {[lsearch -exact [test_ns_uplevel::test_uplevel 1] y]>=0}]
  491. } {1 1}
  492. test namespace-old-7.3 {uplevel can go beyond namespace call frame} {
  493.     lsort [test_ns_uplevel::test_uplevel 2]
  494. } {a b num}
  495. test namespace-old-7.4 {uplevel can go up to global context} {
  496.     expr {[test_ns_uplevel::test_uplevel 3] == [info globals]}
  497. } {1}
  498. test namespace-old-7.5 {absolute call frame references work too} {
  499.     list [expr {[lsearch -exact [test_ns_uplevel::test_uplevel #2] x]>=0}] 
  500.          [expr {[lsearch -exact [test_ns_uplevel::test_uplevel #2] y]>=0}]
  501. } {1 1}
  502. test namespace-old-7.6 {absolute call frame references work too} {
  503.     lsort [test_ns_uplevel::test_uplevel #1]
  504. } {a b num}
  505. test namespace-old-7.7 {absolute call frame references work too} {
  506.     expr {[test_ns_uplevel::test_uplevel #0] == [info globals]}
  507. } {1}
  508. test namespace-old-7.8 {namespaces are included in the call stack} {
  509.     namespace eval test_ns_upvar {
  510.         variable scope "test_ns_upvar"
  511.         proc show_val {var num} {
  512.             upvar $num $var x
  513.             return $x
  514.         }
  515.         proc test_upvar {num} {
  516.             set scope "test_ns_upvar::test_upvar"
  517.             namespace eval ::test_ns_upvar " return [show_val scope $num] "
  518.         }
  519.     }
  520. } {}
  521. test namespace-old-7.9 {upvar can access namespace call frame} {
  522.     test_ns_upvar::test_upvar 1
  523. } {test_ns_upvar}
  524. test namespace-old-7.10 {upvar can go beyond namespace call frame} {
  525.     test_ns_upvar::test_upvar 2
  526. } {test_ns_upvar::test_upvar}
  527. test namespace-old-7.11 {absolute call frame references work too} {
  528.     test_ns_upvar::test_upvar #2
  529. } {test_ns_upvar}
  530. test namespace-old-7.12 {absolute call frame references work too} {
  531.     test_ns_upvar::test_upvar #1
  532. } {test_ns_upvar::test_upvar}
  533. # -----------------------------------------------------------------------
  534. # TEST: variable traces across namespace boundaries
  535. # -----------------------------------------------------------------------
  536. test namespace-old-8.1 {traces work across namespace boundaries} {
  537.     namespace eval test_ns_trace {
  538.         namespace eval foo {
  539.             variable x ""
  540.         }
  541.         variable status ""
  542.         proc monitor {name1 name2 op} {
  543.             variable status
  544.             lappend status "$op: $name1"
  545.         }
  546.         trace variable foo::x rwu [namespace code monitor]
  547.     }
  548.     set test_ns_trace::foo::x "yes!"
  549.     set test_ns_trace::foo::x
  550.     unset test_ns_trace::foo::x
  551.     namespace eval test_ns_trace { set status }
  552. } {{w: test_ns_trace::foo::x} {r: test_ns_trace::foo::x} {u: test_ns_trace::foo::x}}
  553. # -----------------------------------------------------------------------
  554. # TEST: imported commands
  555. # -----------------------------------------------------------------------
  556. test namespace-old-9.1 {empty "namespace export" list} {
  557.     list [catch "namespace export" msg] $msg
  558. } {0 {}}
  559. test namespace-old-9.2 {usage for "namespace export" command} {
  560.     list [catch "namespace export test_ns_trace::zzz" msg] $msg
  561. } {1 {invalid export pattern "test_ns_trace::zzz": pattern can't specify a namespace}}
  562. test namespace-old-9.3 {define test namespaces for import} {
  563.     namespace eval test_ns_export {
  564.         namespace export cmd1 cmd2 cmd3
  565.         proc cmd1 {args} {return "cmd1: $args"}
  566.         proc cmd2 {args} {return "cmd2: $args"}
  567.         proc cmd3 {args} {return "cmd3: $args"}
  568.         proc cmd4 {args} {return "cmd4: $args"}
  569.         proc cmd5 {args} {return "cmd5: $args"}
  570.         proc cmd6 {args} {return "cmd6: $args"}
  571.     }
  572.     lsort [info commands test_ns_export::*]
  573. } {::test_ns_export::cmd1 ::test_ns_export::cmd2 ::test_ns_export::cmd3 ::test_ns_export::cmd4 ::test_ns_export::cmd5 ::test_ns_export::cmd6}
  574. test namespace-old-9.4 {check export status} {
  575.     set x ""
  576.     namespace eval test_ns_import {
  577.         namespace export cmd1 cmd2
  578.         namespace import ::test_ns_export::*
  579.     }
  580.     foreach cmd [lsort [info commands test_ns_import::*]] {
  581.         lappend x $cmd
  582.     }
  583.     set x
  584. } {::test_ns_import::cmd1 ::test_ns_import::cmd2 ::test_ns_import::cmd3}
  585. test namespace-old-9.5 {empty import list in "namespace import" command} {
  586.     namespace import
  587. } {}
  588. test namespace-old-9.6 {empty import list for "namespace import" command} {
  589.     namespace import
  590. } {}
  591. test namespace-old-9.7 {empty forget list for "namespace forget" command} {
  592.     namespace forget
  593. } {}
  594. catch {rename cmd1 {}}
  595. catch {rename cmd2 {}}
  596. catch {rename ncmd {}}
  597. catch {rename ncmd1 {}}
  598. catch {rename ncmd2 {}}
  599. test namespace-old-9.8 {only exported commands are imported} {
  600.     namespace import test_ns_import::cmd*
  601.     set x [lsort [info commands cmd*]]
  602. } {cmd1 cmd2}
  603. test namespace-old-9.9 {imported commands work just the same as original} {
  604.     list [cmd1 test 1 2 3] [test_ns_import::cmd1 test 4 5 6]
  605. } {{cmd1: test 1 2 3} {cmd1: test 4 5 6}}
  606. test namespace-old-9.10 {commands can be imported from many namespaces} {
  607.     namespace eval test_ns_import2 {
  608.         namespace export ncmd ncmd1 ncmd2
  609.         proc ncmd  {args} {return "ncmd: $args"}
  610.         proc ncmd1 {args} {return "ncmd1: $args"}
  611.         proc ncmd2 {args} {return "ncmd2: $args"}
  612.         proc ncmd3 {args} {return "ncmd3: $args"}
  613.     }
  614.     namespace import test_ns_import2::*
  615.     lsort [concat [info commands cmd*] [info commands ncmd*]]
  616. } {cmd1 cmd2 ncmd ncmd1 ncmd2}
  617. test namespace-old-9.11 {imported commands can be removed by deleting them} {
  618.     rename cmd1 ""
  619.     lsort [concat [info commands cmd*] [info commands ncmd*]]
  620. } {cmd2 ncmd ncmd1 ncmd2}
  621. test namespace-old-9.12 {command "namespace forget" checks for valid namespaces} {
  622.     list [catch {namespace forget xyzzy::*} msg] $msg
  623. } {1 {unknown namespace in namespace forget pattern "xyzzy::*"}}
  624. test namespace-old-9.13 {command "namespace forget" ignores patterns that don't match} {
  625.     list [catch {namespace forget test_ns_import::xy*zzy} msg] $msg 
  626.          [lsort [info commands cmd?]]
  627. } {0 {} cmd2}
  628. test namespace-old-9.14 {imported commands can be removed} {
  629.     namespace forget test_ns_import::cmd?
  630.     list [lsort [info commands cmd?]] 
  631.          [catch {cmd1 another test} msg] $msg
  632. } {{} 1 {invalid command name "cmd1"}}
  633. test namespace-old-9.15 {existing commands can't be overwritten} {
  634.     proc cmd1 {x y} {
  635.         return [expr $x+$y]
  636.     }
  637.     list [catch {namespace import test_ns_import::cmd?} msg] $msg 
  638.          [cmd1 3 5]
  639. } {1 {can't import command "cmd1": already exists} 8}
  640. test namespace-old-9.16 {use "-force" option to override existing commands} {
  641.     list [cmd1 3 5] 
  642.          [namespace import -force test_ns_import::cmd?] 
  643.          [cmd1 3 5]
  644. } {8 {} {cmd1: 3 5}}
  645. test namespace-old-9.17 {commands can be imported into many namespaces} {
  646.     namespace eval test_ns_import_use {
  647.         namespace import ::test_ns_import::* ::test_ns_import2::ncmd?
  648.         lsort [concat [info commands ::test_ns_import_use::cmd*] 
  649.                       [info commands ::test_ns_import_use::ncmd*]]
  650.     }
  651. } {::test_ns_import_use::cmd1 ::test_ns_import_use::cmd2 ::test_ns_import_use::ncmd1 ::test_ns_import_use::ncmd2}
  652. test namespace-old-9.18 {when command is deleted, imported commands go away} {
  653.     namespace eval test_ns_import { rename cmd1 "" }
  654.     list [info commands cmd1] 
  655.          [namespace eval test_ns_import_use {info commands cmd1}]
  656. } {{} {}}
  657. test namespace-old-9.19 {when namesp is deleted, all imported commands go away} {
  658.     namespace delete test_ns_import test_ns_import2
  659.     list [info commands cmd*] 
  660.          [info commands ncmd*] 
  661.          [namespace eval test_ns_import_use {info commands cmd*}] 
  662.          [namespace eval test_ns_import_use {info commands ncmd*}] 
  663. } {{} {} {} {}}
  664. # -----------------------------------------------------------------------
  665. # TEST: scoped values
  666. # -----------------------------------------------------------------------
  667. test namespace-old-10.1 {define namespace for scope test} {
  668.     namespace eval test_ns_inscope {
  669.         variable x "x-value"
  670.         proc show {args} {
  671.             return "show: $args"
  672.         }
  673.         proc do {args} {
  674.             return [eval $args]
  675.         }
  676.         list [set x] [show test]
  677.     }
  678. } {x-value {show: test}}
  679. test namespace-old-10.2 {command "namespace code" requires one argument} {
  680.     list [catch {namespace code} msg] $msg
  681. } {1 {wrong # args: should be "namespace code arg"}}
  682. test namespace-old-10.3 {command "namespace code" requires one argument} {
  683.     list [catch {namespace code first "second arg" third} msg] $msg
  684. } {1 {wrong # args: should be "namespace code arg"}}
  685. test namespace-old-10.4 {command "namespace code" gets current namesp context} {
  686.     namespace eval test_ns_inscope {
  687.         namespace code {"1 2 3" "4 5" 6}
  688.     }
  689. } {::namespace inscope ::test_ns_inscope {"1 2 3" "4 5" 6}}
  690. test namespace-old-10.5 {with one arg, first "scope" sticks} {
  691.     set sval [namespace eval test_ns_inscope {namespace code {one two}}]
  692.     namespace code $sval
  693. } {::namespace inscope ::test_ns_inscope {one two}}
  694. test namespace-old-10.6 {with many args, each "scope" adds new args} {
  695.     set sval [namespace eval test_ns_inscope {namespace code {one two}}]
  696.     namespace code "$sval three"
  697. } {::namespace inscope ::test_ns_inscope {one two} three}
  698. test namespace-old-10.7 {scoped commands work with eval} {
  699.     set cref [namespace eval test_ns_inscope {namespace code show}]
  700.     list [eval $cref "a" "b c" "d e f"]
  701. } {{show: a b c d e f}}
  702. test namespace-old-10.8 {scoped commands execute in namespace context} {
  703.     set cref [namespace eval test_ns_inscope {
  704.         namespace code {set x "some new value"}
  705.     }]
  706.     list [set test_ns_inscope::x] [eval $cref] [set test_ns_inscope::x]
  707. } {x-value {some new value} {some new value}}
  708. foreach cmd [info commands test_ns_*] {
  709.     rename $cmd ""
  710. }
  711. catch {rename cmd {}}
  712. catch {rename cmd1 {}}
  713. catch {rename cmd2 {}}
  714. catch {rename ncmd {}}
  715. catch {rename ncmd1 {}}
  716. catch {rename ncmd2 {}}
  717. catch {unset cref}
  718. catch {unset trigger}
  719. catch {unset trigger2}
  720. catch {unset sval}
  721. catch {unset msg}
  722. catch {unset x}
  723. catch {unset test_ns_var_global}
  724. catch {unset cmd}
  725. eval namespace delete [namespace children :: test_ns_*]
  726. # cleanup
  727. ::tcltest::cleanupTests
  728. return