Debian Codegraph @

test/exception.scm

raw
1 ;; test exception handling system
2 ;; this must come after primsyn, error, macro and object tests.
3
4 (use gauche.test)
5 (test-start "exceptions")
6
7 ;;--------------------------------------------------------------------
8 (test-section "bare constructors")
9
10 (test* "make <error>" '(#t #t #t #f)
11 (let ((e (make <error>)))
12 (list (is-a? e <condition>)
13 (is-a? e <serious-condition>)
14 (is-a? e <error>)
15 (ref e 'message))))
16
17 (test* "make <message-condition>" "huge"
18 (ref (make <message-condition> :message "huge") 'message))
19
20 (test* "make <error>" "hoge"
21 (ref (make <error> :message "hoge") 'message))
22
23 (test* "make <system-error>" '("oops" 12)
24 (let ((e (make <system-error> :message "oops" :errno 12)))
25 (map (cut ref e <>) '(message errno))))
26
27 ;;--------------------------------------------------------------------
28 (test-section "SRFI-35 constructors, predicates and accessors")
29
30 (test* "make-condition <error>" '(#t #t #f "moo")
31 (let ((e (make-condition <error> 'message "moo")))
32 (list
33 (condition-has-type? e <error>)
34 (condition-has-type? e <serious-condition>)
35 (condition-has-type? e <read-error>)
36 (condition-ref e 'message))))
37
38 (test* "make-condition <port-error>" `(#t #t #t #f "moo" ,(current-input-port))
39 (let ((e (make-condition <port-error>
40 'port (current-input-port)
41 'message "moo")))
42 (list
43 (condition-has-type? e <error>)
44 (condition-has-type? e <serious-condition>)
45 (condition-has-type? e <io-error>)
46 (condition-has-type? e <read-error>)
47 (condition-ref e 'message)
48 (condition-ref e 'port))))
49
50 (test* "make-compound-condition"
51 `(#t #t #t "sys" 12 ,(current-input-port))
52 (let ((e (make-compound-condition
53 (make-condition <system-error>
54 'message "sys" 'errno 12)
55 (make-condition <io-read-error>
56 'message "io" 'port (current-input-port)))))
57 (list
58 (condition-has-type? e <error>)
59 (condition-has-type? e <system-error>)
60 (condition-has-type? e <io-read-error>)
61 (condition-ref e 'message)
62 (condition-ref e 'errno)
63 (condition-ref e 'port))))
64
65 (test* "make-compound-condition"
66 `(#t #t #t "io" 12 ,(current-input-port))
67 (let ((e (make-compound-condition
68 (make-condition <io-read-error>
69 'message "io" 'port (current-input-port))
70 (make-condition <system-error>
71 'message "sys" 'errno 12))))
72 (list
73 (condition-has-type? e <error>)
74 (condition-has-type? e <system-error>)
75 (condition-has-type? e <io-read-error>)
76 (condition-ref e 'message)
77 (condition-ref e 'errno)
78 (condition-ref e 'port))))
79
80 (test* "make-compound-condition"
81 `(#t #t #t "message" 12 ,(current-input-port))
82 (let ((e (make-compound-condition
83 (make-compound-condition
84 (make-condition <message-condition> 'message "message")
85 (make-condition <io-read-error>
86 'message "io" 'port (current-input-port))
87 (make-condition <system-error>
88 'message "sys" 'errno 12)))))
89 (list
90 (condition-has-type? e <error>)
91 (condition-has-type? e <system-error>)
92 (condition-has-type? e <io-read-error>)
93 (condition-ref e 'message)
94 (condition-ref e 'errno)
95 (condition-ref e 'port))))
96
97 (test* "extract-condition"
98 `(("message")
99 ("message" ,(current-input-port))
100 ("message" 12))
101 (let* ((e (make-compound-condition
102 (make-compound-condition
103 (make-condition <message-condition> 'message "message")
104 (make-condition <io-read-error>
105 'message "io" 'port (current-input-port))
106 (make-condition <system-error>
107 'message "sys" 'errno 12))))
108 (m (extract-condition e <message-condition>))
109 (i (extract-condition e <io-read-error>))
110 (s (extract-condition e <system-error>)))
111 (list
112 (list (condition-ref m 'message))
113 (list (condition-ref i 'message) (condition-ref i 'port))
114 (list (condition-ref s 'message) (condition-ref s 'errno)))
115 ))
116
117 ;;--------------------------------------------------------------------
118 (test-section "SRFI-35 style condition definitions")
119
120 (define-condition-type &c &condition
121 c?
122 (x c-x))
123
124 (define-condition-type &c1 &c
125 c1?
126 (a c1-a))
127
128 (define-condition-type &c2 &c
129 c2?
130 (b c2-b))
131
132 (let ((v1 #f) (v2 #f) (v3 #f) (v4 #f) (v5 #f))
133 (set! v1 (make-condition &c1 'x "V1" 'a "a1"))
134
135 (test* "v1" '(#t #t #f "V1" "a1")
136 (list (c? v1) (c1? v1) (c2? v1) (c-x v1) (c1-a v1)))
137
138 (set! v2 (condition (&c2
139 (x "V2")
140 (b "b2"))))
141
142 (test* "v2" '(#t #f #t "V2" "b2")
143 (list (c? v2) (c1? v2) (c2? v2) (c-x v2) (c2-b v2)))
144
145 (set! v3 (condition (&c1
146 (x "V3/1")
147 (a "a3"))
148 (&c2
149 (b "b3"))))
150 (test* "v3" '(#t #t #t "V3/1" "a3" "b3")
151 (list (c? v3) (c1? v3) (c2? v3) (c-x v3) (c1-a v3) (c2-b v3)))
152
153 (set! v4 (make-compound-condition v1 v2))
154 (test* "v4" '(#t #t #t "V1" "a1" "b2")
155 (list (c? v4) (c1? v4) (c2? v4) (c-x v4) (c1-a v4) (c2-b v4)))
156
157 (set! v5 (make-compound-condition v2 v3))
158 (test* "v5" '(#t #t #t "V2" "a3" "b2")
159 (list (c? v5) (c1? v5) (c2? v5) (c-x v5) (c1-a v5) (c2-b v5)))
160
161 (test* "error (&c1)" "ok"
162 (with-error-handler
163 (lambda (e)
164 (and (is-a? e &c1)
165 (slot-ref e 'a)))
166 (lambda () (error &c1 :a "ok"))))
167 )
168
169 ;;--------------------------------------------------------------------
170 (test-section "with-exception-handler")
171
172 (test* "with-exception-handler" '(got oops)
173 (call/cc
174 (lambda (ret)
175 (with-exception-handler
176 (lambda (e) (ret `(got ,e)))
177 (lambda () (raise 'oops) 'huh?)))))
178
179 (test* "with-exception-handler (returning)" 'huh?
180 (with-exception-handler
181 (lambda (e) `(got ,e))
182 (lambda () (raise 'oops) 'huh?)))
183
184 (test* "with-exception-handler (returning mv)" '(aha! oops)
185 (values->list
186 (with-exception-handler
187 (lambda (e) (values 'aha! e))
188 (lambda () (raise 'oops)))))
189
190 ;; Now built-in with-exception-hanlder has r7rs semantics
191 (test* "with-exception-handler (raise in handler)" '(outer (inner oops))
192 (with-exception-handler
193 (lambda (e) `(outer ,e))
194 (lambda ()
195 (with-exception-handler
196 (lambda (e) (raise `(inner ,e)))
197 (lambda () (raise 'oops))))))
198
199 ;;--------------------------------------------------------------------
200 (test-section "guard")
201
202 (test* "guard" '(symbol . a)
203 (guard (x
204 ((symbol? x) (cons 'symbol x))
205 ((is-a? x <error>) 'caught-error))
206 (raise 'a)))
207
208 (test* "guard" 'caught-error
209 (guard (x
210 ((symbol? x) (cons 'symbol x))
211 ((is-a? x <error>) 'caught-error))
212 (car 'a)))
213
214 (test* "guard" 'caught-error
215 (guard (x
216 ((symbol? x) (cons 'symbol x))
217 ((<error> x) 'caught-error))
218 (car 'a)))
219
220 (test* "guard (uncaught error)" (test-error)
221 (guard (x
222 ((symbol? x) (cons 'symbol x))
223 ((is-a? x <error>) 'caught-error))
224 (raise 4)))
225
226 (test* "guard (uncaught error)" '(else . 4)
227 (guard (x
228 ((symbol? x) (cons 'symbol x))
229 ((is-a? x <error>) 'caught-error)
230 (else (cons 'else x)))
231 (raise 4)))
232
233 (test* "guard (subtype)" 'read-error
234 (guard (x
235 ((<read-error> x) 'read-error)
236 ((<system-error> x) 'system-error)
237 ((<error> x) 'error)
238 (else (cons 'else x)))
239 (read-from-string "(abc")))
240
241 (test* "guard (nested)" 'exn
242 (with-error-handler
243 values
244 (lambda ()
245 (guard (ball
246 (#f (display "Caught exception.")))
247 (guard (ball
248 (#f (raise ball)))
249 (raise 'exn))))))
250
251 (test* "guard (compound)" 'read-error
252 (guard (x
253 ((<read-error> x) 'read-error)
254 ((<system-error> x) 'system-error)
255 ((<error> x) 'error)
256 (else (cons 'else x)))
257 (raise (condition
258 (<io-error> (message "z"))
259 (<read-error> (message "foo"))
260 (<system-error> (message "bar"))))))
261
262 (let ()
263 (define aaa '())
264 (define (foo)
265 (dynamic-wind
266 (lambda () (set! aaa '()))
267 (lambda ()
268 (set! aaa (cons 'a aaa))
269 (error "foo"))
270 (lambda ()
271 (set! aaa (cons 'b aaa)))))
272 (test* "guard w/dynamic-wind" '((b a) (b a))
273 (let1 x (guard (e (else aaa)) (foo))
274 (list x aaa))))
275
276
277 ;;--------------------------------------------------------------------
278 (test-section "unwind-protect")
279
280 ;; NB: unwind-protect behavior on exit can't be tested here, for we
281 ;; need to invoke a subprocess. The tests are in process.scm instead.
282
283 (let ()
284 (define aaa '())
285 (define (foo thunk)
286 (dynamic-wind
287 (lambda () (push! aaa 'a))
288 (lambda () (push! aaa 'b) (thunk) (push! aaa 'c))
289 (lambda () (push! aaa 'd))))
290 (test* "unwind-protect (success)" '(e d c z b a)
291 (guard (e (else (push! aaa e) e))
292 (set! aaa '())
293 (unwind-protect
294 (foo (lambda () (push! aaa 'z)))
295 (push! aaa 'e))
296 aaa))
297 (test* "unwind-protect (error)" '(boo e d b a)
298 (guard (e (else (push! aaa 'boo) aaa))
299 (set! aaa '())
300 (unwind-protect
301 (foo (lambda () (error "boo")))
302 (push! aaa 'e))
303 aaa))
304 ; (test* "unwind-protect (raise)" '(boo d b a)
305 (test* "unwind-protect (raise)" '(boo d a d b a)
306 (guard (e (else (push! aaa e) aaa))
307 (set! aaa '())
308 (unwind-protect
309 (foo (lambda () (raise 'boo)))
310 (push! aaa 'e))
311 aaa))
312 (test* "unwind-protect (raise & continue)" '(e d c boo a d b a)
313 (begin
314 (set! aaa '())
315 (with-exception-handler
316 (lambda (e) (push! aaa e))
317 (lambda ()
318 (unwind-protect
319 (foo (lambda () (raise 'boo)))
320 (push! aaa 'e))))
321 aaa))
322 (test* "unwind-protect (restart)" '(e d c a d z b a)
323 (begin
324 (set! aaa '())
325 (let ((k #f))
326 (let/cc k1
327 (unwind-protect
328 (foo (lambda ()
329 (let/cc k2
330 (set! k k2) (push! aaa 'z) (k1 0))))
331 (push! aaa 'e)))
332 (when k (let ((k0 k)) (set! k #f) (k0 0))))
333 aaa))
334
335 ;; We no longer make this an error. there're legit cases that you want
336 ;; to reenter into BODY even HANDLERs are already called. We just don't
337 ;; execute HANDLERs more than once.
338 (test* "unwind-protect (reenter)"
339 (reverse '(a b z c d e a c d))
340 (begin
341 (set! aaa '())
342 (let ((k #f))
343 (unwind-protect
344 (foo (lambda ()
345 (let/cc k2
346 (set! k k2) (push! aaa 'z))))
347 (push! aaa 'e))
348 (when k (let ((k0 k)) (set! k #f) (k0 0))))
349 aaa))
350 )
351
352 ;;--------------------------------------------------------------------
353 (test-section "subtype")
354
355 (define-class <my-error> (<error>)
356 ((info :init-keyword :info)))
357
358 (define-class <my-exc> (<exception>)
359 ((type :init-keyword :type)))
360
361 (test* "<my-error>" '(#t "msg" "info")
362 (let ((e (make <my-error> :message "msg" :info "info")))
363 (list (is-a? e <error>)
364 (ref e 'message)
365 (ref e 'info))))
366
367 (test* "catching <my-error>" '(caught . "ok")
368 (guard (x
369 ((is-a? x <error>) (cons 'caught (ref x 'message))))
370 (raise (make <my-error> :message "ok"))))
371
372 (test* "<my-exc>" '(#t #f type)
373 (let ((e (make <my-exc> :type 'type)))
374 (list (is-a? e <exception>)
375 (is-a? e <error>)
376 (ref e 'type))))
377
378 (test* "catching <my-exc>" 'exception
379 (guard (x
380 ((is-a? x <error>) 'error)
381 ((is-a? x <exception>) 'exception))
382 (raise (make <my-exc>))))
383
384 ;;--------------------------------------------------------------------
385 (test-section "combinations")
386
387 (test* "guarding read-error" "Warning: read error:(input string port):1"
388 (guard (exc
389 ((condition-has-type? exc <read-error>)
390 (format "Warning: read error:~a:~a"
391 (port-name (ref exc 'port))
392 (ref exc 'line)))
393 ((condition-has-type? exc <io-port-error>)
394 (format "Warning: I/O error occurred on port ~a"
395 (port-name (ref exc 'port))))
396 (else
397 (format "Other error")))
398 (read-from-string "(abc")))
399
400 (test* "guarding read-error (2)"
401 "Warning: read error:(input string port):1"
402 (guard (exc
403 ((<read-error> exc)
404 (format "Warning: read error:~a:~a"
405 (port-name (ref exc 'port))
406 (ref exc 'line)))
407 ((<io-port-error> exc)
408 (format "Warning: I/O error occurred on port ~a"
409 (port-name (ref exc 'port))))
410 (else
411 (format "Other error")))
412 (read-from-string "(abc")))
413
414 (test-end)