Debian Codegraph @

tests/basic.test

raw
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,
7 # and trace.test.
8 #
9 # Sourcing this file into Tcl runs the tests and generates output for
10 # errors. No output means no errors were found.
11 #
12 # Copyright (c) 1997 Sun Microsystems, Inc.
13 # Copyright (c) 1998-1999 by Scriptics Corporation.
14 #
15 # See the file "license.terms" for information on usage and redistribution
16 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
17
18 if {"::tcltest" ni [namespace children]} {
19 package require tcltest 2.5
20 namespace import -force ::tcltest::*
21 }
22
23 ::tcltest::loadTestedCommands
24 catch [list package require -exact Tcltest [info patchlevel]]
25
26 testConstraint testevalex [llength [info commands testevalex]]
27 testConstraint testcmdtoken [llength [info commands testcmdtoken]]
28 testConstraint testcreatecommand [llength [info commands testcreatecommand]]
29 testConstraint exec [llength [info commands exec]]
30
31 catch {namespace delete test_ns_basic}
32 catch {interp delete test_interp}
33 catch {rename p ""}
34 catch {rename q ""}
35 catch {rename cmd ""}
36 unset -nocomplain x
37
38 test basic-1.1 {Tcl_CreateInterp, creates interp's global namespace} {
39 catch {interp delete test_interp}
40 interp create test_interp
41 interp eval test_interp {
42 namespace eval test_ns_basic {
43 proc p {} {
44 return [namespace current]
45 }
46 }
47 }
48 list [interp eval test_interp {test_ns_basic::p}] \
49 [interp delete test_interp]
50 } {::test_ns_basic {}}
51
52 test basic-2.1 {TclHideUnsafeCommands} {emptyTest} {
53 } {}
54
55 test basic-3.1 {Tcl_CallWhenDeleted: see dcall.test} {emptyTest} {
56 } {}
57
58 test basic-4.1 {Tcl_DontCallWhenDeleted: see dcall.test} {emptyTest} {
59 } {}
60
61 test basic-5.1 {Tcl_SetAssocData: see assoc.test} {emptyTest} {
62 } {}
63
64 test basic-6.1 {Tcl_DeleteAssocData: see assoc.test} {emptyTest} {
65 } {}
66
67 test basic-7.1 {Tcl_GetAssocData: see assoc.test} {emptyTest} {
68 } {}
69
70 test basic-8.1 {Tcl_InterpDeleted} {emptyTest} {
71 } {}
72
73 test basic-9.1 {Tcl_DeleteInterp: see interp.test} {emptyTest} {
74 } {}
75
76 test basic-10.1 {DeleteInterpProc, destroys interp's global namespace} {
77 catch {interp delete test_interp}
78 interp create test_interp
79 interp eval test_interp {
80 namespace eval test_ns_basic {
81 namespace export p
82 proc p {} {
83 return [namespace current]
84 }
85 }
86 namespace eval test_ns_2 {
87 namespace import ::test_ns_basic::p
88 variable v 27
89 proc q {} {
90 variable v
91 return "[p] $v"
92 }
93 }
94 }
95 list [interp eval test_interp {test_ns_2::q}] \
96 [interp eval test_interp {namespace delete ::}] \
97 [catch {interp eval test_interp {set a 123}} msg] $msg \
98 [interp delete test_interp]
99 } {{::test_ns_basic 27} {} 1 {invalid command name "set"} {}}
100
101 test basic-11.1 {HiddenCmdsDeleteProc, invalidate cached refs to deleted hidden cmd} {
102 catch {interp delete test_interp}
103 interp create test_interp
104 interp eval test_interp {
105 proc p {} {
106 return 27
107 }
108 }
109 interp alias {} localP test_interp p
110 list [interp eval test_interp {p}] \
111 [localP] \
112 [test_interp hide p] \
113 [catch {localP} msg] $msg \
114 [interp delete test_interp] \
115 [catch {localP} msg] $msg
116 } {27 27 {} 1 {invalid command name "p"} {} 1 {invalid command name "localP"}}
117
118 # NB: More tests about hide/expose are found in interp.test
119
120 test basic-12.1 {Tcl_HideCommand, names of hidden cmds can't have namespace qualifiers} {
121 catch {interp delete test_interp}
122 interp create test_interp
123 interp eval test_interp {
124 namespace eval test_ns_basic {
125 proc p {} {
126 return [namespace current]
127 }
128 }
129 }
130 list [catch {test_interp hide test_ns_basic::p x} msg] $msg \
131 [catch {test_interp hide x test_ns_basic::p} msg1] $msg1 \
132 [interp delete test_interp]
133 } {1 {can only hide global namespace commands (use rename then hide)} 1 {cannot use namespace qualifiers in hidden command token (rename)} {}}
134
135 test basic-12.2 {Tcl_HideCommand, a hidden cmd remembers its containing namespace} {
136 catch {namespace delete test_ns_basic}
137 catch {rename cmd ""}
138 proc cmd {} { ;# note that this is global
139 return [namespace current]
140 }
141 namespace eval test_ns_basic {
142 proc hideCmd {} {
143 interp hide {} cmd
144 }
145 proc exposeCmd {} {
146 interp expose {} cmd
147 }
148 proc callCmd {} {
149 cmd
150 }
151 }
152 list [test_ns_basic::callCmd] \
153 [test_ns_basic::hideCmd] \
154 [catch {cmd} msg] $msg \
155 [test_ns_basic::exposeCmd] \
156 [test_ns_basic::callCmd] \
157 [namespace delete test_ns_basic]
158 } {:: {} 1 {invalid command name "cmd"} {} :: {}}
159
160 test basic-13.1 {Tcl_ExposeCommand, a command stays in the global namespace and cannot go to another namespace} {
161 catch {namespace delete test_ns_basic}
162 catch {rename cmd ""}
163 proc cmd {} { ;# note that this is global
164 return [namespace current]
165 }
166 namespace eval test_ns_basic {
167 proc hideCmd {} {
168 interp hide {} cmd
169 }
170 proc exposeCmdFailing {} {
171 interp expose {} cmd ::test_ns_basic::newCmd
172 }
173 proc exposeCmdWorkAround {} {
174 interp expose {} cmd;
175 rename cmd ::test_ns_basic::newCmd;
176 }
177 proc callCmd {} {
178 cmd
179 }
180 }
181 list [test_ns_basic::callCmd] \
182 [test_ns_basic::hideCmd] \
183 [catch {test_ns_basic::exposeCmdFailing} msg] $msg \
184 [test_ns_basic::exposeCmdWorkAround] \
185 [test_ns_basic::newCmd] \
186 [namespace delete test_ns_basic]
187 } {:: {} 1 {cannot expose to a namespace (use expose to toplevel, then rename)} {} ::test_ns_basic {}}
188 test basic-13.2 {Tcl_ExposeCommand, invalidate cached refs to cmd now being exposed} {
189 catch {rename p ""}
190 catch {rename cmd ""}
191 proc p {} {
192 cmd
193 }
194 proc cmd {} {
195 return 42
196 }
197 list [p] \
198 [interp hide {} cmd] \
199 [proc cmd {} {return Hello}] \
200 [cmd] \
201 [rename cmd ""] \
202 [interp expose {} cmd] \
203 [p]
204 } {42 {} {} Hello {} {} 42}
205
206 test basic-14.1 {Tcl_CreateCommand, new cmd goes into a namespace specified in its name, if any} {testcreatecommand} {
207 catch {namespace delete {*}[namespace children :: test_ns_*]}
208 list [testcreatecommand create] \
209 [test_ns_basic::createdcommand] \
210 [testcreatecommand delete]
211 } {{} {CreatedCommandProc in ::test_ns_basic} {}}
212 test basic-14.2 {Tcl_CreateCommand, namespace code ignore single ":"s in middle or end of names} {testcreatecommand} {
213 catch {namespace delete {*}[namespace children :: test_ns_*]}
214 catch {rename value:at: ""}
215 list [testcreatecommand create2] \
216 [value:at:] \
217 [testcreatecommand delete2]
218 } {{} {CreatedCommandProc2 in ::} {}}
219
220 test basic-15.1 {Tcl_CreateObjCommand, new cmd goes into a namespace specified in its name, if any} {
221 catch {namespace delete {*}[namespace children :: test_ns_*]}
222 namespace eval test_ns_basic {}
223 proc test_ns_basic::cmd {} { ;# proc requires that ns already exist
224 return [namespace current]
225 }
226 list [test_ns_basic::cmd] \
227 [namespace delete test_ns_basic]
228 } {::test_ns_basic {}}
229 test basic-15.2 {Tcl_CreateObjCommand, Bug 0e4d88b650} -setup {
230 proc deleter {ns args} {
231 namespace delete $ns
232 }
233 namespace eval n {
234 proc p {} {}
235 }
236 trace add command n::p delete [list [namespace which deleter] [namespace current]::n]
237 } -body {
238 proc n::p {} {}
239 } -cleanup {
240 namespace delete n
241 rename deleter {}
242 }
243 test basic-15.3 {Tcl_CreateObjCommand, Bug 24d7f1a695} -body {
244 namespace eval ns {
245 proc p {} {}; proc q {} {};
246 namespace delete [namespace current];
247 proc p {} {}; proc q {} {}
248 }
249 # APN - current behavior is that namespace continues to exist
250 # as it is recreated by the proc calls after the ns delete.
251 # I am not sure this is consistent with proc ns::foo raising
252 # an error if ns does not exist. But whatever ...
253 namespace exists ns
254 } -result 1
255
256 test basic-16.1 {TclInvokeStringCommand} {emptyTest} {
257 } {}
258
259 test basic-17.1 {TclInvokeObjCommand} {emptyTest} {
260 } {}
261
262 test basic-18.1 {TclRenameCommand, name of existing cmd can have namespace qualifiers} {
263 catch {namespace delete {*}[namespace children :: test_ns_*]}
264 catch {rename cmd ""}
265 namespace eval test_ns_basic {
266 proc p {} {
267 return "p in [namespace current]"
268 }
269 }
270 list [test_ns_basic::p] \
271 [rename test_ns_basic::p test_ns_basic::q] \
272 [test_ns_basic::q]
273 } {{p in ::test_ns_basic} {} {p in ::test_ns_basic}}
274 test basic-18.2 {TclRenameCommand, existing cmd must be found} {
275 catch {namespace delete {*}[namespace children :: test_ns_*]}
276 list [catch {rename test_ns_basic::p test_ns_basic::q} msg] $msg
277 } {1 {can't rename "test_ns_basic::p": command doesn't exist}}
278 test basic-18.3 {TclRenameCommand, delete cmd if new name is empty} {
279 catch {namespace delete {*}[namespace children :: test_ns_*]}
280 namespace eval test_ns_basic {
281 proc p {} {
282 return "p in [namespace current]"
283 }
284 }
285 list [info commands test_ns_basic::*] \
286 [rename test_ns_basic::p ""] \
287 [info commands test_ns_basic::*]
288 } {::test_ns_basic::p {} {}}
289 test basic-18.4 {TclRenameCommand, bad new name} {
290 catch {namespace delete {*}[namespace children :: test_ns_*]}
291 namespace eval test_ns_basic {
292 proc p {} {
293 return "p in [namespace current]"
294 }
295 }
296 rename test_ns_basic::p :::george::martha
297 } {}
298 test basic-18.5 {TclRenameCommand, new name must not already exist} -setup {
299 if {![llength [info commands :::george::martha]]} {
300 catch {namespace delete {*}[namespace children :: test_ns_*]}
301 namespace eval test_ns_basic {
302 proc p {} {
303 return "p in [namespace current]"
304 }
305 }
306 rename test_ns_basic::p :::george::martha
307 }
308 } -body {
309 namespace eval test_ns_basic {
310 proc q {} {
311 return 42
312 }
313 }
314 list [catch {rename test_ns_basic::q :::george::martha} msg] $msg
315 } -result {1 {can't rename to ":::george::martha": command already exists}}
316 test basic-18.6 {TclRenameCommand, check for command shadowing by newly renamed cmd} {
317 catch {namespace delete {*}[namespace children :: test_ns_*]}
318 catch {rename p ""}
319 catch {rename q ""}
320 proc p {} {
321 return "p in [namespace current]"
322 }
323 proc q {} {
324 return "q in [namespace current]"
325 }
326 namespace eval test_ns_basic {
327 proc callP {} {
328 p
329 }
330 }
331 list [test_ns_basic::callP] \
332 [rename q test_ns_basic::p] \
333 [test_ns_basic::callP]
334 } {{p in ::} {} {q in ::test_ns_basic}}
335
336 test basic-19.1 {Tcl_SetCommandInfo} {emptyTest} {
337 } {}
338
339 test basic-20.1 {Tcl_GetCommandInfo, names for commands created inside namespaces} {testcmdtoken} {
340 catch {namespace delete {*}[namespace children :: test_ns_*]}
341 catch {rename p ""}
342 catch {rename q ""}
343 unset -nocomplain x
344 set x [namespace eval test_ns_basic::test_ns_basic2 {
345 # the following creates a cmd in the global namespace
346 testcmdtoken create p
347 }]
348 list [testcmdtoken name $x] \
349 [rename ::p q] \
350 [testcmdtoken name $x]
351 } {{p ::p} {} {q ::q}}
352 test basic-20.2 {Tcl_GetCommandInfo, names for commands created outside namespaces} {testcmdtoken} {
353 catch {rename q ""}
354 set x [testcmdtoken create test_ns_basic::test_ns_basic2::p]
355 list [testcmdtoken name $x] \
356 [rename test_ns_basic::test_ns_basic2::p q] \
357 [testcmdtoken name $x]
358 } {{p ::test_ns_basic::test_ns_basic2::p} {} {q ::q}}
359 test basic-20.3 {Tcl_GetCommandInfo, #-quoting} testcmdtoken {
360 catch {rename \# ""}
361 set x [testcmdtoken create \#]
362 testcmdtoken name $x
363 } {{#} ::#}
364
365 test basic-21.1 {Tcl_GetCommandName} {emptyTest} {
366 } {}
367
368 test basic-22.1 {Tcl_GetCommandFullName} {
369 catch {namespace delete {*}[namespace children :: test_ns_*]}
370 namespace eval test_ns_basic1 {
371 namespace export cmd*
372 proc cmd1 {} {}
373 proc cmd2 {} {}
374 }
375 namespace eval test_ns_basic2 {
376 namespace export *
377 namespace import ::test_ns_basic1::*
378 proc p {} {}
379 }
380 namespace eval test_ns_basic3 {
381 namespace import ::test_ns_basic2::*
382 proc q {} {}
383 list [namespace which -command foreach] \
384 [namespace which -command q] \
385 [namespace which -command p] \
386 [namespace which -command cmd1] \
387 [namespace which -command ::test_ns_basic2::cmd2]
388 }
389 } {::foreach ::test_ns_basic3::q ::test_ns_basic3::p ::test_ns_basic3::cmd1 ::test_ns_basic2::cmd2}
390
391 test basic-23.1 {Tcl_DeleteCommand} {emptyTest} {
392 } {}
393
394 test basic-24.1 {Tcl_DeleteCommandFromToken, invalidate all compiled code if cmd has compile proc} {
395 catch {interp delete test_interp}
396 unset -nocomplain x
397 interp create test_interp
398 interp eval test_interp {
399 proc useSet {} {
400 return [set a 123]
401 }
402 }
403 set x [interp eval test_interp {useSet}]
404 interp eval test_interp {
405 rename set ""
406 proc set {args} {
407 return "set called with $args"
408 }
409 }
410 list $x \
411 [interp eval test_interp {useSet}] \
412 [interp delete test_interp]
413 } {123 {set called with a 123} {}}
414 test basic-24.2 {Tcl_DeleteCommandFromToken, deleting commands changes command epoch} {
415 catch {namespace delete {*}[namespace children :: test_ns_*]}
416 catch {rename p ""}
417 proc p {} {
418 return "global p"
419 }
420 namespace eval test_ns_basic {
421 proc p {} {
422 return "namespace p"
423 }
424 proc callP {} {
425 p
426 }
427 }
428 list [test_ns_basic::callP] \
429 [rename test_ns_basic::p ""] \
430 [test_ns_basic::callP]
431 } {{namespace p} {} {global p}}
432 test basic-24.3 {Tcl_DeleteCommandFromToken, delete imported cmds that refer to a deleted cmd} {
433 catch {namespace delete {*}[namespace children :: test_ns_*]}
434 catch {rename p ""}
435 namespace eval test_ns_basic {
436 namespace export p
437 proc p {} {return 42}
438 }
439 namespace eval test_ns_basic2 {
440 namespace import ::test_ns_basic::*
441 proc callP {} {
442 p
443 }
444 }
445 list [test_ns_basic2::callP] \
446 [info commands test_ns_basic2::*] \
447 [rename test_ns_basic::p ""] \
448 [catch {test_ns_basic2::callP} msg] $msg \
449 [info commands test_ns_basic2::*]
450 } {42 {::test_ns_basic2::callP ::test_ns_basic2::p} {} 1 {invalid command name "p"} ::test_ns_basic2::callP}
451
452 test basic-25.1 {TclCleanupCommand} {emptyTest} {
453 } {}
454
455 test basic-26.1 {Tcl_EvalObj: preserve object while evaling it} -setup {
456 proc myHandler {msg options} {
457 set ::x [dict get $options -errorinfo]
458 }
459 set handler [interp bgerror {}]
460 interp bgerror {} [namespace which myHandler]
461 set fName [makeFile {} test1]
462 } -body {
463 # If object isn't preserved, errorInfo would be set to
464 # "foo\n while executing\n\"garbage bytes\"" because the object's
465 # string would have been freed, leaving garbage bytes for the error
466 # message.
467 set f [open $fName w]
468 chan event $f writable "chan event $f writable {}; error foo"
469 set x {}
470 vwait x
471 close $f
472 set x
473 } -cleanup {
474 removeFile test1
475 interp bgerror {} $handler
476 rename myHandler {}
477 } -result "foo\n while executing\n\"error foo\""
478
479 test basic-26.2 {Tcl_EvalObjEx, pure-list branch: preserve "objv"} -body {
480 #
481 # Follow the pure-list branch in a manner that
482 # a - the pure-list internal rep is destroyed by shimmering
483 # b - the command returns an error
484 # As the error code in Tcl_EvalObjv accesses the list elements, this will
485 # cause a segfault if [Bug 1119369] has not been fixed.
486 # NOTE: a MEM_DEBUG build may be necessary to guarantee the segfault.
487 #
488
489 set SRC [list foo 1] ;# pure-list command
490 proc foo str {
491 # Shimmer pure-list to cmdName, cleanup and error
492 proc $::SRC {} {}; $::SRC
493 error "BAD CALL"
494 }
495 catch {eval $SRC}
496 } -result 1 -cleanup {
497 rename foo {}
498 rename $::SRC {}
499 unset ::SRC
500 }
501
502 test basic-26.3 {Tcl_EvalObjEx, pure-list branch: preserve "objv"} -body {
503 #
504 # Follow the pure-list branch in a manner that
505 # a - the pure-list internal rep is destroyed by shimmering
506 # b - the command accesses its command line
507 # This will cause a segfault if [Bug 1119369] has not been fixed.
508 # NOTE: a MEM_DEBUG build may be necessary to guarantee the segfault.
509 #
510
511 set SRC [list foo 1] ;# pure-list command
512 proc foo str {
513 # Shimmer pure-list to cmdName, cleanup and error
514 proc $::SRC {} {}; $::SRC
515 info level 0
516 }
517 catch {eval $SRC}
518 } -result 0 -cleanup {
519 rename foo {}
520 rename $::SRC {}
521 unset ::SRC
522 }
523
524 test basic-27.1 {Tcl_ExprLong} {emptyTest} {
525 } {}
526
527 test basic-28.1 {Tcl_ExprDouble} {emptyTest} {
528 } {}
529
530 test basic-29.1 {Tcl_ExprBoolean} {emptyTest} {
531 } {}
532
533 test basic-30.1 {Tcl_ExprLongObj} {emptyTest} {
534 } {}
535
536 test basic-31.1 {Tcl_ExprDoubleObj} {emptyTest} {
537 } {}
538
539 test basic-32.1 {Tcl_ExprBooleanObj} {emptyTest} {
540 } {}
541
542 test basic-36.1 {Tcl_EvalObjv, lookup of "unknown" command} {
543 catch {namespace delete {*}[namespace children :: test_ns_*]}
544 catch {interp delete test_interp}
545 interp create test_interp
546 interp eval test_interp {
547 proc unknown {args} {
548 return "global unknown"
549 }
550 namespace eval test_ns_basic {
551 proc unknown {args} {
552 return "namespace unknown"
553 }
554 }
555 }
556 list [interp alias test_interp newAlias test_interp doesntExist] \
557 [catch {interp eval test_interp {newAlias}} msg] $msg \
558 [interp delete test_interp]
559 } {newAlias 0 {global unknown} {}}
560
561 test basic-37.1 {Tcl_ExprString: see expr.test} {emptyTest} {
562 } {}
563
564 test basic-38.1 {Tcl_ExprObj} {emptyTest} {
565 } {}
566
567 # Tests basic-39.* and basic-40.* refactored into trace.test
568
569 test basic-41.1 {Tcl_AddErrorInfo} {emptyTest} {
570 } {}
571
572 test basic-42.1 {Tcl_AddObjErrorInfo} {emptyTest} {
573 } {}
574
575 test basic-43.1 {Tcl_VarEval} {emptyTest} {
576 } {}
577
578 test basic-44.1 {Tcl_GlobalEval} {emptyTest} {
579 } {}
580
581 test basic-45.1 {Tcl_SetRecursionLimit: see interp.test} {emptyTest} {
582 } {}
583
584 test basic-46.1 {Tcl_AllowExceptions: exception return not allowed} {stdio} {
585 catch {close $f}
586 set res [catch {
587 set f [open |[list [interpreter]] w+]
588 chan configure $f -buffering line
589 puts $f {chan configure stdout -buffering line}
590 puts $f continue
591 puts $f {puts $::errorInfo}
592 puts $f {puts DONE}
593 set newMsg {}
594 set msg {}
595 while {$newMsg != "DONE"} {
596 set newMsg [gets $f]
597 append msg "${newMsg}\n"
598 }
599 close $f
600 } error]
601 list $res $msg
602 } {1 {invoked "continue" outside of a loop
603 while executing
604 "continue"
605 DONE
606 }}
607
608 test basic-46.2 {Tcl_AllowExceptions: exception return not allowed} -setup {
609 set fName [makeFile {
610 puts hello
611 break
612 } BREAKtest]
613 } -constraints {
614 exec
615 } -body {
616 exec [interpreter] $fName
617 } -cleanup {
618 removeFile BREAKtest
619 } -returnCodes error -match glob -result {hello
620 invoked "break" outside of a loop
621 while executing
622 "break"
623 (file "*BREAKtest" line 3)}
624
625 test basic-46.3 {Tcl_AllowExceptions: exception return not allowed} -setup {
626 set fName [makeFile {
627 interp alias {} patch {} info patchlevel
628 patch
629 break
630 } BREAKtest]
631 } -constraints {
632 exec
633 } -body {
634 exec [interpreter] $fName
635 } -cleanup {
636 removeFile BREAKtest
637 } -returnCodes error -match glob -result {invoked "break" outside of a loop
638 while executing
639 "break"
640 (file "*BREAKtest" line 4)}
641
642 test basic-46.4 {Tcl_AllowExceptions: exception return not allowed} -setup {
643 set fName [makeFile {
644 foo [set a 1] [break]
645 } BREAKtest]
646 } -constraints {
647 exec
648 } -body {
649 exec [interpreter] $fName
650 } -cleanup {
651 removeFile BREAKtest
652 } -returnCodes error -match glob -result {invoked "break" outside of a loop
653 while executing*
654 "foo \[set a 1] \[break]"
655 (file "*BREAKtest" line 2)}
656
657 test basic-46.5 {Tcl_AllowExceptions: exception return not allowed} -setup {
658 set fName [makeFile {
659 return -code return
660 } BREAKtest]
661 } -constraints {
662 exec
663 } -body {
664 exec [interpreter] $fName
665 } -cleanup {
666 removeFile BREAKtest
667 } -returnCodes error -match glob -result {command returned bad code: 2
668 while executing
669 "return -code return"
670 (file "*BREAKtest" line 2)}
671
672 test basic-47.1 {Tcl_EvalEx: check for missing close-bracket} -constraints {
673 testevalex
674 } -body {
675 testevalex {a[set b [format cd]}
676 } -returnCodes error -result {missing close-bracket}
677
678 # Some lists for expansion tests to work with
679 set l1 [list a {b b} c d]
680 set l2 [list e f {g g} h]
681 proc l3 {} {
682 list i j k {l l}
683 }
684
685 # Do all tests once byte compiled and once with direct string evaluation
686 for {set noComp 0} {$noComp <= 1} {incr noComp} {
687
688 if {$noComp} {
689 interp alias {} run {} testevalex
690 set constraints testevalex
691 } else {
692 interp alias {} run {} if 1
693 set constraints {}
694 }
695
696 test basic-47.2.$noComp {Tcl_EvalEx: error during word expansion} -body {
697 run {{*}\{}
698 } -constraints $constraints -returnCodes error -result {unmatched open brace in list}
699
700 test basic-47.3.$noComp {Tcl_EvalEx, error during substitution} -body {
701 run {{*}[error foo]}
702 } -constraints $constraints -returnCodes error -result foo
703
704 test basic-47.4.$noComp {Tcl_EvalEx: no expansion} $constraints {
705 run {list {*} {*} {*}}
706 } {* * *}
707
708 test basic-47.5.$noComp {Tcl_EvalEx: expansion} $constraints {
709 run {list {*}{} {*} {*}x {*}"y z"}
710 } {* x y z}
711
712 test basic-47.6.$noComp {Tcl_EvalEx: expansion to zero args} $constraints {
713 run {list {*}{}}
714 } {}
715
716 test basic-47.7.$noComp {Tcl_EvalEx: expansion to one arg} $constraints {
717 run {list {*}x}
718 } x
719
720 test basic-47.8.$noComp {Tcl_EvalEx: expansion to many args} $constraints {
721 run {list {*}"y z"}
722 } {y z}
723
724 test basic-47.9.$noComp {Tcl_EvalEx: expansion and subst order} $constraints {
725 set x 0
726 run {list [incr x] {*}[incr x] [incr x] \
727 {*}[list [incr x] [incr x]] [incr x]}
728 } {1 2 3 4 5 6}
729
730 test basic-47.10.$noComp {Tcl_EvalEx: expand and memory management} $constraints {
731 run {concat {*}{} a b c d e f g h i j k l m n o p q r}
732 } {a b c d e f g h i j k l m n o p q r}
733
734 test basic-47.11.$noComp {Tcl_EvalEx: expand and memory management} $constraints {
735 run {concat {*}1 a b c d e f g h i j k l m n o p q r}
736 } {1 a b c d e f g h i j k l m n o p q r}
737
738 test basic-47.12.$noComp {Tcl_EvalEx: expand and memory management} $constraints {
739 run {concat {*}{1 2} a b c d e f g h i j k l m n o p q r}
740 } {1 2 a b c d e f g h i j k l m n o p q r}
741
742 test basic-47.13.$noComp {Tcl_EvalEx: expand and memory management} $constraints {
743 run {concat {*}{} {*}{1 2} a b c d e f g h i j k l m n o p q}
744 } {1 2 a b c d e f g h i j k l m n o p q}
745
746 test basic-47.14.$noComp {Tcl_EvalEx: expand and memory management} $constraints {
747 run {concat {*}{} a b c d e f g h i j k l m n o p q r s}
748 } {a b c d e f g h i j k l m n o p q r s}
749
750 test basic-47.15.$noComp {Tcl_EvalEx: expand and memory management} $constraints {
751 run {concat {*}1 a b c d e f g h i j k l m n o p q r s}
752 } {1 a b c d e f g h i j k l m n o p q r s}
753
754 test basic-47.16.$noComp {Tcl_EvalEx: expand and memory management} $constraints {
755 run {concat {*}{1 2} a b c d e f g h i j k l m n o p q r s}
756 } {1 2 a b c d e f g h i j k l m n o p q r s}
757
758 test basic-47.17.$noComp {Tcl_EvalEx: expand and memory management} $constraints {
759 run {concat {*}{} {*}{1 2} a b c d e f g h i j k l m n o p q r}
760 } {1 2 a b c d e f g h i j k l m n o p q r}
761
762 test basic-48.1.$noComp {expansion: parsing} $constraints {
763 run { # A comment
764
765 # Another comment
766 list 1 2\
767 3 {*}$::l1
768
769 # Comment again
770 }
771 } {1 2 3 a {b b} c d}
772
773 test basic-48.2.$noComp {no expansion} $constraints {
774 run {list $::l1 $::l2 [l3]}
775 } {{a {b b} c d} {e f {g g} h} {i j k {l l}}}
776
777 test basic-48.3.$noComp {expansion} $constraints {
778 run {list {*}$::l1 $::l2 {*}[l3]}
779 } {a {b b} c d {e f {g g} h} i j k {l l}}
780
781 test basic-48.4.$noComp {expansion: really long cmd} $constraints {
782 set cmd [list list]
783 for {set t 0} {$t < 500} {incr t} {
784 lappend cmd {{*}$::l1}
785 }
786 llength [run [join $cmd]]
787 } 2000
788
789 test basic-48.5.$noComp {expansion: error detection} -setup {
790 set l "a {a b}x y"
791 } -constraints $constraints -body {
792 run {list $::l1 {*}$l}
793 } -cleanup {
794 unset l
795 } -returnCodes 1 -result {list element in braces followed by "x" instead of space}
796
797 test basic-48.6.$noComp {expansion: odd usage} $constraints {
798 run {list {*}$::l1$::l2}
799 } {a {b b} c de f {g g} h}
800
801 test basic-48.7.$noComp {expansion: odd usage} -constraints $constraints -body {
802 run {list {*}[l3]$::l1}
803 } -returnCodes 1 -result {list element in braces followed by "a" instead of space}
804
805 test basic-48.8.$noComp {expansion: odd usage} $constraints {
806 run {list {*}hej$::l1}
807 } {heja {b b} c d}
808
809 test basic-48.9.$noComp {expansion: Not all {*} should trigger} $constraints {
810 run {list {*}$::l1 \{*\}$::l2 "{*}$::l1" {{*} i j k}}
811 } {a {b b} c d {{*}e f {g g} h} {{*}a {b b} c d} {{*} i j k}}
812
813 test basic-48.10.$noComp {expansion: expansion of command word} -setup {
814 set cmd [list string range jultomte]
815 } -constraints $constraints -body {
816 run {{*}$cmd 2 6}
817 } -cleanup {
818 unset cmd
819 } -result ltomt
820
821 test basic-48.11.$noComp {expansion: expansion into nothing} -setup {
822 set cmd {}
823 set bar {}
824 } -constraints $constraints -body {
825 run {{*}$cmd {*}$bar}
826 } -cleanup {
827 unset cmd bar
828 } -result {}
829
830 test basic-48.12.$noComp {expansion: odd usage} $constraints {
831 run {list {*}$::l1 {*}"hej hopp" {*}$::l2}
832 } {a {b b} c d hej hopp e f {g g} h}
833
834 test basic-48.13.$noComp {expansion: odd usage} $constraints {
835 run {list {*}$::l1 {*}{hej hopp} {*}$::l2}
836 } {a {b b} c d hej hopp e f {g g} h}
837
838 test basic-48.14.$noComp {expansion: hash command} -setup {
839 catch {rename \# ""}
840 set cmd "#"
841 } -constraints $constraints -body {
842 run { {*}$cmd apa bepa }
843 } -cleanup {
844 unset cmd
845 } -returnCodes 1 -result {invalid command name "#"}
846
847 test basic-48.15.$noComp {expansion: complex words} -setup {
848 set a(x) [list a {b c} d e]
849 set b x
850 set c [list {f\ g h\ i j k} x y]
851 set d {0\ 1 2 3}
852 } -constraints $constraints -body {
853 run { lappend d {*}$a($b) {*}[lindex $c 0] }
854 } -cleanup {
855 unset a b c d
856 } -result {{0 1} 2 3 a {b c} d e {f g} {h i} j k}
857
858 testConstraint memory [llength [info commands memory]]
859 test basic-48.16.$noComp {expansion: testing for leaks} -setup {
860 proc getbytes {} {
861 set lines [split [memory info] "\n"]
862 lindex [lindex $lines 3] 3
863 }
864 # This test is made to stress the allocation, reallocation and
865 # object reference management in Tcl_EvalEx.
866 proc stress {} {
867 set a x
868 # Create free objects that should disappear
869 set l [list 1$a 2$a 3$a 4$a 5$a 6$a 7$a]
870 # A short number of words and a short result (8)
871 set l [run {list {*}$l $a$a}]
872 # A short number of words and a longer result (27)
873 set l [run {list {*}$l $a$a {*}$l $a$a {*}$l $a$a}]
874 # A short number of words and a longer result, with an error
875 # This is to stress the cleanup in the error case
876 if {![catch {run {_moo_ {*}$l $a$a {*}$l $a$a {*}$l}}]} {
877 error "An error was expected in the previous statement"
878 }
879 # Many words
880 set l [run {list {*}$l $a$a {*}$l $a$a \
881 {*}$l $a$a {*}$l $a$a \
882 {*}$l $a$a {*}$l $a$a \
883 {*}$l $a$a {*}$l $a$a \
884 {*}$l $a$a {*}$l $a$a \
885 {*}$l $a$a {*}$l $a$a \
886 {*}$l $a$a {*}$l $a$a \
887 {*}$l $a$a {*}$l $a$a \
888 {*}$l $a$a {*}$l $a$a \
889 {*}$l $a$a}]
890
891 if {[llength $l] != 19*28} {
892 error "Bad Length: [llength $l] should be [expr {19*28}]"
893 }
894 }
895 } -constraints [linsert $constraints 0 memory] -body {
896 set end [getbytes]
897 for {set i 0} {$i < 5} {incr i} {
898 stress
899 set tmp $end
900 set end [getbytes]
901 }
902 set leak [expr {$end - $tmp}]
903 } -cleanup {
904 unset end i tmp
905 rename getbytes {}
906 rename stress {}
907 } -result 0
908
909 test basic-48.17.$noComp {expansion: object safety} -setup {
910 set old_precision $::tcl_precision
911 set ::tcl_precision 4
912 } -constraints $constraints -body {
913 set third [expr {1.0/3.0}]
914 set l [list $third $third]
915 set x [run {list $third {*}$l $third}]
916 set res [list]
917 foreach t $x {
918 lappend res [expr {$t * 3.0}]
919 }
920 set res
921 } -cleanup {
922 set ::tcl_precision $old_precision
923 unset old_precision res t l x third
924 } -result {1.0 1.0 1.0 1.0}
925
926 test basic-48.18.$noComp {expansion: list semantics} -constraints $constraints -body {
927 set badcmd {
928 list a b
929 set apa 10
930 }
931 set apa 0
932 list [llength [run { {*}$badcmd }]] $apa
933 } -cleanup {
934 unset apa badcmd
935 } -result {5 0}
936
937 test basic-48.19.$noComp {expansion: error checking order} -body {
938 set badlist "a {}x y"
939 set a 0
940 set b 0
941 catch {run {list [incr a] {*}$badlist [incr b]}}
942 list $a $b
943 } -constraints $constraints -cleanup {
944 unset badlist a b
945 } -result {1 0}
946
947 test basic-48.20.$noComp {expansion: odd case with word boundaries} $constraints {
948 run {list {*}$::l1 {*}"hej hopp" {*}$::l2}
949 } {a {b b} c d hej hopp e f {g g} h}
950
951 test basic-48.21.$noComp {expansion: odd case with word boundaries} $constraints {
952 run {list {*}$::l1 {*}{hej hopp} {*}$::l2}
953 } {a {b b} c d hej hopp e f {g g} h}
954
955 test basic-48.22.$noComp {expansion: odd case with word boundaries} -body {
956 run {list {*}$::l1 {*}"hej hopp {*}$::l2}
957 } -constraints $constraints -returnCodes error -result {missing "}
958
959 test basic-48.23.$noComp {expansion: handle return codes} -constraints $constraints -body {
960 set res {}
961 for {set t 0} {$t < 10} {incr t} {
962 run { {*}break }
963 }
964 lappend res $t
965
966 for {set t 0} {$t < 10} {incr t} {
967 run { {*}continue }
968 set t 20
969 }
970 lappend res $t
971
972 lappend res [catch { run { {*}{error Hejsan} } } err]
973 lappend res $err
974 } -cleanup {
975 unset res t
976 } -result {0 10 1 Hejsan}
977
978 test basic-48.24.$noComp {expansion: empty not canonical list, regression test, bug [cc1e91552c]} -constraints $constraints -setup {
979 unset -nocomplain a
980 } -body {
981 run {list [list {*}{ }] [list {*}[format %c 32]] [list {*}[set a { }]]}
982 } -result [lrepeat 3 {}] -cleanup {unset -nocomplain a}
983
984 test basic-48.25.$noComp {Bug cc191552c: expansion: empty non-canonical list} -constraints $constraints -setup {
985 unset -nocomplain ::CRLF
986 set ::CRLF "\r\n"
987 } -body {
988 # Force variant that turned up in Bug 2c154a40be as that's externally
989 # noticeable in an important downstream project.
990 run {scan [list {*}$::CRLF]x %c%c%c}
991 } -cleanup {
992 unset -nocomplain ::CRLF
993 } -result {120 {} {}}
994
995
996 } ;# End of noComp loop
997
998 test basic-49.1 {Tcl_EvalEx: verify TCL_EVAL_GLOBAL operation} testevalex {
999 set ::x global
1000 namespace eval ns {
1001 variable x namespace
1002 testevalex {set x changed} global
1003 set ::result [list $::x $x]
1004 }
1005 namespace delete ns
1006 set ::result
1007 } {changed namespace}
1008 test basic-49.2 {Tcl_EvalEx: verify TCL_EVAL_GLOBAL operation} testevalex {
1009 set ::x global
1010 namespace eval ns {
1011 variable x namespace
1012 testevalex {set ::context $x} global
1013 }
1014 namespace delete ns
1015 set ::context
1016 } {global}
1017
1018 test basic-50.1 {[586e71dce4] EvalObjv level #0 exception handling} -setup {
1019 interp create child
1020 interp alias {} foo child return
1021 } -body {
1022 list [catch foo m] $m
1023 } -cleanup {
1024 unset -nocomplain m
1025 interp delete child
1026 } -result {0 {}}
1027
1028 # Clean up after expand tests
1029 unset noComp l1 l2 constraints
1030 rename l3 {}
1031 rename run {}
1032
1033 #cleanup
1034 catch {namespace delete {*}[namespace children :: test_ns_*]}
1035 catch {namespace delete george}
1036 catch {interp delete test_interp}
1037 catch {rename p ""}
1038 catch {rename q ""}
1039 catch {rename cmd ""}
1040 catch {rename value:at: ""}
1041 unset -nocomplain x
1042 cleanupTests
1043 return