os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/tests/main.test
First public contribution.
1 # This file contains a collection of tests for generic/tclMain.c.
5 if {[catch {package require tcltest 2.0.2}]} {
6 puts stderr "Skipping tests in [info script]. tcltest 2.0.2 required."
10 namespace eval ::tcl::test::main {
12 namespace import ::tcltest::test
13 namespace import ::tcltest::testConstraint
14 namespace import ::tcltest::interpreter
15 namespace import ::tcltest::cleanupTests
16 namespace import ::tcltest::makeFile
17 namespace import ::tcltest::removeFile
18 namespace import ::tcltest::temporaryDirectory
19 namespace import ::tcltest::workingDirectory
22 testConstraint exec [llength [info commands exec]]
24 # Is the Tcltest package loaded?
25 # - that is, the special C-coded testing commands in tclTest.c
26 # - tests use testing commands introduced in Tcltest 8.4
27 testConstraint Tcltest [expr {
28 [llength [package provide Tcltest]]
29 && [package vsatisfies [package provide Tcltest] 8.4]}]
31 # Procedure to simulate interactive typing of commands, line by line
32 proc type {chan script} {
33 foreach line [split $script \n] {
40 # Grrr... Behavior depends on this value.
45 cd [temporaryDirectory]
46 # Tests Tcl_Main-1.*: variable initializations
49 Tcl_Main: startup script - normal
53 makeFile {puts [list $argv0 $argv $tcl_interactive]} script
54 catch {set f [open "|[list [interpreter] script]" r]}
60 } -result [list script {} 0]\n
63 Tcl_Main: startup script - can't begin with '-'
67 makeFile {puts [list $argv0 $argv $tcl_interactive]} -script
68 catch {set f [open "|[list [interpreter] -script]" w+]}
70 puts $f {puts [list $argv0 $argv $tcl_interactive]; exit}
76 } -result [list [interpreter] -script 0]\n
79 Tcl_Main: encoding of arguments: done by system encoding
80 Note the shortcoming explained in Tcl Feature Request 491789
84 makeFile {puts [list $argv0 $argv $tcl_interactive]} script
85 catch {set f [open "|[list [interpreter] script \u00c0]" r]}
91 } -result [list script [list [encoding convertfrom [encoding system] \
92 [encoding convertto [encoding system] \u00c0]]] 0]\n
95 Tcl_Main: encoding of arguments: done by system encoding
96 Note the shortcoming explained in Tcl Feature Request 491789
100 makeFile {puts [list $argv0 $argv $tcl_interactive]} script
101 catch {set f [open "|[list [interpreter] script \u20ac]" r]}
107 } -result [list script [list [encoding convertfrom [encoding system] \
108 [encoding convertto [encoding system] \u20ac]]] 0]\n
111 Tcl_Main: encoding of script name: system encoding loss
112 Note the shortcoming explained in Tcl Feature Request 491789
116 makeFile {puts [list $argv0 $argv $tcl_interactive]} \u00c0
117 catch {set f [open "|[list [interpreter] \u00c0]" r]}
123 } -result [list [list [encoding convertfrom [encoding system] \
124 [encoding convertto [encoding system] \u00c0]]] {} 0]\n
127 Tcl_Main: encoding of script name: system encoding loss
128 Note the shortcoming explained in Tcl Feature Request 491789
132 makeFile {puts [list $argv0 $argv $tcl_interactive]} \u20ac
133 catch {set f [open "|[list [interpreter] \u20ac]" r]}
139 } -result [list [list [encoding convertfrom [encoding system] \
140 [encoding convertto [encoding system] \u20ac]]] {} 0]\n
142 # Tests Tcl_Main-2.*: application-initialization procedure
145 Tcl_Main: appInitProc returns error
149 makeFile {puts "In script"} script
151 exec [interpreter] script -appinitprocerror >& result
158 } -result "application-specific initialization failed: \nIn script\n"
161 Tcl_Main: appInitProc returns error
165 exec [interpreter] << {puts "In script"} -appinitprocerror >& result
171 } -result "application-specific initialization failed: \nIn script\n"
174 Tcl_Main: appInitProc deletes interp
178 makeFile {puts "In script"} script
180 exec [interpreter] script -appinitprocdeleteinterp >& result
187 } -result "application-specific initialization failed: \n"
190 Tcl_Main: appInitProc deletes interp
194 exec [interpreter] << {puts "In script"} \
195 -appinitprocdeleteinterp >& result
201 } -result "application-specific initialization failed: \n"
204 Tcl_Main: appInitProc closes stderr
208 exec [interpreter] << {puts "In script"} \
209 -appinitprocclosestderr >& result
215 } -result "In script\n"
217 # Tests Tcl_Main-3.*: startup script evaluation
220 Tcl_Main: startup script does not exist
224 if {[file exists no-such-file]} {
225 error "Can't run test Tcl_Main-3.1\
226 where a file named \"no-such-file\" exists"
229 set code [catch {exec [interpreter] no-such-file >& result} result]
231 list $code $result [read $f]
235 } -match glob -result [list 1 {child process exited abnormally} \
236 {couldn't read file "no-such-file":*}]
239 Tcl_Main: startup script raises error
243 makeFile {error ERROR} script
245 set code [catch {exec [interpreter] script >& result} result]
247 list $code $result [read $f]
252 } -match glob -result [list 1 {child process exited abnormally} \
253 "ERROR\n while executing*"]
256 Tcl_Main: startup script closes stderr
260 makeFile {close stderr; error ERROR} script
262 set code [catch {exec [interpreter] script >& result} result]
264 list $code $result [read $f]
269 } -result [list 1 {child process exited abnormally} {}]
272 Tcl_Main: startup script holds incomplete script
276 makeFile "if 1 \{" script
278 set code [catch {exec [interpreter] script >& result} result]
280 join [list $code $result [read $f]] \n
285 } -match glob -result [join [list 1 {child process exited abnormally}\
286 "missing close-brace\n while executing*"] \n]
289 Tcl_Main: startup script sets main loop
303 testexithandler create 0
307 exec [interpreter] script >& result
314 } -result "event\nExit MainLoop\nIn exit\neven 0\n"
317 Tcl_Main: startup script sets main loop and closes stdin
333 testexithandler create 0
336 exec [interpreter] script >& result
343 } -result "event\nExit MainLoop\nIn exit\neven 0\n"
346 Tcl_Main: startup script deletes interp
356 testexithandler create 0
360 exec [interpreter] script >& result
370 Tcl_Main: startup script deletes interp and sets mainloop
382 testexithandler create 0
386 exec [interpreter] script >& result
393 } -result "Exit MainLoop\neven 0\n"
396 Tcl_Main: startup script can set tcl_interactive without limit
400 makeFile {set tcl_interactive foo} script
402 exec [interpreter] script >& result
411 # Tests Tcl_Main-4.*: rc file evaluation
414 Tcl_Main: rcFile evaluation deletes interp
418 set rc [makeFile {testinterpdelete {}} rc]
420 exec [interpreter] << {puts "In script"} \
421 -appinitprocsetrcfile $rc >& result
428 } -result "application-specific initialization failed: \n"
431 Tcl_Main: rcFile evaluation closes stdin
435 set rc [makeFile {close stdin} rc]
437 exec [interpreter] << {puts "In script"} \
438 -appinitprocsetrcfile $rc >& result
445 } -result "application-specific initialization failed: \n"
448 Tcl_Main: rcFile evaluation closes stdin and sets main loop
455 after 0 testexitmainloop
456 testexithandler create 0
464 exec [interpreter] << {puts "In script"} \
465 -appinitprocsetrcfile $rc >& result
472 } -result "application-specific initialization failed:\
473 \nExit MainLoop\nIn exit\neven 0\n"
476 Tcl_Main: rcFile evaluation sets main loop
482 after 0 testexitmainloop
483 testexithandler create 0
491 exec [interpreter] << {} \
492 -appinitprocsetrcfile $rc >& result
499 } -result "application-specific initialization failed:\
500 \nExit MainLoop\nIn exit\neven 0\n"
503 Tcl_Main: Bug 1481986
509 after 0 {puts "Event callback"}
512 set f [open "|[list [interpreter] -appinitprocsetrcfile $rc]" w+]
514 type $f {puts {Interactive output}
521 } -result "Event callback\nInteractive output\n"
523 # Tests Tcl_Main-5.*: interactive operations
526 Tcl_Main: tcl_interactive must be boolean
530 exec [interpreter] << {set tcl_interactive foo} >& result
536 } -result "can't set \"tcl_interactive\":\
537 variable must have boolean value\n"
540 Tcl_Main able to handle non-blocking stdin
544 catch {set f [open "|[list [interpreter]]" w+]}
547 fconfigure stdin -blocking 0
550 list [catch {gets $f} line] $line
553 } -result [list 0 SUCCESS]
556 Tcl_Main handles stdin EOF in mid-command
560 catch {set f [open "|[list [interpreter]]" w+]}
561 catch {fconfigure $f -blocking 0}
563 type $f "fconfigure stdin -eofchar \\032
566 fileevent $f readable \
567 [list set [namespace which -variable wait] "child exit"]
568 set id [after 2000 [list set [namespace which -variable wait] timeout]]
569 vwait [namespace which -variable wait]
573 if {[string equal timeout $wait]
574 && [string equal unix $::tcl_platform(platform)]} {
578 } -result {child exit}
581 Tcl_Main handles stdin EOF in mid-command
585 set cmd {makeFile "if 1 \{" script}
586 catch {set f [open "|[list [interpreter]] < [list [eval $cmd]]" r]}
587 catch {fconfigure $f -blocking 0}
590 fileevent $f readable \
591 [list set [namespace which -variable wait] "child exit"]
592 set id [after 2000 [list set [namespace which -variable wait] timeout]]
593 vwait [namespace which -variable wait]
597 if {[string equal timeout $wait]
598 && [string equal unix $::tcl_platform(platform)]} {
603 } -result {child exit}
606 Tcl_Main: error raised in interactive mode
610 exec [interpreter] << {error foo} >& result
619 Tcl_Main: interactive mode: errors don't stop command loop
623 exec [interpreter] << {
632 } -result "foo\nbar\n"
635 Tcl_Main: interactive mode: closed stderr
639 exec [interpreter] << {
652 Tcl_Main: interactive mode: close stdin
653 -> main loop & [exit] & exit handlers
657 exec [interpreter] << {
665 testexithandler create 0
673 } -result "Exit MainLoop\nIn exit\neven 0\n"
676 Tcl_Main: interactive mode: delete interp
677 -> main loop & exit handlers, but no [exit]
681 exec [interpreter] << {
689 testexithandler create 0
697 } -result "Exit MainLoop\neven 0\n"
700 Tcl_Main: exit main loop in mid-interactive command
704 catch {set f [open "|[list [interpreter]]" w+]}
705 catch {fconfigure $f -blocking 0}
707 type $f "testsetmainloop
708 after 2000 testexitmainloop
712 set code1 [catch {gets $f} line1]
713 set code2 [catch {gets $f} line2]
714 set code3 [catch {gets $f} line3]
715 list $code1 $line1 $code2 $line2 $code3 $line3
718 } -result [list 0 {Exit MainLoop} 0 {1 2} 0 {3 4}]
721 Tcl_Main: EOF in interactive main loop
725 exec [interpreter] << {
731 testexithandler create 0
732 after 0 testexitmainloop
740 } -result "Exit MainLoop\nIn exit\neven 0\n"
743 Tcl_Main: close stdin in interactive main loop
747 exec [interpreter] << {
753 testexithandler create 0
754 after 100 testexitmainloop
757 puts "don't reach this"
764 } -result "Exit MainLoop\nIn exit\neven 0\n"
766 # Tests Tcl_Main-6.*: interactive operations with prompts
769 Tcl_Main: enable prompts with tcl_interactive
773 exec [interpreter] << {set tcl_interactive 1} >& result
782 Tcl_Main: prompt deletes interp
786 exec [interpreter] << {
787 set tcl_prompt1 {testinterpdelete {}}
788 set tcl_interactive 1
799 Tcl_Main: prompt closes stdin
803 exec [interpreter] << {
804 set tcl_prompt1 {close stdin}
805 set tcl_interactive 1
816 Tcl_Main: interactive output, closed stdout
820 exec [interpreter] << {
821 set tcl_interactive 1
831 } -result "1\n% YES\n"
834 Tcl_Main: interactive entry to main loop
838 exec [interpreter] << {
839 set tcl_interactive 1
841 testexitmainloop} >& result
847 } -result "1\n% % % Exit MainLoop\n"
850 Tcl_Main: number of prompts during stdin close exit
854 exec [interpreter] << {
855 set tcl_interactive 1
856 close stdin} >& result
865 [unknown]: interactive auto-completion.
869 exec [interpreter] << {
871 set ::auto_noexec xxx
872 set tcl_interactive 1
881 # Tests Tcl_Main-7.*: exiting
884 Tcl_Main: [exit] defined as no-op -> still have exithandlers
888 exec [interpreter] << {
890 testexithandler create 0
900 Tcl_Main: [exit] defined as no-op -> still have exithandlers
904 exec [interpreter] << {
906 testexithandler create 0
907 after 0 testexitmainloop
915 } -result "Exit MainLoop\neven 0\n"
917 # Tests Tcl_Main-8.*: StdinProc operations
920 StdinProc: handles non-blocking stdin
924 exec [interpreter] << {
926 fconfigure stdin -blocking 0
934 } -result "Exit MainLoop\n"
937 StdinProc: handles stdin EOF
941 exec [interpreter] << {
943 testexithandler create 0
949 after 100 testexitmainloop
956 } -result "Exit MainLoop\nIn exit\neven 0\n"
959 StdinProc: handles interactive stdin EOF
963 exec [interpreter] << {
965 testexithandler create 0
971 set tcl_interactive 1} >& result
977 } -result "1\n% even 0\n"
980 StdinProc: handles stdin close
984 exec [interpreter] << {
991 after 100 testexitmainloop
1000 } -result "1\nExit MainLoop\nIn exit\n"
1003 StdinProc: handles interactive stdin close
1007 exec [interpreter] << {
1009 set tcl_interactive 1
1015 after 100 testexitmainloop
1024 } -result "1\n% % % after#0\n% after#1\n% 1\nExit MainLoop\nIn exit\n"
1027 StdinProc: handles event loop re-entry
1031 exec [interpreter] << {
1033 after 100 {puts 1; set delay 1}
1043 } -result "1\n2\nExit MainLoop\n"
1046 StdinProc: handling of errors
1050 exec [interpreter] << {
1060 } -result "foo\nExit MainLoop\n"
1063 StdinProc: handling of errors, closed stderr
1067 exec [interpreter] << {
1078 } -result "Exit MainLoop\n"
1081 StdinProc: interactive output
1085 exec [interpreter] << {
1087 set tcl_interactive 1
1088 testexitmainloop} >& result
1094 } -result "1\n% % Exit MainLoop\n"
1096 test Tcl_Main-8.10 {
1097 StdinProc: interactive output, closed stdout
1101 exec [interpreter] << {
1104 set tcl_interactive 1
1114 test Tcl_Main-8.11 {
1115 StdinProc: prompt deletes interp
1119 exec [interpreter] << {
1121 set tcl_prompt1 {testinterpdelete {}}
1122 set tcl_interactive 1} >& result
1130 test Tcl_Main-8.12 {
1131 StdinProc: prompt closes stdin
1135 exec [interpreter] << {
1137 set tcl_prompt1 {close stdin}
1138 after 100 testexitmainloop
1139 set tcl_interactive 1
1147 } -result "1\nExit MainLoop\n"
1149 # Tests Tcl_Main-9.*: Prompt operations
1152 Prompt: custom prompt variables
1156 exec [interpreter] << {
1157 set tcl_prompt1 {puts -nonewline stdout "one "}
1158 set tcl_prompt2 {puts -nonewline stdout "two "}
1159 set tcl_interactive 1
1167 } -result "1\none two This is\n\t\ta test\none "
1170 Prompt: error in custom prompt variables
1174 exec [interpreter] << {
1175 set tcl_prompt1 {error foo}
1176 set tcl_interactive 1
1177 set errorInfo} >& result
1183 } -result "1\nfoo\n% foo\n while executing\n\"error foo\"\n (script\
1184 that generates prompt)\nfoo\n% "
1187 Prompt: error in custom prompt variables, closed stderr
1191 exec [interpreter] << {
1192 set tcl_prompt1 {close stderr; error foo}
1193 set tcl_interactive 1} >& result
1202 Prompt: error in custom prompt variables, closed stdout
1206 exec [interpreter] << {
1207 set tcl_prompt1 {close stdout; error foo}
1208 set tcl_interactive 1} >& result
1214 } -result "1\nfoo\n"
1216 cd [workingDirectory]
1221 namespace delete ::tcl::test::main