1 ;;;; srfi-35.test --- Test suite for SRFI-35 -*- Scheme -*-
2 ;;;; Ludovic Courtès <ludo@gnu.org>
4 ;;;; Copyright (C) 2007, 2008 Free Software Foundation, Inc.
6 ;;;; This program is free software; you can redistribute it and/or modify
7 ;;;; it under the terms of the GNU General Public License as published by
8 ;;;; the Free Software Foundation; either version 2, or (at your option)
9 ;;;; any later version.
11 ;;;; This program is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;;; GNU General Public License for more details.
16 ;;;; You should have received a copy of the GNU General Public License
17 ;;;; along with this software; see the file COPYING. If not, write to
18 ;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 ;;;; Boston, MA 02110-1301 USA
21 (define-module (test-srfi-35)
22 :use-module (test-suite lib)
23 :use-module (srfi srfi-35))
26 (with-test-prefix "cond-expand"
28 (cond-expand (srfi-35 #t)
32 (with-test-prefix "condition types"
34 (condition-type? &condition))
36 (pass-if "make-condition-type"
37 (condition-type? (make-condition-type 'foo &condition '(a b)))))
41 (with-test-prefix "conditions"
44 (let ((c (make-condition &condition)))
46 (condition-has-type? c &condition))))
48 (pass-if "simple condition"
49 (let* ((ct (make-condition-type 'chbouib &condition '(a b)))
50 (c (make-condition ct 'b 1 'a 0)))
52 (condition-has-type? c ct))))
54 (pass-if "simple condition with inheritance"
55 (let* ((top (make-condition-type 'foo &condition '(a b)))
56 (ct (make-condition-type 'bar top '(c d)))
57 (c (make-condition ct 'a 1 'b 2 'c 3 'd 4)))
59 (condition-has-type? c ct)
60 (condition-has-type? c top))))
62 (pass-if "condition-ref"
63 (let* ((ct (make-condition-type 'chbouib &condition '(a b)))
64 (c (make-condition ct 'b 1 'a 0)))
65 (and (eq? (condition-ref c 'a) 0)
66 (eq? (condition-ref c 'b) 1))))
68 (pass-if "condition-ref with inheritance"
69 (let* ((top (make-condition-type 'foo &condition '(a b)))
70 (ct (make-condition-type 'bar top '(c d)))
71 (c (make-condition ct 'b 1 'a 0 'd 3 'c 2)))
72 (and (eq? (condition-ref c 'a) 0)
73 (eq? (condition-ref c 'b) 1)
74 (eq? (condition-ref c 'c) 2)
75 (eq? (condition-ref c 'd) 3))))
77 (pass-if "extract-condition"
78 (let* ((ct (make-condition-type 'chbouib &condition '(a b)))
79 (c (make-condition ct 'b 1 'a 0)))
80 (equal? c (extract-condition c ct)))))
83 (with-test-prefix "compound conditions"
84 (pass-if "condition-has-type?"
85 (let* ((t1 (make-condition-type 'foo &condition '(a b)))
86 (t2 (make-condition-type 'bar &condition '(c d)))
87 (c1 (make-condition t1 'a 0 'b 1))
88 (c2 (make-condition t2 'c 2 'd 3))
89 (c (make-compound-condition c1 c2)))
91 (condition-has-type? c t1)
92 (condition-has-type? c t2))))
94 (pass-if "condition-ref"
95 (let* ((t1 (make-condition-type 'foo &condition '(a b)))
96 (t2 (make-condition-type 'bar &condition '(c d)))
97 (c1 (make-condition t1 'a 0 'b 1))
98 (c2 (make-condition t2 'c 2 'd 3))
99 (c (make-compound-condition c1 c2)))
100 (equal? (map (lambda (field)
101 (condition-ref c field))
105 (pass-if "condition-ref with same-named fields"
106 (let* ((t1 (make-condition-type 'foo &condition '(a b)))
107 (t2 (make-condition-type 'bar &condition '(a c d)))
108 (c1 (make-condition t1 'a 0 'b 1))
109 (c2 (make-condition t2 'a -1 'c 2 'd 3))
110 (c (make-compound-condition c1 c2)))
111 (equal? (map (lambda (field)
112 (condition-ref c field))
116 (pass-if "extract-condition"
117 (let* ((t1 (make-condition-type 'foo &condition '(a b)))
118 (t2 (make-condition-type 'bar &condition '(c d)))
119 (c1 (make-condition t1 'a 0 'b 1))
120 (c2 (make-condition t2 'c 2 'd 3))
121 (c (make-compound-condition c1 c2)))
122 (and (equal? c1 (extract-condition c t1))
123 (equal? c2 (extract-condition c t2)))))
125 (pass-if "extract-condition with same-named fields"
126 (let* ((t1 (make-condition-type 'foo &condition '(a b)))
127 (t2 (make-condition-type 'bar &condition '(a c)))
128 (c1 (make-condition t1 'a 0 'b 1))
129 (c2 (make-condition t2 'a -1 'c 2))
130 (c (make-compound-condition c1 c2)))
131 (and (equal? c1 (extract-condition c t1))
132 (equal? c2 (extract-condition c t2))))))
136 (with-test-prefix "syntax"
137 (pass-if "define-condition-type"
138 (let ((m (current-module)))
139 (eval '(define-condition-type &chbouib &condition
144 (eval '(and (condition-type? &chbouib)
145 (procedure? chbouib?)
146 (let ((c (make-condition &chbouib 'one 1 'two 2)))
149 (eq? (chbouib-one c) 1)
150 (eq? (chbouib-two c) 2))))
154 (let* ((t (make-condition-type 'chbouib &condition '(a b)))
155 (c (condition (t (b 2) (a 1)))))
157 (condition-has-type? c t)
158 (equal? (map (lambda (f)
163 (pass-if-exception "condition with missing fields"
164 exception:miscellaneous-error
165 (let ((t (make-condition-type 'chbouib &condition '(a b c))))
166 (condition (t (a 1) (b 2)))))
168 (pass-if "compound condition"
169 (let* ((t1 (make-condition-type 'foo &condition '(a b)))
170 (t2 (make-condition-type 'bar &condition '(c d)))
171 (c1 (make-condition t1 'a 0 'b 1))
172 (c2 (make-condition t2 'c 2 'd 3))
173 (c (condition (t1 (a 0) (b 1))
175 (and (equal? c1 (extract-condition c t1))
176 (equal? c2 (extract-condition c t2))))))
180 ;;; Examples from the SRFI.
183 (define-condition-type &c &condition
187 (define-condition-type &c1 &c
191 (define-condition-type &c2 &c
196 (make-condition &c1 'x "V1" 'a "a1"))
199 (condition (&c2 (x "V2") (b "b2"))))
202 (condition (&c1 (x "V3/1") (a "a3"))
206 (make-compound-condition v1 v2))
209 (make-compound-condition v2 v3))
212 (with-test-prefix "examples"
223 (pass-if "(not (c2? v1))"
227 (equal? (c-x v1) "V1"))
230 (equal? (c1-a v1) "a1"))
242 (pass-if "(not (c1? v2))"
246 (equal? (c-x v2) "V2"))
249 (equal? (c2-b v2) "b2"))
265 (equal? (c-x v3) "V3/1"))
268 (equal? (c1-a v3) "a3"))
271 (equal? (c2-b v3) "b3"))
287 (equal? (c-x v4) "V1"))
290 (equal? (c1-a v4) "a1"))
293 (equal? (c2-b v4) "b2"))
309 (equal? (c-x v5) "V2"))
312 (equal? (c1-a v5) "a3"))
315 (equal? (c2-b v5) "b2")))