os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/tests/basic.test
Update contrib.
1 # This file contains tests for the tclBasic.c source file. Tests appear in
2 # the same order as the C code that they test. The set of tests is
3 # currently incomplete since it currently includes only new tests for
4 # code changed for the addition of Tcl namespaces. Other variable-
5 # related tests appear in several other test files including
6 # assocd.test, cmdInfo.test, eval.test, expr.test, interp.test,
9 # Sourcing this file into Tcl runs the tests and generates output for
10 # errors. No output means no errors were found.
12 # Copyright (c) 1997 Sun Microsystems, Inc.
13 # Copyright (c) 1998-1999 by Scriptics Corporation.
15 # See the file "license.terms" for information on usage and redistribution
16 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
18 # RCS: @(#) $Id: basic.test,v 1.25.2.7 2005/03/18 16:33:43 dgp Exp $
21 package require tcltest 2
22 namespace import -force ::tcltest::*
24 testConstraint testcmdtoken [llength [info commands testcmdtoken]]
25 testConstraint testcmdtrace [llength [info commands testcmdtrace]]
26 testConstraint testcreatecommand [llength [info commands testcreatecommand]]
27 testConstraint testevalex [llength [info commands testevalex]]
28 testConstraint exec [llength [info commands exec]]
30 # This variable needs to be changed when the major or minor version number for
34 catch {namespace delete test_ns_basic}
35 catch {interp delete test_interp}
41 test basic-1.1 {Tcl_CreateInterp, creates interp's global namespace} {
42 catch {interp delete test_interp}
43 interp create test_interp
44 interp eval test_interp {
45 namespace eval test_ns_basic {
47 return [namespace current]
51 list [interp eval test_interp {test_ns_basic::p}] \
52 [interp delete test_interp]
53 } {::test_ns_basic {}}
55 test basic-2.1 {TclHideUnsafeCommands} {emptyTest} {
58 test basic-3.1 {Tcl_CallWhenDeleted: see dcall.test} {emptyTest} {
61 test basic-4.1 {Tcl_DontCallWhenDeleted: see dcall.test} {emptyTest} {
64 test basic-5.1 {Tcl_SetAssocData: see assoc.test} {emptyTest} {
67 test basic-6.1 {Tcl_DeleteAssocData: see assoc.test} {emptyTest} {
70 test basic-7.1 {Tcl_GetAssocData: see assoc.test} {emptyTest} {
73 test basic-8.1 {Tcl_InterpDeleted} {emptyTest} {
76 test basic-9.1 {Tcl_DeleteInterp: see interp.test} {emptyTest} {
79 test basic-10.1 {DeleteInterpProc, destroys interp's global namespace} {
80 catch {interp delete test_interp}
81 interp create test_interp
82 interp eval test_interp {
83 namespace eval test_ns_basic {
86 return [namespace current]
89 namespace eval test_ns_2 {
90 namespace import ::test_ns_basic::p
98 list [interp eval test_interp {test_ns_2::q}] \
99 [interp eval test_interp {namespace delete ::}] \
100 [catch {interp eval test_interp {set a 123}} msg] $msg \
101 [interp delete test_interp]
102 } {{::test_ns_basic 27} {} 1 {invalid command name "set"} {}}
104 test basic-11.1 {HiddenCmdsDeleteProc, invalidate cached refs to deleted hidden cmd} {
105 catch {interp delete test_interp}
106 interp create test_interp
107 interp eval test_interp {
112 interp alias {} localP test_interp p
113 list [interp eval test_interp {p}] \
115 [test_interp hide p] \
116 [catch {localP} msg] $msg \
117 [interp delete test_interp] \
118 [catch {localP} msg] $msg
119 } {27 27 {} 1 {invalid command name "p"} {} 1 {invalid command name "localP"}}
121 # NB: More tests about hide/expose are found in interp.test
123 test basic-12.1 {Tcl_HideCommand, names of hidden cmds can't have namespace qualifiers} {
124 catch {interp delete test_interp}
125 interp create test_interp
126 interp eval test_interp {
127 namespace eval test_ns_basic {
129 return [namespace current]
133 list [catch {test_interp hide test_ns_basic::p x} msg] $msg \
134 [catch {test_interp hide x test_ns_basic::p} msg1] $msg1 \
135 [interp delete test_interp]
136 } {1 {can only hide global namespace commands (use rename then hide)} 1 {cannot use namespace qualifiers in hidden command token (rename)} {}}
138 test basic-12.2 {Tcl_HideCommand, a hidden cmd remembers its containing namespace} {
139 catch {namespace delete test_ns_basic}
140 catch {rename cmd ""}
141 proc cmd {} { ;# note that this is global
142 return [namespace current]
144 namespace eval test_ns_basic {
155 list [test_ns_basic::callCmd] \
156 [test_ns_basic::hideCmd] \
157 [catch {cmd} msg] $msg \
158 [test_ns_basic::exposeCmd] \
159 [test_ns_basic::callCmd] \
160 [namespace delete test_ns_basic]
161 } {:: {} 1 {invalid command name "cmd"} {} :: {}}
163 test basic-13.1 {Tcl_ExposeCommand, a command stays in the global namespace and can not go to another namespace} {
164 catch {namespace delete test_ns_basic}
165 catch {rename cmd ""}
166 proc cmd {} { ;# note that this is global
167 return [namespace current]
169 namespace eval test_ns_basic {
173 proc exposeCmdFailing {} {
174 interp expose {} cmd ::test_ns_basic::newCmd
176 proc exposeCmdWorkAround {} {
177 interp expose {} cmd;
178 rename cmd ::test_ns_basic::newCmd;
184 list [test_ns_basic::callCmd] \
185 [test_ns_basic::hideCmd] \
186 [catch {test_ns_basic::exposeCmdFailing} msg] $msg \
187 [test_ns_basic::exposeCmdWorkAround] \
188 [test_ns_basic::newCmd] \
189 [namespace delete test_ns_basic]
190 } {:: {} 1 {can not expose to a namespace (use expose to toplevel, then rename)} {} ::test_ns_basic {}}
191 test basic-13.2 {Tcl_ExposeCommand, invalidate cached refs to cmd now being exposed} {
193 catch {rename cmd ""}
201 [interp hide {} cmd] \
202 [proc cmd {} {return Hello}] \
205 [interp expose {} cmd] \
207 } {42 {} {} Hello {} {} 42}
209 test basic-14.1 {Tcl_CreateCommand, new cmd goes into a namespace specified in its name, if any} {testcreatecommand} {
210 catch {eval namespace delete [namespace children :: test_ns_*]}
211 list [testcreatecommand create] \
212 [test_ns_basic::createdcommand] \
213 [testcreatecommand delete]
214 } {{} {CreatedCommandProc in ::test_ns_basic} {}}
215 test basic-14.2 {Tcl_CreateCommand, namespace code ignore single ":"s in middle or end of names} {testcreatecommand} {
216 catch {eval namespace delete [namespace children :: test_ns_*]}
217 catch {rename value:at: ""}
218 list [testcreatecommand create2] \
220 [testcreatecommand delete2]
221 } {{} {CreatedCommandProc2 in ::} {}}
223 test basic-15.1 {Tcl_CreateObjCommand, new cmd goes into a namespace specified in its name, if any} {
224 catch {eval namespace delete [namespace children :: test_ns_*]}
225 namespace eval test_ns_basic {}
226 proc test_ns_basic::cmd {} { ;# proc requires that ns already exist
227 return [namespace current]
229 list [test_ns_basic::cmd] \
230 [namespace delete test_ns_basic]
231 } {::test_ns_basic {}}
233 test basic-16.1 {TclInvokeStringCommand} {emptyTest} {
236 test basic-17.1 {TclInvokeObjCommand} {emptyTest} {
239 test basic-18.1 {TclRenameCommand, name of existing cmd can have namespace qualifiers} {
240 catch {eval namespace delete [namespace children :: test_ns_*]}
241 catch {rename cmd ""}
242 namespace eval test_ns_basic {
244 return "p in [namespace current]"
247 list [test_ns_basic::p] \
248 [rename test_ns_basic::p test_ns_basic::q] \
250 } {{p in ::test_ns_basic} {} {p in ::test_ns_basic}}
251 test basic-18.2 {TclRenameCommand, existing cmd must be found} {
252 catch {eval namespace delete [namespace children :: test_ns_*]}
253 list [catch {rename test_ns_basic::p test_ns_basic::q} msg] $msg
254 } {1 {can't rename "test_ns_basic::p": command doesn't exist}}
255 test basic-18.3 {TclRenameCommand, delete cmd if new name is empty} {
256 catch {eval namespace delete [namespace children :: test_ns_*]}
257 namespace eval test_ns_basic {
259 return "p in [namespace current]"
262 list [info commands test_ns_basic::*] \
263 [rename test_ns_basic::p ""] \
264 [info commands test_ns_basic::*]
265 } {::test_ns_basic::p {} {}}
266 test basic-18.4 {TclRenameCommand, bad new name} {
267 catch {eval namespace delete [namespace children :: test_ns_*]}
268 namespace eval test_ns_basic {
270 return "p in [namespace current]"
273 rename test_ns_basic::p :::george::martha
275 test basic-18.5 {TclRenameCommand, new name must not already exist} {
276 namespace eval test_ns_basic {
281 list [catch {rename test_ns_basic::q :::george::martha} msg] $msg
282 } {1 {can't rename to ":::george::martha": command already exists}}
283 test basic-18.6 {TclRenameCommand, check for command shadowing by newly renamed cmd} {
284 catch {eval namespace delete [namespace children :: test_ns_*]}
288 return "p in [namespace current]"
291 return "q in [namespace current]"
293 namespace eval test_ns_basic {
298 list [test_ns_basic::callP] \
299 [rename q test_ns_basic::p] \
300 [test_ns_basic::callP]
301 } {{p in ::} {} {q in ::test_ns_basic}}
303 test basic-19.1 {Tcl_SetCommandInfo} {emptyTest} {
306 test basic-20.1 {Tcl_GetCommandInfo, names for commands created inside namespaces} {testcmdtoken} {
307 catch {eval namespace delete [namespace children :: test_ns_*]}
311 set x [namespace eval test_ns_basic::test_ns_basic2 {
312 # the following creates a cmd in the global namespace
313 testcmdtoken create p
315 list [testcmdtoken name $x] \
317 [testcmdtoken name $x]
318 } {{p ::p} {} {q ::q}}
319 test basic-20.2 {Tcl_GetCommandInfo, names for commands created outside namespaces} {testcmdtoken} {
321 set x [testcmdtoken create test_ns_basic::test_ns_basic2::p]
322 list [testcmdtoken name $x] \
323 [rename test_ns_basic::test_ns_basic2::p q] \
324 [testcmdtoken name $x]
325 } {{p ::test_ns_basic::test_ns_basic2::p} {} {q ::q}}
327 test basic-21.1 {Tcl_GetCommandName} {emptyTest} {
330 test basic-22.1 {Tcl_GetCommandFullName} {
331 catch {eval namespace delete [namespace children :: test_ns_*]}
332 namespace eval test_ns_basic1 {
333 namespace export cmd*
337 namespace eval test_ns_basic2 {
339 namespace import ::test_ns_basic1::*
342 namespace eval test_ns_basic3 {
343 namespace import ::test_ns_basic2::*
345 list [namespace which -command foreach] \
346 [namespace which -command q] \
347 [namespace which -command p] \
348 [namespace which -command cmd1] \
349 [namespace which -command ::test_ns_basic2::cmd2]
351 } {::foreach ::test_ns_basic3::q ::test_ns_basic3::p ::test_ns_basic3::cmd1 ::test_ns_basic2::cmd2}
353 test basic-23.1 {Tcl_DeleteCommand} {emptyTest} {
356 test basic-24.1 {Tcl_DeleteCommandFromToken, invalidate all compiled code if cmd has compile proc} {
357 catch {interp delete test_interp}
359 interp create test_interp
360 interp eval test_interp {
365 set x [interp eval test_interp {useSet}]
366 interp eval test_interp {
369 return "set called with $args"
373 [interp eval test_interp {useSet}] \
374 [interp delete test_interp]
375 } {123 {set called with a 123} {}}
376 test basic-24.2 {Tcl_DeleteCommandFromToken, deleting commands changes command epoch} {
377 catch {eval namespace delete [namespace children :: test_ns_*]}
382 namespace eval test_ns_basic {
390 list [test_ns_basic::callP] \
391 [rename test_ns_basic::p ""] \
392 [test_ns_basic::callP]
393 } {{namespace p} {} {global p}}
394 test basic-24.3 {Tcl_DeleteCommandFromToken, delete imported cmds that refer to a deleted cmd} {
395 catch {eval namespace delete [namespace children :: test_ns_*]}
397 namespace eval test_ns_basic {
399 proc p {} {return 42}
401 namespace eval test_ns_basic2 {
402 namespace import ::test_ns_basic::*
407 list [test_ns_basic2::callP] \
408 [info commands test_ns_basic2::*] \
409 [rename test_ns_basic::p ""] \
410 [catch {test_ns_basic2::callP} msg] $msg \
411 [info commands test_ns_basic2::*]
412 } {42 {::test_ns_basic2::callP ::test_ns_basic2::p} {} 1 {invalid command name "p"} ::test_ns_basic2::callP}
414 test basic-25.1 {TclCleanupCommand} {emptyTest} {
417 test basic-26.1 {Tcl_EvalObj: preserve object while evaling it} {
418 # If object isn't preserved, errorInfo would be set to
419 # "foo\n while executing\n\"garbage bytes\"" because the object's
420 # string would have been freed, leaving garbage bytes for the error
423 proc bgerror {args} {set ::x $::errorInfo}
424 set fName [makeFile {} test1]
425 set f [open $fName w]
426 fileevent $f writable "fileevent $f writable {}; error foo"
433 } "foo\n while executing\n\"error foo\""
435 test basic-26.2 {Tcl_EvalObjEx, pure-list branch: preserve "objv"} {
437 # Follow the pure-list branch in a manner that
438 # a - the pure-list internal rep is destroyed by shimmering
439 # b - the command returns an error
440 # As the error code in Tcl_EvalObjv accesses the list elements, this will
441 # cause a segfault if [Bug 1119369] has not been fixed.
444 set SRC [list foo 1] ;# pure-list command
446 # Shimmer pure-list to cmdName, cleanup and error
447 proc $::SRC {} {}; $::SRC
453 test basic-27.1 {Tcl_ExprLong} {emptyTest} {
456 test basic-28.1 {Tcl_ExprDouble} {emptyTest} {
459 test basic-29.1 {Tcl_ExprBoolean} {emptyTest} {
462 test basic-30.1 {Tcl_ExprLongObj} {emptyTest} {
465 test basic-31.1 {Tcl_ExprDoubleObj} {emptyTest} {
468 test basic-32.1 {Tcl_ExprBooleanObj} {emptyTest} {
471 test basic-33.1 {TclInvoke} {emptyTest} {
474 test basic-34.1 {TclGlobalInvoke} {emptyTest} {
477 test basic-35.1 {TclObjInvokeGlobal} {emptyTest} {
480 test basic-36.1 {TclObjInvoke, lookup of "unknown" command} {
481 catch {eval namespace delete [namespace children :: test_ns_*]}
482 catch {interp delete test_interp}
483 interp create test_interp
484 interp eval test_interp {
485 proc unknown {args} {
486 return "global unknown"
488 namespace eval test_ns_basic {
489 proc unknown {args} {
490 return "namespace unknown"
494 list [interp alias test_interp newAlias test_interp doesntExist] \
495 [catch {interp eval test_interp {newAlias}} msg] $msg \
496 [interp delete test_interp]
497 } {newAlias 0 {global unknown} {}}
499 test basic-37.1 {Tcl_ExprString: see expr.test} {emptyTest} {
502 test basic-38.1 {Tcl_ExprObj} {emptyTest} {
505 test basic-39.1 {Tcl_CreateTrace, correct command and argc/argv arguments of trace proc} {testcmdtrace} {
506 testcmdtrace tracetest {set stuff [expr 14 + 16]}
507 } {{expr 14 + 16} {expr 14 + 16} {set stuff [expr 14 + 16]} {set stuff 30}}
508 test basic-39.2 {Tcl_CreateTrace, correct command and argc/argv arguments of trace proc} {testcmdtrace} {
509 testcmdtrace tracetest {set stuff [info tclversion]}
510 } [list {info tclversion} {info tclversion} {set stuff [info tclversion]} "set stuff $tclvers"]
511 test basic-39.3 {Tcl_CreateTrace, correct command and argc/argv arguments of trace proc} {testcmdtrace} {
512 testcmdtrace deletetest {set stuff [info tclversion]}
514 test basic-39.4 {Tcl_CreateTrace, check that tracing doesn't cause memory faults} {testcmdtrace} {
515 # Note that the proc call is the same as the variable name, and that
516 # the call can be direct or indirect by way of another procedure
517 proc tracer {args} {}
518 proc tracedLoop {level} {
521 foreach tracer [expr {$level==1 ? {1 2} : {}}] {tracedLoop $level}
523 testcmdtrace tracetest {tracedLoop 0}
524 } {{tracedLoop 0} {tracedLoop 0} {incr level} {incr level} tracer {tracer} {expr {$level==1 ? {1 2} : {}}} {expr {$level==1 ? {1 2} : {}}} {foreach tracer [expr {$level==1 ? {1 2} : {}}] {tracedLoop $level}} {foreach tracer {1 2} {tracedLoop $level}} {tracedLoop $level} {tracedLoop 1} {incr level} {incr level} tracer {tracer} {expr {$level==1 ? {1 2} : {}}} {expr {$level==1 ? {1 2} : {}}} {foreach tracer [expr {$level==1 ? {1 2} : {}}] {tracedLoop $level}} {foreach tracer {} {tracedLoop $level}} {tracedLoop $level} {tracedLoop 1} {incr level} {incr level} tracer {tracer} {expr {$level==1 ? {1 2} : {}}} {expr {$level==1 ? {1 2} : {}}} {foreach tracer [expr {$level==1 ? {1 2} : {}}] {tracedLoop $level}} {foreach tracer {} {tracedLoop $level}}}
525 catch {rename tracer {}}
526 catch {rename tracedLoop {}}
528 test basic-39.5 {Tcl_CreateObjTrace, status return TCL_ERROR} {testcmdtrace} {
529 proc Error { args } { error "Shouldn't get here" }
531 list [catch {testcmdtrace resulttest {Error $x}} result] [set result]
534 test basic-39.6 {Tcl_CreateObjTrace, status return TCL_RETURN} {testcmdtrace} {
535 proc Return { args } { error "Shouldn't get here" }
537 list [catch {testcmdtrace resulttest {Return $x}} result] [set result]
540 test basic-39.7 {Tcl_CreateObjTrace, status return TCL_BREAK} {testcmdtrace} {
541 proc Break { args } { error "Shouldn't get here" }
543 list [catch {testcmdtrace resulttest {Break $x}} result] [set result]
546 test basic-39.8 {Tcl_CreateObjTrace, status return TCL_CONTINUE} {testcmdtrace} {
547 proc Continue { args } { error "Shouldn't get here" }
549 list [catch {testcmdtrace resulttest {Continue $x}} result] [set result]
552 test basic-39.9 {Tcl_CreateObjTrace, status return unknown} {testcmdtrace} {
553 proc OtherStatus { args } { error "Shouldn't get here" }
555 list [catch {testcmdtrace resulttest {OtherStatus $x}} result] [set result]
558 test basic-39.10 {Tcl_CreateTrace, correct level interpretation} {testcmdtrace} {
559 proc foo {} {uplevel 1 bar}
560 proc bar {} {uplevel 1 grok}
561 proc grok {} {uplevel 1 spock}
562 proc spock {} {uplevel 1 fascinating}
563 proc fascinating {} {}
564 testcmdtrace leveltest {foo}
565 } {foo {foo} {uplevel 1 bar} {uplevel 1 bar} bar {bar} {uplevel 1 grok} {uplevel 1 grok}}
567 test basic-40.1 {Tcl_DeleteTrace} {emptyTest} {
568 # the above tests have tested Tcl_DeleteTrace
571 test basic-41.1 {Tcl_AddErrorInfo} {emptyTest} {
574 test basic-42.1 {Tcl_AddObjErrorInfo} {emptyTest} {
577 test basic-43.1 {Tcl_VarEval} {emptyTest} {
580 test basic-44.1 {Tcl_GlobalEval} {emptyTest} {
583 test basic-45.1 {Tcl_SetRecursionLimit: see interp.test} {emptyTest} {
586 test basic-46.1 {Tcl_AllowExceptions: exception return not allowed} {stdio} {
589 set f [open |[list [interpreter]] w+]
590 fconfigure $f -buffering line
591 puts $f {fconfigure stdout -buffering line}
593 puts $f {puts $errorInfo}
597 while {$newMsg != "DONE"} {
599 append msg "${newMsg}\n"
604 } {1 {invoked "continue" outside of a loop
610 test basic-46.2 {Tcl_AllowExceptions: exception return not allowed} -setup {
611 set fName [makeFile {
618 exec [interpreter] $fName
621 } -returnCodes error -match glob -result {hello
622 invoked "break" outside of a loop
625 (file "*BREAKtest" line 3)}
627 test basic-46.3 {Tcl_AllowExceptions: exception return not allowed} -setup {
628 set fName [makeFile {
629 interp alias {} patch {} info patchlevel
636 exec [interpreter] $fName
639 } -returnCodes error -match glob -result {invoked "break" outside of a loop
642 (file "*BREAKtest" line 4)}
644 test basic-46.4 {Tcl_AllowExceptions: exception return not allowed} -setup {
645 set fName [makeFile {
646 foo [set a 1] [break]
651 exec [interpreter] $fName
654 } -returnCodes error -match glob -result {invoked "break" outside of a loop
656 "foo \[set a 1] \[break]"
657 (file "*BREAKtest" line 2)}
659 test basic-46.5 {Tcl_AllowExceptions: exception return not allowed} -setup {
660 set fName [makeFile {
666 exec [interpreter] $fName
669 } -returnCodes error -match glob -result {command returned bad code: 2
671 "return -code return"
672 (file "*BREAKtest" line 2)}
674 test basic-47.1 {Tcl_EvalEx: check for missing close-bracket} -body {
675 subst {a[set b [format cd]}
676 } -returnCodes error -result {missing close-bracket}
678 test basic-49.1 {Tcl_EvalEx: verify TCL_EVAL_GLOBAL operation} testevalex {
682 testevalex {set x changed} global
683 set ::result [list $::x $x]
687 } {changed namespace}
688 test basic-49.2 {Tcl_EvalEx: verify TCL_EVAL_GLOBAL operation} testevalex {
692 testevalex {set ::context $x} global
699 catch {eval namespace delete [namespace children :: test_ns_*]}
700 catch {namespace delete george}
701 catch {interp delete test_interp}
704 catch {rename cmd ""}
705 catch {rename value:at: ""}
707 ::tcltest::cleanupTests