3 source [file dirname [info script]]/testing.tcl
8 test defer-1.1 {defer in proc} {
12 # This does nothing since it increments a local variable
14 # This increments the global variable
16 # Will return "-", not "-L" since return happens before defer triggers
22 test defer-1.2 {set $defer directly} {
25 lappend jim::defer {append ::x a}
26 lappend jim::defer {append ::x b}
30 } {{{append ::x a} {append ::x b}} -ba}
33 test defer-1.3 {unset $defer} {
37 # unset, to remove all defer actions
44 test defer-1.4 {error in defer - error} {
47 # First defer script will not happen because of error in next defer script
49 # Error ignored because of error from proc
51 # Last defer script will happen
53 # This error will take precedence over the error from defer
56 set rc [catch {a} msg]
57 list [info ret $rc] $msg $x
60 test defer-1.5 {error in defer - return} {
63 # First defer script will not happen
66 # Last defer script will happen
70 set rc [catch {a} msg]
71 list [info ret $rc] $msg $x
72 } {error {invalid command name "blah"} -b}
74 test defer-1.6 {error in defer - ok} {
77 # First defer script will not happen
79 # Error ignored because of error from proc
81 # Last defer script will happen
84 set rc [catch {a} msg]
85 list [info ret $rc] $msg $x
86 } {error {invalid command name "blah"} -b}
88 test defer-1.7 {error in defer - break} {
91 # First defer script will not happen
93 # This non-zero return code will take precedence over the proc return
94 defer {return -code 30 ret30}
95 # Last defer script will happen
100 set rc [catch {a} msg]
101 list [info ret $rc] $msg $x
104 test defer-1.8 {error in defer - tailcall} {
107 # This will prevent tailcall from happening
110 # Tailcall will not happen because of error in defer
111 tailcall append ::x a
113 set rc [catch {a} msg]
114 list [info ret $rc] $msg $x
115 } {error {invalid command name "blah"} -}
117 test defer-1.9 {Add to defer in defer body} {
121 # Add to defer in defer
123 # This will do nothing
133 test defer-1.10 {Unset defer in defer body} {
137 # This will do nothing
138 unset -nocomplain jim::defer
146 test defer-1.11 {defer through tailcall} {
154 # c will be invoked as through called from a but this
155 # won't make any difference for defer
165 test defer-1.12 {defer in recursive call} {
168 # defer happens just before the return, so after the recursive call to a
169 defer {lappend ::x $n}
178 test defer-1.13 {defer in recursive tailcall} {
181 # defer happens just before the return, so before the tailcall to a
182 defer {lappend ::x $n}
191 test defer-1.14 {defer capture variables} {
195 # A normal defer will evaluate at the end of the proc, so $y may change
196 defer {lappend ::x $y}
199 # What if we want to capture the value of y here? list will work
200 defer [list lappend ::x $y]
203 # But with multiple statements, list doesn't work, so use a lambda
204 # to capture the value instead
205 defer [lambda {} {y} {
216 test defer-2.1 {defer from interp} -body {
218 # defer needs to have some effect to detect on exit,
220 file delete defer.tmp
223 [open defer.tmp w] puts "leaving child"
226 set a [file exists defer.tmp]
228 # Now the file should exist
229 set f [open defer.tmp]
233 } -result {0 {leaving child}} -cleanup {
234 file delete defer.tmp