test-exceptions.scm 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. ;;; Copyright (C) 2023, 2024 Igalia, S.L.
  2. ;;;
  3. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  4. ;;; you may not use this file except in compliance with the License.
  5. ;;; You may obtain a copy of the License at
  6. ;;;
  7. ;;; http://www.apache.org/licenses/LICENSE-2.0
  8. ;;;
  9. ;;; Unless required by applicable law or agreed to in writing, software
  10. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  11. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. ;;; See the License for the specific language governing permissions and
  13. ;;; limitations under the License.
  14. ;;; Commentary:
  15. ;;;
  16. ;;; Exception tests.
  17. ;;;
  18. ;;; Code:
  19. (use-modules (hoot compile)
  20. (hoot reflect)
  21. (srfi srfi-64)
  22. (test utils))
  23. (test-begin "test-exceptions")
  24. (test-call "79" (lambda ()
  25. (with-exception-handler
  26. (lambda (exn) 42)
  27. (lambda () (+ 10 69)))))
  28. (test-call "52" (lambda ()
  29. (with-exception-handler
  30. (lambda (exn) 42)
  31. (lambda () (+ 10 (raise-continuable 69))))))
  32. (test-call "42" (lambda ()
  33. (with-exception-handler
  34. (lambda (exn) 42)
  35. (lambda () (+ 10 (raise-continuable 69)))
  36. #:unwind? #t)))
  37. (test-call "69" (lambda ()
  38. (with-exception-handler
  39. (lambda (exn) exn)
  40. (lambda () (+ 10 (raise-continuable 69)))
  41. #:unwind? #t)))
  42. (test-call "42" (lambda ()
  43. (with-exception-handler
  44. (lambda (exn) 42)
  45. (lambda () (+ 10 (raise 69)))
  46. #:unwind? #t)))
  47. (test-call "69" (lambda ()
  48. (with-exception-handler
  49. (lambda (exn) exn)
  50. (lambda () (+ 10 (raise 69)))
  51. #:unwind? #t)))
  52. (test-call "42" (lambda ()
  53. (with-exception-handler
  54. (lambda (exn) 42)
  55. (lambda () (error "what"))
  56. #:unwind? #t)))
  57. (with-additional-imports
  58. ((hoot exceptions))
  59. (test-call "#(#t \"hey\" (ho))"
  60. (lambda (message irritants)
  61. (let ((exn (make-compound-exception
  62. (list
  63. (make-exception-with-message message)
  64. (make-exception-with-irritants irritants)))))
  65. (vector (error-object? exn)
  66. (error-object-message exn)
  67. (error-object-irritants exn))))
  68. "hey"
  69. '(ho)))
  70. (test-call "42"
  71. (lambda ()
  72. (guard (condition
  73. ((assq 'a condition) => cdr)
  74. ((assq 'b condition)))
  75. (raise (list (cons 'a 42))))))
  76. (test-call "(b . 23)"
  77. (lambda ()
  78. (guard (condition
  79. ((assq 'a condition) => cdr)
  80. ((assq 'b condition)))
  81. (raise (list (cons 'b 23))))))
  82. ;; Exception thrown from stdlib
  83. (test-call "42"
  84. (lambda (x y)
  85. (with-exception-handler
  86. (lambda (exn) 42)
  87. (lambda () (+ x y))
  88. #:unwind? #t))
  89. 1 "two")
  90. ;; Unwind for type tests
  91. (with-additional-imports ((hoot exceptions))
  92. ;; Simple exception
  93. (test-call "42"
  94. (lambda ()
  95. (with-exception-handler (lambda (exn) 42)
  96. (lambda ()
  97. (with-exception-handler (lambda (exn) 69)
  98. (lambda ()
  99. (raise (make-assertion-violation)))
  100. #:unwind? #t
  101. #:unwind-for-type &error))
  102. #:unwind? #t
  103. #:unwind-for-type &assertion)))
  104. ;; Parent type of simple exception
  105. (test-call "42"
  106. (lambda ()
  107. (with-exception-handler (lambda (exn) 42)
  108. (lambda ()
  109. (with-exception-handler (lambda (exn) 69)
  110. (lambda ()
  111. (raise (make-assertion-violation)))
  112. #:unwind? #t
  113. #:unwind-for-type &error))
  114. #:unwind? #t
  115. #:unwind-for-type &violation)))
  116. ;; Compound exception
  117. (test-call "42"
  118. (lambda ()
  119. (with-exception-handler (lambda (exn) 42)
  120. (lambda ()
  121. (with-exception-handler (lambda (exn) 69)
  122. (lambda ()
  123. (raise
  124. (make-exception (make-assertion-violation)
  125. (make-exception-with-message "test"))))
  126. #:unwind? #t
  127. #:unwind-for-type &error))
  128. #:unwind? #t
  129. #:unwind-for-type &assertion)))
  130. ;; Parent type of a component of a compound exception.
  131. (test-call "42"
  132. (lambda ()
  133. (with-exception-handler (lambda (exn) 42)
  134. (lambda ()
  135. (with-exception-handler (lambda (exn) 69)
  136. (lambda ()
  137. (raise
  138. (make-exception (make-assertion-violation)
  139. (make-exception-with-message "test"))))
  140. #:unwind? #t
  141. #:unwind-for-type &error))
  142. #:unwind? #t
  143. #:unwind-for-type &violation))))
  144. (test-equal "re-entrant exception handling"
  145. 42
  146. (compile-value '(let ()
  147. (define-foreign callback
  148. "host" "callback"
  149. (ref null extern) -> none)
  150. (with-exception-handler (lambda (exn) 42)
  151. (lambda ()
  152. (callback
  153. (procedure->external
  154. (lambda () (error "uh oh")))))
  155. #:unwind? #t))
  156. #:imports `(,@%default-program-imports
  157. (hoot exceptions)
  158. (hoot ffi))
  159. #:wasm-imports
  160. `(("host" .
  161. (("callback" .
  162. ,(lambda (proc)
  163. (proc))))))))
  164. (test-end* "test-exceptions")