1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | fs = require('fs')
|
19 | path = require('path')
|
20 | Liquid = require('huginn-liquid')
|
21 |
|
22 | _lib = path.resolve(__dirname, '..', 'lib')
|
23 | Liquid.Template.registerFilter require("#{_lib}/filters.coffee")
|
24 |
|
25 |
|
26 |
|
27 | for $name in fs.readdirSync("#{_lib}/tags")
|
28 | $tag = require("#{_lib}/tags/#{$name}")
|
29 | $tag Liquid, _site, build
|
30 |
|
31 |
|
32 |
|
33 | describe 'liquid', ->
|
34 |
|
35 |
|
36 | describe 'Verify API', ->
|
37 |
|
38 |
|
39 | it "methods", ->
|
40 |
|
41 | Liquid.should.be.an 'object'
|
42 | Liquid.should.have.property 'Template'
|
43 | Liquid.should.have.property 'Drop'
|
44 | Liquid.should.have.property 'Tag'
|
45 | Liquid.should.have.property 'Block'
|
46 |
|
47 |
|
48 | describe "Plain text pass-thru", ->
|
49 |
|
50 |
|
51 | it "'plain text'", ->
|
52 |
|
53 | render('plain text').should.equal 'plain text'
|
54 |
|
55 |
|
56 | describe "Testing variables...", ->
|
57 |
|
58 |
|
59 | it "{{ 'string literal' }}", ->
|
60 |
|
61 | render('{{"string literal"}}').should.equal 'string literal'
|
62 | render('{{ "string literal" }}').should.equal 'string literal'
|
63 | render("{{'string literal'}}").should.equal 'string literal'
|
64 | render("{{ 'string literal' }}").should.equal 'string literal'
|
65 | render("{{'string \"literal\"'}}").should.equal 'string "literal"'
|
66 | render("{{ 'string \"literal\"' }}").should.equal 'string "literal"'
|
67 |
|
68 |
|
69 | it "{{ 10 }}", ->
|
70 | render('{{10}}').should.equal '10'
|
71 | render('{{ 10 }}').should.equal '10'
|
72 |
|
73 |
|
74 | it "{{ 5.5 }}", ->
|
75 | render('{{5.5}}').should.equal '5.5'
|
76 | render('{{ 5.5 }}').should.equal '5.5'
|
77 |
|
78 |
|
79 | it "{{ (1..5) }}", ->
|
80 | render('{{(1..5)}}').should.equal '1,2,3,4,5'
|
81 | render('{{ (1..5) }}').should.equal '1,2,3,4,5'
|
82 |
|
83 |
|
84 |
|
85 | it "{{ (a..e) }}", ->
|
86 | render('{{(a..e)}}').should.equal 'a,b,c,d,e'
|
87 |
|
88 |
|
89 | it "{{ varname }}", ->
|
90 | render("{{ user }}", {user:'Bob'}).should.equal 'Bob'
|
91 |
|
92 |
|
93 | it "{{ parent.child }}", ->
|
94 | render("{{ user.name }}", {user:{ name:'Bob' }}).should.equal 'Bob'
|
95 |
|
96 |
|
97 | it "{{ collection[0] }}", ->
|
98 | render("{{ users[0] }}", {users:['Bob']}).should.equal 'Bob'
|
99 |
|
100 |
|
101 | it "{{ collection[0].child }}", ->
|
102 | render("{{ users[0].name }}", {users:[{name:'Bob'}]}).should.equal 'Bob'
|
103 |
|
104 |
|
105 |
|
106 | describe "Testing filters...", ->
|
107 |
|
108 |
|
109 | it "{{ string | size }}", ->
|
110 | render("{{user | size}}", {user:'Bob'}).should.equal '3'
|
111 | render("{{user|size}}", {user:'Bob'}).should.equal '3'
|
112 | render("{{ user | size }}", {user:'Bob'}).should.equal '3'
|
113 |
|
114 |
|
115 | it "{{ collection | size }}", ->
|
116 | render("{{user|size}}", {user:['','','']}).should.equal '3'
|
117 | render("{{ user | size }}", {user:['','','']}).should.equal '3'
|
118 |
|
119 |
|
120 | it "{{ string | upcase }}", ->
|
121 | render("{{user|upcase}}", {user:'Bob'}).should.equal 'BOB'
|
122 | render("{{ user | upcase }}", {user:'Bob'}).should.equal 'BOB'
|
123 |
|
124 |
|
125 | it "{{ string | downcase }}", ->
|
126 | render("{{user|downcase}}", {user:'Bob'}).should.equal 'bob'
|
127 | render("{{ user | downcase }}", {user:'Bob'}).should.equal 'bob'
|
128 |
|
129 |
|
130 | it "{{ string | capitalize }}", ->
|
131 | render("{{user|capitalize}}", {user:'bob'}).should.equal 'Bob'
|
132 | render("{{ user | capitalize }}", {user:'bob'}).should.equal 'Bob'
|
133 |
|
134 |
|
135 | it "{{ string | escape }}", ->
|
136 | render("{{'<br/>'|escape}}", {user:'bob'}).should.equal '<br/>'
|
137 | render("{{ '<br/>' | escape }}", {user:'bob'}).should.equal '<br/>'
|
138 | render("{{ 'this & \"that\"' | escape }}", {user:'bob'}).should.equal 'this & "that"'
|
139 |
|
140 |
|
141 | it "{{ string | truncate }}", ->
|
142 |
|
143 | render("{{'I am the very model of a modern major general, really.'|truncate}}")
|
144 | .should.equal 'I am the very model of a modern major general, rea...'
|
145 |
|
146 | render("{{'I am the very model of a modern major general, really.' | truncate}}")
|
147 | .should.equal 'I am the very model of a modern major general, rea...'
|
148 |
|
149 |
|
150 | it "{{ string | truncate:2 }}", ->
|
151 | render("{{user|truncate:2}}", {user:'Bob'}).should.equal 'Bo...'
|
152 | render("{{ user | truncate:2 }}", {user:'Bob'}).should.equal 'Bo...'
|
153 | render("{{ user | truncate: 2 }}", {user:'Bob'}).should.equal 'Bo...'
|
154 |
|
155 |
|
156 | it "{{ string | truncate:1,'-' }}", ->
|
157 | render("{{user|truncate:1,'-'}}", {user:'Bob'}).should.equal 'B-'
|
158 | render("{{ user | truncate:1,'-' }}", {user:'Bob'}).should.equal 'B-'
|
159 | render("{{ user | truncate: 1,'-' }}", {user:'Bob'}).should.equal 'B-'
|
160 | render("{{ user | truncate: 1, '-' }}", {user:'Bob'}).should.equal 'B-'
|
161 |
|
162 |
|
163 | it "{{ string | truncatewords }}", ->
|
164 | render("{{'a b c d e f g h i j k l m n o p q r s t u v w x y z'|truncatewords}}")
|
165 | .should.equal 'a b c d e f g h i j k l m n o...'
|
166 | render("{{ 'a b c d e f g h i j k l m n o p q r s t u v w x y z' | truncatewords }}")
|
167 | .should.equal 'a b c d e f g h i j k l m n o...'
|
168 |
|
169 |
|
170 | it "{{ string | truncatewords:5 }}", ->
|
171 | render("{{'a b c d e f g h i j k l m n o p q r s t u v w x y z'|truncatewords:5}}")
|
172 | .should.equal 'a b c d e...'
|
173 | render("{{ 'a b c d e f g h i j k l m n o p q r s t u v w x y z' | truncatewords:5 }}")
|
174 | .should.equal 'a b c d e...'
|
175 |
|
176 |
|
177 | it "{{ string | truncatewords:5,'-' }}", ->
|
178 | render("{{'a b c d e f g h i j k l m n o p q r s t u v w x y z'|truncatewords:5,'-'}}")
|
179 | .should.equal 'a b c d e-'
|
180 | render("{{ 'a b c d e f g h i j k l m n o p q r s t u v w x y z' | truncatewords:5,'-' }}")
|
181 | .should.equal 'a b c d e-'
|
182 |
|
183 |
|
184 | it "{{ string | strip_html }}", ->
|
185 | render("{{'hello <b>bob</b>'|strip_html}}")
|
186 | .should.equal 'hello bob'
|
187 | render("{{ 'hello <b>bob</b>' | strip_html }}")
|
188 | .should.equal 'hello bob'
|
189 |
|
190 |
|
191 | it "{{ string | strip_newlines }}", ->
|
192 |
|
193 | src = "\nhello \nbob \n\nold\n friend\n"
|
194 |
|
195 | render("{{src|strip_newlines}}", {src:src})
|
196 | .should.equal 'hello bob old friend'
|
197 | render("{{ src | strip_newlines }}", {src:src})
|
198 | .should.equal 'hello bob old friend'
|
199 |
|
200 |
|
201 | it "{{ collection | join }}", ->
|
202 | render("{{(1..3)|join}}").should.equal "1 2 3"
|
203 | render("{{ (1..3) | join }}").should.equal "1 2 3"
|
204 |
|
205 |
|
206 | it "{{ collection | join:',' }}", ->
|
207 | render("{{(1..3)|join:','}}").should.equal "1,2,3"
|
208 | render("{{ (1..3) | join:',' }}").should.equal "1,2,3"
|
209 |
|
210 |
|
211 | it "{{ collection | sort }}", ->
|
212 | render("{{c|sort}}", {c:[2,1,3]}).should.equal "1,2,3"
|
213 | render("{{ c | sort }}", {c:[2,1,3]}).should.equal "1,2,3"
|
214 | render("{{(1..3)|sort}}").should.equal "1,2,3"
|
215 | render("{{ (1..3) | sort }}").should.equal "1,2,3"
|
216 |
|
217 | it "{{ collection | reverse }}", ->
|
218 | render("{{(1..3)|reverse}}").should.equal "3,2,1"
|
219 | render("{{ (1..3) | reverse }}").should.equal "3,2,1"
|
220 | render("{{c|reverse}}", {c:[1,2,3]}).should.equal "3,2,1"
|
221 | render("{{ c | reverse }}", {c:[1,2,3]}).should.equal "3,2,1"
|
222 |
|
223 | it "{{ string | relace:string }}", ->
|
224 | render("{{'bananas'|replace:'a'}}").should.equal "bnns"
|
225 | render("{{ 'bananas' | replace:'a' }}").should.equal "bnns"
|
226 |
|
227 | it "{{ string | relace_first:string }}", ->
|
228 | render("{{'bananas'|replace_first:'a'}}").should.equal "bnanas"
|
229 | render("{{ 'bananas' | replace_first:'a' }}").should.equal "bnanas"
|
230 |
|
231 | it "{{ string | newline_to_br }}", ->
|
232 | src = "Hello,\nHow are you?\nI'm glad to here it."
|
233 | exp = "Hello,<br/>\nHow are you?<br/>\nI'm glad to here it."
|
234 | render("{{src|newline_to_br}}", {src:src}).should.equal exp
|
235 | render("{{ src | newline_to_br }}", {src:src}).should.equal exp
|
236 |
|
237 | it "{{ 'now' | date:'format' }}", ->
|
238 | exp = String((new Date()).getFullYear())
|
239 | render("{{'now' | date: '%Y'}}", {}).should.equal exp
|
240 |
|
241 | it "{{ date | date:'format' }}", ->
|
242 | src = new Date('8/30/2008')
|
243 | exp = "08.30.2008"
|
244 | fmt = "%m.%d.%Y"
|
245 | render("{{src|date:'%m.%d.%Y'}}", {src:src, fmt:fmt}).should.equal exp
|
246 | render("{{ src | date:'%m.%d.%Y' }}", {src:src, fmt:fmt}).should.equal exp
|
247 | render("{{src|date:fmt}}", {src:src, fmt:fmt}).should.equal exp
|
248 | render("{{ src | date:fmt }}", {src:src, fmt:fmt}).should.equal exp
|
249 |
|
250 |
|
251 | it "{{ collection | first }}", ->
|
252 | render("{{(1..3)|first}}").should.equal "1"
|
253 | render("{{ (1..3) | first }}").should.equal "1"
|
254 | render("{{c|first}}", {c:[1,2,3]}).should.equal "1"
|
255 | render("{{ c | first }}", {c:[1,2,3]}).should.equal "1"
|
256 |
|
257 | it "{{ collection | last }}", ->
|
258 | render("{{(1..3)|last}}").should.equal "3"
|
259 | render("{{ (1..3) | last }}").should.equal "3"
|
260 | render("{{c|last}}", {c:[1,2,3]}).should.equal "3"
|
261 | render("{{ c | last }}", {c:[1,2,3]}).should.equal "3"
|
262 |
|
263 |
|
264 | describe "Testing tags...", ->
|
265 |
|
266 | it "{% assign varname = value %}", ->
|
267 | tmpl = Liquid.parse("{% assign myVar = 'VALUE' %}.{{ myVar }}.");
|
268 | tmpl.render().should.equal '.VALUE.'
|
269 |
|
270 | tmpl = Liquid.parse("{% assign myVar = 10 %}.{{ myVar }}.");
|
271 | tmpl.render().should.equal '.10.'
|
272 |
|
273 | tmpl = Liquid.parse("{% assign myVar = 5.5 %}.{{ myVar }}.");
|
274 | tmpl.render().should.equal '.5.5.'
|
275 |
|
276 | tmpl = Liquid.parse("{% assign myVar = (1..3) %}.{{ myVar }}.");
|
277 | tmpl.render().should.equal ".1,2,3."
|
278 |
|
279 |
|
280 | tmpl = Liquid.parse("{% assign myVar = 'foo' %}");
|
281 | tmpl.render().should.equal ''
|
282 |
|
283 |
|
284 | it "{% capture varname %} content {% endcapture %}", ->
|
285 | src = "{% capture myContent %}Good 'old content!{% endcapture %}Before {{ myContent }}";
|
286 | Liquid.parse(src).render().should.equal "Before Good 'old content!"
|
287 |
|
288 |
|
289 | it "{% case conditionLeft %} {% when conditionRight %} {% else %} {% endcase %}", ->
|
290 | src = """
|
291 | {% case testVar %}
|
292 | {% when 1 %} One!{% when 2 %} Two!{% when 'test' %} Test!{% else %} Got me{% endcase %}
|
293 | """
|
294 | tmpl = Liquid.parse(src)
|
295 |
|
296 | tmpl.render({ testVar:1 }).should.equal " One!"
|
297 | tmpl.render({ testVar:2 }).should.equal " Two!"
|
298 | tmpl.render({ testVar:'test' }).should.equal " Test!"
|
299 | tmpl.render({ testVar:null }).should.equal " Got me"
|
300 | tmpl.render({ }).should.equal " Got me"
|
301 |
|
302 | it "{% comment %} content {% endcomment %}", ->
|
303 | render("{% comment %} I'm a comment! {% endcomment %}").should.equal ""
|
304 |
|
305 | it "{% cycle 'odd', 'even' %}", ->
|
306 | src = "{% cycle 'odd', 'even' %} {% cycle 'odd', 'even' %} {% cycle 'odd', 'even' %}";
|
307 | render(src).should.equal 'odd even odd'
|
308 |
|
309 | src = "{% cycle 'odd', 'even' %}{% cycle 'odd', 'even' %}{% cycle 'odd', 'even' %}";
|
310 | render(src).should.equal 'oddevenodd'
|
311 |
|
312 | it "{% for item in collection %}{% endfor %}", ->
|
313 | render("{% for item in (1..3) %}{{ item }}{% endfor %}").should.equal "123"
|
314 | render("{% for item in (1..3) %} {{ forloop.index }} {% endfor %}").should.equal " 1 2 3 "
|
315 | render("{% for item in (1..3) %} {{ forloop.index0 }} {% endfor %}").should.equal " 0 1 2 "
|
316 | render("{% for item in (1..3) %} {{ forloop.first }} {% endfor %}").should.equal " true false false "
|
317 | render("{% for item in (1..3) %} {{ forloop.last }} {% endfor %}").should.equal " false false true "
|
318 |
|
319 | it "{% if conditions %}{% else %}{% endif %}", ->
|
320 | render("{% if true %}TRUE{% endif %}").should.equal "TRUE"
|
321 | render("{% if 1 == 1 %}TRUE{% endif %}").should.equal "TRUE"
|
322 | render("{% if 1 != 1 %}TRUE{% endif %}").should.equal ""
|
323 | render("{% if 1 > 1 %}TRUE{% endif %}").should.equal ""
|
324 | render("{% if 1 < 1 %}TRUE{% endif %}").should.equal ""
|
325 | render("{% if 1 <= 1 %}TRUE{% endif %}").should.equal "TRUE"
|
326 | render("{% if 1 >= 1 %}TRUE{% endif %}").should.equal "TRUE"
|
327 | render("{% if 'Test' contains 'T' %}TRUE{% endif %}").should.equal "TRUE"
|
328 |
|
329 | render("{% if true %}TRUE{% else %}FALSE{% endif %}").should.equal "TRUE"
|
330 | render("{% if 1 == 1 %}TRUE{% else %}FALSE{% endif %}").should.equal "TRUE"
|
331 | render("{% if 1 != 1 %}TRUE{% else %}FALSE{% endif %}").should.equal "FALSE"
|
332 | render("{% if 1 > 1 %}TRUE{% else %}FALSE{% endif %}").should.equal "FALSE"
|
333 | render("{% if 1 < 1 %}TRUE{% else %}FALSE{% endif %}").should.equal "FALSE"
|
334 | render("{% if 1 <= 1 %}TRUE{% else %}FALSE{% endif %}").should.equal "TRUE"
|
335 | render("{% if 1 >= 1 %}TRUE{% else %}FALSE{% endif %}").should.equal "TRUE"
|
336 | render("{% if 'Test' contains 'T' %}TRUE{% else %}FALSE{% endif %}").should.equal "TRUE"
|
337 |
|
338 | it "{% ifchanged %}{% endifchanged %}", ->
|
339 | render("{% for item in col %}{% ifchanged %}{{ item }}{% endifchanged %}{% endfor %}", {col:[1,1,1,2,2,2]})
|
340 | .should.equal "12"
|
341 |
|
342 | it "{% include 'templateName' %}", ->
|
343 | Liquid.readTemplateFile = (path) ->
|
344 | if(path == 'simple')
|
345 | return "simple INCLUDED!";
|
346 | else
|
347 | return "{{ data }} INCLUDED!";
|
348 |
|
349 | render("{% include 'simple' %}").should.equal "simple INCLUDED!"
|
350 | render("{% include 'variable' with data:'Data' %}").should.equal "Data INCLUDED!"
|
351 |
|
352 | it "{% unless conditions %}{% else %}{% endunless %}", ->
|
353 | render("{% unless true %}TRUE{% endunless %}").should.equal ""
|
354 | render("{% unless 1 == 1 %}TRUE{% endunless %}").should.equal ""
|
355 | render("{% unless 1 != 1 %}TRUE{% endunless %}").should.equal "TRUE"
|
356 | render("{% unless 1 > 1 %}TRUE{% endunless %}").should.equal "TRUE"
|
357 | render("{% unless 1 < 1 %}TRUE{% endunless %}").should.equal "TRUE"
|
358 | render("{% unless 1 <= 1 %}TRUE{% endunless %}").should.equal ""
|
359 | render("{% unless 1 >= 1 %}TRUE{% endunless %}").should.equal ""
|
360 | render("{% unless 'Test' contains 'T' %}TRUE{% endunless %}").should.equal ""
|
361 |
|
362 | render("{% unless true %}TRUE{% else %}FALSE{% endunless %}").should.equal "FALSE"
|
363 | render("{% unless 1 == 1 %}TRUE{% else %}FALSE{% endunless %}").should.equal "FALSE"
|
364 | render("{% unless 1 != 1 %}TRUE{% else %}FALSE{% endunless %}").should.equal "TRUE"
|
365 | render("{% unless 1 > 1 %}TRUE{% else %}FALSE{% endunless %}").should.equal "TRUE"
|
366 | render("{% unless 1 < 1 %}TRUE{% else %}FALSE{% endunless %}").should.equal "TRUE"
|
367 | render("{% unless 1 <= 1 %}TRUE{% else %}FALSE{% endunless %}").should.equal "FALSE"
|
368 | render("{% unless 1 >= 1 %}TRUE{% else %}FALSE{% endunless %}").should.equal "FALSE"
|
369 | render("{% unless 'Test' contains 'T' %}TRUE{% else %}FALSE{% endunless %}").should.equal "FALSE"
|
370 |
|
371 |
|
372 | describe "Testing context...", ->
|
373 | it "{{ collection['missing_key'].value }}", ->
|
374 | render("{{ collection['missing_key'].value }}").should.equal ""
|
375 | render("{{ collection['missing_key'].value }}", {collection: {}}).should.equal ""
|