|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-module (test-suite test-srfi-34) |
|
:duplicates (last) |
|
:use-module (test-suite lib) |
|
:use-module (srfi srfi-13) |
|
:use-module (srfi srfi-34)) |
|
|
|
(define (expr-prints-and-evals-to? expr printout result) |
|
(let ((actual-result *unspecified*)) |
|
(let ((actual-printout |
|
(string-trim-both |
|
(with-output-to-string |
|
(lambda () |
|
(set! actual-result |
|
(eval expr (current-module)))))))) |
|
|
|
|
|
(and (equal? actual-printout printout) |
|
(equal? actual-result result))))) |
|
|
|
(with-test-prefix "SRFI 34" |
|
|
|
(pass-if "cond-expand" |
|
(cond-expand (srfi-34 #t) |
|
(else #f))) |
|
|
|
(pass-if "example 1" |
|
(expr-prints-and-evals-to? |
|
'(call-with-current-continuation |
|
(lambda (k) |
|
(with-exception-handler (lambda (x) |
|
(display "condition: ") |
|
(write x) |
|
(newline) |
|
(k 'exception)) |
|
(lambda () |
|
(+ 1 (raise 'an-error)))))) |
|
"condition: an-error" |
|
'exception)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if "example 2" |
|
(expr-prints-and-evals-to? |
|
'(false-if-exception |
|
(call-with-current-continuation |
|
(lambda (k) |
|
(with-exception-handler (lambda (x) |
|
(display "something went wrong") |
|
(newline) |
|
'dont-care) |
|
(lambda () |
|
(+ 1 (raise 'an-error))))))) |
|
"something went wrong" |
|
#f)) |
|
|
|
(pass-if "example 3" |
|
(expr-prints-and-evals-to? |
|
'(guard (condition |
|
(else |
|
(display "condition: ") |
|
(write condition) |
|
(newline) |
|
'exception)) |
|
(+ 1 (raise 'an-error))) |
|
"condition: an-error" |
|
'exception)) |
|
|
|
(pass-if "example 4" |
|
(expr-prints-and-evals-to? |
|
'(guard (condition |
|
(else |
|
(display "something went wrong") |
|
(newline) |
|
'dont-care)) |
|
(+ 1 (raise 'an-error))) |
|
"something went wrong" |
|
'dont-care)) |
|
|
|
(pass-if "example 5" |
|
(expr-prints-and-evals-to? |
|
'(call-with-current-continuation |
|
(lambda (k) |
|
(with-exception-handler (lambda (x) |
|
(display "reraised ") (write x) (newline) |
|
(k 'zero)) |
|
(lambda () |
|
(guard (condition |
|
((positive? condition) 'positive) |
|
((negative? condition) 'negative)) |
|
(raise 1)))))) |
|
"" |
|
'positive)) |
|
|
|
(pass-if "example 6" |
|
(expr-prints-and-evals-to? |
|
'(call-with-current-continuation |
|
(lambda (k) |
|
(with-exception-handler (lambda (x) |
|
(display "reraised ") (write x) (newline) |
|
(k 'zero)) |
|
(lambda () |
|
(guard (condition |
|
((positive? condition) 'positive) |
|
((negative? condition) 'negative)) |
|
(raise -1)))))) |
|
"" |
|
'negative)) |
|
|
|
(pass-if "example 7" |
|
(expr-prints-and-evals-to? |
|
'(call-with-current-continuation |
|
(lambda (k) |
|
(with-exception-handler (lambda (x) |
|
(display "reraised ") (write x) (newline) |
|
(k 'zero)) |
|
(lambda () |
|
(guard (condition |
|
((positive? condition) 'positive) |
|
((negative? condition) 'negative)) |
|
(raise 0)))))) |
|
"reraised 0" |
|
'zero)) |
|
|
|
(pass-if "example 8" |
|
(expr-prints-and-evals-to? |
|
'(guard (condition |
|
((assq 'a condition) => cdr) |
|
((assq 'b condition))) |
|
(raise (list (cons 'a 42)))) |
|
"" |
|
42)) |
|
|
|
(pass-if "example 9" |
|
(expr-prints-and-evals-to? |
|
'(guard (condition |
|
((assq 'a condition) => cdr) |
|
((assq 'b condition))) |
|
(raise (list (cons 'b 23)))) |
|
"" |
|
'(b . 23))) |
|
|
|
(pass-if "`with-exception-handler' invokes HANDLER in THUNK's dynamic env." |
|
|
|
|
|
|
|
(call/cc |
|
(lambda (return) |
|
(let ((inside? #f)) |
|
(with-exception-handler |
|
(lambda (c) |
|
|
|
(return inside?)) |
|
(lambda () |
|
(dynamic-wind |
|
(lambda () |
|
(set! inside? #t)) |
|
(lambda () |
|
(raise 'some-exception)) |
|
(lambda () |
|
|
|
|
|
(set! inside? #f)))))))))) |
|
|