Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/tests/namespace-old.test @ 25

Last change on this file since 25 was 25, checked in by landauf, 16 years ago

added tcl to libs

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