1 | # Commands covered: error, catch |
---|
2 | # |
---|
3 | # This file contains a collection of tests for one or more of the Tcl |
---|
4 | # built-in commands. Sourcing this file into Tcl runs the tests and |
---|
5 | # generates output for errors. No output means no errors were found. |
---|
6 | # |
---|
7 | # Copyright (c) 1991-1993 The Regents of the University of California. |
---|
8 | # Copyright (c) 1994-1996 Sun Microsystems, Inc. |
---|
9 | # Copyright (c) 1998-1999 by Scriptics Corporation. |
---|
10 | # |
---|
11 | # See the file "license.terms" for information on usage and redistribution |
---|
12 | # of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
13 | # |
---|
14 | # RCS: @(#) $Id: error.test,v 1.16 2006/10/09 19:15:44 msofer Exp $ |
---|
15 | |
---|
16 | if {[lsearch [namespace children] ::tcltest] == -1} { |
---|
17 | package require tcltest 2 |
---|
18 | namespace import -force ::tcltest::* |
---|
19 | } |
---|
20 | |
---|
21 | namespace eval ::tcl::test::error { |
---|
22 | proc foo {} { |
---|
23 | global errorInfo |
---|
24 | set a [catch {format [error glorp2]} b] |
---|
25 | error {Human-generated} |
---|
26 | } |
---|
27 | |
---|
28 | proc foo2 {} { |
---|
29 | global errorInfo |
---|
30 | set a [catch {format [error glorp2]} b] |
---|
31 | error {Human-generated} $errorInfo |
---|
32 | } |
---|
33 | |
---|
34 | # Catch errors occurring in commands and errors from "error" command |
---|
35 | |
---|
36 | test error-1.1 {simple errors from commands} { |
---|
37 | catch {format [string index]} b |
---|
38 | } 1 |
---|
39 | |
---|
40 | test error-1.2 {simple errors from commands} { |
---|
41 | catch {format [string index]} b |
---|
42 | set b |
---|
43 | } {wrong # args: should be "string index string charIndex"} |
---|
44 | |
---|
45 | test error-1.3 {simple errors from commands} { |
---|
46 | catch {format [string index]} b |
---|
47 | set ::errorInfo |
---|
48 | # this used to return '... while executing ...', but |
---|
49 | # string index is fully compiled as of 8.4a3 |
---|
50 | } {wrong # args: should be "string index string charIndex" |
---|
51 | while executing |
---|
52 | "string index"} |
---|
53 | |
---|
54 | test error-1.4 {simple errors from commands} { |
---|
55 | catch {error glorp} b |
---|
56 | } 1 |
---|
57 | |
---|
58 | test error-1.5 {simple errors from commands} { |
---|
59 | catch {error glorp} b |
---|
60 | set b |
---|
61 | } glorp |
---|
62 | |
---|
63 | test error-1.6 {simple errors from commands} { |
---|
64 | catch {catch a b c d} b |
---|
65 | } 1 |
---|
66 | |
---|
67 | test error-1.7 {simple errors from commands} { |
---|
68 | catch {catch a b c d} b |
---|
69 | set b |
---|
70 | } {wrong # args: should be "catch script ?resultVarName? ?optionVarName?"} |
---|
71 | |
---|
72 | test error-1.8 {simple errors from commands} { |
---|
73 | # This test is non-portable: it generates a memory fault on |
---|
74 | # machines like DEC Alphas (infinite recursion overflows |
---|
75 | # stack?) |
---|
76 | # |
---|
77 | # That claims sounds like a bug to be fixed rather than a portability |
---|
78 | # problem. Anyhow, I believe it's out of date (bug's been fixed) so |
---|
79 | # this test is re-enabled. |
---|
80 | |
---|
81 | proc p {} { |
---|
82 | uplevel 1 catch p error |
---|
83 | } |
---|
84 | p |
---|
85 | } 0 |
---|
86 | |
---|
87 | # Check errors nested in procedures. Also check the optional argument |
---|
88 | # to "error" to generate a new error trace. |
---|
89 | |
---|
90 | test error-2.1 {errors in nested procedures} { |
---|
91 | catch foo b |
---|
92 | } 1 |
---|
93 | |
---|
94 | test error-2.2 {errors in nested procedures} { |
---|
95 | catch foo b |
---|
96 | set b |
---|
97 | } {Human-generated} |
---|
98 | |
---|
99 | test error-2.3 {errors in nested procedures} { |
---|
100 | catch foo b |
---|
101 | set ::errorInfo |
---|
102 | } {Human-generated |
---|
103 | while executing |
---|
104 | "error {Human-generated}" |
---|
105 | (procedure "foo" line 4) |
---|
106 | invoked from within |
---|
107 | "foo"} |
---|
108 | |
---|
109 | test error-2.4 {errors in nested procedures} { |
---|
110 | catch foo2 b |
---|
111 | } 1 |
---|
112 | |
---|
113 | test error-2.5 {errors in nested procedures} { |
---|
114 | catch foo2 b |
---|
115 | set b |
---|
116 | } {Human-generated} |
---|
117 | |
---|
118 | test error-2.6 {errors in nested procedures} { |
---|
119 | catch foo2 b |
---|
120 | set ::errorInfo |
---|
121 | } {glorp2 |
---|
122 | while executing |
---|
123 | "error glorp2" |
---|
124 | (procedure "foo2" line 3) |
---|
125 | invoked from within |
---|
126 | "foo2"} |
---|
127 | |
---|
128 | # Error conditions related to "catch". |
---|
129 | |
---|
130 | test error-3.1 {errors in catch command} { |
---|
131 | list [catch {catch} msg] $msg |
---|
132 | } {1 {wrong # args: should be "catch script ?resultVarName? ?optionVarName?"}} |
---|
133 | test error-3.2 {errors in catch command} { |
---|
134 | list [catch {catch a b c} msg] $msg |
---|
135 | } {0 1} |
---|
136 | test error-3.3 {errors in catch command} { |
---|
137 | catch {unset a} |
---|
138 | set a(0) 22 |
---|
139 | list [catch {catch {format 44} a} msg] $msg |
---|
140 | } {1 {couldn't save command result in variable}} |
---|
141 | catch {unset a} |
---|
142 | |
---|
143 | # More tests related to errorInfo and errorCode |
---|
144 | |
---|
145 | test error-4.1 {errorInfo and errorCode variables} { |
---|
146 | list [catch {error msg1 msg2 msg3} msg] $msg $::errorInfo $::errorCode |
---|
147 | } {1 msg1 msg2 msg3} |
---|
148 | test error-4.2 {errorInfo and errorCode variables} { |
---|
149 | list [catch {error msg1 {} msg3} msg] $msg $::errorInfo $::errorCode |
---|
150 | } {1 msg1 {msg1 |
---|
151 | while executing |
---|
152 | "error msg1 {} msg3"} msg3} |
---|
153 | test error-4.3 {errorInfo and errorCode variables} { |
---|
154 | list [catch {error msg1 {}} msg] $msg $::errorInfo $::errorCode |
---|
155 | } {1 msg1 {msg1 |
---|
156 | while executing |
---|
157 | "error msg1 {}"} NONE} |
---|
158 | test error-4.4 {errorInfo and errorCode variables} { |
---|
159 | set ::errorCode bogus |
---|
160 | list [catch {error msg1} msg] $msg $::errorInfo $::errorCode |
---|
161 | } {1 msg1 {msg1 |
---|
162 | while executing |
---|
163 | "error msg1"} NONE} |
---|
164 | test error-4.5 {errorInfo and errorCode variables} { |
---|
165 | set ::errorCode bogus |
---|
166 | list [catch {error msg1 msg2 {}} msg] $msg $::errorInfo $::errorCode |
---|
167 | } {1 msg1 msg2 {}} |
---|
168 | |
---|
169 | # Errors in error command itself |
---|
170 | |
---|
171 | test error-5.1 {errors in error command} { |
---|
172 | list [catch {error} msg] $msg |
---|
173 | } {1 {wrong # args: should be "error message ?errorInfo? ?errorCode?"}} |
---|
174 | test error-5.2 {errors in error command} { |
---|
175 | list [catch {error a b c d} msg] $msg |
---|
176 | } {1 {wrong # args: should be "error message ?errorInfo? ?errorCode?"}} |
---|
177 | |
---|
178 | # Make sure that catch resets error information |
---|
179 | |
---|
180 | test error-6.1 {catch must reset error state} { |
---|
181 | catch {error outer [catch {error inner inner.errorInfo inner.errorCode}]} |
---|
182 | list $::errorCode $::errorInfo |
---|
183 | } {NONE 1} |
---|
184 | test error-6.2 {catch must reset error state} { |
---|
185 | catch {error outer [catch {return -level 0 -code error -errorcode BUG}]} |
---|
186 | list $::errorCode $::errorInfo |
---|
187 | } {NONE 1} |
---|
188 | test error-6.3 {catch must reset error state} { |
---|
189 | set ::errorCode BUG |
---|
190 | catch {error outer [catch set]} |
---|
191 | list $::errorCode $::errorInfo |
---|
192 | } {NONE 1} |
---|
193 | test error-6.4 {catch must reset error state} { |
---|
194 | catch {error [catch {error foo bar baz}] 1} |
---|
195 | list $::errorCode $::errorInfo |
---|
196 | } {NONE 1} |
---|
197 | test error-6.5 {catch must reset error state} { |
---|
198 | catch {error [catch {return -level 0 -code error -errorcode BUG}] 1} |
---|
199 | list $::errorCode $::errorInfo |
---|
200 | } {NONE 1} |
---|
201 | test error-6.6 {catch must reset error state} { |
---|
202 | catch {return -level 0 -code error -errorinfo [catch {error foo bar baz}]} |
---|
203 | list $::errorCode $::errorInfo |
---|
204 | } {NONE 1} |
---|
205 | test error-6.7 {catch must reset error state} { |
---|
206 | proc foo {} { |
---|
207 | return -code error -errorinfo [catch {error foo bar baz}] |
---|
208 | } |
---|
209 | catch foo |
---|
210 | list $::errorCode |
---|
211 | } {NONE} |
---|
212 | test error-6.8 {catch must reset error state} { |
---|
213 | catch {return -level 0 -code error [catch {error foo bar baz}]} |
---|
214 | list $::errorCode |
---|
215 | } {NONE} |
---|
216 | test error-6.9 {catch must reset error state} { |
---|
217 | proc foo {} { |
---|
218 | return -code error [catch {error foo bar baz}] |
---|
219 | } |
---|
220 | catch foo |
---|
221 | list $::errorCode |
---|
222 | } {NONE} |
---|
223 | |
---|
224 | test error-7.0 {Bug 1397843} -body { |
---|
225 | variable cmds |
---|
226 | proc EIWrite args { |
---|
227 | variable cmds |
---|
228 | lappend cmds [lindex [info level -2] 0] |
---|
229 | } |
---|
230 | proc BadProc {} { |
---|
231 | set i a |
---|
232 | incr i |
---|
233 | } |
---|
234 | trace add variable ::errorInfo write [namespace code EIWrite] |
---|
235 | catch BadProc |
---|
236 | trace remove variable ::errorInfo write [namespace code EIWrite] |
---|
237 | set cmds |
---|
238 | } -match glob -result {*BadProc*} |
---|
239 | } |
---|
240 | namespace delete ::tcl::test::error |
---|
241 | |
---|
242 | # cleanup |
---|
243 | catch {rename p ""} |
---|
244 | ::tcltest::cleanupTests |
---|
245 | return |
---|