Debian Codegraph @

dojox/dtl/tests/text/tag.js

raw symbols SCIP
1 dojo.provide("dojox.dtl.tests.text.tag");
2
3 dojo.require("dojox.dtl");
4 dojo.require("dojox.dtl.Context");
5
6 if(!("_getTemplateString" in dojox.dtl.text)){
7 dojox.dtl.text._getTemplateString = dojox.dtl.text.getTemplateString;
8 dojox.dtl.text.getTemplateString = function(file){
9 return dojox.dtl.text._getTemplateString(file).replace(/(\r?\n)$/, ''); // remove optional trailing newline from template HTML file to make testing consistent
10 };
11 }
12
13 doh.register("dojox.dtl.text.tag",
14 [
15 function test_tag_block_and_extends(t){
16 var dd = dojox.dtl;
17
18 // Simple (messy) string-based extension
19 var template = new dd.Template('{% extends "../../dojox/dtl/tests/templates/pocket.html" %}{% block pocket %}Simple{% endblock %}');
20 t.is("Simple Pocket", template.render());
21
22 // Variable replacement
23 var context = new dd.Context({
24 parent: "../../dojox/dtl/tests/templates/pocket.html"
25 })
26 template = new dd.Template('{% extends parent %}{% block pocket %}Variabled{% endblock %}');
27 t.is("Variabled Pocket", template.render(context));
28
29 // Nicer dojo.moduleUrl and variable based extension
30 context.parent = dojo.moduleUrl("dojox.dtl.tests.templates", "pocket.html");
31 template = new dd.Template('{% extends parent %}{% block pocket %}Slightly More Advanced{% endblock %}');
32 t.is("Slightly More Advanced Pocket", template.render(context));
33
34 // dojo.moduleUrl with support for more variables.
35 // This is important for HTML templates where the "shared" flag will be important.
36 context.parent = {
37 url: dojo.moduleUrl("dojox.dtl.tests.templates", "pocket.html")
38 }
39 template = new dd.Template('{% extends parent %}{% block pocket %}Super{% endblock %}');
40 t.is("Super Pocket", template.render(context));
41 },
42 function test_tag_block(t){
43 var dd = dojox.dtl;
44
45 var context = new dd.Context({
46 parent: dojo.moduleUrl("dojox.dtl.tests.templates", "pocket2.html"),
47 items: ["apple", "banana", "lemon" ]
48 });
49
50 var template = new dd.Template("{% extends parent %}{% block pocket %}My {{ item }}{% endblock %}");
51 t.is("(My apple) (My banana) (My lemon) Pocket", template.render(context));
52 },
53 function test_tag_block_super(t){
54 var dd = dojox.dtl;
55
56 var context = new dd.Context({
57 parent: dojo.moduleUrl("dojox.dtl.tests.templates", "pocket2.html"),
58 items: ["apple", "banana", "lemon" ]
59 });
60
61 var template = new dd.Template("{% extends parent %}{% block pocket %}My {{ item }} {{ block.super }}{% endblock %}");
62 t.is("(My apple Hot) (My banana Hot) (My lemon Hot) Pocket", template.render(context));
63 },
64 function test_tag_comment(t){
65 var dd = dojox.dtl;
66
67 var template = new dd.Template('Hot{% comment %}<strong>Make me disappear</strong>{% endcomment %} Pocket');
68 t.is("Hot Pocket", template.render());
69
70 var found = false;
71 try{
72 template = new dd.Template('Hot{% comment %}<strong>Make me disappear</strong> Pocket');
73 }catch(e){
74 t.is("Unclosed tag found when looking for endcomment", e.message);
75 found = true;
76 }
77 t.t(found);
78 },
79 function test_tag_cycle(t){
80 var dd = dojox.dtl;
81
82 var context = new dd.Context({
83 items: ["apple", "banana", "lemon"],
84 unplugged: "Torrey"
85 });
86 var template = new dd.Template("{% for item in items %}{% cycle 'Hot' 'Diarrhea' unplugged 'Extra' %} Pocket. {% endfor %}");
87 t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. ", template.render(context));
88 // Make sure that it doesn't break on re-render
89 t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. ", template.render(context));
90
91 // Test repeating the loop
92 context.items.push("guava", "mango", "pineapple");
93 t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. Extra Pocket. Hot Pocket. Diarrhea Pocket. ", template.render(context));
94
95 // Repeat the above tests for the old style
96 // ========================================
97 context.items = context.items.slice(0, 3);
98 template = new dd.Template("{% for item in items %}{% cycle Hot,Diarrhea,Torrey,Extra %} Pocket. {% endfor %}");
99 t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. ", template.render(context));
100 // Make sure that it doesn't break on re-render
101 t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. ", template.render(context));
102
103 // Test repeating the loop
104 context.items.push("guava", "mango", "pineapple");
105 t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. Extra Pocket. Hot Pocket. Diarrhea Pocket. ", template.render(context));
106
107 // Now test outside of the for loop
108 // ================================
109 context = new dojox.dtl.Context({ unplugged: "Torrey" });
110 template = new dd.Template("{% cycle 'Hot' 'Diarrhea' unplugged 'Extra' as steakum %} Pocket. {% cycle steakum %} Pocket. {% cycle steakum %} Pocket.");
111 t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket.", template.render(context));
112
113 template = new dd.Template("{% cycle 'Hot' 'Diarrhea' unplugged 'Extra' as steakum %} Pocket. {% cycle steakum %} Pocket. {% cycle steakum %} Pocket. {% cycle steakum %} Pocket. {% cycle steakum %} Pocket. {% cycle steakum %} Pocket.");
114 t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. Extra Pocket. Hot Pocket. Diarrhea Pocket.", template.render(context));
115 //t.t(false)
116 // Test for nested objects
117 context.items = {
118 list: ["apple", "banana", "lemon"]
119 };
120 template = new dd.Template("{% for item in items.list %}{% cycle 'Hot' 'Diarrhea' unplugged 'Extra' %} Pocket. {% endfor %}");
121 t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. ", template.render(context));
122 // Make sure that it doesn't break on re-render
123 t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. ", template.render(context));
124 },
125 function test_tag_debug(t){
126 var dd = dojox.dtl;
127
128 var context = new dd.Context({
129 items: ["apple", "banana", "lemon"],
130 unplugged: "Torrey"
131 });
132 var template = new dd.Template("{% debug %}");
133 t.is('[items: object]\n[unplugged: string]\n', template.render(context));
134 },
135 function test_tag_filter(t){
136 var dd = dojox.dtl;
137
138 var template = new dd.Template('{% filter lower|center:"15" %}Hot Pocket{% endfilter %}');
139 t.is(" hot pocket ", template.render());
140 },
141 function test_tag_firstof(t){
142 var dd = dojox.dtl;
143
144 var context = new dd.Context({
145 found: "unicorn"
146 });
147
148 var template = new dd.Template("{% firstof one two three four found %}");
149 t.is("unicorn", template.render(context));
150
151 context.four = null;
152 t.is("null", template.render(context));
153
154 context.three = false;
155 t.is("false", template.render(context));
156 },
157 function test_tag_for(t){
158 var dd = dojox.dtl;
159
160 var context = new dd.Context({
161 items: ["apple", "banana", "lemon"]
162 });
163 var template = new dd.Template("{% for item in items %}<li>{{ item }}</li>{% endfor %}");
164 t.is("<li>apple</li><li>banana</li><li>lemon</li>", template.render(context));
165
166 template = new dd.Template("{% for item in items reversed %}<li>{{ item }}</li>{% endfor %}");
167 t.is("<li>lemon</li><li>banana</li><li>apple</li>", template.render(context));
168
169 context.items = {
170 apple: "Red Delicious",
171 banana: "Cavendish",
172 lemon: "Citrus"
173 };
174 template = new dd.Template("{% for key, value in items.items %}<li>{{ value }} {{ key|title }}</li>{% endfor %}");
175 t.is("<li>Red Delicious Apple</li><li>Cavendish Banana</li><li>Citrus Lemon</li>", template.render(context));
176
177 // The same thing above, but using "zipped" sets
178 context.items = [
179 ["apple", "Red Delicious", 1.99],
180 ["banana", "Cavendish", 0.49],
181 ["lemon", "Citrus", 0.29]
182 ];
183 template = new dd.Template("{% for fruit, type, price in items %}<li>{{ type }} {{ fruit|title }} costs ${{ price}}</li>{% endfor %}");
184 t.is("<li>Red Delicious Apple costs $1.99</li><li>Cavendish Banana costs $0.49</li><li>Citrus Lemon costs $0.29</li>", template.render(context));
185
186 template = new dd.Template("{% for fruit, type, price in items reversed %}<li>{{ type }} {{ fruit|title }} costs ${{ price}}</li>{% endfor %}");
187 t.is("<li>Citrus Lemon costs $0.29</li><li>Cavendish Banana costs $0.49</li><li>Red Delicious Apple costs $1.99</li>", template.render(context));
188
189 // Now to create some errors
190 var found = false;
191 try {
192 template = new dd.Template("{% for item initems %}<li>{{ item }}</li>{% endfor %}");
193 }catch(e){
194 found = true;
195 t.is("'for' statements should have at least four words: for item initems", e.message);
196 }
197 t.t(found);
198
199 found = false;
200 try {
201 template = new dd.Template("{% for item ni items %}<li>{{ item }}</li>{% endfor %}");
202 }catch(e){
203 found = true;
204 t.is("'for' tag received an invalid argument: for item ni items", e.message);
205 }
206 t.t(found);
207
208 found = false;
209 try {
210 template = new dd.Template("{% for my item in items %}<li>{{ item }}</li>{% endfor %}");
211 }catch(e){
212 found = true;
213 t.is("'for' tag received an invalid argument: for my item in items", e.message);
214 }
215 t.t(found);
216 },
217 function test_tag_if(t){
218 var dd = dojox.dtl;
219
220 var context = new dd.Context({
221 jokes: {
222 hot_pockets: true,
223 unicycles: true,
224 bacon: true
225 }
226 });
227 var template = new dd.Template("Comedian is {% if jokes.hot_pockets and jokes.unicycles and jokes.bacon %}funny{% else %}not funny{% endif %}");
228 t.is("Comedian is funny", template.render(context));
229
230 context.jokes.unicycles = false;
231 t.is("Comedian is not funny", template.render(context));
232
233 context.comedians = {
234 hedberg: true,
235 gaffigan: true,
236 cook: true
237 };
238 template = new dd.Template("Show will be {% if comedians.hedberg or comedians.gaffigan %}worth seeing{% else %}not worth seeing{% endif %}");
239 t.is("Show will be worth seeing", template.render(context));
240
241 // NOTE: "and" is implied by nesting. eg {% if sunny %}{% if windy %}It's Sunny and Windy{% endif %}{% endif %}
242 // Not mixing ands and ors allows for MUCH faster rendering
243 template = new dd.Template("Show will {% if comedians.hedberg or comedians.gaffigan %}{% if comedians.cook %}not {% endif %}be worth seeing{% else %}not be worth seeing{% endif %}");
244 t.is("Show will not be worth seeing", template.render(context));
245
246 context.comedians.cook = false;
247 t.is("Show will be worth seeing", template.render(context));
248
249 template = new dd.Template("Show will be {% if comedians.hedberg and comedians.gaffigan and not comedians.cook %}AWESOME{% else %}almost awesome{% endif %}");
250 t.is("Show will be AWESOME", template.render(context));
251
252 context.comedians.cook = true;
253 t.is("Show will be almost awesome", template.render(context));
254
255 // Now we test for errors.
256 var found = false;
257 try {
258 template = new dd.Template("Show will be {% if comedians.hedberg or comedians.gaffigan and not comedians.cook %}worth seeing{% else %}not worth seeing{% endif %}");
259 }catch(e){
260 found = true;
261 t.is("'if' tags can't mix 'and' and 'or'", e.message);
262 }
263 t.t(found);
264 },
265 function test_tag_ifchanged(t){
266 var dd = dojox.dtl;
267
268 var context = new dd.Context({
269 year: 2008,
270 days: [
271 new Date(2008, 0, 12),
272 new Date(2008, 0, 28),
273 new Date(2008, 1, 1),
274 new Date(2008, 1, 1),
275 new Date(2008, 1, 1)
276 ]
277 });
278
279 var template = new dd.Template("<h1>Archive for {{ year }}</h1>"+
280 "{% for date in days %}"+
281 '{% ifchanged %}<h3>{{ date|date:"F" }}</h3>{% endifchanged %}'+
282 '<a href="{{ date|date:\'M/d\'|lower }}/">{{ date|date:\'j\' }}</a>'+
283 "{% endfor %}");
284 t.is('<h1>Archive for 2008</h1>'+
285 '<h3>January</h3>'+
286 '<a href="jan/12/">12</a>'+
287 '<a href="jan/28/">28</a>'+
288 '<h3>February</h3>'+
289 '<a href="feb/01/">1</a>'+
290 '<a href="feb/01/">1</a>'+
291 '<a href="feb/01/">1</a>', template.render(context));
292
293 template = new dd.Template('{% for date in days %}'+
294 '{% ifchanged date.date %} {{ date.date }} {% endifchanged %}'+
295 '{% ifchanged date.hour date.date %}'+
296 '{{ date.hour }}'+
297 '{% endifchanged %}'+
298 '{% endfor %}');
299 t.is(' 2008-01-12 0 2008-01-28 0 2008-02-01 0', template.render(context));
300 },
301 function test_tag_ifequal(t){
302 var dd = dojox.dtl;
303
304 var context = new dd.Context({
305 user: {
306 id: 314
307 },
308 comment: {
309 user_id: 314
310 }
311 });
312
313 var template = new dd.Template("{% ifequal user.id comment.user_id %}You posted this{% endifequal %}");
314 t.is("You posted this", template.render(context));
315
316 context.user.id = 313;
317 t.is("", template.render(context));
318
319 // Errors
320 var found = false;
321 try {
322 template = new dd.Template("{% ifequal user.id %}You posted this{% endifequal %}");
323 }catch(e){
324 found = true;
325 t.is("ifequal takes two arguments", e.message);
326 }
327 t.t(found);
328
329 found = false;
330 try {
331 template = new dd.Template("{% ifequal user.id comment.user_id %}You posted this{% endif %}");
332 }catch(e){
333 found = true;
334 t.is("No tag found for endif", e.message);
335 }
336 t.t(found);
337
338 template = new dd.Template('{% ifequal empty "" %}empty{% endifequal %}');
339 doh.is('empty', template.render());
340 },
341 function test_tag_ifnotequal(t){
342 var dd = dojox.dtl;
343
344 var context = new dd.Context({
345 favorite: "hedberg",
346 comedian: "cook"
347 });
348
349 var template = new dd.Template("{% ifnotequal favorite comedian %}Not your favorite{% else %}Your favorite{% endifnotequal %}");
350 t.is("Not your favorite", template.render(context));
351
352 context.comedian = "hedberg";
353 t.is("Your favorite", template.render(context));
354 },
355 function test_tag_include(t){
356 var dd = dojox.dtl;
357
358 var context = new dd.Context({
359 hello: dojo.moduleUrl("dojox.dtl.tests.templates", "hello.html"),
360 person: "Bob",
361 people: ["Charles", "Ralph", "Julia"]
362 });
363
364 var template = new dd.Template("{% include hello %}");
365 t.is("Hello, <span>Bob</span>", template.render(context));
366
367 template = new dd.Template('{% include "../../dojox/dtl/tests/templates/hello.html" %}');
368 t.is("Hello, <span>Bob</span>", template.render(context));
369
370 template = new dd.Template('{% for person in people %}{% include hello %} {% endfor %}');
371 t.is("Hello, <span>Charles</span> Hello, <span>Ralph</span> Hello, <span>Julia</span> ", template.render(context));
372 },
373 function test_tag_load(t){
374 t.f(dojox.dtl.tests.text.load);
375 new dojox.dtl.Template("{% load dojox.dtl.tests.text.load %}");
376 t.t(dojox.dtl.tests.text.load);
377 },
378 function test_tag_now(t){
379 var dd = dojox.dtl;
380
381 var template = new dd.Template('It is {% now "jS F Y H:i" %}');
382 t.t(template.render().match(/^It is \d{1,2}[a-z]{2} [A-Z][a-z]+ [0-9]{4,} \d{2}:\d{2}$/));
383
384 template = new dd.Template('It is the {% now "jS \\o\\f F" %}');
385 t.t(template.render().match(/^It is the \d{1,2}[a-z]{2} of [A-Z][a-z]+$/));
386
387 template = new dd.Template("It is the {% now 'jS \\o\\f F' %}");
388 t.t(template.render().match(/^It is the \d{1,2}[a-z]{2} of [A-Z][a-z]+$/));
389
390 var found = false;
391 try{
392 template = new dd.Template("It is the {% now 'jS \\o\\f F %}");
393 }catch(e){
394 found = true;
395 t.is("'now' statement takes one argument", e.message);
396 }
397 t.t(found);
398
399 found = false;
400 try{
401 template = new dd.Template('It is the {% now "jS \\o\\f F %}');
402 }catch(e){
403 found = true;
404 t.is("'now' statement takes one argument", e.message);
405 }
406 t.t(found);
407 },
408 function test_tag_regroup(t){
409 var dd = dojox.dtl;
410
411 var context = new dd.Context({
412 people: [
413 { firstName: "Bill", lastName: "Clinton", gender: "Male" },
414 { firstName: "Margaret", lastName: "Thatcher", gender: "Female" },
415 { firstName: "Path", lastName: "Smith", gender: "Unknown" },
416 { firstName: "Condoleezza", lastName: "Rice", gender: "Female" },
417 { firstName: "George", lastName: "Bush", gender: "Male" }
418 ]
419 });
420
421 var template = new dd.Template("{% regroup people|dictsort:'gender' by gender as grouped %}<ul>{% for group in grouped %}<li>{{ group.grouper }}<ul>{% for item in group.list %}<li>{{ item.firstName }} {{ item.lastName }}</li>{% endfor %}</ul></li>{% endfor %}</ul>");
422 t.t(template.render(context).match(new RegExp("^<ul><li>Female<ul><li>(Condoleezza Rice|Margaret Thatcher)</li><li>(Condoleezza Rice|Margaret Thatcher)</li></ul></li><li>Male<ul><li>(Bill Clinton|George Bush)</li><li>(Bill Clinton|George Bush)</li></ul></li><li>Unknown<ul><li>Path Smith</li></ul></li></ul>$")));
423 },
424 function test_tag_spaceless(t){
425 var dd = dojox.dtl;
426
427 var template = new dd.Template("{% spaceless %}<ul> \n <li>Hot</li> \n\n<li>Pocket </li>\n </ul>{% endspaceless %}");
428 t.is("<ul><li>Hot</li><li>Pocket </li></ul>", template.render());
429 },
430 function test_tag_ssi(t){
431 var dd = dojox.dtl;
432
433 var context = new dd.Context({
434 hello: dojo.moduleUrl("dojox.dtl.tests.templates", "hello.html"),
435 person: "Bob",
436 people: ["Charles", "Ralph", "Julia"]
437 });
438
439 var template = new dd.Template("{% ssi hello parsed %}");
440 t.is("Hello, <span>Bob</span>", template.render(context));
441
442 template = new dd.Template("{% ssi hello %}");
443 t.is("Hello, <span>{{ person }}</span>", template.render(context));
444
445 template = new dd.Template('{% ssi "../../dojox/dtl/tests/templates/hello.html" parsed %}');
446 t.is("Hello, <span>Bob</span>", template.render(context));
447
448 template = new dd.Template('{% for person in people %}{% ssi hello parsed %} {% endfor %}');
449 t.is("Hello, <span>Charles</span> Hello, <span>Ralph</span> Hello, <span>Julia</span> ", template.render(context));
450 },
451 function test_tag_templatetag(t){
452 var dd = dojox.dtl;
453
454 var template = new dd.Template("{% templatetag openblock %}");
455 t.is("{%", template.render());
456 template = new dd.Template("{% templatetag closeblock %}");
457 t.is("%}", template.render());
458 template = new dd.Template("{% templatetag openvariable %}");
459 t.is("{{", template.render());
460 template = new dd.Template("{% templatetag closevariable %}");
461 t.is("}}", template.render());
462 template = new dd.Template("{% templatetag openbrace %}");
463 t.is("{", template.render());
464 template = new dd.Template("{% templatetag closebrace %}");
465 t.is("}", template.render());
466 template = new dd.Template("{% templatetag opencomment %}");
467 t.is("{#", template.render());
468 template = new dd.Template("{% templatetag closecomment %}");
469 t.is("#}", template.render());
470 },
471 function test_tag_widthratio(t){
472 var dd = dojox.dtl;
473
474 var context = new dd.Context({
475 this_value: 175,
476 max_value: 200
477 });
478
479 var template = new dd.Template('<img src="bar.gif" height="10" width="{% widthratio this_value max_value 100 %}" />');
480 t.is('<img src="bar.gif" height="10" width="88" />', template.render(context));
481 },
482 function test_tag_with(t){
483 var dd = dojox.dtl;
484
485 var context = new dd.Context({
486 person: {
487 someSqlMethod: function(){
488 return 4815162342;
489 }
490 }
491 });
492
493 var template = new dd.Template('{% with person.someSqlMethod as total %}{{ total }} object{{ total|pluralize }}{% endwith %}')
494 t.is("4815162342 objects", template.render(context));
495 }
496 ]
497 );