| 1 |
;;;; exceptions.test --- tests for Guile's exception handling -*- scheme -*- |
| 2 |
;;;; Copyright (C) 2001, 2003, 2004, 2006, 2010 Free Software Foundation, Inc. |
| 3 |
;;;; |
| 4 |
;;;; This library is free software; you can redistribute it and/or |
| 5 |
;;;; modify it under the terms of the GNU Lesser General Public |
| 6 |
;;;; License as published by the Free Software Foundation; either |
| 7 |
;;;; version 3 of the License, or (at your option) any later version. |
| 8 |
;;;; |
| 9 |
;;;; This library is distributed in the hope that it will be useful, |
| 10 |
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
;;;; Lesser General Public License for more details. |
| 13 |
;;;; |
| 14 |
;;;; You should have received a copy of the GNU Lesser General Public |
| 15 |
;;;; License along with this library; if not, write to the Free Software |
| 16 |
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 |
|
| 18 |
|
| 19 |
(define-module (test-suite exceptions) |
| 20 |
#:use-module (ice-9 control) |
| 21 |
#:use-module (test-suite lib)) |
| 22 |
|
| 23 |
(define-syntax-parameter push |
| 24 |
(lambda (stx) |
| 25 |
(syntax-violation 'push "push used outside of throw-test" stx))) |
| 26 |
|
| 27 |
(define-syntax-rule (throw-test title result expr ...) |
| 28 |
(pass-if-equal title result |
| 29 |
(let ((stack '())) |
| 30 |
(syntax-parameterize ((push (syntax-rules () |
| 31 |
((push val) |
| 32 |
(set! stack (cons val stack)))))) |
| 33 |
expr ... |
| 34 |
;;(format #t "~a: ~s~%" title (reverse stack)) |
| 35 |
(reverse stack))))) |
| 36 |
|
| 37 |
(with-test-prefix "throw/catch" |
| 38 |
|
| 39 |
(with-test-prefix "wrong type argument" |
| 40 |
|
| 41 |
(pass-if-exception "(throw 1)" |
| 42 |
exception:wrong-type-arg |
| 43 |
(throw 1))) |
| 44 |
|
| 45 |
(with-test-prefix "wrong number of arguments" |
| 46 |
|
| 47 |
(pass-if-exception "(throw)" |
| 48 |
exception:wrong-num-args |
| 49 |
(throw)) |
| 50 |
|
| 51 |
(pass-if-exception "throw 1 / catch 0" |
| 52 |
exception:wrong-num-args |
| 53 |
(catch 'a |
| 54 |
(lambda () (throw 'a)) |
| 55 |
(lambda () #f))) |
| 56 |
|
| 57 |
(pass-if-exception "throw 2 / catch 1" |
| 58 |
exception:wrong-num-args |
| 59 |
(catch 'a |
| 60 |
(lambda () (throw 'a 2)) |
| 61 |
(lambda (x) #f))) |
| 62 |
|
| 63 |
(pass-if-exception "throw 1 / catch 2" |
| 64 |
exception:wrong-num-args |
| 65 |
(catch 'a |
| 66 |
(lambda () (throw 'a)) |
| 67 |
(lambda (x y) #f))) |
| 68 |
|
| 69 |
(pass-if-exception "throw 3 / catch 2" |
| 70 |
exception:wrong-num-args |
| 71 |
(catch 'a |
| 72 |
(lambda () (throw 'a 2 3)) |
| 73 |
(lambda (y x) #f))) |
| 74 |
|
| 75 |
(pass-if-exception "throw 1 / catch 2+" |
| 76 |
exception:wrong-num-args |
| 77 |
(catch 'a |
| 78 |
(lambda () (throw 'a)) |
| 79 |
(lambda (x y . rest) #f)))) |
| 80 |
|
| 81 |
(with-test-prefix "with pre-unwind handler" |
| 82 |
|
| 83 |
(pass-if "pre-unwind fluid state" |
| 84 |
(equal? '(inner outer arg) |
| 85 |
(let ((fluid-parm (make-fluid)) |
| 86 |
(inner-val #f)) |
| 87 |
(fluid-set! fluid-parm 'outer) |
| 88 |
(catch 'misc-exc |
| 89 |
(lambda () |
| 90 |
(with-fluids ((fluid-parm 'inner)) |
| 91 |
(throw 'misc-exc 'arg))) |
| 92 |
(lambda (key . args) |
| 93 |
(list inner-val |
| 94 |
(fluid-ref fluid-parm) |
| 95 |
(car args))) |
| 96 |
(lambda (key . args) |
| 97 |
(set! inner-val (fluid-ref fluid-parm)))))))) |
| 98 |
|
| 99 |
(throw-test "normal catch" |
| 100 |
'(1 2) |
| 101 |
(catch 'a |
| 102 |
(lambda () |
| 103 |
(push 1) |
| 104 |
(throw 'a)) |
| 105 |
(lambda (key . args) |
| 106 |
(push 2)))) |
| 107 |
|
| 108 |
(throw-test "catch and with-throw-handler" |
| 109 |
'(1 2 3 4) |
| 110 |
(catch 'a |
| 111 |
(lambda () |
| 112 |
(push 1) |
| 113 |
(with-throw-handler |
| 114 |
'a |
| 115 |
(lambda () |
| 116 |
(push 2) |
| 117 |
(throw 'a)) |
| 118 |
(lambda (key . args) |
| 119 |
(push 3)))) |
| 120 |
(lambda (key . args) |
| 121 |
(push 4)))) |
| 122 |
|
| 123 |
(throw-test "catch with rethrowing throw-handler" |
| 124 |
'(1 2 3 4) |
| 125 |
(catch 'a |
| 126 |
(lambda () |
| 127 |
(push 1) |
| 128 |
(with-throw-handler |
| 129 |
'a |
| 130 |
(lambda () |
| 131 |
(push 2) |
| 132 |
(throw 'a)) |
| 133 |
(lambda (key . args) |
| 134 |
(push 3) |
| 135 |
(apply throw key args)))) |
| 136 |
(lambda (key . args) |
| 137 |
(push 4)))) |
| 138 |
|
| 139 |
(throw-test "catch with pre-unwind handler" |
| 140 |
'(1 3 2) |
| 141 |
(catch 'a |
| 142 |
(lambda () |
| 143 |
(push 1) |
| 144 |
(throw 'a)) |
| 145 |
(lambda (key . args) |
| 146 |
(push 2)) |
| 147 |
(lambda (key . args) |
| 148 |
(push 3)))) |
| 149 |
|
| 150 |
(throw-test "catch with rethrowing pre-unwind handler" |
| 151 |
'(1 3 2) |
| 152 |
(catch 'a |
| 153 |
(lambda () |
| 154 |
(push 1) |
| 155 |
(throw 'a)) |
| 156 |
(lambda (key . args) |
| 157 |
(push 2)) |
| 158 |
(lambda (key . args) |
| 159 |
(push 3) |
| 160 |
(apply throw key args)))) |
| 161 |
|
| 162 |
(throw-test "catch with throw handler" |
| 163 |
'(1 2 3 4) |
| 164 |
(catch 'a |
| 165 |
(lambda () |
| 166 |
(push 1) |
| 167 |
(with-throw-handler 'a |
| 168 |
(lambda () |
| 169 |
(push 2) |
| 170 |
(throw 'a)) |
| 171 |
(lambda (key . args) |
| 172 |
(push 3)))) |
| 173 |
(lambda (key . args) |
| 174 |
(push 4)))) |
| 175 |
|
| 176 |
(throw-test "catch with rethrowing throw handler" |
| 177 |
'(1 2 3 4) |
| 178 |
(catch 'a |
| 179 |
(lambda () |
| 180 |
(push 1) |
| 181 |
(with-throw-handler 'a |
| 182 |
(lambda () |
| 183 |
(push 2) |
| 184 |
(throw 'a)) |
| 185 |
(lambda (key . args) |
| 186 |
(push 3) |
| 187 |
(apply throw key args)))) |
| 188 |
(lambda (key . args) |
| 189 |
(push 4)))) |
| 190 |
|
| 191 |
(throw-test "effect of with-throw-handler not-unwinding on throw to another key" |
| 192 |
'(1 2 3 5 4 6) |
| 193 |
(catch 'a |
| 194 |
(lambda () |
| 195 |
(push 1) |
| 196 |
(with-throw-handler 'b |
| 197 |
(lambda () |
| 198 |
(push 2) |
| 199 |
(catch 'a |
| 200 |
(lambda () |
| 201 |
(push 3) |
| 202 |
(throw 'b)) |
| 203 |
(lambda (key . args) |
| 204 |
(push 4)))) |
| 205 |
(lambda (key . args) |
| 206 |
(push 5) |
| 207 |
(throw 'a))) |
| 208 |
(push 6)) |
| 209 |
(lambda (key . args) |
| 210 |
(push 7)))) |
| 211 |
|
| 212 |
(throw-test "with-throw-handler chaining" |
| 213 |
'(1 2 3 4 6 8) |
| 214 |
(catch 'a |
| 215 |
(lambda () |
| 216 |
(push 1) |
| 217 |
(with-throw-handler 'a |
| 218 |
(lambda () |
| 219 |
(push 2) |
| 220 |
(with-throw-handler 'a |
| 221 |
(lambda () |
| 222 |
(push 3) |
| 223 |
(throw 'a)) |
| 224 |
(lambda (key . args) |
| 225 |
(push 4))) |
| 226 |
(push 5)) |
| 227 |
(lambda (key . args) |
| 228 |
(push 6))) |
| 229 |
(push 7)) |
| 230 |
(lambda (key . args) |
| 231 |
(push 8)))) |
| 232 |
|
| 233 |
(throw-test "throw handlers throwing to each other recursively" |
| 234 |
'(1 2 3 4 8 6 10 12) |
| 235 |
(catch #t |
| 236 |
(lambda () |
| 237 |
(push 1) |
| 238 |
(with-throw-handler 'a |
| 239 |
(lambda () |
| 240 |
(push 2) |
| 241 |
(with-throw-handler 'b |
| 242 |
(lambda () |
| 243 |
(push 3) |
| 244 |
(with-throw-handler 'c |
| 245 |
(lambda () |
| 246 |
(push 4) |
| 247 |
(throw 'b) |
| 248 |
(push 5)) |
| 249 |
(lambda (key . args) |
| 250 |
(push 6) |
| 251 |
(throw 'a))) |
| 252 |
(push 7)) |
| 253 |
(lambda (key . args) |
| 254 |
(push 8) |
| 255 |
(throw 'c))) |
| 256 |
(push 9)) |
| 257 |
(lambda (key . args) |
| 258 |
(push 10) |
| 259 |
(throw 'b))) |
| 260 |
(push 11)) |
| 261 |
(lambda (key . args) |
| 262 |
(push 12)))) |
| 263 |
|
| 264 |
(throw-test "throw handler throwing to lexically inside catch" |
| 265 |
'(1 2 7 5 4 6 9) |
| 266 |
(with-throw-handler 'a |
| 267 |
(lambda () |
| 268 |
(push 1) |
| 269 |
(catch 'b |
| 270 |
(lambda () |
| 271 |
(push 2) |
| 272 |
(throw 'a) |
| 273 |
(push 3)) |
| 274 |
(lambda (key . args) |
| 275 |
(push 4)) |
| 276 |
(lambda (key . args) |
| 277 |
(push 5))) |
| 278 |
(push 6)) |
| 279 |
(lambda (key . args) |
| 280 |
(push 7) |
| 281 |
(throw 'b) |
| 282 |
(push 8))) |
| 283 |
(push 9)) |
| 284 |
|
| 285 |
(throw-test "reuse of same throw handler after lexically inside catch" |
| 286 |
'(0 1 2 7 5 4 6 7 10) |
| 287 |
(catch 'b |
| 288 |
(lambda () |
| 289 |
(push 0) |
| 290 |
(with-throw-handler 'a |
| 291 |
(lambda () |
| 292 |
(push 1) |
| 293 |
(catch 'b |
| 294 |
(lambda () |
| 295 |
(push 2) |
| 296 |
(throw 'a) |
| 297 |
(push 3)) |
| 298 |
(lambda (key . args) |
| 299 |
(push 4)) |
| 300 |
(lambda (key . args) |
| 301 |
(push 5))) |
| 302 |
(push 6) |
| 303 |
(throw 'a)) |
| 304 |
(lambda (key . args) |
| 305 |
(push 7) |
| 306 |
(throw 'b) |
| 307 |
(push 8))) |
| 308 |
(push 9)) |
| 309 |
(lambda (key . args) |
| 310 |
(push 10)))) |
| 311 |
|
| 312 |
(throw-test "again but with two chained throw handlers" |
| 313 |
'(0 1 11 2 13 7 5 4 12 13 7 10) |
| 314 |
(catch 'b |
| 315 |
(lambda () |
| 316 |
(push 0) |
| 317 |
(with-throw-handler 'a |
| 318 |
(lambda () |
| 319 |
(push 1) |
| 320 |
(with-throw-handler 'a |
| 321 |
(lambda () |
| 322 |
(push 11) |
| 323 |
(catch 'b |
| 324 |
(lambda () |
| 325 |
(push 2) |
| 326 |
(throw 'a) |
| 327 |
(push 3)) |
| 328 |
(lambda (key . args) |
| 329 |
(push 4)) |
| 330 |
(lambda (key . args) |
| 331 |
(push 5))) |
| 332 |
(push 12) |
| 333 |
(throw 'a)) |
| 334 |
(lambda (key . args) |
| 335 |
(push 13))) |
| 336 |
(push 6)) |
| 337 |
(lambda (key . args) |
| 338 |
(push 7) |
| 339 |
(throw 'b))) |
| 340 |
(push 9)) |
| 341 |
(lambda (key . args) |
| 342 |
(push 10)))) |
| 343 |
|
| 344 |
) |
| 345 |
|
| 346 |
(with-test-prefix "false-if-exception" |
| 347 |
|
| 348 |
(pass-if (false-if-exception #t)) |
| 349 |
(pass-if (not (false-if-exception #f))) |
| 350 |
(pass-if (not (false-if-exception (error "xxx")))) |
| 351 |
|
| 352 |
;; Not yet working. |
| 353 |
;; |
| 354 |
;; (with-test-prefix "in empty environment" |
| 355 |
;; ;; an environment with no bindings at all |
| 356 |
;; (define empty-environment |
| 357 |
;; (make-module 1)) |
| 358 |
;; |
| 359 |
;; (pass-if "#t" |
| 360 |
;; (eval `(,false-if-exception #t) |
| 361 |
;; empty-environment)) |
| 362 |
;; (pass-if "#f" |
| 363 |
;; (not (eval `(,false-if-exception #f) |
| 364 |
;; empty-environment))) |
| 365 |
;; (pass-if "exception" |
| 366 |
;; (not (eval `(,false-if-exception (,error "xxx")) |
| 367 |
;; empty-environment)))) |
| 368 |
) |
| 369 |
|
| 370 |
(with-test-prefix "delimited exception handlers" |
| 371 |
(define (catch* key thunk) |
| 372 |
(let ((tag (make-prompt-tag))) |
| 373 |
(call-with-prompt tag |
| 374 |
(lambda () |
| 375 |
(catch key |
| 376 |
(lambda () |
| 377 |
(abort-to-prompt tag) |
| 378 |
(thunk)) |
| 379 |
(lambda args args))) |
| 380 |
(lambda (k) k)))) |
| 381 |
(pass-if-equal '(foo) |
| 382 |
(let ((thunk (catch* 'foo (lambda () (throw 'foo))))) |
| 383 |
(thunk))) |
| 384 |
(pass-if-equal '(foo) |
| 385 |
(let* ((thunk1 (catch* 'foo (lambda () (throw 'foo)))) |
| 386 |
(thunk2 (catch* 'bar (lambda () (thunk1))))) |
| 387 |
(thunk1))) |
| 388 |
(pass-if-equal '(foo) |
| 389 |
(let* ((thunk1 (catch* 'foo (lambda () (throw 'foo)))) |
| 390 |
(thunk2 (catch* 'bar (lambda () (thunk1))))) |
| 391 |
(thunk2))) |
| 392 |
(pass-if-equal '(bar) |
| 393 |
(let* ((thunk1 (catch* 'foo (lambda () (throw 'bar)))) |
| 394 |
(thunk2 (catch* 'bar (lambda () (thunk1))))) |
| 395 |
(thunk2)))) |
| 396 |
|
| 397 |
(with-test-prefix "throwing within exception handlers" |
| 398 |
(pass-if "https://github.com/wingo/fibers/issues/76" |
| 399 |
(let/ec return |
| 400 |
(with-exception-handler |
| 401 |
(lambda (e) |
| 402 |
(catch #t |
| 403 |
(lambda () (error "bar")) |
| 404 |
(lambda args #f)) |
| 405 |
(return #t)) |
| 406 |
(lambda () (error "foo")))))) |