srfi-34.test 5.6 KB

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