srfi-34.test 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. ;;;; srfi-34.test --- test suite for SRFI-34 -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2003, 2004, 2006, 2008 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This program is free software; you can redistribute it and/or modify
  6. ;;;; it under the terms of the GNU General Public License as published by
  7. ;;;; the Free Software Foundation; either version 2, or (at your option)
  8. ;;;; any later version.
  9. ;;;;
  10. ;;;; This program is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;;;; GNU General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU General Public License
  16. ;;;; along with this software; see the file COPYING. If not, write to
  17. ;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. ;;;; Boston, MA 02110-1301 USA
  19. (define-module (test-suite test-srfi-34)
  20. :duplicates (last) ;; avoid warning about srfi-34 replacing `raise'
  21. :use-module (test-suite lib)
  22. :use-module (srfi srfi-13)
  23. :use-module (srfi srfi-34))
  24. (define (expr-prints-and-evals-to? expr printout result)
  25. (let ((actual-result *unspecified*))
  26. (let ((actual-printout
  27. (string-trim-both
  28. (with-output-to-string
  29. (lambda ()
  30. (set! actual-result
  31. (eval expr (current-module))))))))
  32. ;;(write (list actual-printout printout actual-result result))
  33. ;;(newline)
  34. (and (equal? actual-printout printout)
  35. (equal? actual-result result)))))
  36. (with-test-prefix "SRFI 34"
  37. (pass-if "cond-expand"
  38. (cond-expand (srfi-34 #t)
  39. (else #f)))
  40. (pass-if "example 1"
  41. (expr-prints-and-evals-to?
  42. '(call-with-current-continuation
  43. (lambda (k)
  44. (with-exception-handler (lambda (x)
  45. (display "condition: ")
  46. (write x)
  47. (newline)
  48. (k 'exception))
  49. (lambda ()
  50. (+ 1 (raise 'an-error))))))
  51. "condition: an-error"
  52. 'exception))
  53. ;; SRFI 34 specifies that the behaviour of the call/cc expression
  54. ;; after printing "something went wrong" is unspecified, which is
  55. ;; tricky to test for in a positive way ... Guile behaviour at time
  56. ;; of writing is to signal a "lazy-catch handler did return" error,
  57. ;; which feels about right to me.
  58. (pass-if "example 2"
  59. (expr-prints-and-evals-to?
  60. '(false-if-exception
  61. (call-with-current-continuation
  62. (lambda (k)
  63. (with-exception-handler (lambda (x)
  64. (display "something went wrong")
  65. (newline)
  66. 'dont-care)
  67. (lambda ()
  68. (+ 1 (raise 'an-error)))))))
  69. "something went wrong"
  70. #f))
  71. (pass-if "example 3"
  72. (expr-prints-and-evals-to?
  73. '(guard (condition
  74. (else
  75. (display "condition: ")
  76. (write condition)
  77. (newline)
  78. 'exception))
  79. (+ 1 (raise 'an-error)))
  80. "condition: an-error"
  81. 'exception))
  82. (pass-if "example 4"
  83. (expr-prints-and-evals-to?
  84. '(guard (condition
  85. (else
  86. (display "something went wrong")
  87. (newline)
  88. 'dont-care))
  89. (+ 1 (raise 'an-error)))
  90. "something went wrong"
  91. 'dont-care))
  92. (pass-if "example 5"
  93. (expr-prints-and-evals-to?
  94. '(call-with-current-continuation
  95. (lambda (k)
  96. (with-exception-handler (lambda (x)
  97. (display "reraised ") (write x) (newline)
  98. (k 'zero))
  99. (lambda ()
  100. (guard (condition
  101. ((positive? condition) 'positive)
  102. ((negative? condition) 'negative))
  103. (raise 1))))))
  104. ""
  105. 'positive))
  106. (pass-if "example 6"
  107. (expr-prints-and-evals-to?
  108. '(call-with-current-continuation
  109. (lambda (k)
  110. (with-exception-handler (lambda (x)
  111. (display "reraised ") (write x) (newline)
  112. (k 'zero))
  113. (lambda ()
  114. (guard (condition
  115. ((positive? condition) 'positive)
  116. ((negative? condition) 'negative))
  117. (raise -1))))))
  118. ""
  119. 'negative))
  120. (pass-if "example 7"
  121. (expr-prints-and-evals-to?
  122. '(call-with-current-continuation
  123. (lambda (k)
  124. (with-exception-handler (lambda (x)
  125. (display "reraised ") (write x) (newline)
  126. (k 'zero))
  127. (lambda ()
  128. (guard (condition
  129. ((positive? condition) 'positive)
  130. ((negative? condition) 'negative))
  131. (raise 0))))))
  132. "reraised 0"
  133. 'zero))
  134. (pass-if "example 8"
  135. (expr-prints-and-evals-to?
  136. '(guard (condition
  137. ((assq 'a condition) => cdr)
  138. ((assq 'b condition)))
  139. (raise (list (cons 'a 42))))
  140. ""
  141. 42))
  142. (pass-if "example 9"
  143. (expr-prints-and-evals-to?
  144. '(guard (condition
  145. ((assq 'a condition) => cdr)
  146. ((assq 'b condition)))
  147. (raise (list (cons 'b 23))))
  148. ""
  149. '(b . 23)))
  150. (pass-if "`with-exception-handler' invokes HANDLER in THUNK's dynamic env."
  151. ;; In Guile 1.8.5 and earlier, unwinders would be called before
  152. ;; the exception handler, which reads "The handler is called in
  153. ;; the dynamic environment of the call to `raise'".
  154. (call/cc
  155. (lambda (return)
  156. (let ((inside? #f))
  157. (with-exception-handler
  158. (lambda (c)
  159. ;; This handler must be called before the unwinder below.
  160. (return inside?))
  161. (lambda ()
  162. (dynamic-wind
  163. (lambda ()
  164. (set! inside? #t))
  165. (lambda ()
  166. (raise 'some-exception))
  167. (lambda ()
  168. ;; This unwinder should not be executed before the
  169. ;; handler is called.
  170. (set! inside? #f))))))))))