| 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 |
|
| 19 |
;;; Commentary: |
| 20 |
;; |
| 21 |
;; Unit tests for (sxml fold). |
| 22 |
;; |
| 23 |
;;; Code: |
| 24 |
|
| 25 |
(define-module (test-suite sxml-fold) |
| 26 |
#:use-module (test-suite lib) |
| 27 |
#:use-module ((srfi srfi-1) #:select (fold)) |
| 28 |
#:use-module (sxml fold)) |
| 29 |
|
| 30 |
(define atom? (@@ (sxml fold) atom?)) |
| 31 |
(define (id x) x) |
| 32 |
(define-syntax accept |
| 33 |
(syntax-rules () |
| 34 |
((_ expr) |
| 35 |
(call-with-values (lambda () expr) list)))) |
| 36 |
|
| 37 |
(with-test-prefix "test-fold" |
| 38 |
(define test-doc |
| 39 |
'(presentation |
| 40 |
(@ (width 1024) |
| 41 |
(height 768) |
| 42 |
(title-style "font-family:Georgia") |
| 43 |
(title-height 72) |
| 44 |
(title-baseline-y 96) |
| 45 |
(title-x 48) |
| 46 |
(text-height 64) |
| 47 |
(text-style "font-family:Georgia") |
| 48 |
(text-upper-left-x 96) |
| 49 |
(text-upper-left-y 216)) |
| 50 |
(slide |
| 51 |
(@ (title "Declarative interface")) |
| 52 |
(p "The declarative interface" |
| 53 |
"lets you be more concise" |
| 54 |
"when making the slides.")) |
| 55 |
(slide |
| 56 |
(@ (title "Still cumbersome")) |
| 57 |
(p "Parentheses are still" |
| 58 |
"cumbersome.")))) |
| 59 |
|
| 60 |
(pass-if (atom? 'foo)) |
| 61 |
(pass-if (atom? '())) |
| 62 |
(pass-if (not (atom? '(1 2 3)))) |
| 63 |
|
| 64 |
(pass-if "foldt identity" |
| 65 |
(equal? (foldt id id test-doc) test-doc)) |
| 66 |
|
| 67 |
(pass-if "fold cons == reverse" |
| 68 |
(equal? (fold cons '() test-doc) |
| 69 |
(reverse test-doc))) |
| 70 |
|
| 71 |
(pass-if "foldts identity" |
| 72 |
(equal? (foldts (lambda (seed tree) '()) |
| 73 |
(lambda (seed kid-seed tree) |
| 74 |
(cons (reverse kid-seed) seed)) |
| 75 |
(lambda (seed tree) |
| 76 |
(cons tree seed)) |
| 77 |
'() |
| 78 |
test-doc) |
| 79 |
(cons test-doc '()))) |
| 80 |
|
| 81 |
(pass-if "foldts* identity" |
| 82 |
(equal? (foldts* (lambda (seed tree) (values '() tree)) |
| 83 |
(lambda (seed kid-seed tree) |
| 84 |
(cons (reverse kid-seed) seed)) |
| 85 |
(lambda (seed tree) |
| 86 |
(cons tree seed)) |
| 87 |
'() |
| 88 |
test-doc) |
| 89 |
(cons test-doc '()))) |
| 90 |
|
| 91 |
(pass-if "fold-values == fold" |
| 92 |
(equal? (fold-values cons test-doc '()) |
| 93 |
(fold cons '() test-doc))) |
| 94 |
|
| 95 |
(pass-if "foldts*-values == foldts*" |
| 96 |
(equal? (foldts*-values |
| 97 |
(lambda (tree seed) (values tree '())) |
| 98 |
(lambda (tree seed kid-seed) |
| 99 |
(cons (reverse kid-seed) seed)) |
| 100 |
(lambda (tree seed) |
| 101 |
(cons tree seed)) |
| 102 |
test-doc |
| 103 |
'()) |
| 104 |
(foldts* (lambda (seed tree) (values '() tree)) |
| 105 |
(lambda (seed kid-seed tree) |
| 106 |
(cons (reverse kid-seed) seed)) |
| 107 |
(lambda (seed tree) |
| 108 |
(cons tree seed)) |
| 109 |
'() |
| 110 |
test-doc))) |
| 111 |
|
| 112 |
(let () |
| 113 |
(define (replace pred val list) |
| 114 |
(reverse |
| 115 |
(fold |
| 116 |
(lambda (x xs) |
| 117 |
(cons (if (pred x) val x) xs)) |
| 118 |
'() |
| 119 |
list))) |
| 120 |
|
| 121 |
(define (car-eq? x what) |
| 122 |
(and (pair? x) (eq? (car x) what))) |
| 123 |
|
| 124 |
;; avoid entering <slide> |
| 125 |
(pass-if "foldts* *pre* behaviour" |
| 126 |
(equal? (foldts*-values |
| 127 |
(lambda (tree seed) |
| 128 |
(values (if (car-eq? tree 'slide) '() tree) '())) |
| 129 |
(lambda (tree seed kid-seed) |
| 130 |
(cons (reverse kid-seed) seed)) |
| 131 |
(lambda (tree seed) |
| 132 |
(cons tree seed)) |
| 133 |
test-doc |
| 134 |
'()) |
| 135 |
(cons |
| 136 |
(replace (lambda (x) (car-eq? x 'slide)) |
| 137 |
'() |
| 138 |
test-doc) |
| 139 |
'())))) |
| 140 |
|
| 141 |
(let () |
| 142 |
(define (all-elts tree) |
| 143 |
(reverse! |
| 144 |
(foldts*-values |
| 145 |
(lambda (tree seed) |
| 146 |
(values tree seed)) |
| 147 |
(lambda (tree seed kid-seed) |
| 148 |
kid-seed) |
| 149 |
(lambda (tree seed) |
| 150 |
(cons tree seed)) |
| 151 |
tree |
| 152 |
'()))) |
| 153 |
|
| 154 |
(define (len tree) |
| 155 |
(foldts*-values |
| 156 |
(lambda (tree seed) |
| 157 |
(values tree seed)) |
| 158 |
(lambda (tree seed kid-seed) |
| 159 |
kid-seed) |
| 160 |
(lambda (tree seed) |
| 161 |
(1+ seed)) |
| 162 |
tree |
| 163 |
0)) |
| 164 |
|
| 165 |
(pass-if "foldts length" |
| 166 |
(equal? (length (all-elts test-doc)) |
| 167 |
(len test-doc))))) |
| 168 |
|
| 169 |
(with-test-prefix "test-fold-layout" |
| 170 |
(define test-doc |
| 171 |
'(presentation |
| 172 |
(@ (width 1024) |
| 173 |
(height 768) |
| 174 |
(title-style "font-family:Georgia") |
| 175 |
(title-height 72) |
| 176 |
(title-baseline-y 96) |
| 177 |
(title-x 48) |
| 178 |
(text-height 64) |
| 179 |
(text-style "font-family:Georgia") |
| 180 |
(text-upper-left-x 96) |
| 181 |
(text-upper-left-y 216)) |
| 182 |
(slide |
| 183 |
(@ (title "Declarative interface")) |
| 184 |
(p "The declarative interface" |
| 185 |
"lets you be more concise" |
| 186 |
"when making the slides.")) |
| 187 |
(slide |
| 188 |
(@ (title "Still cumbersome")) |
| 189 |
(p "Parentheses are still" |
| 190 |
"cumbersome.")))) |
| 191 |
|
| 192 |
(define (identity-layout tree) |
| 193 |
(fold-layout |
| 194 |
tree |
| 195 |
`((*default* |
| 196 |
. ,(lambda (tag params old-layout layout kids) |
| 197 |
(values layout |
| 198 |
(if (null? (car params)) |
| 199 |
(cons tag kids) |
| 200 |
(cons* tag (cons '@ (car params)) kids))))) |
| 201 |
(*text* |
| 202 |
. ,(lambda (text params layout) |
| 203 |
(values layout text)))) |
| 204 |
'() |
| 205 |
(cons 0 0) |
| 206 |
'())) |
| 207 |
|
| 208 |
(pass-if "fold-layout" |
| 209 |
(equal? (accept (identity-layout test-doc)) |
| 210 |
(list test-doc (cons 0 0))))) |