koans, looks like exception handling with signals

This commit is contained in:
efim
2022-08-07 16:13:43 +00:00
parent 6f09e0019c
commit f4759c5685
2 changed files with 41 additions and 36 deletions

View File

@@ -37,25 +37,26 @@
(define-test type-hierarchy
;; Inheritance for condition types works the same way as for classes.
(let ((condition (make-condition 'my-condition)))
(true-or-false? ____ (typep condition 'my-condition))
(true-or-false? ____ (typep condition 'condition))
(true-or-false? ____ (typep condition 'warning))
(true-or-false? ____ (typep condition 'error)))
(true-or-false? t (typep condition 'my-condition))
(true-or-false? t (typep condition 'condition))
(true-or-false? nil (typep condition 'warning)) ; nope
(true-or-false? nil (typep condition 'error))) ; nope
(let ((condition (make-condition 'my-warning)))
(true-or-false? ____ (typep condition 'my-warning))
(true-or-false? ____ (typep condition 'warning))
(true-or-false? ____ (typep condition 'error)))
(true-or-false? t (typep condition 'my-warning))
(true-or-false? t (typep condition 'warning))
(true-or-false? nil (typep condition 'error)))
(let ((condition (make-condition 'my-serious-condition)))
(true-or-false? ____ (typep condition 'my-serious-condition))
(true-or-false? ____ (typep condition 'serious-condition))
(true-or-false? ____ (typep condition 'warning))
(true-or-false? ____ (typep condition 'error)))
(true-or-false? t (typep condition 'my-serious-condition))
(true-or-false? t (typep condition 'serious-condition))
(true-or-false? nil (typep condition 'warning))
(true-or-false? nil (typep condition 'error)))
(let ((condition (make-condition 'my-error)))
(true-or-false? ____ (typep condition 'my-error))
(true-or-false? ____ (typep condition 'my-serious-condition))
(true-or-false? ____ (typep condition 'serious-condition))
(true-or-false? ____ (typep condition 'warning))
(true-or-false? ____ (typep condition 'error))))
(true-or-false? t (typep condition 'my-error))
(true-or-false? nil (typep condition 'my-serious-condition))
(true-or-false? t (typep condition 'serious-condition)) ; not on first try
(true-or-false? nil (typep condition 'warning))
(true-or-false? t (typep condition 'error))))
;; heh
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -90,16 +91,17 @@
(silly-condition #'handle-silly-condition)
(most-silly-condition #'handle-most-silly-condition))
(signal (make-condition 'most-silly-condition)))
(assert-equal ____ *list*)))
(assert-equal '(:most-silly-condition :silly-condition :very-silly-condition) *list*)))
;; list is reverse order of functions in #'handler-bind
(define-test handler-order
;; The order of binding handlers matters.
(let ((*list* '()))
(handler-bind ((silly-condition #'handle-silly-condition)
(very-silly-condition #'handle-very-silly-condition)
(most-silly-condition #'handle-most-silly-condition))
(signal (make-condition 'most-silly-condition)))
(assert-equal ____ *list*)))
;; The order of binding handlers matters.
(let ((*list* '()))
(handler-bind ((silly-condition #'handle-silly-condition)
(very-silly-condition #'handle-very-silly-condition)
(most-silly-condition #'handle-most-silly-condition))
(signal (make-condition 'most-silly-condition)))
(assert-equal ____ *list*)))
(define-test multiple-handler-binds
;; It is possible to bind handlers in steps.