srfi-35.test 8.4 KB

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