-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest-markup.lisp
More file actions
407 lines (342 loc) · 11.1 KB
/
test-markup.lisp
File metadata and controls
407 lines (342 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
;; Copyright 2019, Modern Interpreters Inc
;; -*- coding: utf-8 -*-
(defpackage #:test-markup
(:use #:markup
#:fiveam
#:cl)
(:import-from #:markup/markup
#:+empty+
#:empty-attribute
#:make-toplevel-node
#:read-tag)
(:import-from #:markup/optimizer
#:*disable-optimizer*))
(in-package #:test-markup)
(named-readtables:in-readtable markup:syntax)
(def-suite* :markup)
(def-suite* :markup.test-markup :in :markup)
(def-fixture state ()
(&body)
(let ((*disable-optimizer* t))
(&body)))
(test simple
(with-fixture state ()
(is (equal 2 (+ 1 1)))))
(test read-tag
(with-fixture state ()
(is (equal "foo" (read-tag (make-string-input-stream "foo>"))))
(is (equal "foo" (read-tag (make-string-input-stream "foo bar>"))))))
(test read-xml
(with-fixture state ()
(is (equal
'(make-xml-tag :foo)
(read-xml-from-string "<:foo />") ))))
(test read-child
(with-fixture state ()
(is (equal
'(make-xml-tag :foo :children
(list (make-xml-tag :bar)))
(read-xml-from-string "<:foo><:bar /></:foo>")))
(is (equal
'(make-xml-tag :foo :children
(list " " (make-xml-tag :bar)))
(read-xml-from-string "<:foo> <:bar /></:foo>")))
(is (equal
'(make-xml-tag :foo :children
(list " " (make-xml-tag :bar :children (list " "))))
(read-xml-from-string "<:foo > <:bar> </:bar></:foo>")))))
(test read-strings
(with-fixture state ()
(is (equal
'(make-xml-tag :foo :children
(list "blah"))
(read-xml-from-string "<:foo>blah</:foo>")))))
(test read-empty-attribute
(with-fixture state ()
(is (equal
'(make-xml-tag :foo :attributes
(list (cons "car" +empty+)))
(read-xml-from-string "<:foo car/>")))))
(test read-attributes
(with-fixture state ()
(is (equal
'(make-xml-tag :foo :attributes
(list (cons "car" "bar")))
(read-xml-from-string "<:foo car=\"bar\"></:foo>")))))
(test reader
(with-fixture state ()
(is (equal
'(make-toplevel-node
(make-xml-tag :foo :children
(list (make-xml-tag :bar))))
(quote <:foo><:bar></:bar></:foo>)))))
(test reader-with-attr
(with-fixture state ()
(is (equal
'(make-toplevel-node
(make-xml-tag :foo :attributes (list (cons "car" "bar"))))
(quote <:foo car="bar"></:foo>)))))
(test write-html
(with-fixture state ()
(is (equal
"<foo></foo>"
(markup:write-html <:foo></:foo>)))
(is (equal
"<foo><bar></bar><car></car></foo>"
(markup:write-html <:foo><:bar></:bar><:car></:car></:foo>)))))
(test void-tag
(with-fixture state ()
(is (equal
"<br />"
(markup:write-html <:br />)))
(is (equal
"<br />"
(markup:write-html <br />)))))
(test write-html-string
(with-fixture state ()
(is (equal
"<foo>car dar</foo>"
(markup:write-html <:foo>car dar</:foo>)))))
(test write-attributes
(with-fixture state ()
(is (equal
"<foo bar=\"car\"></foo>"
(markup:write-html <:foo bar="car"></:foo>)))))
(deftag simple-wrapper ()
<div></div>)
(test function-calls
(with-fixture state ()
(is (equal
(markup:write-html <div></div>)
(markup:write-html <simple-wrapper></simple-wrapper>)))))
(test escaping
(with-fixture state ()
(let ((val "blah"))
(is (equal
(markup:write-html <div>blah</div>)
(markup:write-html <div>,(progn val)</div>))))))
(test escaping-in-between
(with-fixture state ()
(let ((val "blah"))
(is (equal
(markup:write-html <div>1blah2</div>)
(markup:write-html <div>1,(progn val)2</div>))))))
(test escaping-with-space-in-between
(with-fixture state ()
(let ((val "blah"))
(is (equal
(markup:write-html <div>blah car</div>)
(markup:write-html <div>,(progn val) ,(progn "car")</div>))))))
(test escaping-attr-vals
(with-fixture state ()
(let ((val "blah"))
(is (equal
(markup:write-html <div arg="blah"></div>)
(markup:write-html <div arg=(progn val)></div>))))))
(test escaping-attr-vals-2
(with-fixture state ()
(let ((val "blah"))
(is (equal
(markup:write-html <div arg="blah"></div>)
(markup:write-html <div arg=val ></div>))))))
(markup:deftag wrapper (children &key foo)
(declare (ignore children))
<:h1>Foo was ,(progn foo)</:h1>)
(test function-calls-3
(with-fixture state ()
(is (equal
(markup:write-html <:h1>Foo was blah</:h1>)
(markup:write-html <wrapper foo="blah"></wrapper>)))
(is (equal
(markup:write-html <:h1>Foo was </:h1>)
(markup:write-html <wrapper></wrapper>)))))
(test call-wrapper-as-function
(with-fixture state ()
(is (equal
(markup:write-html <:h1>Foo was blah</:h1>)
(markup:write-html (wrapper :foo "blah"))))))
(markup:deftag wrapper2 (children &key (foo "defaultvalue"))
(declare (ignore children))
<:h1>Foo was ,(progn foo)</:h1>)
(test function-calls-2
(with-fixture state ()
(is (equal
(markup:write-html <:h1>Foo was blah</:h1>)
(markup:write-html <wrapper2 foo="blah"></wrapper2>)))
(is (equal
(markup:write-html <:h1>Foo was defaultvalue</:h1>)
(markup:write-html <wrapper2></wrapper2>)))))
(markup:deftag wrapper-with-children (children &key (foo "defaultvalue"))
<:h1>Foo was ,(progn foo),@(progn children)</:h1>)
(test function-calls-wrappre
(with-fixture state ()
(is (equal
(markup:write-html <:h1>Foo was blah</:h1>)
(markup:write-html <wrapper-with-children foo="blah"></wrapper-with-children>)))
(is (equal
(markup:write-html <:h1>Foo was defaultvalue<:blah></:blah></:h1>)
(markup:write-html <wrapper-with-children><:blah></:blah></wrapper-with-children>)))))
(test function-calls-wrapper-as-func
(with-fixture state ()
(is (equal
(markup:write-html <:h1>Foo was blah</:h1>)
(markup:write-html
(wrapper-with-children :foo "blah"))))
(is (equal
(markup:write-html <:h1>Foo was defaultvalue<:blah></:blah></:h1>)
(markup:write-html
(wrapper-with-children
<:blah></:blah>))))))
(test commas
(with-fixture state ()
(is (equal
"<h1>Hello, world</h1>"
(markup:write-html <:h1>Hello, world</:h1>)))))
(markup:deftag without-key (children)
(declare (ignore children))
<h1>hello world</h1>)
(test without-key
(with-fixture state ()
(is (equal "<h1>hello world</h1>"
(markup:write-html <without-key />)))))
(markup:deftag without-children ()
<h1>hello world</h1>)
(test without-children
(with-fixture state ()
(is (equal "<h1>hello world</h1>"
(markup:write-html <without-children />)))))
(markup:deftag with-only-key (&key att)
<h1>hello ,(progn att)</h1>)
(test with-only-key
(with-fixture state ()
(is (equal "<h1>hello zoidberg</h1>"
(markup:write-html <with-only-key att="zoidberg" />)))))
(test operators
(with-fixture state ()
(is (equal t (< 1 2)))
(is (equal nil (< 2 1)))
(is (equal t (<= 1 2)))))
(test escaping-1
(with-fixture state ()
(let ((body "<body>"))
(is (equal
"<h1><body></h1>"
(markup:write-html <:h1>,(progn body)</:h1>))))
(let ((body "\"body\""))
(is (equal
"<h1 arg=\""body"\">foo</h1>"
(markup:write-html <:h1 arg=(progn body)>foo</:h1>))))))
(test unescaped-string
(with-fixture state ()
(let ((body "<h1></h1>"))
(is (equal
"<body><h1></h1></body>"
(markup:write-html <:body>,(progn (markup:unescaped body))</:body>))))))
(test reading-void-tags
(with-fixture state ()
(is (equal
"<img />"
(markup:write-html <img>)))
(is (equal
"<p><img /></p>"
(markup:write-html <p><img></p>)))))
(test comments
(with-fixture state ()
(is (equal
"<body> <!-- this is a test --></body>"
(markup:write-html <:body> <!-- this is a test --></:body>)))))
(test comments-without-prefix-space
(with-fixture state ()
(is (equal
"<body> <!--nospace --></body>"
(markup:write-html <:body> <!--nospace --></:body>)))))
(test comments-without-space
(with-fixture state ()
(is (equal
"<body> <!--nospace--></body>"
(markup:write-html <:body> <!--nospace--></:body>)))))
(test comments-with-multiple-hyphens
(with-fixture state ()
(is (equal
"<body> <!----nospace----></body>"
(markup:write-html <:body> <!----nospace----></:body>)))))
(test />-without-space
(with-fixture state ()
(is (equal
"<body></body>"
(markup:write-html <:body/>)))))
(test undefined-tag-signals-condition
(with-fixture state ()
;; this convoluted eval avoids a style-warning while running the tests
(signals undefined-markup-tag-condition (eval '<undefined />))))
(test default-escaping
(with-fixture state ()
(is (equal
"<body>News & Events</body>"
(markup:write-html <body>News & Events</body>
)))))
(test but-escapes-inline-commas
(with-fixture state ()
(let ((val "News & Events"))
(is (equal
"<body>News & Events</body>"
(markup:write-html <body>,(progn val)</body>))))))
(test utf-8
(with-fixture state ()
(is (equal
"<h1>they’re</h1>"
(markup:write-html <h1>they’re</h1>)))))
(test write-empty-attribute
(with-fixture state ()
(is (equal
"<foo car></foo>"
(markup:write-html <:foo car/>)))))
(defun compiled-expr (val)
<:option selected=val />)
(test write-nil-attribute ()
(with-fixture state ()
(let ((val nil))
(is (equal "<option></option>"
(markup:write-html (compiled-expr val))))
(is (equal "<option selected=\"car\"></option>"
(markup:write-html (compiled-expr "car")))))))
(test |escaping-inside-,@|
(let ((val "<script>alert(1)</script>"))
(is
(equal
"<a><script>alert(1)</script></a>"
(markup:write-html
<a>,(progn val)</a>)))
(is
(equal
"<a><script>alert(1)</script></a>"
(markup:write-html
<a>,@(list val)</a>)))
(is
(equal
"<a><script>alert(1)</script><b>hello</b></a>"
(markup:write-html
<a>,@(list val <b>hello</b>)</a>)))
(is
(equal
"<a><b>hello world</b></a>"
(markup:write-html
<a>,@(list <b>hello ,@ (list "world")</b>)</a>)))))
(markup:deftag xyz1 (children)
<a>,@(progn children)</a>)
(test a-weird-complex-interaction-of-escapes
(let ((var "foobar"))
(is
(equal "<a>foobar</a>"
(markup:write-html
<xyz1>,(progn var)</xyz1>)))))
(test unescaped-remains-unescaped-interaction-of-escapes
(let ((var "<h1></h1>"))
(is
(equal "<a><h1></h1></a>"
(markup:write-html
<a>,(markup:unescaped var)</a>)))
(is
(equal "<a><h1></h1></a>"
(markup:write-html
<xyz1>,(markup:unescaped var)</xyz1>)))))