OSDN Git Service

Updated to tcl 8.4.1
[pf3gnuchains/sourceware.git] / tcl / tests / info.test
1 # Commands covered:  info
2 #
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.
6 #
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.
10 #
11 # See the file "license.terms" for information on usage and redistribution
12 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 #
14 # RCS: @(#) $Id$
15
16 if {[lsearch [namespace children] ::tcltest] == -1} {
17     package require tcltest
18     namespace import -force ::tcltest::*
19 }
20
21 # Set up namespaces needed to test operation of "info args", "info body",
22 # "info default", and "info procs" with imported procedures.
23
24 catch {namespace delete test_ns_info1 test_ns_info2}
25
26 namespace eval test_ns_info1 {
27     namespace export *
28     proc p {x} {return "x=$x"}
29     proc q {{y 27} {z {}}} {return "y=$y"}
30 }
31
32
33 test info-1.1 {info args option} {
34     proc t1 {a bbb c} {return foo}
35     info args t1
36 } {a bbb c}
37 test info-1.2 {info args option} {
38     proc t1 {{a default1} {bbb default2} {c default3} args} {return foo}
39     info a t1
40 } {a bbb c args}
41 test info-1.3 {info args option} {
42     proc t1 "" {return foo}
43     info args t1
44 } {}
45 test info-1.4 {info args option} {
46     catch {rename t1 {}}
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}
54     t1 1 2
55     info args t1
56 } {a b}
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]
62     }
63 } {x {y z}}
64
65 test info-2.1 {info body option} {
66     proc t1 {} {body of t1}
67     info body t1
68 } {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]
80     }
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} {
87     catch {unset args}
88     proc foo {args} {
89         foreach v $args {
90             upvar $v var
91             return "variable $v existence: [info exists var]"
92         }
93     }
94     foo a
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]]
104 } {9 9}
105
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]
112     set y 12345
113     set z [info cm]
114     expr $z-$x
115 }
116 test info-3.1 {info cmdcount compiled} {
117     testinfocmdcount
118 } 1
119 test info-3.2 {info cmdcount evaled} {
120     set x [info cmdcount]
121     set y 12345
122     set z [info cm]
123     expr $z-$x
124 } 3
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"}}
129
130 test info-4.1 {info commands option} {
131     proc t1 {} {}
132     proc t2 {} {}
133     set x " [info commands] "
134     list [string match {* t1 *} $x] [string match {* t2 *} $x] \
135             [string match {* set *} $x] [string match {* list *} $x]
136 } {1 1 1 1}
137 test info-4.2 {info commands option} {
138     proc t1 {} {}
139     rename t1 {}
140     set x [info comm]
141     string match {* t1 *} $x
142 } 0
143 test info-4.3 {info commands option} {
144     proc _t1_ {} {}
145     proc _t2_ {} {}
146     info commands _t1_
147 } _t1_
148 test info-4.4 {info commands option} {
149     proc _t1_ {} {}
150     proc _t2_ {} {}
151     lsort [info commands _t*]
152 } {_t1_ _t2_}
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?"}}
158
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} {
163     info complete abc
164 } 1
165 test info-5.2 {info complete option} {
166     info complete "\{abcd "
167 } 0
168 test info-5.3 {info complete option} {
169     info complete {# Comment should be complete command}
170 } 1
171 test info-5.4 {info complete option} {
172     info complete {[a [b] }
173 } 0
174 test info-5.5 {info complete option} {
175     info complete {[a [b]}
176 } 0
177
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
181 } 0
182 test info-6.2 {info default option} {
183     proc t1 {a b {c d} {e "long default value"}} {}
184     set value 12345
185     info d t1 a value
186     set value
187 } {}
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
191 } 1
192 test info-6.4 {info default option} {
193     proc t1 {a b {c d} {e "long default value"}} {}
194     set value 12345
195     info default t1 c value
196     set value
197 } d
198 test info-6.5 {info default option} {
199     proc t1 {a b {c d} {e "long default value"}} {}
200     set value 12345
201     set x [info default t1 e value]
202     list $x $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} {
211     proc t1 {a b} {}
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} {
215     catch {unset a}
216     set a(0) 88
217     proc t1 {a b} {}
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} {
221     catch {unset a}
222     set a(0) 88
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
231     }
232 } {0 {} 1 27}
233 catch {unset a}
234
235 test info-7.1 {info exists option} {
236     set value foo
237     info exists value
238 } 1
239 catch {unset _nonexistent_}
240 test info-7.2 {info exists option} {
241     info exists _nonexistent_
242 } 0
243 test info-7.3 {info exists option} {
244     proc t1 {x} {return [info exists x]}
245     t1 2
246 } 1
247 test info-7.4 {info exists option} {
248     proc t1 {x} {
249         global _nonexistent_
250         return [info exists _nonexistent_]
251     }
252     t1 2
253 } 0
254 test info-7.5 {info exists option} {
255     proc t1 {x} {
256         set y 47
257         return [info exists y]
258     }
259     t1 2
260 } 1
261 test info-7.6 {info exists option} {
262     proc t1 {x} {return [info exists value]}
263     t1 2
264 } 0
265 test info-7.7 {info exists option} {
266     catch {unset x}
267     set x(2) 44
268     list [info exists x] [info exists x(1)] [info exists x(2)]
269 } {1 0 1}
270 catch {unset x}
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"}}
277
278 test info-8.1 {info globals option} {
279     set x 1
280     set y 2
281     set value 23
282     set a " [info globals] "
283     list [string match {* x *} $a] [string match {* y *} $a] \
284             [string match {* value *} $a] [string match {* _foobar_ *} $a]
285 } {1 1 1 0}
286 test info-8.2 {info globals option} {
287     set _xxx1 1
288     set _xxx2 2
289     lsort [info g _xxx*]
290 } {_xxx1 _xxx2}
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?"}}
294
295 test info-9.1 {info level option} {
296     info level
297 } 0
298 test info-9.2 {info level option} {
299     proc t1 {a b} {
300         set x [info le]
301         set y [info level 1]
302         list $x $y
303     }
304     t1 146 testString
305 } {1 {t1 146 testString}}
306 test info-9.3 {info level option} {
307     proc t1 {a b} {
308         t2 [expr $a*2] $b
309     }
310     proc t2 {x y} {
311         list [info level] [info level 1] [info level 2] [info level -1] \
312                 [info level 0]
313     }
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} {
317     proc t1 {} {
318         set x [info level]
319         set y [info level 1]
320         list $x $y
321     }
322     t1
323 } {1 t1}
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}]
343     namespace delete t
344     set msg
345 } {namespace eval t {info level 0}}
346
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
353     info library
354 } {12345}
355 test info-10.3 {info library option} {
356     unset tcl_library
357     list [catch {info library} msg] $msg
358 } {1 {no library has been specified for Tcl}}
359 set tcl_library $savedLibrary
360
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"}}
367
368 test info-12.1 {info locals option} {
369     set a 22
370     proc t1 {x y} {
371         set b 13
372         set c testing
373         global a
374         global aa
375         set aa 23
376         return [info locals]
377     }
378     lsort [t1 23 24]
379 } {b c x y}
380 test info-12.2 {info locals option} {
381     proc t1 {x y} {
382         set xx1 2
383         set xx2 3
384         set y 4
385         return [info loc x*]
386     }
387     lsort [t1 2 3]
388 } {x xx1 xx2}
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} {
393     info locals
394 } {}
395 test info-12.5 {info locals option} {
396     proc t1 {} {return [info locals]}
397     t1
398 } {}
399 test info-12.6 {info locals vs unset compiled locals} {
400     proc t1 {lst} {
401         foreach $lst $lst {}
402         unset lst
403         return [info locals]
404     }
405     lsort [t1 {a b c c d e f}]
406 } {a b c d e f}
407 test info-12.7 {info locals with temporary variables} {
408     proc t1 {} {
409         foreach a {b c} {}
410         info locals
411     }
412     t1
413 } {a}
414
415 test info-13.1 {info nameofexecutable option} {
416     list [catch {info nameofexecutable foo} msg] $msg
417 } {1 {wrong # args: should be "info nameofexecutable"}}
418
419 test info-14.1 {info patchlevel option} {
420     set a [info patchlevel]
421     regexp {[0-9]+\.[0-9]+([p[0-9]+)?} $a
422 } 1
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
428     unset tcl_patchLevel
429     set result [list [catch {info patchlevel} msg] $msg]
430     set tcl_patchLevel $t
431     set result
432 } {1 {can't read "tcl_patchLevel": no such variable}}
433
434 test info-15.1 {info procs option} {
435     proc t1 {} {}
436     proc t2 {} {}
437     set x " [info procs] "
438     list [string match {* t1 *} $x] [string match {* t2 *} $x] \
439             [string match {* _undefined_ *} $x]
440 } {1 1 0}
441 test info-15.2 {info procs option} {
442     proc _tt1 {} {}
443     proc _tt2 {} {}
444     lsort [info pr _tt*]
445 } {_tt1 _tt2}
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::*
455         proc r {} {}
456         list [info procs] [info procs p*]
457     }
458 } {{p q r} 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 {
462         proc p1 { arg } {
463             puts cmd
464         }
465         proc p2 { arg } {
466             puts cmd
467         }
468     }
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 {
474         proc p1 { arg } {
475             puts cmd
476         }
477         proc p2 { arg } {
478             puts cmd
479         }
480     }
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 } {
486         puts cmd
487     }
488     namespace eval test_ns_info2 {
489         proc string_cmd { arg } {
490             puts cmd
491         }
492     }
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")
498 if {0} {
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 } {
502         puts cmd
503     }
504     proc string_cmd2 { arg } {
505         puts cmd
506     }
507     namespace eval test_ns_info2 {
508         proc string_cmd { arg } {
509             puts cmd
510         }
511     }
512     namespace eval test_ns_info2 {
513         lsort [info procs string*]
514     }
515 } [lsort [list string_cmd string_cmd2]]
516 }
517
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} {
522     file tail [info sc]
523 } "info.test"
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]
531 } "info.test"
532 test info-16.5 {resetting "info script" after errors} {
533     catch {source _nonexistent_}
534     file tail [info script]
535 } "info.test"
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]
548 removeFile gorp.info
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]
553 removeFile gorp.info
554
555 test info-17.1 {info sharedlibextension option} {
556     list [catch {info sharedlibextension foo} msg] $msg
557 } {1 {wrong # args: should be "info sharedlibextension"}}
558
559 test info-18.1 {info tclversion option} {
560     set x [info tclversion]
561     scan $x "%d.%d%c" a b c
562 } 2
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} {
567     set t $tcl_version
568     unset tcl_version
569     set result [list [catch {info tclversion} msg] $msg]
570     set tcl_version $t
571     set result
572 } {1 {can't read "tcl_version": no such variable}}
573
574 test info-19.1 {info vars option} {
575     set a 1
576     set b 2
577     proc t1 {x y} {
578         global a b
579         set c 33
580         return [info vars]
581     }
582     lsort [t1 18 19]
583 } {a b c x y}
584 test info-19.2 {info vars option} {
585     set xxx1 1
586     set xxx2 2
587     proc t1 {xxa y} {
588         global xxx1 xxx2
589         set c 33
590         return [info vars x*]
591     }
592     lsort [t1 18 19]
593 } {xxa xxx1 xxx2}
594 test info-19.3 {info vars option} {
595     lsort [info vars]
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} {
601     proc t1 {} {
602         foreach a {b c} {}
603         info vars
604     }
605     t1
606 } {a}
607
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}
611 } else {
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}
613 }
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?"}}
625
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}}
641
642 # cleanup
643 catch {namespace delete test_ns_info1 test_ns_info2}
644 ::tcltest::cleanupTests
645 return