| 1 |
;;;; srfi-35.test --- SRFI-35. -*- mode: scheme; coding: utf-8; -*- |
| 2 |
;;;; Ludovic Courtès <[email protected]> |
| 3 |
;;;; |
| 4 |
;;;; Copyright (C) 2007, 2008, 2009, 2010, 2022 Free Software Foundation, Inc. |
| 5 |
;;;; |
| 6 |
;;;; This library is free software; you can redistribute it and/or |
| 7 |
;;;; modify it under the terms of the GNU Lesser General Public |
| 8 |
;;;; License as published by the Free Software Foundation; either |
| 9 |
;;;; version 3 of the License, or (at your option) any later version. |
| 10 |
;;;; |
| 11 |
;;;; This library 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 GNU |
| 14 |
;;;; Lesser General Public License for more details. |
| 15 |
;;;; |
| 16 |
;;;; You should have received a copy of the GNU Lesser General Public |
| 17 |
;;;; License along with this library; if not, write to the Free Software |
| 18 |
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 |
|
| 20 |
(define-module (test-srfi-35) |
| 21 |
:use-module (test-suite lib) |
| 22 |
:use-module (srfi srfi-35)) |
| 23 |
|
| 24 |
|
| 25 |
(with-test-prefix "cond-expand" |
| 26 |
(pass-if "srfi-35" |
| 27 |
(cond-expand (srfi-35 #t) |
| 28 |
(else #f)))) |
| 29 |
|
| 30 |
|
| 31 |
(with-test-prefix "condition types" |
| 32 |
(pass-if "&condition" |
| 33 |
(condition-type? &condition)) |
| 34 |
|
| 35 |
(pass-if "make-condition-type" |
| 36 |
(condition-type? (make-condition-type 'foo &condition '(a b)))) |
| 37 |
|
| 38 |
(pass-if "struct-vtable-name" |
| 39 |
(let ((ct (make-condition-type 'chbouib &condition '(a b)))) |
| 40 |
(eq? 'chbouib (struct-vtable-name ct))))) |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
(with-test-prefix "conditions" |
| 45 |
|
| 46 |
(pass-if "&condition" |
| 47 |
(let ((c (make-condition &condition))) |
| 48 |
(and (condition? c) |
| 49 |
(condition-has-type? c &condition)))) |
| 50 |
|
| 51 |
(pass-if "simple condition" |
| 52 |
(let* ((ct (make-condition-type 'chbouib &condition '(a b))) |
| 53 |
(c (make-condition ct 'b 1 'a 0))) |
| 54 |
(and (condition? c) |
| 55 |
(condition-has-type? c ct)))) |
| 56 |
|
| 57 |
(pass-if "simple condition with inheritance" |
| 58 |
(let* ((top (make-condition-type 'foo &condition '(a b))) |
| 59 |
(ct (make-condition-type 'bar top '(c d))) |
| 60 |
(c (make-condition ct 'a 1 'b 2 'c 3 'd 4))) |
| 61 |
(and (condition? c) |
| 62 |
(condition-has-type? c ct) |
| 63 |
(condition-has-type? c top)))) |
| 64 |
|
| 65 |
(pass-if "condition-ref" |
| 66 |
(let* ((ct (make-condition-type 'chbouib &condition '(a b))) |
| 67 |
(c (make-condition ct 'b 1 'a 0))) |
| 68 |
(and (eqv? (condition-ref c 'a) 0) |
| 69 |
(eqv? (condition-ref c 'b) 1)))) |
| 70 |
|
| 71 |
(pass-if "condition-ref with inheritance" |
| 72 |
(let* ((top (make-condition-type 'foo &condition '(a b))) |
| 73 |
(ct (make-condition-type 'bar top '(c d))) |
| 74 |
(c (make-condition ct 'b 1 'a 0 'd 3 'c 2))) |
| 75 |
(and (eqv? (condition-ref c 'a) 0) |
| 76 |
(eqv? (condition-ref c 'b) 1) |
| 77 |
(eqv? (condition-ref c 'c) 2) |
| 78 |
(eqv? (condition-ref c 'd) 3)))) |
| 79 |
|
| 80 |
(pass-if "extract-condition" |
| 81 |
(let* ((ct (make-condition-type 'chbouib &condition '(a b))) |
| 82 |
(c (make-condition ct 'b 1 'a 0))) |
| 83 |
(equal? c (extract-condition c ct))))) |
| 84 |
|
| 85 |
|
| 86 |
(with-test-prefix "compound conditions" |
| 87 |
(pass-if "condition-has-type?" |
| 88 |
(let* ((t1 (make-condition-type 'foo &condition '(a b))) |
| 89 |
(t2 (make-condition-type 'bar &condition '(c d))) |
| 90 |
(c1 (make-condition t1 'a 0 'b 1)) |
| 91 |
(c2 (make-condition t2 'c 2 'd 3)) |
| 92 |
(c (make-compound-condition c1 c2))) |
| 93 |
(and (condition? c) |
| 94 |
(condition-has-type? c t1) |
| 95 |
(condition-has-type? c t2)))) |
| 96 |
|
| 97 |
(pass-if "condition-ref" |
| 98 |
(let* ((t1 (make-condition-type 'foo &condition '(a b))) |
| 99 |
(t2 (make-condition-type 'bar &condition '(c d))) |
| 100 |
(c1 (make-condition t1 'a 0 'b 1)) |
| 101 |
(c2 (make-condition t2 'c 2 'd 3)) |
| 102 |
(c (make-compound-condition c1 c2))) |
| 103 |
(equal? (map (lambda (field) |
| 104 |
(condition-ref c field)) |
| 105 |
'(a b c d)) |
| 106 |
'(0 1 2 3)))) |
| 107 |
|
| 108 |
(pass-if "condition-ref with same-named fields" |
| 109 |
(let* ((t1 (make-condition-type 'foo &condition '(a b))) |
| 110 |
(t2 (make-condition-type 'bar &condition '(a c d))) |
| 111 |
(c1 (make-condition t1 'a 0 'b 1)) |
| 112 |
(c2 (make-condition t2 'a -1 'c 2 'd 3)) |
| 113 |
(c (make-compound-condition c1 c2))) |
| 114 |
(equal? (map (lambda (field) |
| 115 |
(condition-ref c field)) |
| 116 |
'(a b c d)) |
| 117 |
'(0 1 2 3)))) |
| 118 |
|
| 119 |
(pass-if "extract-condition" |
| 120 |
(let* ((t1 (make-condition-type 'foo &condition '(a b))) |
| 121 |
(t2 (make-condition-type 'bar &condition '(c d))) |
| 122 |
(c1 (make-condition t1 'a 0 'b 1)) |
| 123 |
(c2 (make-condition t2 'c 2 'd 3)) |
| 124 |
(c (make-compound-condition c1 c2))) |
| 125 |
(and (equal? c1 (extract-condition c t1)) |
| 126 |
(equal? c2 (extract-condition c t2))))) |
| 127 |
|
| 128 |
(pass-if "extract-condition with same-named fields" |
| 129 |
(let* ((t1 (make-condition-type 'foo &condition '(a b))) |
| 130 |
(t2 (make-condition-type 'bar &condition '(a c))) |
| 131 |
(c1 (make-condition t1 'a 0 'b 1)) |
| 132 |
(c2 (make-condition t2 'a -1 'c 2)) |
| 133 |
(c (make-compound-condition c1 c2))) |
| 134 |
(and (equal? c1 (extract-condition c t1)) |
| 135 |
(equal? c2 (extract-condition c t2)))))) |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
(with-test-prefix "syntax" |
| 140 |
(pass-if "define-condition-type" |
| 141 |
(let ((m (current-module))) |
| 142 |
(eval '(define-condition-type &chbouib &condition |
| 143 |
chbouib? |
| 144 |
(one chbouib-one) |
| 145 |
(two chbouib-two)) |
| 146 |
m) |
| 147 |
(eval '(and (condition-type? &chbouib) |
| 148 |
(procedure? chbouib?) |
| 149 |
(let ((c (make-condition &chbouib 'one 1 'two 2))) |
| 150 |
(and (condition? c) |
| 151 |
(chbouib? c) |
| 152 |
(eqv? (chbouib-one c) 1) |
| 153 |
(eqv? (chbouib-two c) 2)))) |
| 154 |
m))) |
| 155 |
|
| 156 |
(pass-if "condition" |
| 157 |
(let* ((t (make-condition-type 'chbouib &condition '(a b))) |
| 158 |
(c (condition (t (b 2) (a 1))))) |
| 159 |
(and (condition? c) |
| 160 |
(condition-has-type? c t) |
| 161 |
(equal? (map (lambda (f) |
| 162 |
(condition-ref c f)) |
| 163 |
'(a b)) |
| 164 |
'(1 2))))) |
| 165 |
|
| 166 |
(pass-if-exception "condition with missing fields" |
| 167 |
exception:miscellaneous-error |
| 168 |
(let ((t (make-condition-type 'chbouib &condition '(a b c)))) |
| 169 |
(condition (t (a 1) (b 2))))) |
| 170 |
|
| 171 |
(pass-if "compound condition" |
| 172 |
(let* ((t1 (make-condition-type 'foo &condition '(a b))) |
| 173 |
(t2 (make-condition-type 'bar &condition '(c d))) |
| 174 |
(c1 (make-condition t1 'a 0 'b 1)) |
| 175 |
(c2 (make-condition t2 'c 2 'd 3)) |
| 176 |
(c (condition (t1 (a 0) (b 1)) |
| 177 |
(t2 (c 2) (d 3))))) |
| 178 |
(and (equal? c1 (extract-condition c t1)) |
| 179 |
(equal? c2 (extract-condition c t2))))) |
| 180 |
|
| 181 |
(pass-if "compound condition, hygienic macro expansion" |
| 182 |
;; In Guile 3.0.8, the 'condition' form below would refer to |
| 183 |
;; 'make-compound-condition' in an unhygienic fashion, leading to |
| 184 |
;; "unbound variable: make-compound-condition" if (srfi srfi-35) is |
| 185 |
;; not imported or imported with different bindings. |
| 186 |
(let ((c (eval '(begin |
| 187 |
(use-modules ((srfi srfi-35) #:prefix s:)) |
| 188 |
|
| 189 |
(s:condition (s:&error) |
| 190 |
(s:&message (message "m")))) |
| 191 |
(make-fresh-user-module)))) |
| 192 |
(and (condition? c) |
| 193 |
(error? c) (message-condition? c))))) |
| 194 |
|
| 195 |
|
| 196 |
;;; |
| 197 |
;;; Examples from the SRFI. |
| 198 |
;;; |
| 199 |
|
| 200 |
(define-condition-type &c &condition |
| 201 |
c? |
| 202 |
(x c-x)) |
| 203 |
|
| 204 |
(define-condition-type &c1 &c |
| 205 |
c1? |
| 206 |
(a c1-a)) |
| 207 |
|
| 208 |
(define-condition-type &c2 &c |
| 209 |
c2? |
| 210 |
(b c2-b)) |
| 211 |
|
| 212 |
(define v1 |
| 213 |
(make-condition &c1 'x "V1" 'a "a1")) |
| 214 |
|
| 215 |
(define v2 |
| 216 |
(condition (&c2 (x "V2") (b "b2")))) |
| 217 |
|
| 218 |
(define v3 |
| 219 |
(condition (&c1 (x "V3/1") (a "a3")) |
| 220 |
(&c2 (x #f) (b "b3")))) |
| 221 |
|
| 222 |
(define v4 |
| 223 |
(make-compound-condition v1 v2)) |
| 224 |
|
| 225 |
(define v5 |
| 226 |
(make-compound-condition v2 v3)) |
| 227 |
|
| 228 |
|
| 229 |
(with-test-prefix "examples" |
| 230 |
|
| 231 |
(pass-if "v1" |
| 232 |
(condition? v1)) |
| 233 |
|
| 234 |
(pass-if "(c? v1)" |
| 235 |
(c? v1)) |
| 236 |
|
| 237 |
(pass-if "(c1? v1)" |
| 238 |
(c1? v1)) |
| 239 |
|
| 240 |
(pass-if "(not (c2? v1))" |
| 241 |
(not (c2? v1))) |
| 242 |
|
| 243 |
(pass-if "(c-x v1)" |
| 244 |
(equal? (c-x v1) "V1")) |
| 245 |
|
| 246 |
(pass-if "(c1-a v1)" |
| 247 |
(equal? (c1-a v1) "a1")) |
| 248 |
|
| 249 |
|
| 250 |
(pass-if "v2" |
| 251 |
(condition? v2)) |
| 252 |
|
| 253 |
(pass-if "(c? v2)" |
| 254 |
(c? v2)) |
| 255 |
|
| 256 |
(pass-if "(c2? v2)" |
| 257 |
(c2? v2)) |
| 258 |
|
| 259 |
(pass-if "(not (c1? v2))" |
| 260 |
(not (c1? v2))) |
| 261 |
|
| 262 |
(pass-if "(c-x v2)" |
| 263 |
(equal? (c-x v2) "V2")) |
| 264 |
|
| 265 |
(pass-if "(c2-b v2)" |
| 266 |
(equal? (c2-b v2) "b2")) |
| 267 |
|
| 268 |
|
| 269 |
(pass-if "v3" |
| 270 |
(condition? v3)) |
| 271 |
|
| 272 |
(pass-if "(c? v3)" |
| 273 |
(c? v3)) |
| 274 |
|
| 275 |
(pass-if "(c1? v3)" |
| 276 |
(c1? v3)) |
| 277 |
|
| 278 |
(pass-if "(c2? v3)" |
| 279 |
(c2? v3)) |
| 280 |
|
| 281 |
(pass-if "(c-x v3)" |
| 282 |
(equal? (c-x v3) "V3/1")) |
| 283 |
|
| 284 |
(pass-if "(c1-a v3)" |
| 285 |
(equal? (c1-a v3) "a3")) |
| 286 |
|
| 287 |
(pass-if "(c2-b v3)" |
| 288 |
(equal? (c2-b v3) "b3")) |
| 289 |
|
| 290 |
|
| 291 |
(pass-if "v4" |
| 292 |
(condition? v4)) |
| 293 |
|
| 294 |
(pass-if "(c? v4)" |
| 295 |
(c? v4)) |
| 296 |
|
| 297 |
(pass-if "(c1? v4)" |
| 298 |
(c1? v4)) |
| 299 |
|
| 300 |
(pass-if "(c2? v4)" |
| 301 |
(c2? v4)) |
| 302 |
|
| 303 |
(pass-if "(c-x v4)" |
| 304 |
(equal? (c-x v4) "V1")) |
| 305 |
|
| 306 |
(pass-if "(c1-a v4)" |
| 307 |
(equal? (c1-a v4) "a1")) |
| 308 |
|
| 309 |
(pass-if "(c2-b v4)" |
| 310 |
(equal? (c2-b v4) "b2")) |
| 311 |
|
| 312 |
|
| 313 |
(pass-if "v5" |
| 314 |
(condition? v5)) |
| 315 |
|
| 316 |
(pass-if "(c? v5)" |
| 317 |
(c? v5)) |
| 318 |
|
| 319 |
(pass-if "(c1? v5)" |
| 320 |
(c1? v5)) |
| 321 |
|
| 322 |
(pass-if "(c2? v5)" |
| 323 |
(c2? v5)) |
| 324 |
|
| 325 |
(pass-if "(c-x v5)" |
| 326 |
(equal? (c-x v5) "V2")) |
| 327 |
|
| 328 |
(pass-if "(c1-a v5)" |
| 329 |
(equal? (c1-a v5) "a3")) |
| 330 |
|
| 331 |
(pass-if "(c2-b v5)" |
| 332 |
(equal? (c2-b v5) "b2"))) |