srfi-35.test 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. ;;;; srfi-35.test --- SRFI-35. -*- mode: scheme; coding: utf-8; -*-
  2. ;;;; Ludovic Courtès <ludo@gnu.org>
  3. ;;;;
  4. ;;;; Copyright (C) 2007, 2008, 2009, 2010 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. (define-module (test-srfi-35)
  20. :use-module (test-suite lib)
  21. :use-module (srfi srfi-35))
  22. (with-test-prefix "cond-expand"
  23. (pass-if "srfi-35"
  24. (cond-expand (srfi-35 #t)
  25. (else #f))))
  26. (with-test-prefix "condition types"
  27. (pass-if "&condition"
  28. (condition-type? &condition))
  29. (pass-if "make-condition-type"
  30. (condition-type? (make-condition-type 'foo &condition '(a b))))
  31. (pass-if "struct-vtable-name"
  32. (let ((ct (make-condition-type 'chbouib &condition '(a b))))
  33. (eq? 'chbouib (struct-vtable-name ct)))))
  34. (with-test-prefix "conditions"
  35. (pass-if "&condition"
  36. (let ((c (make-condition &condition)))
  37. (and (condition? c)
  38. (condition-has-type? c &condition))))
  39. (pass-if "simple condition"
  40. (let* ((ct (make-condition-type 'chbouib &condition '(a b)))
  41. (c (make-condition ct 'b 1 'a 0)))
  42. (and (condition? c)
  43. (condition-has-type? c ct))))
  44. (pass-if "simple condition with inheritance"
  45. (let* ((top (make-condition-type 'foo &condition '(a b)))
  46. (ct (make-condition-type 'bar top '(c d)))
  47. (c (make-condition ct 'a 1 'b 2 'c 3 'd 4)))
  48. (and (condition? c)
  49. (condition-has-type? c ct)
  50. (condition-has-type? c top))))
  51. (pass-if "condition-ref"
  52. (let* ((ct (make-condition-type 'chbouib &condition '(a b)))
  53. (c (make-condition ct 'b 1 'a 0)))
  54. (and (eqv? (condition-ref c 'a) 0)
  55. (eqv? (condition-ref c 'b) 1))))
  56. (pass-if "condition-ref with inheritance"
  57. (let* ((top (make-condition-type 'foo &condition '(a b)))
  58. (ct (make-condition-type 'bar top '(c d)))
  59. (c (make-condition ct 'b 1 'a 0 'd 3 'c 2)))
  60. (and (eqv? (condition-ref c 'a) 0)
  61. (eqv? (condition-ref c 'b) 1)
  62. (eqv? (condition-ref c 'c) 2)
  63. (eqv? (condition-ref c 'd) 3))))
  64. (pass-if "extract-condition"
  65. (let* ((ct (make-condition-type 'chbouib &condition '(a b)))
  66. (c (make-condition ct 'b 1 'a 0)))
  67. (equal? c (extract-condition c ct)))))
  68. (with-test-prefix "compound conditions"
  69. (pass-if "condition-has-type?"
  70. (let* ((t1 (make-condition-type 'foo &condition '(a b)))
  71. (t2 (make-condition-type 'bar &condition '(c d)))
  72. (c1 (make-condition t1 'a 0 'b 1))
  73. (c2 (make-condition t2 'c 2 'd 3))
  74. (c (make-compound-condition c1 c2)))
  75. (and (condition? c)
  76. (condition-has-type? c t1)
  77. (condition-has-type? c t2))))
  78. (pass-if "condition-ref"
  79. (let* ((t1 (make-condition-type 'foo &condition '(a b)))
  80. (t2 (make-condition-type 'bar &condition '(c d)))
  81. (c1 (make-condition t1 'a 0 'b 1))
  82. (c2 (make-condition t2 'c 2 'd 3))
  83. (c (make-compound-condition c1 c2)))
  84. (equal? (map (lambda (field)
  85. (condition-ref c field))
  86. '(a b c d))
  87. '(0 1 2 3))))
  88. (pass-if "condition-ref with same-named fields"
  89. (let* ((t1 (make-condition-type 'foo &condition '(a b)))
  90. (t2 (make-condition-type 'bar &condition '(a c d)))
  91. (c1 (make-condition t1 'a 0 'b 1))
  92. (c2 (make-condition t2 'a -1 'c 2 'd 3))
  93. (c (make-compound-condition c1 c2)))
  94. (equal? (map (lambda (field)
  95. (condition-ref c field))
  96. '(a b c d))
  97. '(0 1 2 3))))
  98. (pass-if "extract-condition"
  99. (let* ((t1 (make-condition-type 'foo &condition '(a b)))
  100. (t2 (make-condition-type 'bar &condition '(c d)))
  101. (c1 (make-condition t1 'a 0 'b 1))
  102. (c2 (make-condition t2 'c 2 'd 3))
  103. (c (make-compound-condition c1 c2)))
  104. (and (equal? c1 (extract-condition c t1))
  105. (equal? c2 (extract-condition c t2)))))
  106. (pass-if "extract-condition with same-named fields"
  107. (let* ((t1 (make-condition-type 'foo &condition '(a b)))
  108. (t2 (make-condition-type 'bar &condition '(a c)))
  109. (c1 (make-condition t1 'a 0 'b 1))
  110. (c2 (make-condition t2 'a -1 'c 2))
  111. (c (make-compound-condition c1 c2)))
  112. (and (equal? c1 (extract-condition c t1))
  113. (equal? c2 (extract-condition c t2))))))
  114. (with-test-prefix "syntax"
  115. (pass-if "define-condition-type"
  116. (let ((m (current-module)))
  117. (eval '(define-condition-type &chbouib &condition
  118. chbouib?
  119. (one chbouib-one)
  120. (two chbouib-two))
  121. m)
  122. (eval '(and (condition-type? &chbouib)
  123. (procedure? chbouib?)
  124. (let ((c (make-condition &chbouib 'one 1 'two 2)))
  125. (and (condition? c)
  126. (chbouib? c)
  127. (eqv? (chbouib-one c) 1)
  128. (eqv? (chbouib-two c) 2))))
  129. m)))
  130. (pass-if "condition"
  131. (let* ((t (make-condition-type 'chbouib &condition '(a b)))
  132. (c (condition (t (b 2) (a 1)))))
  133. (and (condition? c)
  134. (condition-has-type? c t)
  135. (equal? (map (lambda (f)
  136. (condition-ref c f))
  137. '(a b))
  138. '(1 2)))))
  139. (pass-if-exception "condition with missing fields"
  140. exception:miscellaneous-error
  141. (let ((t (make-condition-type 'chbouib &condition '(a b c))))
  142. (condition (t (a 1) (b 2)))))
  143. (pass-if "compound condition"
  144. (let* ((t1 (make-condition-type 'foo &condition '(a b)))
  145. (t2 (make-condition-type 'bar &condition '(c d)))
  146. (c1 (make-condition t1 'a 0 'b 1))
  147. (c2 (make-condition t2 'c 2 'd 3))
  148. (c (condition (t1 (a 0) (b 1))
  149. (t2 (c 2) (d 3)))))
  150. (and (equal? c1 (extract-condition c t1))
  151. (equal? c2 (extract-condition c t2))))))
  152. ;;;
  153. ;;; Examples from the SRFI.
  154. ;;;
  155. (define-condition-type &c &condition
  156. c?
  157. (x c-x))
  158. (define-condition-type &c1 &c
  159. c1?
  160. (a c1-a))
  161. (define-condition-type &c2 &c
  162. c2?
  163. (b c2-b))
  164. (define v1
  165. (make-condition &c1 'x "V1" 'a "a1"))
  166. (define v2
  167. (condition (&c2 (x "V2") (b "b2"))))
  168. (define v3
  169. (condition (&c1 (x "V3/1") (a "a3"))
  170. (&c2 (x #f) (b "b3"))))
  171. (define v4
  172. (make-compound-condition v1 v2))
  173. (define v5
  174. (make-compound-condition v2 v3))
  175. (with-test-prefix "examples"
  176. (pass-if "v1"
  177. (condition? v1))
  178. (pass-if "(c? v1)"
  179. (c? v1))
  180. (pass-if "(c1? v1)"
  181. (c1? v1))
  182. (pass-if "(not (c2? v1))"
  183. (not (c2? v1)))
  184. (pass-if "(c-x v1)"
  185. (equal? (c-x v1) "V1"))
  186. (pass-if "(c1-a v1)"
  187. (equal? (c1-a v1) "a1"))
  188. (pass-if "v2"
  189. (condition? v2))
  190. (pass-if "(c? v2)"
  191. (c? v2))
  192. (pass-if "(c2? v2)"
  193. (c2? v2))
  194. (pass-if "(not (c1? v2))"
  195. (not (c1? v2)))
  196. (pass-if "(c-x v2)"
  197. (equal? (c-x v2) "V2"))
  198. (pass-if "(c2-b v2)"
  199. (equal? (c2-b v2) "b2"))
  200. (pass-if "v3"
  201. (condition? v3))
  202. (pass-if "(c? v3)"
  203. (c? v3))
  204. (pass-if "(c1? v3)"
  205. (c1? v3))
  206. (pass-if "(c2? v3)"
  207. (c2? v3))
  208. (pass-if "(c-x v3)"
  209. (equal? (c-x v3) "V3/1"))
  210. (pass-if "(c1-a v3)"
  211. (equal? (c1-a v3) "a3"))
  212. (pass-if "(c2-b v3)"
  213. (equal? (c2-b v3) "b3"))
  214. (pass-if "v4"
  215. (condition? v4))
  216. (pass-if "(c? v4)"
  217. (c? v4))
  218. (pass-if "(c1? v4)"
  219. (c1? v4))
  220. (pass-if "(c2? v4)"
  221. (c2? v4))
  222. (pass-if "(c-x v4)"
  223. (equal? (c-x v4) "V1"))
  224. (pass-if "(c1-a v4)"
  225. (equal? (c1-a v4) "a1"))
  226. (pass-if "(c2-b v4)"
  227. (equal? (c2-b v4) "b2"))
  228. (pass-if "v5"
  229. (condition? v5))
  230. (pass-if "(c? v5)"
  231. (c? v5))
  232. (pass-if "(c1? v5)"
  233. (c1? v5))
  234. (pass-if "(c2? v5)"
  235. (c2? v5))
  236. (pass-if "(c-x v5)"
  237. (equal? (c-x v5) "V2"))
  238. (pass-if "(c1-a v5)"
  239. (equal? (c1-a v5) "a3"))
  240. (pass-if "(c2-b v5)"
  241. (equal? (c2-b v5) "b2")))