1 | (function() {
|
2 | var expect, util, lib;
|
3 |
|
4 | if(typeof require != 'undefined') {
|
5 | expect = require('expect.js');
|
6 | util = require('./util');
|
7 | lib = require('../src/lib');
|
8 | }
|
9 | else {
|
10 | expect = window.expect;
|
11 | util = window.util;
|
12 | lib = nunjucks.require('lib');
|
13 | }
|
14 |
|
15 | var render = util.render;
|
16 | var equal = util.equal;
|
17 | var finish = util.finish;
|
18 |
|
19 | describe('filter', function() {
|
20 | it('abs', function(done) {
|
21 | equal('{{ -3|abs }}', '3');
|
22 | equal('{{ -3.456|abs }}', '3.456');
|
23 | finish(done);
|
24 | });
|
25 |
|
26 | it('batch', function(done) {
|
27 | equal('{% for a in [1,2,3,4,5,6]|batch(2) %}' +
|
28 | '-{% for b in a %}' +
|
29 | '{{ b }}' +
|
30 | '{% endfor %}-' +
|
31 | '{% endfor %}',
|
32 |
|
33 | '-12--34--56-');
|
34 |
|
35 | finish(done);
|
36 | });
|
37 |
|
38 | it('capitalize', function(done) {
|
39 | equal('{{ "foo" | capitalize }}', 'Foo');
|
40 | finish(done);
|
41 | });
|
42 |
|
43 | it('center', function(done) {
|
44 | equal('{{ "fooo" | center }}',
|
45 | lib.repeat(' ', 38) + 'fooo' +
|
46 | lib.repeat(' ', 38));
|
47 |
|
48 | equal('{{ "foo" | center }}',
|
49 | lib.repeat(' ', 38) + 'foo' +
|
50 | lib.repeat(' ', 39));
|
51 | finish(done);
|
52 | });
|
53 |
|
54 | it('default', function(done) {
|
55 | equal('{{ false | default("foo") }}', 'foo');
|
56 | equal('{{ "bar" | default("foo") }}', 'bar');
|
57 | finish(done);
|
58 | });
|
59 |
|
60 | it('escape', function(done) {
|
61 | equal('{{ "<html>" | escape }}', '<html>');
|
62 | finish(done);
|
63 | });
|
64 |
|
65 | it("dictsort", function(done) {
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | equal('{% for item in items | dictsort %}' +
|
73 | '{{ item[0] }}{% endfor %}',
|
74 | {
|
75 | items: {
|
76 | "e": 1,
|
77 | "d": 2,
|
78 | "c": 3,
|
79 | "a": 4,
|
80 | "f": 5,
|
81 | "b": 6
|
82 | }
|
83 | },
|
84 | 'abcdef');
|
85 |
|
86 |
|
87 | equal('{% for item in items | dictsort(true) %}' +
|
88 | '{{ item[0] }},{% endfor %}', {
|
89 | items: {
|
90 | "ABC": 6,
|
91 | "ABc": 5,
|
92 | "Abc": 1,
|
93 | "abc": 2
|
94 | }
|
95 | },
|
96 | "ABC,ABc,Abc,abc,");
|
97 |
|
98 |
|
99 | equal('{% for item in items | dictsort(false, "value") %}' +
|
100 | '{{ item[0] }}{% endfor %}', {
|
101 | items: {
|
102 | "a": 6,
|
103 | "b": 5,
|
104 | "c": 1,
|
105 | "d": 2
|
106 | }
|
107 | },
|
108 | 'cdba');
|
109 |
|
110 | finish(done);
|
111 | });
|
112 |
|
113 | it('first', function(done) {
|
114 | equal('{{ [1,2,3] | first }}', '1');
|
115 | finish(done);
|
116 | });
|
117 |
|
118 | it('float/int', function(done) {
|
119 | equal('{{ "3.5" | float }}', '3.5');
|
120 | equal('{{ "3.5" | int }}', '3');
|
121 | equal('{{ "0" | int }}', '0');
|
122 | equal('{{ "0" | float }}', '0');
|
123 | equal('{{ "bob" | int("cat") }}', 'cat');
|
124 | equal('{{ "bob" | float("cat") }}', 'cat');
|
125 |
|
126 | finish(done);
|
127 | });
|
128 |
|
129 | it('groupby', function(done) {
|
130 | equal('{% for type, items in items | groupby("type") %}' +
|
131 | ':{{ type }}:' +
|
132 | '{% for item in items %}' +
|
133 | '{{ item.name }}' +
|
134 | '{% endfor %}' +
|
135 | '{% endfor %}',
|
136 | { items: [{ name: 'james',
|
137 | type: 'green' },
|
138 | { name: 'john',
|
139 | type: 'blue' },
|
140 | { name: 'jim',
|
141 | type: 'blue' },
|
142 | { name: 'jessie',
|
143 | type: 'green' }]},
|
144 | ':green:jamesjessie:blue:johnjim');
|
145 |
|
146 | finish(done);
|
147 | });
|
148 |
|
149 | it('indent', function(done) {
|
150 | equal('{{ "one\ntwo\nthree" | indent }}',
|
151 | 'one\n two\n three\n');
|
152 | equal('{{ "one\ntwo\nthree" | indent(2) }}',
|
153 | 'one\n two\n three\n');
|
154 | equal('{{ "one\ntwo\nthree" | indent(2, true) }}',
|
155 | ' one\n two\n three\n');
|
156 | finish(done);
|
157 | });
|
158 |
|
159 | it('join', function(done) {
|
160 | equal('{{ items | join }}',
|
161 | { items: [1, 2, 3] },
|
162 | '123');
|
163 |
|
164 | equal('{{ items | join(",") }}',
|
165 | { items: ['foo', 'bar', 'bear'] },
|
166 | 'foo,bar,bear');
|
167 |
|
168 | equal('{{ items | join(",", "name") }}',
|
169 | { items: [{ name: 'foo' },
|
170 | { name: 'bar' },
|
171 | { name: 'bear' }] },
|
172 | 'foo,bar,bear');
|
173 | finish(done);
|
174 | });
|
175 |
|
176 | it('last', function(done) {
|
177 | equal('{{ [1,2,3] | last }}', '3');
|
178 | finish(done);
|
179 | });
|
180 |
|
181 | it('length', function(done) {
|
182 | equal('{{ [1,2,3] | length }}', '3');
|
183 | finish(done);
|
184 | });
|
185 |
|
186 | it('length handle undefined variables', function(done) {
|
187 | equal('{{ blah|length }}', '0');
|
188 | finish(done);
|
189 | });
|
190 |
|
191 | it('list', function(done) {
|
192 | equal('{% for i in "foobar" | list %}{{ i }},{% endfor %}',
|
193 | 'f,o,o,b,a,r,');
|
194 | finish(done);
|
195 | });
|
196 |
|
197 | it('lower', function(done) {
|
198 | equal('{{ "fOObAr" | lower }}', 'foobar');
|
199 | finish(done);
|
200 | });
|
201 |
|
202 | it('random', function(done) {
|
203 | for(var i=0; i<100; i++) {
|
204 | render('{{ [1,2,3,4,5,6,7,8,9] | random }}', function(err, res) {
|
205 | var val = parseInt(res);
|
206 | expect(val).to.be.within(1, 9);
|
207 | });
|
208 | }
|
209 |
|
210 | finish(done);
|
211 | });
|
212 |
|
213 | it('replace', function(done) {
|
214 | equal('{{ "aaabbbccc" | replace("a", "x") }}', 'xxxbbbccc');
|
215 | equal('{{ "aaabbbccc" | replace("a", "x", 2) }}', 'xxabbbccc');
|
216 | equal('{{ "aaabbbbbccc" | replace("b", "y", 4) }}', 'aaayyyybccc');
|
217 | finish(done);
|
218 | });
|
219 |
|
220 | it('reverse', function(done) {
|
221 | equal('{{ "abcdef" | reverse }}', 'fedcba');
|
222 | equal('{% for i in [1, 2, 3, 4] | reverse %}{{ i }}{% endfor %}', '4321');
|
223 | finish(done);
|
224 | });
|
225 |
|
226 | it('round', function(done) {
|
227 | equal('{{ 4.5 | round }}', '5');
|
228 | equal('{{ 4.5 | round(0, "floor") }}', '4');
|
229 | equal('{{ 4.12345 | round(4) }}', '4.1235');
|
230 | equal('{{ 4.12344 | round(4) }}', ('4.1234'));
|
231 | finish(done);
|
232 | });
|
233 |
|
234 | it('slice', function(done) {
|
235 | var tmpl = '{% for items in arr | slice(3) %}' +
|
236 | '--' +
|
237 | '{% for item in items %}' +
|
238 | '{{ item }}' +
|
239 | '{% endfor %}' +
|
240 | '--' +
|
241 | '{% endfor %}';
|
242 |
|
243 | equal(tmpl,
|
244 | { arr: [1,2,3,4,5,6,7,8,9] },
|
245 | '--123----456----789--');
|
246 |
|
247 | equal(tmpl,
|
248 | { arr: [1,2,3,4,5,6,7,8,9,10] },
|
249 | '--1234----567----8910--');
|
250 |
|
251 | finish(done);
|
252 | });
|
253 |
|
254 | it('sort', function(done) {
|
255 | equal('{% for i in [3,5,2,1,4,6] | sort %}{{ i }}{% endfor %}',
|
256 | '123456');
|
257 |
|
258 | equal('{% for i in ["fOo", "Foo"] | sort %}{{ i }}{% endfor %}',
|
259 | 'fOoFoo');
|
260 |
|
261 | equal('{% for i in [1,6,3,7] | sort(true) %}' +
|
262 | '{{ i }}{% endfor %}',
|
263 | '7631');
|
264 |
|
265 | equal('{% for i in ["fOo", "Foo"] | sort(false, true) %}' +
|
266 | '{{ i }}{% endfor %}',
|
267 | 'FoofOo');
|
268 |
|
269 | equal('{% for item in items | sort(false, false, "name") %}' +
|
270 | '{{ item.name }}{% endfor %}',
|
271 | { items: [{ name: 'james' },
|
272 | { name: 'fred' },
|
273 | { name: 'john' }]},
|
274 | 'fredjamesjohn');
|
275 |
|
276 | finish(done);
|
277 | });
|
278 |
|
279 | it('string', function(done) {
|
280 | equal('{% for i in 1234 | string | list %}{{ i }},{% endfor %}',
|
281 | '1,2,3,4,');
|
282 | finish(done);
|
283 | });
|
284 |
|
285 | it('title', function(done) {
|
286 | equal('{{ "foo bar baz" | title }}', 'Foo Bar Baz');
|
287 | finish(done);
|
288 | });
|
289 |
|
290 | it('trim', function(done) {
|
291 | equal('{{ " foo " | trim }}', 'foo');
|
292 | finish(done);
|
293 | });
|
294 |
|
295 | it('truncate', function(done) {
|
296 | equal('{{ "foo bar" | truncate(3) }}', 'foo...');
|
297 | equal('{{ "foo bar baz" | truncate(6) }}', 'foo...');
|
298 | equal('{{ "foo bar baz" | truncate(7) }}', 'foo bar...');
|
299 | equal('{{ "foo bar baz" | truncate(5, true) }}', 'foo b...');
|
300 | equal('{{ "foo bar baz" | truncate(6, true, "?") }}', 'foo ba?');
|
301 |
|
302 | finish(done);
|
303 | });
|
304 |
|
305 | it('upper', function(done) {
|
306 | equal('{{ "foo" | upper }}', 'FOO');
|
307 | finish(done);
|
308 | });
|
309 |
|
310 | it('urlencode', function(done) {
|
311 | equal('{{ "&" | urlencode }}', '%26');
|
312 | equal('{{ arr | urlencode }}', { arr: [[1,2],['&1','&2']] }, '1=2&%261=%262');
|
313 | equal('{{ obj | urlencode }}', { obj: {'1': 2, '&1': '&2'}}, '1=2&%261=%262');
|
314 | finish(done);
|
315 | });
|
316 |
|
317 | it('urlize', function(done) {
|
318 |
|
319 |
|
320 | equal('{{ "foo http://www.example.com/ bar"|urlize }}',
|
321 | 'foo <a href="http://www.example.com/">' +
|
322 | 'http://www.example.com/</a> bar')
|
323 |
|
324 |
|
325 | equal('{{ "" | urlize }}', '');
|
326 | equal('{{ "foo" | urlize }}', 'foo');
|
327 |
|
328 |
|
329 |
|
330 | equal('{{ "http://jinja.pocoo.org/docs/templates/" | urlize }}',
|
331 | '<a href="http://jinja.pocoo.org/docs/templates/">http://jinja.pocoo.org/docs/templates/</a>');
|
332 |
|
333 |
|
334 | equal('{{ "https://jinja.pocoo.org/docs/templates/" | urlize }}',
|
335 | '<a href="https://jinja.pocoo.org/docs/templates/">https://jinja.pocoo.org/docs/templates/</a>');
|
336 |
|
337 |
|
338 | equal('{{ "www.pocoo.org/docs/templates/" | urlize }}',
|
339 | '<a href="http://www.pocoo.org/docs/templates/">www.pocoo.org/docs/templates/</a>');
|
340 |
|
341 |
|
342 | equal('{{ "pocoo.org/docs/templates/" | urlize }}',
|
343 | '<a href="http://pocoo.org/docs/templates/">pocoo.org/docs/templates/</a>');
|
344 | equal('{{ "pocoo.net/docs/templates/" | urlize }}',
|
345 | '<a href="http://pocoo.net/docs/templates/">pocoo.net/docs/templates/</a>');
|
346 | equal('{{ "pocoo.com/docs/templates/" | urlize }}',
|
347 | '<a href="http://pocoo.com/docs/templates/">pocoo.com/docs/templates/</a>');
|
348 | equal('{{ "pocoo.com:80" | urlize }}',
|
349 | '<a href="http://pocoo.com:80">pocoo.com:80</a>');
|
350 | equal('{{ "pocoo.com" | urlize }}',
|
351 | '<a href="http://pocoo.com">pocoo.com</a>');
|
352 | equal('{{ "pocoo.commune" | urlize }}',
|
353 | 'pocoo.commune');
|
354 |
|
355 |
|
356 | equal('{{ "http://jinja.pocoo.org/docs/templates/" | urlize(12, true) }}',
|
357 | '<a href="http://jinja.pocoo.org/docs/templates/" rel="nofollow">http://jinja</a>');
|
358 |
|
359 |
|
360 | equal('{{ "(http://jinja.pocoo.org/docs/templates/" | urlize }}',
|
361 | '<a href="http://jinja.pocoo.org/docs/templates/">http://jinja.pocoo.org/docs/templates/</a>');
|
362 | equal('{{ "<http://jinja.pocoo.org/docs/templates/" | urlize }}',
|
363 | '<a href="http://jinja.pocoo.org/docs/templates/">http://jinja.pocoo.org/docs/templates/</a>');
|
364 | equal('{{ "<http://jinja.pocoo.org/docs/templates/" | urlize }}',
|
365 | '<a href="http://jinja.pocoo.org/docs/templates/">http://jinja.pocoo.org/docs/templates/</a>');
|
366 |
|
367 |
|
368 | equal('{{ "http://jinja.pocoo.org/docs/templates/," | urlize }}',
|
369 | '<a href="http://jinja.pocoo.org/docs/templates/">http://jinja.pocoo.org/docs/templates/</a>');
|
370 | equal('{{ "http://jinja.pocoo.org/docs/templates/." | urlize }}',
|
371 | '<a href="http://jinja.pocoo.org/docs/templates/">http://jinja.pocoo.org/docs/templates/</a>');
|
372 | equal('{{ "http://jinja.pocoo.org/docs/templates/)" | urlize }}',
|
373 | '<a href="http://jinja.pocoo.org/docs/templates/">http://jinja.pocoo.org/docs/templates/</a>');
|
374 | equal('{{ "http://jinja.pocoo.org/docs/templates/\n" | urlize }}',
|
375 | '<a href="http://jinja.pocoo.org/docs/templates/">http://jinja.pocoo.org/docs/templates/</a>');
|
376 | equal('{{ "http://jinja.pocoo.org/docs/templates/>" | urlize }}',
|
377 | '<a href="http://jinja.pocoo.org/docs/templates/">http://jinja.pocoo.org/docs/templates/</a>');
|
378 |
|
379 |
|
380 | equal('{{ "http://testuser@testuser.com" | urlize }}',
|
381 | '<a href="http://testuser@testuser.com">http://testuser@testuser.com</a>');
|
382 |
|
383 |
|
384 | equal('{{ "testuser@testuser.com" | urlize }}',
|
385 | '<a href="mailto:testuser@testuser.com">testuser@testuser.com</a>');
|
386 |
|
387 | finish(done);
|
388 | });
|
389 |
|
390 | it('wordcount', function(done) {
|
391 | equal('{{ "foo bar baz" | wordcount }}', '3');
|
392 | finish(done);
|
393 | });
|
394 | });
|
395 | })();
|