sxml.fold.test 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. ;;;; sxml.fold.test -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2010 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 3 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ;;; Commentary:
  19. ;;
  20. ;; Unit tests for (sxml fold).
  21. ;;
  22. ;;; Code:
  23. (define-module (test-suite sxml-fold)
  24. #:use-module (test-suite lib)
  25. #:use-module ((srfi srfi-1) #:select (fold))
  26. #:use-module (sxml fold))
  27. (define atom? (@@ (sxml fold) atom?))
  28. (define (id x) x)
  29. (define-syntax accept
  30. (syntax-rules ()
  31. ((_ expr)
  32. (call-with-values (lambda () expr) list))))
  33. (with-test-prefix "test-fold"
  34. (define test-doc
  35. '(presentation
  36. (@ (width 1024)
  37. (height 768)
  38. (title-style "font-family:Georgia")
  39. (title-height 72)
  40. (title-baseline-y 96)
  41. (title-x 48)
  42. (text-height 64)
  43. (text-style "font-family:Georgia")
  44. (text-upper-left-x 96)
  45. (text-upper-left-y 216))
  46. (slide
  47. (@ (title "Declarative interface"))
  48. (p "The declarative interface"
  49. "lets you be more concise"
  50. "when making the slides."))
  51. (slide
  52. (@ (title "Still cumbersome"))
  53. (p "Parentheses are still"
  54. "cumbersome."))))
  55. (pass-if (atom? 'foo))
  56. (pass-if (atom? '()))
  57. (pass-if (not (atom? '(1 2 3))))
  58. (pass-if "foldt identity"
  59. (equal? (foldt id id test-doc) test-doc))
  60. (pass-if "fold cons == reverse"
  61. (equal? (fold cons '() test-doc)
  62. (reverse test-doc)))
  63. (pass-if "foldts identity"
  64. (equal? (foldts (lambda (seed tree) '())
  65. (lambda (seed kid-seed tree)
  66. (cons (reverse kid-seed) seed))
  67. (lambda (seed tree)
  68. (cons tree seed))
  69. '()
  70. test-doc)
  71. (cons test-doc '())))
  72. (pass-if "foldts* identity"
  73. (equal? (foldts* (lambda (seed tree) (values '() tree))
  74. (lambda (seed kid-seed tree)
  75. (cons (reverse kid-seed) seed))
  76. (lambda (seed tree)
  77. (cons tree seed))
  78. '()
  79. test-doc)
  80. (cons test-doc '())))
  81. (pass-if "fold-values == fold"
  82. (equal? (fold-values cons test-doc '())
  83. (fold cons '() test-doc)))
  84. (pass-if "foldts*-values == foldts*"
  85. (equal? (foldts*-values
  86. (lambda (tree seed) (values tree '()))
  87. (lambda (tree seed kid-seed)
  88. (cons (reverse kid-seed) seed))
  89. (lambda (tree seed)
  90. (cons tree seed))
  91. test-doc
  92. '())
  93. (foldts* (lambda (seed tree) (values '() tree))
  94. (lambda (seed kid-seed tree)
  95. (cons (reverse kid-seed) seed))
  96. (lambda (seed tree)
  97. (cons tree seed))
  98. '()
  99. test-doc)))
  100. (let ()
  101. (define (replace pred val list)
  102. (reverse
  103. (fold
  104. (lambda (x xs)
  105. (cons (if (pred x) val x) xs))
  106. '()
  107. list)))
  108. (define (car-eq? x what)
  109. (and (pair? x) (eq? (car x) what)))
  110. ;; avoid entering <slide>
  111. (pass-if "foldts* *pre* behaviour"
  112. (equal? (foldts*-values
  113. (lambda (tree seed)
  114. (values (if (car-eq? tree 'slide) '() tree) '()))
  115. (lambda (tree seed kid-seed)
  116. (cons (reverse kid-seed) seed))
  117. (lambda (tree seed)
  118. (cons tree seed))
  119. test-doc
  120. '())
  121. (cons
  122. (replace (lambda (x) (car-eq? x 'slide))
  123. '()
  124. test-doc)
  125. '()))))
  126. (let ()
  127. (define (all-elts tree)
  128. (reverse!
  129. (foldts*-values
  130. (lambda (tree seed)
  131. (values tree seed))
  132. (lambda (tree seed kid-seed)
  133. kid-seed)
  134. (lambda (tree seed)
  135. (cons tree seed))
  136. tree
  137. '())))
  138. (define (len tree)
  139. (foldts*-values
  140. (lambda (tree seed)
  141. (values tree seed))
  142. (lambda (tree seed kid-seed)
  143. kid-seed)
  144. (lambda (tree seed)
  145. (1+ seed))
  146. tree
  147. 0))
  148. (pass-if "foldts length"
  149. (equal? (length (all-elts test-doc))
  150. (len test-doc)))))
  151. (with-test-prefix "test-fold-layout"
  152. (define test-doc
  153. '(presentation
  154. (@ (width 1024)
  155. (height 768)
  156. (title-style "font-family:Georgia")
  157. (title-height 72)
  158. (title-baseline-y 96)
  159. (title-x 48)
  160. (text-height 64)
  161. (text-style "font-family:Georgia")
  162. (text-upper-left-x 96)
  163. (text-upper-left-y 216))
  164. (slide
  165. (@ (title "Declarative interface"))
  166. (p "The declarative interface"
  167. "lets you be more concise"
  168. "when making the slides."))
  169. (slide
  170. (@ (title "Still cumbersome"))
  171. (p "Parentheses are still"
  172. "cumbersome."))))
  173. (define (identity-layout tree)
  174. (fold-layout
  175. tree
  176. `((*default*
  177. . ,(lambda (tag params old-layout layout kids)
  178. (values layout
  179. (if (null? (car params))
  180. (cons tag kids)
  181. (cons* tag (cons '@ (car params)) kids)))))
  182. (*text*
  183. . ,(lambda (text params layout)
  184. (values layout text))))
  185. '()
  186. (cons 0 0)
  187. '()))
  188. (pass-if "fold-layout"
  189. (equal? (accept (identity-layout test-doc))
  190. (list test-doc (cons 0 0)))))