1 | |
---|
2 | |
---|
3 | (use test ersatz-lib datatype) |
---|
4 | |
---|
5 | (define kwargs '()) |
---|
6 | |
---|
7 | (define (tval-equal? t1 t2) |
---|
8 | (cases tvalue (eq-eq t1 t2) |
---|
9 | (Tbool (ret) ret) |
---|
10 | (else (error 'tval-equal "invalid value")))) |
---|
11 | |
---|
12 | (define (alist->models xs) |
---|
13 | (map (lambda (x) (cons (car x) (sexpr->tvalue (cdr x)))) xs)) |
---|
14 | |
---|
15 | (test-group "runtime test" |
---|
16 | |
---|
17 | |
---|
18 | (test-group "tvalue string representation" |
---|
19 | (test "a" (->string (Tstr "a"))) |
---|
20 | (test "1" (->string (Tint 1))) |
---|
21 | (test "1.0" (->string (Tfloat 1.0))) |
---|
22 | (test "1.2" (->string (Tfloat 1.2))) |
---|
23 | (test "<list>" (->string (Tlist (list (Tint 0) (Tint 1))))) |
---|
24 | (test "<obj>" (->string (Tobj (list (cons 'name (Tstr "value")))))) |
---|
25 | ) |
---|
26 | |
---|
27 | (test-group "arithmetic and logic" |
---|
28 | (test (Tint 2) (op-plus (Tint 1) (Tint 1))) |
---|
29 | (test (Tfloat 2.0) (op-plus (Tfloat 1.0) (Tfloat 1.0))) |
---|
30 | (test (Tint 0) (op-minus (Tint 1) (Tint 1))) |
---|
31 | (test (Tint 2) (op-minus (Tint 1) (Tint -1))) |
---|
32 | (test (Tfloat -1.0) (op-minus (Tint 0) (Tfloat 1.0))) |
---|
33 | (test (Tfloat 1.0) (op-minus (Tint 1) (Tfloat 0.0))) |
---|
34 | (test-assert (tval-equal? (op-abs (Tint -1) kwargs) (Tint 1))) |
---|
35 | (test-assert (tval-equal? (op-abs (Tint 1) kwargs) (Tint 1))) |
---|
36 | (test-assert (tval-equal? (op-round (Tstr "floor") (Tfloat 1.5) kwargs) |
---|
37 | (Tfloat 1.0))) |
---|
38 | (test-assert (tval-equal? (op-round (Tstr "ceil") (Tfloat 1.5) kwargs) |
---|
39 | (Tfloat 2.0))) |
---|
40 | (test-assert (tval-equal? (op-range (Tint 0) (Tint 2) kwargs) |
---|
41 | (Tlist (list (Tint 0) (Tint 1) (Tint 2))))) |
---|
42 | (test-assert (tval-equal? (op-range (Tint 2) (Tint 0) kwargs) |
---|
43 | (Tlist (list (Tint 2) (Tint 1) (Tint 0))))) |
---|
44 | (test-assert (tval-equal? (op-range (Tint 2012) (Tint 2006) kwargs) |
---|
45 | (Tlist (list (Tint 2012) (Tint 2011) (Tint 2010) (Tint 2009) |
---|
46 | (Tint 2008) (Tint 2007) (Tint 2006))))) |
---|
47 | ;; (test-assert (tval-equal? (op-sum (Tlist (Tint 0) (Tint 1) (Tint 2) kwargs)) |
---|
48 | ;; (Tint 3))) |
---|
49 | ;; (test-assert (tval-equal? (op-sum (Tlist (Tint 0) (Tint 1) (Tfloat 2.1) kwargs)) |
---|
50 | ;; (Tfloat 3.1))) |
---|
51 | |
---|
52 | |
---|
53 | (test-assert (tval-equal? (op-times (Tint 0) (Tint 1)) (Tint 0))) |
---|
54 | (test-assert (tval-equal? (op-times (Tint 1) (Tint 1)) (Tint 1))) |
---|
55 | (test-assert (tval-equal? (op-times (Tint 2) (Tint 2)) (Tint 4))) |
---|
56 | (test-assert (tval-equal? (op-times (Tint 2) (Tint 3)) (Tint 6))) |
---|
57 | (test-assert (tval-equal? (op-times (Tfloat 1.0) (Tint 2)) (Tfloat 2.0))) |
---|
58 | (test-assert (tval-equal? (op-times (Tfloat 2.0) (Tfloat 2.0)) (Tfloat 4.0))) |
---|
59 | (test-assert (tval-equal? (op-times (Tfloat 2.0) (Tfloat 3.0)) (Tfloat 6.0))) |
---|
60 | (test-assert (tval-equal? (op-times (Tfloat 0.0) (Tfloat 2.0)) (Tfloat 0.0))) |
---|
61 | (test-assert (tval-equal? (op-times (Tfloat 0.0) (Tint 1)) (Tfloat 0.0))) |
---|
62 | |
---|
63 | (test-assert (tval-equal? (op-power (Tint 2) (Tint -1)) (Tfloat 1.0))) |
---|
64 | (test-assert (tval-equal? (op-power (Tint 2) (Tint 0)) (Tfloat 1.0))) |
---|
65 | (test-assert (tval-equal? (op-power (Tint 2) (Tint 1)) (Tfloat 2.0))) |
---|
66 | (test-assert (tval-equal? (op-power (Tint 2) (Tint 10)) (Tfloat 1024.0))) |
---|
67 | |
---|
68 | (test-assert (tval-equal? (op-div (Tint 4) (Tint 2)) (Tint 2))) |
---|
69 | (test-assert (tval-equal? (op-div (Tfloat 4.0) (Tint 2)) (Tfloat 2.0))) |
---|
70 | |
---|
71 | (test-assert (tval-equal? (op-mod (Tint 4) (Tint 3)) (Tint 1))) |
---|
72 | (test-assert (tval-equal? (op-mod (Tint 4) (Tint 1)) (Tint 0))) |
---|
73 | |
---|
74 | (test-assert (tval-equal? (op-and (Tbool #t) (Tbool #t)) (Tbool #t))) |
---|
75 | (test-assert (tval-equal? (op-and (Tbool #t) (Tbool #f)) (Tbool #f))) |
---|
76 | (test-assert (tval-equal? (op-and (Tbool #f) (Tbool #t)) (Tbool #f))) |
---|
77 | (test-assert (tval-equal? (op-and (Tbool #f) (Tbool #f)) (Tbool #f))) |
---|
78 | |
---|
79 | (test-assert (tval-equal? (op-or (Tbool #t) (Tbool #t)) (Tbool #t))) |
---|
80 | (test-assert (tval-equal? (op-or (Tbool #t) (Tbool #f)) (Tbool #t))) |
---|
81 | (test-assert (tval-equal? (op-or (Tbool #f) (Tbool #t)) (Tbool #t))) |
---|
82 | (test-assert (tval-equal? (op-or (Tbool #f) (Tbool #f)) (Tbool #f))) |
---|
83 | |
---|
84 | |
---|
85 | (test-assert (tval-equal? (op-toint (Tint 1) kwargs) (Tint 1))) |
---|
86 | (test-assert (tval-equal? (op-toint (Tfloat 1.0) kwargs) (Tint 1))) |
---|
87 | (test-assert (tval-equal? (op-tofloat (Tint 1) kwargs) (Tfloat 1.0))) |
---|
88 | (test-assert (tval-equal? (op-tofloat (Tfloat 1.0) kwargs) (Tfloat 1.0))) |
---|
89 | ) |
---|
90 | |
---|
91 | |
---|
92 | (let ((lst1 (Tlist (list (Tint 0) (Tint 1) (Tint 2)))) |
---|
93 | (lst2 (Tlist (list (Tint 0) (Tint 1) (Tint 2)))) |
---|
94 | (lst3 (Tlist (list (Tint 0) (Tint 1) (Tint 3)))) |
---|
95 | (lst4 (Tlist (list (Tint 0) (Tint 1))))) |
---|
96 | (test-group "list equality" |
---|
97 | (test-assert (tval-equal? (Tbool #t) (list-same lst1 lst2))) |
---|
98 | (test-assert (tval-equal? (Tbool #f) (list-same lst1 lst3))) |
---|
99 | (test-assert (tval-equal? (Tbool #f) (list-same lst1 lst4))))) |
---|
100 | |
---|
101 | (let ((obj1 (Tobj (list (cons 'name (Tstr "john")) |
---|
102 | (cons 'age (Tint 20))))) |
---|
103 | (obj2 (Tobj (list (cons 'name (Tstr "john")) |
---|
104 | (cons 'age (Tint 20))))) |
---|
105 | (obj3 (Tobj (list (cons 'name (Tstr "mary")) |
---|
106 | (cons 'age (Tint 22))))) |
---|
107 | (obj4 (Tobj (list (cons 'age (Tint 20)) |
---|
108 | (cons 'name (Tstr "john")))))) |
---|
109 | (test-group "object equality" |
---|
110 | (test-assert (tval-equal? (Tbool #t) (obj-same obj1 obj2))) |
---|
111 | (test-assert (tval-equal? (Tbool #f) (obj-same obj1 obj3))) |
---|
112 | (test-assert (tval-equal? (Tbool #t) (obj-same obj1 obj4)))) |
---|
113 | ) |
---|
114 | |
---|
115 | |
---|
116 | (test-group "eq-eq" |
---|
117 | (test-assert (tval-equal? (eq-eq (Tint 1) (Tint 1)) (Tbool #t))) |
---|
118 | (test-assert (tval-equal? (eq-eq (Tint 1) (Tfloat 1.0)) (Tbool #f))) |
---|
119 | (test-assert (tval-equal? (eq-eq (Tfloat 1.0) (Tfloat 1.0)) (Tbool #t))) |
---|
120 | (test-assert (tval-equal? (eq-eq (Tstr "aaa") (Tstr "aaa")) (Tbool #t))) |
---|
121 | (test-assert (tval-equal? (eq-eq (Tstr "aaa") (Tstr "bbb")) (Tbool #f))) |
---|
122 | (test-assert (tval-equal? (eq-eq (Tstr "æ¥æ¬èª") (Tstr "æ¥æ¬èª")) (Tbool #t))) |
---|
123 | (test-assert (tval-equal? (eq-eq (Tstr "æ¥æ¬èª") (Tstr "è±èª")) (Tbool #f))) |
---|
124 | (test-assert (tval-equal? (eq-eq (Tstr "aaa") (Tint 0)) (Tbool #f))) |
---|
125 | ) |
---|
126 | |
---|
127 | (test-group "string operations" |
---|
128 | (test-assert "upper" (tval-equal? (op-upper (Tstr "aaa") kwargs) (Tstr "AAA"))) |
---|
129 | (test-assert "lower" (tval-equal? (op-lower (Tstr "AAA") kwargs) (Tstr "aaa"))) |
---|
130 | (test-assert "join" (tval-equal? (op-join (Tstr ",") (Tlist (list (Tstr "a") (Tstr "b"))) kwargs) (Tstr "a,b"))) |
---|
131 | (test-assert "substring" (tval-equal? (op-substring (Tint 0) (Tint 1) (Tstr "hello") kwargs) (Tstr "h"))) |
---|
132 | (test-assert "substring" (tval-equal? (op-substring (Tint 4) (Tint 1) (Tstr "hello") kwargs) (Tstr "o"))) |
---|
133 | (test-assert "substring" (tval-equal? (op-substring (Tint 0) (Tint 2) (Tstr "hello") kwargs) (Tstr "he"))) |
---|
134 | (test-assert "substring" (tval-equal? (op-substring (Tint 0) (Tint 2) (Tstr "æ¥æ¬èª") kwargs) (Tstr "æ¥æ¬"))) |
---|
135 | |
---|
136 | (test-assert "replace" (tval-equal? (op-replace (Tstr "t") (Tstr "d") (Tstr "test") kwargs) (Tstr "desd"))) |
---|
137 | (test-assert "replace" (tval-equal? (op-replace (Tstr "te") (Tstr "ta") (Tstr "test") kwargs) (Tstr "tast"))) |
---|
138 | (test-assert "replace" (tval-equal? (op-replace (Tstr "æ¥") (Tstr "è±") (Tstr "æ¥èª") kwargs) (Tstr "è±èª"))) |
---|
139 | |
---|
140 | (test-assert "truncate" |
---|
141 | (tval-equal? (op-truncate (Tint 3) (Tstr "123456789") kwargs) (Tstr "123"))) |
---|
142 | |
---|
143 | (test-assert "capitalize" |
---|
144 | (tval-equal? (op-capitalize (Tstr "car") kwargs) (Tstr "Car"))) |
---|
145 | |
---|
146 | (test-assert "escape-html" |
---|
147 | (tval-equal? |
---|
148 | (Tstr "<script>") |
---|
149 | (op-escape-html (Tstr "<script>") kwargs) |
---|
150 | )) |
---|
151 | |
---|
152 | |
---|
153 | (test-assert "wordcount" |
---|
154 | (tval-equal? |
---|
155 | (op-wordcount (Tstr "xy yz zz") kwargs) (Tint 3))) |
---|
156 | |
---|
157 | (test-assert "wordcount" |
---|
158 | (tval-equal? |
---|
159 | (op-wordcount (Tstr "è±èª æ¥èª") kwargs) (Tint 2))) |
---|
160 | |
---|
161 | (test-assert "urlize" |
---|
162 | (tval-equal? |
---|
163 | (op-urlize (Tstr "go to http://yahoo.co.jp" ) kwargs) |
---|
164 | (Tstr "go to <a href='http://yahoo.co.jp'>http://yahoo.co.jp</a>"))) |
---|
165 | |
---|
166 | (test-assert "title" |
---|
167 | (tval-equal? |
---|
168 | (op-title (Tstr "this is a title" ) kwargs) |
---|
169 | (Tstr "This Is A Title"))) |
---|
170 | |
---|
171 | (test-assert "striptags" |
---|
172 | (tval-equal? |
---|
173 | (op-striptags (Tstr "<p class='indent'>xxx</p> yyy <b>zzz</b>" ) kwargs) |
---|
174 | (Tstr "xxx yyy zzz"))) |
---|
175 | |
---|
176 | (test-assert "trim" |
---|
177 | (tval-equal? |
---|
178 | (op-trim (Tstr " this is a test " ) kwargs) |
---|
179 | (Tstr "this is a test"))) |
---|
180 | |
---|
181 | (test-assert "pad" |
---|
182 | (tval-equal? |
---|
183 | (op-pad (Tint 5) (Tstr "this is a test" ) kwargs) |
---|
184 | (Tstr " this is a test"))) |
---|
185 | |
---|
186 | ) |
---|
187 | |
---|
188 | |
---|
189 | (test-group "sorting" |
---|
190 | |
---|
191 | (let ((lst (Tlist (list (Tint 3) (Tint 1) (Tint 2))))) |
---|
192 | (test-assert (tval-equal? (op-sort lst kwargs) |
---|
193 | (Tlist (list (Tint 1) (Tint 2) (Tint 3)))))) |
---|
194 | |
---|
195 | (let ((lst (Tlist (list (Tfloat 3.0) (Tfloat 1.0) (Tfloat 2.0))))) |
---|
196 | (test-assert (tval-equal? (op-sort lst kwargs) |
---|
197 | (Tlist (list (Tfloat 1.0) (Tfloat 2.0) (Tfloat 3.0)))))) |
---|
198 | |
---|
199 | (let ((lst (Tlist (list (Tstr "baba") (Tstr "aa") (Tstr "kaka"))))) |
---|
200 | (test-assert (tval-equal? (op-sort lst kwargs) |
---|
201 | (Tlist (list (Tstr "aa") (Tstr "baba") (Tstr "kaka")))))) |
---|
202 | ) |
---|
203 | |
---|
204 | |
---|
205 | (test-group "length" |
---|
206 | (test-assert (tval-equal? (op-length (Tstr "test") kwargs) (Tint 4))) |
---|
207 | (test-assert (tval-equal? (op-length (Tstr "æ¥æ¬èª") kwargs) (Tint 3))) |
---|
208 | (test-assert (tval-equal? (op-length (Tlist (list (Tint 0) (Tint 1))) kwargs) (Tint 2))) |
---|
209 | ) |
---|
210 | |
---|
211 | (test-assert "reverse" (tval-equal? (op-reverse (Tlist (list (Tint 0) (Tint 1) (Tint 2))) kwargs) |
---|
212 | (Tlist (list (Tint 2) (Tint 1) (Tint 0))))) |
---|
213 | |
---|
214 | |
---|
215 | (test-assert "slice" (tval-equal? (op-slice (Tint 2) (Tlist (list (Tint 0) (Tint 1) (Tint 2) (Tint 3) (Tint 4))) kwargs) |
---|
216 | (Tlist (list |
---|
217 | (Tlist (list (Tint 0) (Tint 1))) |
---|
218 | (Tlist (list (Tint 2) (Tint 3))) |
---|
219 | (Tlist (list (Tint 4))))))) |
---|
220 | |
---|
221 | (test-assert "last" (tval-equal? (op-last (Tlist (list (Tint 0) (Tint 1) (Tint 2))) kwargs) |
---|
222 | (Tint 2))) |
---|
223 | |
---|
224 | (test-assert "default" (tval-equal? (op-default (Tstr "hello") (Tnull) kwargs) (Tstr "hello"))) |
---|
225 | |
---|
226 | (test-assert "list" (tval-equal? (op-list (Tstr "test") kwargs) |
---|
227 | (Tlist (list (Tstr "t") (Tstr "e") (Tstr "s") (Tstr "t"))))) |
---|
228 | |
---|
229 | (test-assert "sublist" (tval-equal? (op-sublist (Tint 1) (Tint 3) (Tlist (list (Tint 0) (Tint 1) (Tint 2) (Tint 3))) kwargs) |
---|
230 | (Tlist (list (Tint 1) (Tint 2) (Tint 3))))) |
---|
231 | |
---|
232 | (test-assert "groupBy" (tval-equal? (op-group-by (Tint 3) (Tlist (list (Tint 0) (Tint 1) (Tint 2) (Tint 3))) kwargs) |
---|
233 | (Tlist (list (Tlist (list (Tint 0) (Tint 1) (Tint 2))) (Tlist (list (Tint 3))))))) |
---|
234 | |
---|
235 | (test-assert "expand and escape" |
---|
236 | (let* ((script "<script>alert(1)</script>") |
---|
237 | (output (from-string "{{danger}}" |
---|
238 | models: (alist->models `((danger . ,script))))) |
---|
239 | ) |
---|
240 | (tval-equal? (Tstr output) (op-escape-html (Tstr script) kwargs)))) |
---|
241 | |
---|
242 | (test-assert "safe expand" |
---|
243 | (let* ((script "<script>alert(1)</script>") |
---|
244 | (output (from-string "{{danger|safe}}" |
---|
245 | models: (alist->models `((danger . ,script))))) |
---|
246 | ) |
---|
247 | (tval-equal? (Tstr output) (Tstr script)))) |
---|
248 | |
---|
249 | (test-assert "expand with filter" |
---|
250 | (let* ( |
---|
251 | (output (from-string "{{pi|int}}" |
---|
252 | models: (alist->models '((pi . 3.14))))) |
---|
253 | ) |
---|
254 | (tval-equal? (Tstr output) (Tstr "3")))) |
---|
255 | |
---|
256 | (test-assert "if" |
---|
257 | (let* ( (source "{% if x <= 1 %}one{% elseif x == 2 %}two{% else %}three{% endif %}") ) |
---|
258 | (and (tval-equal? (Tstr (from-string source |
---|
259 | models: (alist->models '((x . 1))))) |
---|
260 | (Tstr "one")) |
---|
261 | (tval-equal? (Tstr (from-string source |
---|
262 | models: (alist->models '((x . 2))))) |
---|
263 | (Tstr "two")) |
---|
264 | (tval-equal? (Tstr (from-string source |
---|
265 | models: (alist->models '((x . 3))))) |
---|
266 | (Tstr "three")) |
---|
267 | ))) |
---|
268 | (print (from-string "{% if not (x == 1) %}not one{% endif %}" |
---|
269 | models: (alist->models '((x . 2))))) |
---|
270 | (print (from-string "{% if not x == 1 %}not one{% endif %}" |
---|
271 | models: (alist->models '((x . 2))))) |
---|
272 | (test-assert "if" |
---|
273 | (let* ( (source "{% if not x == 1 %}not one{% endif %}") ) |
---|
274 | (and (tval-equal? (Tstr (from-string source |
---|
275 | models: (alist->models '((x . 2))))) |
---|
276 | (Tstr "not one")) |
---|
277 | ))) |
---|
278 | |
---|
279 | |
---|
280 | (test |
---|
281 | #<<EOF |
---|
282 | |
---|
283 | <div class="blog-post"> |
---|
284 | <h3>Post One title</h3> |
---|
285 | <div class="post-body"> |
---|
286 | Post One body </div> |
---|
287 | </div> |
---|
288 | |
---|
289 | <div class="blog-post"> |
---|
290 | <h3>Post Two title</h3> |
---|
291 | <div class="post-body"> |
---|
292 | Post Two body </div> |
---|
293 | </div> |
---|
294 | |
---|
295 | EOF |
---|
296 | |
---|
297 | (let* ( (source #<<EOF |
---|
298 | {% for p in posts %} |
---|
299 | <div class="blog-post"> |
---|
300 | <h3>{{ p.title }}</h3> |
---|
301 | <div class="post-body"> |
---|
302 | {{ p.body }} |
---|
303 | </div> |
---|
304 | </div> |
---|
305 | {% endfor %} |
---|
306 | EOF |
---|
307 | )) |
---|
308 | (from-string source models: |
---|
309 | (alist->models |
---|
310 | '((posts . (((title . "Post One title") |
---|
311 | (body . "Post One body")) |
---|
312 | ((title . "Post Two title") |
---|
313 | (body . "Post Two body")) |
---|
314 | ))) |
---|
315 | )) |
---|
316 | |
---|
317 | )) |
---|
318 | |
---|
319 | |
---|
320 | |
---|
321 | (test-assert "loop index/revindex" |
---|
322 | |
---|
323 | (and |
---|
324 | (let* ( (source "{% for i in range(1,3) %}{{loop.index}}{% endfor %}" )) |
---|
325 | (tval-equal? (Tstr (from-string source)) (Tstr "123"))) |
---|
326 | (let* ( (source "{% for i in range(1,3) %}{{loop.index0}}{% endfor %}" )) |
---|
327 | (tval-equal? (Tstr (from-string source)) (Tstr "012"))) |
---|
328 | (let* ( (source "{% for i in range(1,3) %}{{loop.revindex}}{% endfor %}" )) |
---|
329 | (tval-equal? (Tstr (from-string source)) (Tstr "321"))) |
---|
330 | (let* ( (source "{% for i in range(1,3) %}{{loop.revindex0}}{% endfor %}" )) |
---|
331 | (tval-equal? (Tstr (from-string source)) (Tstr "210"))) |
---|
332 | |
---|
333 | )) |
---|
334 | |
---|
335 | |
---|
336 | |
---|
337 | (test-assert "loop cycle" |
---|
338 | (let* ( (source "{% for i in range(1,3) %}{{loop.cycle(\"foo\",\"bar\",\"test\")}}{% endfor %}" )) |
---|
339 | (tval-equal? (Tstr (from-string source)) (Tstr "foobartest")))) |
---|
340 | |
---|
341 | (test-assert "loop first" |
---|
342 | (let* ( (source "{% for i in range(1,3) %}{{loop.first}}{% endfor %}" )) |
---|
343 | (tval-equal? (Tstr (from-string source)) (Tstr "truefalsefalse")))) |
---|
344 | |
---|
345 | (test-assert "loop last" |
---|
346 | (let* ( (source "{% for i in range(1,3) %}{{loop.last}}{% endfor %}" )) |
---|
347 | (tval-equal? (Tstr (from-string source)) (Tstr "falsefalsetrue")))) |
---|
348 | |
---|
349 | (test-assert "loop last" |
---|
350 | (let* ( (source "{% for i in range(1,3) %}{{not loop.last}}{% endfor %}" )) |
---|
351 | (tval-equal? (Tstr (from-string source)) (Tstr "truetruefalse")))) |
---|
352 | |
---|
353 | (test-assert "loop length" |
---|
354 | (let* ( (source "{% for i in range(1,3) %}{{loop.length}}{% endfor %}" )) |
---|
355 | (tval-equal? (Tstr (from-string source)) (Tstr "333")))) |
---|
356 | |
---|
357 | (test-assert "in" |
---|
358 | (let* ( (source "{{ 'aa' in ['bb', 'aa', 'cc'] }}" )) |
---|
359 | (tval-equal? (Tstr (from-string source)) (Tstr "true")))) |
---|
360 | |
---|
361 | |
---|
362 | (test-assert "is" |
---|
363 | (let* ( (source "{{ 6 is divisibleby 3 }}" )) |
---|
364 | (tval-equal? (Tstr (from-string source)) (Tstr "true")))) |
---|
365 | |
---|
366 | (test-assert "set" |
---|
367 | (let* ( (source "{% set x = 'aa' %}{{ x }}" )) |
---|
368 | (tval-equal? (Tstr (from-string source)) (Tstr "aa")))) |
---|
369 | |
---|
370 | (test-assert "set/with" |
---|
371 | (let* ( (source "{% set x = 'aa' %}{% with x = 'bb', y = 'cc' %}{{ x }}{{ y }}{% endwith %}{{ x }}{{ y }}" )) |
---|
372 | (tval-equal? (Tstr (from-string source)) (Tstr "bbccaa")))) |
---|
373 | |
---|
374 | (test-assert "set/with" |
---|
375 | (let* ( (source "{% set x = '/home' %}{% with x = '/usr', y = '/bin' %}{{ x }}{{ y }}{% endwith %}{{ x }}{{ y }}" )) |
---|
376 | (tval-equal? (Tstr (from-string source)) (Tstr "/usr/bin/home")))) |
---|
377 | |
---|
378 | (test-assert "defined" |
---|
379 | (let* ( (source "{% set obj = { age:10, name: 'aa' } %} {{ obj.age is defined }}" )) |
---|
380 | (tval-equal? (Tstr (from-string source)) (Tstr " true")))) |
---|
381 | |
---|
382 | (test-assert "defined" |
---|
383 | (let* ( (source "{% set obj = { age:10, name: 'aa' } %} {{ obj['name'] is defined }}" )) |
---|
384 | (tval-equal? (Tstr (from-string source)) (Tstr " true")))) |
---|
385 | |
---|
386 | (test-assert "extends" |
---|
387 | (tval-equal? (Tstr (from-file "extends.tmpl" |
---|
388 | env: (template-std-env search-path: '("tests/tmpl" "tmpl")))) |
---|
389 | (Tstr "extended\n\n"))) |
---|
390 | |
---|
391 | (test-assert "include" |
---|
392 | (tval-equal? (Tstr (from-file "include.tmpl" |
---|
393 | env: (template-std-env search-path: '("tests/tmpl" "tmpl")))) |
---|
394 | (Tstr "this is included\n\n"))) |
---|
395 | |
---|
396 | (let ((macro-three-words #<<EOF |
---|
397 | {% macro three_words(one,two,three) %} |
---|
398 | {{one}} {{two}} {{three}}{{caller (' by','chicken','scheme')}} |
---|
399 | {% endmacro %} |
---|
400 | EOF |
---|
401 | )) |
---|
402 | |
---|
403 | |
---|
404 | (test-assert "macro" |
---|
405 | (tval-equal? |
---|
406 | (Tstr (from-string |
---|
407 | (string-append macro-three-words |
---|
408 | "{{ three_words(\"this\", \"is\", \"it!\") }}"))) |
---|
409 | (Tstr "\nthis is it!"))) |
---|
410 | |
---|
411 | (test-assert "caller" |
---|
412 | (tval-equal? |
---|
413 | (Tstr (from-string |
---|
414 | (string-append macro-three-words |
---|
415 | (string-append |
---|
416 | "{% call(a,b,c) three_words('this', 'is', 'it!') %}" |
---|
417 | "{{a}} {{b}} {{c}}"; |
---|
418 | "{% endcall %}") |
---|
419 | ))) |
---|
420 | (Tstr "\nthis is it! by chicken scheme"))) |
---|
421 | |
---|
422 | ) |
---|
423 | |
---|
424 | (test-assert "filter" |
---|
425 | (tval-equal? |
---|
426 | (Tstr (from-string "{% filter upper %}must be upper{% endfilter %}")) |
---|
427 | (Tstr "MUST BE UPPER"))) |
---|
428 | |
---|
429 | |
---|
430 | (test-assert "set" |
---|
431 | (tval-equal? |
---|
432 | (Tstr (from-string "{% set x = \"test\" %}{{x}}")) |
---|
433 | (Tstr "test"))) |
---|
434 | |
---|
435 | (test-assert "make-lexer-table" |
---|
436 | (let* ((lexer-table (make-lexer-table begin-expand: "%{{" end-expand: "%}}" compile: #f)) |
---|
437 | (env (template-std-env lexer-table: lexer-table)) |
---|
438 | (script "<script>alert(1)</script>") |
---|
439 | (output (from-string "%{{danger%}}" |
---|
440 | env: env |
---|
441 | models: (list (cons 'danger (Tstr script))))) |
---|
442 | ) |
---|
443 | (tval-equal? (Tstr output) (op-escape-html (Tstr script) kwargs)))) |
---|
444 | #| |
---|
445 | (test-assert "make-lexer-table (compiled)" |
---|
446 | (let* ((lexer-table (make-lexer-table begin-expand: "%{{" end-expand: "%}}" compile: #t)) |
---|
447 | (env (template-std-env lexer-table: lexer-table)) |
---|
448 | (script "<script>alert(1)</script>") |
---|
449 | (output (from-string "%{{danger%}}" |
---|
450 | env: env |
---|
451 | models: (list (cons 'danger (Tstr script))))) |
---|
452 | ) |
---|
453 | (tval-equal? (Tstr output) (op-escape-html (Tstr script) kwargs)))) |
---|
454 | |# |
---|
455 | |
---|
456 | |
---|
457 | ) |
---|
458 | |
---|
459 | (print (from-file "cheatsheet.tmpl" |
---|
460 | env: (template-std-env search-path: '("tests/tmpl" "tmpl")))) |
---|
461 | |
---|