r6rs-exceptions.test 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. ;;; r6rs-exceptions.test --- Test suite for R6RS (rnrs exceptions) -*- scheme -*-
  2. ;; Copyright (C) 2010, 2013 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. (define-module (test-suite test-rnrs-exceptions)
  18. :use-module ((rnrs conditions) :version (6))
  19. :use-module ((rnrs exceptions) :version (6))
  20. :use-module (system foreign)
  21. :use-module (test-suite lib))
  22. (with-test-prefix "with-exception-handler"
  23. (pass-if "handler invoked on raise"
  24. (let ((success #f))
  25. (call/cc
  26. (lambda (continuation)
  27. (with-exception-handler
  28. (lambda (condition) (set! success #t) (continuation))
  29. (lambda () (raise (make-violation))))))
  30. success))
  31. (pass-if "handler not invoked unless raise"
  32. (let ((success #f))
  33. (call/cc
  34. (lambda (continuation)
  35. (with-exception-handler
  36. (lambda (condition) (continuation))
  37. (lambda () (set! success #t)))))
  38. success)))
  39. (with-test-prefix "raise"
  40. (pass-if "raise causes &non-continuable after handler"
  41. (let ((success #f))
  42. (call/cc
  43. (lambda (continuation)
  44. (with-exception-handler
  45. (lambda (condition)
  46. (set! success (non-continuable-violation? condition))
  47. (continuation))
  48. (lambda ()
  49. (with-exception-handler
  50. (lambda (condition) #f)
  51. (lambda () (raise (make-violation))))))))
  52. success)))
  53. (with-test-prefix "raise-continuable"
  54. (pass-if "raise-continuable invokes continuation after handler"
  55. (let ((handled #f)
  56. (continued #f))
  57. (call/cc
  58. (lambda (continuation)
  59. (with-exception-handler
  60. (lambda (condition) (set! handled #t))
  61. (lambda ()
  62. (raise-continuable (make-violation))
  63. (set! continued #t)))))
  64. (and handled continued))))
  65. (with-test-prefix "guard"
  66. (pass-if "guard with matching cond without else"
  67. (let ((success #f))
  68. (guard (condition ((error? condition) (set! success #t)))
  69. (raise (make-error)))
  70. success))
  71. (pass-if "guard without matching cond without else"
  72. (let ((success #f))
  73. (call/cc
  74. (lambda (continuation)
  75. (with-exception-handler
  76. (lambda (condition) (set! success (error? condition)) (continuation))
  77. (lambda ()
  78. (guard (condition ((irritants-condition? condition) #f))
  79. (raise (make-error)))))))
  80. success))
  81. (pass-if "guard with else and without matching cond"
  82. (let ((success #f))
  83. (guard (condition ((irritants-condition? condition) #f)
  84. (else (set! success #t)))
  85. (raise (make-error)))
  86. success))
  87. (pass-if "guard with cond => syntax"
  88. (guard (condition (condition => error?)) (raise (make-error)))))
  89. (with-test-prefix "guile condition conversions"
  90. (define-syntax-rule (pass-if-condition name expected-condition? body ...)
  91. (pass-if name
  92. (guard (obj ((expected-condition? obj) #t)
  93. (else #f))
  94. body ... #f)))
  95. (pass-if "rethrown native guile exceptions"
  96. (catch #t
  97. (lambda ()
  98. (guard (obj ((syntax-violation? obj) #f))
  99. (vector-ref '#(0 1) 2)
  100. #f))
  101. (lambda (key . args)
  102. (eq? key 'out-of-range))))
  103. (pass-if-condition "syntax-error"
  104. syntax-violation?
  105. (eval '(let) (current-module)))
  106. (pass-if-condition "unbound-variable"
  107. undefined-violation?
  108. variable-that-does-not-exist)
  109. (pass-if-condition "out-of-range"
  110. assertion-violation?
  111. (vector-ref '#(0 1) 2))
  112. (pass-if-condition "wrong-number-of-args"
  113. assertion-violation?
  114. ((lambda () #f) 'unwanted-argument))
  115. (pass-if-condition "wrong-type-arg"
  116. assertion-violation?
  117. (vector-ref '#(0 1) 'invalid-index))
  118. (pass-if-condition "keyword-argument-error"
  119. assertion-violation?
  120. ((lambda* (#:key a) #f) #:unwanted-keyword 'val))
  121. (pass-if-condition "regular-expression-syntax"
  122. assertion-violation?
  123. (make-regexp "[missing-close-square-bracket"))
  124. (pass-if-condition "null-pointer-error"
  125. assertion-violation?
  126. (dereference-pointer (make-pointer 0)))
  127. (pass-if-condition "read-error"
  128. lexical-violation?
  129. (read (open-input-string "(missing-close-paren"))))
  130. ;;; Local Variables:
  131. ;;; eval: (put 'pass-if-condition 'scheme-indent-function 1)
  132. ;;; End: