1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
#
# tryeval.test
#
# Tests for the try_eval command.
#---------------------------------------------------------------------------
# Copyright 1997-1999 Karl Lehenbauer and Mark Diekhans.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted, provided
# that the above copyright notice appear in all copies. Karl Lehenbauer and
# Mark Diekhans make no representations about the suitability of this
# software for any purpose. It is provided "as is" without express or
# implied warranty.
#------------------------------------------------------------------------------
# $Id: tryeval.test,v 1.3 2002/04/04 06:10:30 hobbs Exp $
#------------------------------------------------------------------------------
#
if {[cequal [info procs Test] {}]} {
source [file join [file dirname [info script]] testlib.tcl]
}
#
# Use a proc to do some tests to see if errorInfo and errorCode are preserved
# and imported correctly.
#
proc test_try_eval {code catch {finally {}}} {
try_eval $code $catch $finally
}
proc test_try_eval_results {code catch {finally {}}} {
set code [catch {test_try_eval $code $catch $finally} result]
global errorInfo errorCode
return [list $code $result $errorInfo $errorCode]
}
proc init_error_vars {errorInfoVal errorCodeVal} {
global errorInfo errorCode
set errorInfo $errorInfoVal
set errorCode $errorCodeVal
}
test try_eval-1.1 {try_eval argument checking} {
list [catch {try_eval} msg] $msg
} {1 {wrong # args: try_eval code catch ?finally?}}
test try_eval-1.2 {try_eval argument checking} {
list [catch {try_eval badcommand} msg] $msg
} {1 {wrong # args: try_eval code catch ?finally?}}
test try_eval-1.3 {try_eval argument checking} {
list [catch {try_eval badcommand badcommand2 badcommand3 badcommand4} msg]\
$msg
} {1 {wrong # args: try_eval code catch ?finally?}}
test try_eval-2.1 {try_eval ok result} {
init_error_vars "errorInfo-2.1" "errorCode-2.1"
set result [test_try_eval_results {
concat "try_eval result 2.1"
} {
error "error should not execute"
} {
global final
set final "finally-2.1"
concat "finally result should not be returned"
}]
global final
concat $result $final
} {0 {try_eval result 2.1} errorInfo-2.1 errorCode-2.1 finally-2.1}
test try_eval-2.2 {try_eval ok result} {
init_error_vars "errorInfo-2.2" "errorCode-2.2"
set result [test_try_eval_results {
concat "try_eval result 2.2"
} {
error "error should not execute"
}
]
list $result
} {{0 {try_eval result 2.2} errorInfo-2.2 errorCode-2.2}}
test try_eval-2.3 {try_eval ok result} {
init_error_vars "errorInfo-2.3" "errorCode-2.3"
set result [test_try_eval_results {
concat "try_eval result 2.3"
} {} {
global final
set final "finally-2.3"
concat "finally result should not be returned"
}
]
global final
concat $result $final
} {0 {try_eval result 2.3} errorInfo-2.3 errorCode-2.3 finally-2.3}
test try_eval-3.1 {try_eval error result} {
init_error_vars "errorInfo-3.1" "errorCode-3.1"
set result [test_try_eval_results {
error "try_eval error 3.1" errorInfo-error-3.1 errorCode-error-3.1
} {
error "cont: $errorResult" "cont: $errorInfo" "cont: $errorCode"
} {
catch {error bow wow arf} ;# Hose errorInfo/Code
global final
set final "finally-3.1"
concat "finally result should not be returned"
}
]
global final
concat $result $final
} {1 {cont: try_eval error 3.1} {cont: errorInfo-error-3.1
invoked from within
"try_eval $code $catch $finally"
(procedure "test_try_eval" line 2)
invoked from within
"test_try_eval $code $catch $finally"} {cont: errorCode-error-3.1} finally-3.1}
test try_eval-3.2 {try_eval error result} {
init_error_vars "errorInfo-3.2" "errorCode-3.2"
set result [test_try_eval_results {
error "try_eval error 3.2" errorInfo-error-3.2 errorCode-error-3.2
} {
error "cont: $errorResult" "cont: $errorInfo" "cont: $errorCode"
}
]
list $result
} {{1 {cont: try_eval error 3.2} {cont: errorInfo-error-3.2
invoked from within
"try_eval $code $catch $finally"
(procedure "test_try_eval" line 2)
invoked from within
"test_try_eval $code $catch $finally"} {cont: errorCode-error-3.2}}}
test try_eval-3.3 {try_eval error result} {
init_error_vars "errorInfo-3.3" "errorCode-3.3"
set result [test_try_eval_results {
error "try_eval error 3.3" errorInfo-error-3.3 errorCode-error-3.3
} {} {
catch {error bow wow arf} ;# Hose errorInfo/Code
global final
set final "finally-3.3"
concat "finally result should not be returned"
}
]
global final
concat $result $final
} {1 {try_eval error 3.3} {errorInfo-error-3.3
invoked from within
"try_eval $code $catch $finally"
(procedure "test_try_eval" line 2)
invoked from within
"test_try_eval $code $catch $finally"} errorCode-error-3.3 finally-3.3}
test try_eval-3.4 {try_eval error result} {
init_error_vars "errorInfo-3.4" "errorCode-3.4"
set result [test_try_eval_results {
error "try_eval error 3.4" errorInfo-error-3.4 errorCode-error-3.4
} {} {
catch {error bow wow arf} ;# Hose errorInfo/Code
global final
set final "finally-3.4"
concat "finally result should not be returned"
}
]
global final
list $result $final
} {{1 {try_eval error 3.4} {errorInfo-error-3.4
invoked from within
"try_eval $code $catch $finally"
(procedure "test_try_eval" line 2)
invoked from within
"test_try_eval $code $catch $finally"} errorCode-error-3.4} finally-3.4}
test try_eval-3.5 {try_eval error result} {
init_error_vars "errorInfo-3.5" "errorCode-3.5"
set result [test_try_eval_results {
error "try_eval error 3.5" errorInfo-error-3.5 errorCode-error-3.5
} {
concat "Ok now 3.5"
} {
catch {error bow wow arf} ;# Hose errorInfo/Code
global final
set final "finally-3.5"
concat "finally result should not be returned"
}
]
global final
concat $result $final
} {0 {Ok now 3.5} errorInfo-error-3.5 errorCode-error-3.5 finally-3.5}
test try_eval-3.6 {try_eval error result} {
init_error_vars "errorInfo-3.6" "errorCode-3.6"
set result [test_try_eval_results {
error "try_eval error 3.6" errorInfo-error-3.6 errorCode-error-3.6
} {
concat "Ok now 3.6"
}
]
list $result
} {{0 {Ok now 3.6} errorInfo-error-3.6 errorCode-error-3.6}}
# cleanup
::tcltest::cleanupTests
return
|