summaryrefslogtreecommitdiff
path: root/test-suite/tests/r6rs-conditions.test
blob: 7480b9c47fdaef380540d6f69b20ee1c6b589a2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
;;; r6rs-conditions.test --- Test suite for R6RS (rnrs conditions)

;;      Copyright (C) 2010 Free Software Foundation, Inc.
;;
;; This library is free software; you can redistribute it and/or
;; modify it under the terms of the GNU Lesser General Public
;; License as published by the Free Software Foundation; either
;; version 3 of the License, or (at your option) any later version.
;; 
;; This library is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; Lesser General Public License for more details.
;; 
;; You should have received a copy of the GNU Lesser General Public
;; License along with this library; if not, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA


(define-module (test-suite test-rnrs-conditions)
  :use-module ((rnrs base) :version (6))
  :use-module ((rnrs conditions) :version (6))
  :use-module (test-suite lib))

(define-condition-type &a &condition make-a-condition a-condition? (foo a-foo))
(define-condition-type &b &condition make-b-condition b-condition? (bar b-bar))
(define-condition-type &c &condition make-c-condition c-condition?
  (baz c-baz)
  (qux c-qux)
  (frobotz c-frobotz))

(with-test-prefix "condition?"
  (pass-if "condition? is #t for simple conditions"
    (condition? (make-error)))

  (pass-if "condition? is #t for compound conditions"
    (condition? (condition (make-error) (make-assertion-violation))))

  (pass-if "condition? is #f for non-conditions"
    (not (condition? 'foo))))

(with-test-prefix "simple-conditions"
  (pass-if "simple-conditions returns condition components"
    (let* ((error (make-error))
	   (assertion (make-assertion-violation))
	   (c (condition error assertion))
	   (scs (simple-conditions c)))
      (equal? scs (list error assertion))))

  (pass-if "simple-conditions flattens compound conditions"
    (let* ((implementation-restriction 
	    (make-implementation-restriction-violation))
	   (error1 (make-error))
	   (c1 (condition implementation-restriction error1))
	   (error2 (make-error))
	   (assertion (make-assertion-violation))
	   (c2 (condition error2 assertion c1))
	   (scs (simple-conditions c2)))
      (equal? scs (list error2 assertion implementation-restriction error1)))))

(with-test-prefix "condition-predicate"
  (pass-if "returned procedure identifies matching simple conditions"
    (let ((mp (condition-predicate &message))
	  (mc (make-message-condition "test")))
      (mp mc)))

  (pass-if "returned procedure identifies matching compound conditions"
    (let* ((sp (condition-predicate &serious))
	   (vp (condition-predicate &violation))
	   (sc (make-serious-condition))
	   (vc (make-violation))
	   (c (condition sc vc)))
      (and (sp c) (vp c))))

  (pass-if "returned procedure is #f for non-matching simple"
    (let ((sp (condition-predicate &serious)))
      (not (sp 'foo))))

  (pass-if "returned procedure is #f for compound without match"
    (let* ((ip (condition-predicate &irritants))
	   (sc (make-serious-condition))
	   (vc (make-violation))
	   (c (condition sc vc)))
      (not (ip c)))))

(with-test-prefix "condition-accessor"
  (pass-if "accessor applies proc to field from simple condition"
    (let* ((proc (lambda (c) (condition-message c)))
	   (ma (condition-accessor &message proc))
	   (mc (make-message-condition "foo")))
      (equal? (ma mc) "foo")))

  (pass-if "accessor applies proc to field from compound condition"
    (let* ((proc (lambda (c) (condition-message c)))
	   (ma (condition-accessor &message proc))
	   (mc (make-message-condition "foo"))
	   (vc (make-violation))
	   (c (condition vc mc)))
      (equal? (ma c) "foo"))))

(with-test-prefix "define-condition-type"
  (pass-if "define-condition-type produces proper accessors"
    (let ((c (condition (make-a-condition 'foo) (make-b-condition 'bar))))
      (and (eq? (a-foo c) 'foo) (eq? (b-bar c) 'bar))))
  (pass-if "define-condition-type works for multiple fields"
    (let ((c (condition (make-a-condition 'foo)
                        (make-c-condition 1 2 3))))
      (and (eq? (a-foo c) 'foo)
           (= (c-baz c) 1)
           (= (c-qux c) 2)
           (= (c-frobotz c) 3)))))