aboutsummaryrefslogtreecommitdiff
path: root/tests/case.test
diff options
context:
space:
mode:
authorSteve Bennett <steveb@workware.net.au>2010-03-03 15:50:50 +1000
committerSteve Bennett <steveb@workware.net.au>2010-10-15 11:02:48 +1000
commit6a9fcd338b28fe76cb980867632068dd2bec533c (patch)
tree7e4046bd5d6ae0fa018dcfc51208c010b00ef472 /tests/case.test
parentec3d0d6cfddfa055d00c820a2ed99a7d6858aa82 (diff)
downloadjimtcl-6a9fcd338b28fe76cb980867632068dd2bec533c.zip
jimtcl-6a9fcd338b28fe76cb980867632068dd2bec533c.tar.gz
jimtcl-6a9fcd338b28fe76cb980867632068dd2bec533c.tar.bz2
Improvements to catch, return, signal, try
Improve the ability to rethrow errors * Allow return to rethrow an error by accepting '-errorinfo stacktrace' * Also, 'catch ... opts' now also stores opts(-errorinfo) on error * Use these to provide better stack traces from 'case' and 'try' * Implement 'return -level' Make try/on/finally more Tcl 8.6 compatible * With support for 'on' handlers and docs Add support for catch options to try * Otherwise it's hard to use try to catch signals Improvements to signal handling * catch -signal now sets a list of the handled signals as the result * catch -signal won't execute the body at all if a handled signal is pending * up to 64 (jim_wide) signals can now be handled * if catch -signal is nested, the innermost catch will catch the error * new 'signal catch' allows ignored/blocked signals to be examined and cleared. * update docs on signal handling exec should indicate which signal killed the child Signed-off-by: Steve Bennett <steveb@workware.net.au>
Diffstat (limited to 'tests/case.test')
-rw-r--r--tests/case.test92
1 files changed, 35 insertions, 57 deletions
diff --git a/tests/case.test b/tests/case.test
index a74f265..4a594ad 100644
--- a/tests/case.test
+++ b/tests/case.test
@@ -1,56 +1,12 @@
source testing.tcl
-# Test that control structures can be implemented in a proc
-
-proc control {cond code} {
- set iscond [uplevel 1 expr $cond]
- #puts "$cond -> $iscond"
- if {$iscond} {
- set rc [catch [list uplevel 1 $code] error opts]
- #puts "$code -> rc=$rc, error=$error, opts=$opts"
- if {$rc == 2 && $opts(-code) != 0} {
- set rc $opts(-code)
- }
- return -code $rc $error
- }
-}
-
-test control-1.1 "False case" {
- control 0 bogus
-} {}
-
-test control-1.2 "Simple case" {
- control 1 {return result}
-} {result}
-
-test control-1.3 "Break from proc" {
- set result {}
- foreach i {1 2 3 4 5} {
- control {$i == 4} {break}
- lappend result $i
- }
- set result
-} {1 2 3}
-
-test control-1.4 "Return from proc" {
- foreach i {1 2 3 4 5} {
- control {$i == 3} {return $i}
- }
-} {3}
-
-test control-1.5 "Continue from proc" {
- set result {}
- foreach i {1 2 3 4 5} {
- control {$i == 2} {continue}
- lappend result $i
- }
- set result
-} {1 3 4 5}
-
# case is a proc, but it should be able
# to cause a return in do_case
proc do_case {var} {
case $var in {
+ 0 {
+ return
+ }
1 {
return one
}
@@ -61,23 +17,45 @@ proc do_case {var} {
return -code continue three
}
4 {
- return 44
+ return -code break four
}
5 {
- return -code break five
+ continue
}
6 {
- return eight
+ break
}
}
return zero
}
-test control-2.1 "Return from case" {
- set result {}
- foreach i {0 1 2 3 4 5 6} {
- lappend result [do_case $i]
- }
- set result
-} {zero one two 44}
+test case-2.0 "Plain from case" {
+ do_case 0
+} {}
+
+test case-2.1 "Return from case with value" {
+ do_case 1
+} {one}
+test case-2.2 "Return -code ok from case" {
+ do_case 2
+ list [catch {do_case 2} msg] $msg
+} {0 two}
+
+test case-2.3 "Return -code continue from case" {
+ list [catch {do_case 3} msg] $msg
+} {4 three}
+
+test case-2.4 "Return -code break from case" {
+ list [catch {do_case 4} msg] $msg
+} {3 four}
+
+if {0} {
+test case-2.5 "continue from case" {
+ list [catch {do_case 5} msg] $msg
+} {1 {invoked "continue" outside of a loop}}
+
+test case-2.6 "break from case" {
+ list [catch {do_case 6} msg] $msg
+} {1 {invoked "break" outside of a loop}}
+}