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