1 # Commands covered: info
3 # This file contains a collection of tests for one or more of the Tcl
4 # built-in commands. Sourcing this file into Tcl runs the tests and
5 # generates output for errors. No output means no errors were found.
7 # Copyright (c) 1991-1994 The Regents of the University of California.
8 # Copyright (c) 1994-1997 Sun Microsystems, Inc.
9 # Copyright (c) 1998-1999 by Scriptics Corporation.
11 # See the file "license.terms" for information on usage and redistribution
12 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
16 if {[lsearch [namespace children] ::tcltest] == -1} {
17 package require tcltest
18 namespace import -force ::tcltest::*
21 # Set up namespaces needed to test operation of "info args", "info body",
22 # "info default", and "info procs" with imported procedures.
24 catch {namespace delete test_ns_info1 test_ns_info2}
26 namespace eval test_ns_info1 {
28 proc p {x} {return "x=$x"}
29 proc q {{y 27} {z {}}} {return "y=$y"}
33 test info-1.1 {info args option} {
34 proc t1 {a bbb c} {return foo}
37 test info-1.2 {info args option} {
38 proc t1 {{a default1} {bbb default2} {c default3} args} {return foo}
41 test info-1.3 {info args option} {
42 proc t1 "" {return foo}
45 test info-1.4 {info args option} {
47 list [catch {info args t1} msg] $msg
48 } {1 {"t1" isn't a procedure}}
49 test info-1.5 {info args option} {
50 list [catch {info args set} msg] $msg
51 } {1 {"set" isn't a procedure}}
52 test info-1.6 {info args option} {
53 proc t1 {a b} {set c 123; set d $c}
57 test info-1.7 {info args option} {
58 catch {namespace delete test_ns_info2}
59 namespace eval test_ns_info2 {
60 namespace import ::test_ns_info1::*
61 list [info args p] [info args q]
65 test info-2.1 {info body option} {
66 proc t1 {} {body of t1}
69 test info-2.2 {info body option} {
70 list [catch {info body set} msg] $msg
71 } {1 {"set" isn't a procedure}}
72 test info-2.3 {info body option} {
73 list [catch {info args set 1} msg] $msg
74 } {1 {wrong # args: should be "info args procname"}}
75 test info-2.4 {info body option} {
76 catch {namespace delete test_ns_info2}
77 namespace eval test_ns_info2 {
78 namespace import ::test_ns_info1::*
79 list [info body p] [info body q]
81 } {{return "x=$x"} {return "y=$y"}}
82 # Prior to 8.3.0 this would cause a crash because [info body]
83 # would return the bytecompiled version of foo, which the catch
84 # would then try and eval out of the foo context, accessing
85 # compiled local indices
86 test info-2.5 {info body option, returning bytecompiled bodies} {
91 return "variable $v existence: [info exists var]"
95 list [catch [info body foo] msg] $msg
96 } {1 {can't read "args": no such variable}}
97 # Fix for problem tested for in info-2.5 caused problems when
98 # procedure body had no string rep (i.e. was not yet bytecode)
99 # causing an empty string to be returned [Bug #545644]
100 test info-2.6 {info body option, returning list bodies} {
101 proc foo args [list subst bar]
102 list [string bytelength [info body foo]] \
103 [foo; string bytelength [info body foo]]
106 # "info cmdcount" is no longer accurate for compiled commands!
107 # The expected result for info-3.1 used to be "3" and is now "1"
108 # since the "set"s have been compiled away. info-3.2 was corrected
109 # in 8.3 because the eval'ed body won't be compiled.
110 proc testinfocmdcount {} {
111 set x [info cmdcount]
116 test info-3.1 {info cmdcount compiled} {
119 test info-3.2 {info cmdcount evaled} {
120 set x [info cmdcount]
125 test info-3.3 {info cmdcount evaled} [info body testinfocmdcount] 3
126 test info-3.4 {info cmdcount option} {
127 list [catch {info cmdcount 1} msg] $msg
128 } {1 {wrong # args: should be "info cmdcount"}}
130 test info-4.1 {info commands option} {
133 set x " [info commands] "
134 list [string match {* t1 *} $x] [string match {* t2 *} $x] \
135 [string match {* set *} $x] [string match {* list *} $x]
137 test info-4.2 {info commands option} {
141 string match {* t1 *} $x
143 test info-4.3 {info commands option} {
148 test info-4.4 {info commands option} {
151 lsort [info commands _t*]
153 catch {rename _t1_ {}}
154 catch {rename _t2_ {}}
155 test info-4.5 {info commands option} {
156 list [catch {info commands a b} msg] $msg
157 } {1 {wrong # args: should be "info commands ?pattern?"}}
159 test info-5.1 {info complete option} {
160 list [catch {info complete} msg] $msg
161 } {1 {wrong # args: should be "info complete command"}}
162 test info-5.2 {info complete option} {
165 test info-5.2 {info complete option} {
166 info complete "\{abcd "
168 test info-5.3 {info complete option} {
169 info complete {# Comment should be complete command}
171 test info-5.4 {info complete option} {
172 info complete {[a [b] }
174 test info-5.5 {info complete option} {
175 info complete {[a [b]}
178 test info-6.1 {info default option} {
179 proc t1 {a b {c d} {e "long default value"}} {}
180 info default t1 a value
182 test info-6.2 {info default option} {
183 proc t1 {a b {c d} {e "long default value"}} {}
188 test info-6.3 {info default option} {
189 proc t1 {a b {c d} {e "long default value"}} {}
190 info default t1 c value
192 test info-6.4 {info default option} {
193 proc t1 {a b {c d} {e "long default value"}} {}
195 info default t1 c value
198 test info-6.5 {info default option} {
199 proc t1 {a b {c d} {e "long default value"}} {}
201 set x [info default t1 e value]
203 } {1 {long default value}}
204 test info-6.6 {info default option} {
205 list [catch {info default a b} msg] $msg
206 } {1 {wrong # args: should be "info default procname arg varname"}}
207 test info-6.7 {info default option} {
208 list [catch {info default _nonexistent_ a b} msg] $msg
209 } {1 {"_nonexistent_" isn't a procedure}}
210 test info-6.8 {info default option} {
212 list [catch {info default t1 x value} msg] $msg
213 } {1 {procedure "t1" doesn't have an argument "x"}}
214 test info-6.9 {info default option} {
218 list [catch {info default t1 a a} msg] $msg
219 } {1 {couldn't store default value in variable "a"}}
220 test info-6.10 {info default option} {
223 proc t1 {{a 18} b} {}
224 list [catch {info default t1 a a} msg] $msg
225 } {1 {couldn't store default value in variable "a"}}
226 test info-6.11 {info default option} {
227 catch {namespace delete test_ns_info2}
228 namespace eval test_ns_info2 {
229 namespace import ::test_ns_info1::*
230 list [info default p x foo] $foo [info default q y bar] $bar
235 test info-7.1 {info exists option} {
239 catch {unset _nonexistent_}
240 test info-7.2 {info exists option} {
241 info exists _nonexistent_
243 test info-7.3 {info exists option} {
244 proc t1 {x} {return [info exists x]}
247 test info-7.4 {info exists option} {
250 return [info exists _nonexistent_]
254 test info-7.5 {info exists option} {
257 return [info exists y]
261 test info-7.6 {info exists option} {
262 proc t1 {x} {return [info exists value]}
265 test info-7.7 {info exists option} {
268 list [info exists x] [info exists x(1)] [info exists x(2)]
271 test info-7.8 {info exists option} {
272 list [catch {info exists} msg] $msg
273 } {1 {wrong # args: should be "info exists varName"}}
274 test info-7.9 {info exists option} {
275 list [catch {info exists 1 2} msg] $msg
276 } {1 {wrong # args: should be "info exists varName"}}
278 test info-8.1 {info globals option} {
282 set a " [info globals] "
283 list [string match {* x *} $a] [string match {* y *} $a] \
284 [string match {* value *} $a] [string match {* _foobar_ *} $a]
286 test info-8.2 {info globals option} {
291 test info-8.3 {info globals option} {
292 list [catch {info globals 1 2} msg] $msg
293 } {1 {wrong # args: should be "info globals ?pattern?"}}
295 test info-9.1 {info level option} {
298 test info-9.2 {info level option} {
305 } {1 {t1 146 testString}}
306 test info-9.3 {info level option} {
311 list [info level] [info level 1] [info level 2] [info level -1] \
314 t1 146 {a {b c} {{{c}}}}
315 } {2 {t1 146 {a {b c} {{{c}}}}} {t2 292 {a {b c} {{{c}}}}} {t1 146 {a {b c} {{{c}}}}} {t2 292 {a {b c} {{{c}}}}}}
316 test info-9.4 {info level option} {
324 test info-9.5 {info level option} {
325 list [catch {info level 1 2} msg] $msg
326 } {1 {wrong # args: should be "info level ?number?"}}
327 test info-9.6 {info level option} {
328 list [catch {info level 123a} msg] $msg
329 } {1 {expected integer but got "123a"}}
330 test info-9.7 {info level option} {
331 list [catch {info level 0} msg] $msg
332 } {1 {bad level "0"}}
333 test info-9.8 {info level option} {
334 proc t1 {} {info level -1}
335 list [catch {t1} msg] $msg
336 } {1 {bad level "-1"}}
337 test info-9.9 {info level option} {
338 proc t1 {x} {info level $x}
339 list [catch {t1 -3} msg] $msg
340 } {1 {bad level "-3"}}
341 test info-9.10 {info level option, namespaces} {
342 set msg [namespace eval t {info level 0}]
345 } {namespace eval t {info level 0}}
347 set savedLibrary $tcl_library
348 test info-10.1 {info library option} {
349 list [catch {info library x} msg] $msg
350 } {1 {wrong # args: should be "info library"}}
351 test info-10.2 {info library option} {
352 set tcl_library 12345
355 test info-10.3 {info library option} {
357 list [catch {info library} msg] $msg
358 } {1 {no library has been specified for Tcl}}
359 set tcl_library $savedLibrary
361 test info-11.1 {info loaded option} {
362 list [catch {info loaded a b} msg] $msg
363 } {1 {wrong # args: should be "info loaded ?interp?"}}
364 test info-11.2 {info loaded option} {
365 list [catch {info loaded {}}] [catch {info loaded gorp} msg] $msg
366 } {0 1 {could not find interpreter "gorp"}}
368 test info-12.1 {info locals option} {
380 test info-12.2 {info locals option} {
389 test info-12.3 {info locals option} {
390 list [catch {info locals 1 2} msg] $msg
391 } {1 {wrong # args: should be "info locals ?pattern?"}}
392 test info-12.4 {info locals option} {
395 test info-12.5 {info locals option} {
396 proc t1 {} {return [info locals]}
399 test info-12.6 {info locals vs unset compiled locals} {
405 lsort [t1 {a b c c d e f}]
407 test info-12.7 {info locals with temporary variables} {
415 test info-13.1 {info nameofexecutable option} {
416 list [catch {info nameofexecutable foo} msg] $msg
417 } {1 {wrong # args: should be "info nameofexecutable"}}
419 test info-14.1 {info patchlevel option} {
420 set a [info patchlevel]
421 regexp {[0-9]+\.[0-9]+([p[0-9]+)?} $a
423 test info-14.2 {info patchlevel option} {
424 list [catch {info patchlevel a} msg] $msg
425 } {1 {wrong # args: should be "info patchlevel"}}
426 test info-14.3 {info patchlevel option} {
427 set t $tcl_patchLevel
429 set result [list [catch {info patchlevel} msg] $msg]
430 set tcl_patchLevel $t
432 } {1 {can't read "tcl_patchLevel": no such variable}}
434 test info-15.1 {info procs option} {
437 set x " [info procs] "
438 list [string match {* t1 *} $x] [string match {* t2 *} $x] \
439 [string match {* _undefined_ *} $x]
441 test info-15.2 {info procs option} {
446 catch {rename _tt1 {}}
447 catch {rename _tt2 {}}
448 test info-15.3 {info procs option} {
449 list [catch {info procs 2 3} msg] $msg
450 } {1 {wrong # args: should be "info procs ?pattern?"}}
451 test info-15.4 {info procs option} {
452 catch {namespace delete test_ns_info2}
453 namespace eval test_ns_info2 {
454 namespace import ::test_ns_info1::*
456 list [info procs] [info procs p*]
459 test info-15.5 {info procs option with a proc in a namespace} {
460 catch {namespace delete test_ns_info2}
461 namespace eval test_ns_info2 {
469 info procs ::test_ns_info2::p1
470 } {::test_ns_info2::p1}
471 test info-15.6 {info procs option with a pattern in a namespace} {
472 catch {namespace delete test_ns_info2}
473 namespace eval test_ns_info2 {
481 lsort [info procs ::test_ns_info2::p*]
482 } [lsort [list ::test_ns_info2::p1 ::test_ns_info2::p2]]
483 test info-15.7 {info procs option with a global shadowing proc} {
484 catch {namespace delete test_ns_info2}
485 proc string_cmd { arg } {
488 namespace eval test_ns_info2 {
489 proc string_cmd { arg } {
493 info procs test_ns_info2::string*
494 } {::test_ns_info2::string_cmd}
495 # This regression test is currently commented out because it requires
496 # that the implementation of "info procs" looks into the global namespace,
497 # which it does not (in contrast to "info commands")
499 test info-15.8 {info procs option with a global shadowing proc} {
500 catch {namespace delete test_ns_info2}
501 proc string_cmd { arg } {
504 proc string_cmd2 { arg } {
507 namespace eval test_ns_info2 {
508 proc string_cmd { arg } {
512 namespace eval test_ns_info2 {
513 lsort [info procs string*]
515 } [lsort [list string_cmd string_cmd2]]
518 test info-16.1 {info script option} {
519 list [catch {info script x x} msg] $msg
520 } {1 {wrong # args: should be "info script ?filename?"}}
521 test info-16.2 {info script option} {
524 set gorpfile [makeFile "info script\n" gorp.info]
525 test info-16.3 {info script option} {
526 list [source $gorpfile] [file tail [info script]]
527 } [list $gorpfile info.test]
528 test info-16.4 {resetting "info script" after errors} {
529 catch {source ~_nobody_/foo}
530 file tail [info script]
532 test info-16.5 {resetting "info script" after errors} {
533 catch {source _nonexistent_}
534 file tail [info script]
536 test info-16.6 {info script option} {
537 set script [info script]
538 list [file tail [info script]] \
539 [info script newname.txt] \
540 [file tail [info script $script]]
541 } [list info.test newname.txt info.test]
542 test info-16.7 {info script option} {
543 set script [info script]
544 info script newname.txt
545 list [source $gorpfile] [file tail [info script]] \
546 [file tail [info script $script]]
547 } [list $gorpfile newname.txt info.test]
549 set gorpfile [makeFile {list [info script] [info script foo.bar]} gorp.info]
550 test info-16.8 {info script option} {
551 list [source $gorpfile] [file tail [info script]]
552 } [list [list $gorpfile foo.bar] info.test]
555 test info-17.1 {info sharedlibextension option} {
556 list [catch {info sharedlibextension foo} msg] $msg
557 } {1 {wrong # args: should be "info sharedlibextension"}}
559 test info-18.1 {info tclversion option} {
560 set x [info tclversion]
561 scan $x "%d.%d%c" a b c
563 test info-18.2 {info tclversion option} {
564 list [catch {info t 2} msg] $msg
565 } {1 {wrong # args: should be "info tclversion"}}
566 test info-18.3 {info tclversion option} {
569 set result [list [catch {info tclversion} msg] $msg]
572 } {1 {can't read "tcl_version": no such variable}}
574 test info-19.1 {info vars option} {
584 test info-19.2 {info vars option} {
590 return [info vars x*]
594 test info-19.3 {info vars option} {
596 } [lsort [info globals]]
597 test info-19.4 {info vars option} {
598 list [catch {info vars a b} msg] $msg
599 } {1 {wrong # args: should be "info vars ?pattern?"}}
600 test info-19.5 {info vars with temporary variables} {
608 # Check whether the extra testing functions are defined...
609 if {([catch {expr T1()} msg] == 1) && ($msg == {unknown math function "T1"})} {
610 set functions {abs acos asin atan atan2 ceil cos cosh double exp floor fmod hypot int log log10 pow rand round sin sinh sqrt srand tan tanh wide}
612 set functions {T1 T2 T3 abs acos asin atan atan2 ceil cos cosh double exp floor fmod hypot int log log10 pow rand round sin sinh sqrt srand tan tanh wide}
614 test info-20.1 {info functions option} {info functions sin} sin
615 test info-20.2 {info functions option} {lsort [info functions]} $functions
616 test info-20.3 {info functions option} {
617 lsort [info functions a*]
618 } {abs acos asin atan atan2}
619 test info-20.4 {info functions option} {
620 lsort [info functions *tan*]
621 } {atan atan2 tan tanh}
622 test info-20.5 {info functions option} {
623 list [catch {info functions raise an error} msg] $msg
624 } {1 {wrong # args: should be "info functions ?pattern?"}}
626 test info-21.1 {miscellaneous error conditions} {
627 list [catch {info} msg] $msg
628 } {1 {wrong # args: should be "info option ?arg arg ...?"}}
629 test info-21.2 {miscellaneous error conditions} {
630 list [catch {info gorp} msg] $msg
631 } {1 {bad option "gorp": must be args, body, cmdcount, commands, complete, default, exists, functions, globals, hostname, level, library, loaded, locals, nameofexecutable, patchlevel, procs, script, sharedlibextension, tclversion, or vars}}
632 test info-21.3 {miscellaneous error conditions} {
633 list [catch {info c} msg] $msg
634 } {1 {ambiguous option "c": must be args, body, cmdcount, commands, complete, default, exists, functions, globals, hostname, level, library, loaded, locals, nameofexecutable, patchlevel, procs, script, sharedlibextension, tclversion, or vars}}
635 test info-21.4 {miscellaneous error conditions} {
636 list [catch {info l} msg] $msg
637 } {1 {ambiguous option "l": must be args, body, cmdcount, commands, complete, default, exists, functions, globals, hostname, level, library, loaded, locals, nameofexecutable, patchlevel, procs, script, sharedlibextension, tclversion, or vars}}
638 test info-21.5 {miscellaneous error conditions} {
639 list [catch {info s} msg] $msg
640 } {1 {ambiguous option "s": must be args, body, cmdcount, commands, complete, default, exists, functions, globals, hostname, level, library, loaded, locals, nameofexecutable, patchlevel, procs, script, sharedlibextension, tclversion, or vars}}
643 catch {namespace delete test_ns_info1 test_ns_info2}
644 ::tcltest::cleanupTests