aboutsummaryrefslogtreecommitdiff
path: root/includes/js/dojox/dtl/tests/text/tag.js
blob: 7abbc43e34ada4cdc1479110dd999696dd4b328c (plain)
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
if(!dojo._hasResource["dojox.dtl.tests.text.tag"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.dtl.tests.text.tag"] = true;
dojo.provide("dojox.dtl.tests.text.tag");

dojo.require("dojox.dtl");
dojo.require("dojox.dtl.Context");

doh.register("dojox.dtl.text.tag", 
	[
		function test_tag_block_and_extends(t){
			var dd = dojox.dtl;

			// Simple (messy) string-based extension
			var template = new dd.Template('{% extends "../../dojox/dtl/tests/templates/pocket.html" %}{% block pocket %}Simple{% endblock %}');
			t.is("Simple Pocket", template.render());

			// Variable replacement
			var context = new dd.Context({
				parent: "../../dojox/dtl/tests/templates/pocket.html"
			})
			template = new dd.Template('{% extends parent %}{% block pocket %}Variabled{% endblock %}');
			t.is("Variabled Pocket", template.render(context));

			// Nicer dojo.moduleUrl and variable based extension
			context.parent = dojo.moduleUrl("dojox.dtl.tests.templates", "pocket.html");
			template = new dd.Template('{% extends parent %}{% block pocket %}Slightly More Advanced{% endblock %}');
			t.is("Slightly More Advanced Pocket", template.render(context));

			// dojo.moduleUrl with support for more variables.
			// This is important for HTML templates where the "shared" flag will be important.
			context.parent = {
				url: dojo.moduleUrl("dojox.dtl.tests.templates", "pocket.html")
			}
			template = new dd.Template('{% extends parent %}{% block pocket %}Super{% endblock %}');
			t.is("Super Pocket", template.render(context));
		},
		function test_tag_block(t){
			var dd = dojox.dtl;

			var context = new dd.Context({
				parent: dojo.moduleUrl("dojox.dtl.tests.templates", "pocket2.html"),
				items: ["apple", "banana", "lemon" ]
			});

			var template = new dd.Template("{% extends parent %}{% block pocket %}My {{ item }}{% endblock %}");
			t.is("(My apple) (My banana) (My lemon) Pocket", template.render(context));
		},
		function test_tag_comment(t){
			var dd = dojox.dtl;

			var template = new dd.Template('Hot{% comment %}<strong>Make me disappear</strong>{% endcomment %} Pocket');
			t.is("Hot Pocket", template.render());

			var found = false;
			try{
				template = new dd.Template('Hot{% comment %}<strong>Make me disappear</strong> Pocket');
			}catch(e){
				t.is("Unclosed tag found when looking for endcomment", e.message);
				found = true;
			}
			t.t(found);
		},
		function test_tag_cycle(t){
			var dd = dojox.dtl;

			var context = new dd.Context({
				items: ["apple", "banana", "lemon"],
				unplugged: "Torrey"
			});
			var template = new dd.Template("{% for item in items %}{% cycle 'Hot' 'Diarrhea' unplugged 'Extra' %} Pocket. {% endfor %}");
			t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. ", template.render(context));
			// Make sure that it doesn't break on re-render
			t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. ", template.render(context));

			// Test repeating the loop
			context.items.push("guava", "mango", "pineapple");
			t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. Extra Pocket. Hot Pocket. Diarrhea Pocket. ", template.render(context));

			// Repeat the above tests for the old style
			// ========================================
			context.items = context.items.slice(0, 3);
			template = new dd.Template("{% for item in items %}{% cycle Hot,Diarrhea,Torrey,Extra %} Pocket. {% endfor %}");
			t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. ", template.render(context));
			// Make sure that it doesn't break on re-render
			t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. ", template.render(context));

			// Test repeating the loop
			context.items.push("guava", "mango", "pineapple");
			t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. Extra Pocket. Hot Pocket. Diarrhea Pocket. ", template.render(context));

			// Now test outside of the for loop
			// ================================
			context = new dojox.dtl.Context({ unplugged: "Torrey" });
			template = new dd.Template("{% cycle 'Hot' 'Diarrhea' unplugged 'Extra' as steakum %} Pocket. {% cycle steakum %} Pocket. {% cycle steakum %} Pocket.");
			t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket.", template.render(context));

			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.");
			t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. Extra Pocket. Hot Pocket. Diarrhea Pocket.", template.render(context));
//t.t(false)
			// Test for nested objects
			context.items = {
				list: ["apple", "banana", "lemon"]
			};
			template = new dd.Template("{% for item in items.list %}{% cycle 'Hot' 'Diarrhea' unplugged 'Extra' %} Pocket. {% endfor %}");
			t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. ", template.render(context));
			// Make sure that it doesn't break on re-render
			t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. ", template.render(context));
		},
		function test_tag_debug(t){
			var dd = dojox.dtl;

			var context = new dd.Context({
				items: ["apple", "banana", "lemon"],
				unplugged: "Torrey"
			});
			var template = new dd.Template("{% debug %}");
			t.is('items: ["apple","banana","lemon"]\n\nunplugged: "Torrey"\n\n', template.render(context));
		},
		function test_tag_filter(t){
			var dd = dojox.dtl;

			var template = new dd.Template('{% filter lower|center:"15" %}Hot Pocket{% endfilter %}');
			t.is("  hot pocket   ", template.render());
		},
		function test_tag_firstof(t){
			var dd = dojox.dtl;

			var context = new dd.Context({
				found: "unicorn"
			});

			var template = new dd.Template("{% firstof one two three four found %}");
			t.is("unicorn", template.render(context));

			context.four = null;
			t.is("null", template.render(context));

			context.three = false;
			t.is("false", template.render(context));
		},
		function test_tag_for(t){
			var dd = dojox.dtl;

			var context = new dd.Context({
				items: ["apple", "banana", "lemon"]
			});
			var template = new dd.Template("{% for item in items %}<li>{{ item }}</li>{% endfor %}");
			t.is("<li>apple</li><li>banana</li><li>lemon</li>", template.render(context));

			template = new dd.Template("{% for item in items reversed %}<li>{{ item }}</li>{% endfor %}");
			t.is("<li>lemon</li><li>banana</li><li>apple</li>", template.render(context));

			context.items = {
				apple: "Red Delicious",
				banana: "Cavendish",
				lemon: "Citrus"
			};
			template = new dd.Template("{% for key, value in items.items %}<li>{{ value }} {{ key|title }}</li>{% endfor %}");
			t.is("<li>Red Delicious Apple</li><li>Cavendish Banana</li><li>Citrus Lemon</li>", template.render(context));

			// The same thing above, but using "zipped" sets
			context.items = [
				["apple", "Red Delicious", 1.99],
				["banana", "Cavendish", 0.49],
				["lemon", "Citrus", 0.29]
			];
			template = new dd.Template("{% for fruit, type, price in items %}<li>{{ type }} {{ fruit|title }} costs ${{ price}}</li>{% endfor %}");
			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));

			template = new dd.Template("{% for fruit, type, price in items reversed %}<li>{{ type }} {{ fruit|title }} costs ${{ price}}</li>{% endfor %}");
			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));

			// Now to create some errors
			var found = false;
			try {
				template = new dd.Template("{% for item initems %}<li>{{ item }}</li>{% endfor %}");
			}catch(e){
				found = true;
				t.is("'for' statements should have at least four words: for item initems", e.message);
			}
			t.t(found);

			found = false;
			try {
				template = new dd.Template("{% for item ni items %}<li>{{ item }}</li>{% endfor %}");
			}catch(e){
				found = true;
				t.is("'for' tag received an invalid argument: for item ni items", e.message);
			}
			t.t(found);

			found = false;
			try {
				template = new dd.Template("{% for my item in items %}<li>{{ item }}</li>{% endfor %}");
			}catch(e){
				found = true;
				t.is("'for' tag received an invalid argument: for my item in items", e.message);
			}
			t.t(found);
		},
		function test_tag_if(t){
			var dd = dojox.dtl;

			var context = new dd.Context({
				jokes: {
					hot_pockets: true,
					unicycles: true,
					bacon: true
				}
			});
			var template = new dd.Template("Comedian is {% if jokes.hot_pockets and jokes.unicycles and jokes.bacon %}funny{% else %}not funny{% endif %}");
			t.is("Comedian is funny", template.render(context));

			context.jokes.unicycles = false;
			t.is("Comedian is not funny", template.render(context));

			context.comedians = {
				hedberg: true,
				gaffigan: true,
				cook: true
			};
			template = new dd.Template("Show will be {% if comedians.hedberg or comedians.gaffigan %}worth seeing{% else %}not worth seeing{% endif %}");
			t.is("Show will be worth seeing", template.render(context));

			// NOTE: "and" is implied by nesting. eg {% if sunny %}{% if windy %}It's Sunny and Windy{% endif %}{% endif %}
			// Not mixing ands and ors allows for MUCH faster rendering
			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 %}");
			t.is("Show will not be worth seeing", template.render(context));

			context.comedians.cook = false;
			t.is("Show will be worth seeing", template.render(context));

			template = new dd.Template("Show will be {% if comedians.hedberg and comedians.gaffigan and not comedians.cook %}AWESOME{% else %}almost awesome{% endif %}");
			t.is("Show will be AWESOME", template.render(context));

			context.comedians.cook = true;
			t.is("Show will be almost awesome", template.render(context));

			// Now we test for errors.
			var found = false;
			try {
				template = new dd.Template("Show will be {% if comedians.hedberg or comedians.gaffigan and not comedians.cook %}worth seeing{% else %}not worth seeing{% endif %}");
			}catch(e){
				found = true;
				t.is("'if' tags can't mix 'and' and 'or'", e.message);
			}
			t.t(found);
		},
		function test_tag_ifchanged(t){
			var dd = dojox.dtl;

			var context = new dd.Context({
				year: 2008,
				days: [
					new Date(2008, 0, 12),
					new Date(2008, 0, 28),
					new Date(2008, 1, 1),
					new Date(2008, 1, 1),
					new Date(2008, 1, 1)
				]
			});

			var template = new dd.Template("<h1>Archive for {{ year }}</h1>"+
"{% for date in days %}"+
'{% ifchanged %}<h3>{{ date|date:"F" }}</h3>{% endifchanged %}'+
'<a href="{{ date|date:\'M/d\'|lower }}/">{{ date|date:\'j\' }}</a>'+
"{% endfor %}");
			t.is('<h1>Archive for 2008</h1>'+
'<h3>January</h3>'+
'<a href="jan/12/">12</a>'+
'<a href="jan/28/">28</a>'+
'<h3>February</h3>'+
'<a href="feb/01/">1</a>'+
'<a href="feb/01/">1</a>'+
'<a href="feb/01/">1</a>', template.render(context));

			template = new dd.Template('{% for date in days %}'+
'{% ifchanged date.date %} {{ date.date }} {% endifchanged %}'+
'{% ifchanged date.hour date.date %}'+
'{{ date.hour }}'+
'{% endifchanged %}'+
'{% endfor %}');
			t.is(' 2008-01-12 0 2008-01-28 0 2008-02-01 0', template.render(context));
		},
		function test_tag_ifequal(t){
			var dd = dojox.dtl;

			var context = new dd.Context({
				user: {
					id: 314
				},
				comment: {
					user_id: 314
				}
			});

			var template = new dd.Template("{% ifequal user.id comment.user_id %}You posted this{% endifequal %}");
			t.is("You posted this", template.render(context));

			context.user.id = 313;
			t.is("", template.render(context));

			// Errors
			var found = false;
			try {
				template = new dd.Template("{% ifequal user.id %}You posted this{% endifequal %}");
			}catch(e){
				found = true;
				t.is("ifequal takes two arguments", e.message);
			}
			t.t(found);

			found = false;
			try {
				template = new dd.Template("{% ifequal user.id comment.user_id %}You posted this{% endif %}");
			}catch(e){
				found = true;
				t.is("No tag found for endif", e.message);
			}
			t.t(found);
		},
		function test_tag_ifnotequal(t){
			var dd = dojox.dtl;

			var context = new dd.Context({
				favorite: "hedberg",
				comedian: "cook"
			});

			var template = new dd.Template("{% ifnotequal favorite comedian %}Not your favorite{% else %}Your favorite{% endifnotequal %}");
			t.is("Not your favorite", template.render(context));

			context.comedian = "hedberg";
			t.is("Your favorite", template.render(context));
		},
		function test_tag_include(t){
			var dd = dojox.dtl;

			var context = new dd.Context({
				hello: dojo.moduleUrl("dojox.dtl.tests.templates", "hello.html"),
				person: "Bob",
				people: ["Charles", "Ralph", "Julia"]
			});

			var template = new dd.Template("{% include hello %}");
			t.is("Hello, <span>Bob</span>", template.render(context));

			template = new dd.Template('{% include "../../dojox/dtl/tests/templates/hello.html" %}');
			t.is("Hello, <span>Bob</span>", template.render(context));

			template = new dd.Template('{% for person in people %}{% include hello %} {% endfor %}');
			t.is("Hello, <span>Charles</span> Hello, <span>Ralph</span> Hello, <span>Julia</span> ", template.render(context));
		},
		function test_tag_load(t){
			t.f(dojox.dtl.tests.text.load);
			new dojox.dtl.Template("{% load dojox.dtl.tests.text.load %}");
			t.t(dojox.dtl.tests.text.load);
		},
		function test_tag_now(t){
			var dd = dojox.dtl;

			var template = new dd.Template('It is {% now "jS F Y H:i" %}');
			t.t(template.render().match(/^It is \d{1,2}[a-z]{2} [A-Z][a-z]+ [0-9]{4,} \d{2}:\d{2}$/));

			template = new dd.Template('It is the {% now "jS \\o\\f F" %}');
			t.t(template.render().match(/^It is the \d{1,2}[a-z]{2} of [A-Z][a-z]+$/));

			template = new dd.Template("It is the {% now 'jS \\o\\f F' %}");
			t.t(template.render().match(/^It is the \d{1,2}[a-z]{2} of [A-Z][a-z]+$/));

			var found = false;
			try{
				template = new dd.Template("It is the {% now 'jS \\o\\f F %}");
			}catch(e){
				found = true;
				t.is("'now' statement takes one argument", e.message);
			}
			t.t(found);

			found = false;
			try{
				template = new dd.Template('It is the {% now "jS \\o\\f F %}');
			}catch(e){
				found = true;
				t.is("'now' statement takes one argument", e.message);
			}
			t.t(found);
		},
		function test_tag_regroup(t){
			var dd = dojox.dtl;

			var context = new dd.Context({
				people: [
					{ firstName: "Bill", lastName: "Clinton", gender: "Male" },
					{ firstName: "Margaret", lastName: "Thatcher", gender: "Female" },
					{ firstName: "Path", lastName: "Smith", gender: "Unkown" },
					{ firstName: "Condoleezza", lastName: "Rice", gender: "Female" },
					{ firstName: "George", lastName: "Bush", gender: "Male" }
				]
			});

			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>");
			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>$")));
		},
		function test_tag_spaceless(t){
			var dd = dojox.dtl;

			var template = new dd.Template("{% spaceless %}<ul> \n <li>Hot</li> \n\n<li>Pocket </li>\n </ul>{% endspaceless %}");
			t.is("<ul><li>Hot</li><li>Pocket </li></ul>", template.render());
		},
		function test_tag_ssi(t){
			var dd = dojox.dtl;

			var context = new dd.Context({
				hello: dojo.moduleUrl("dojox.dtl.tests.templates", "hello.html"),
				person: "Bob",
				people: ["Charles", "Ralph", "Julia"]
			});

			var template = new dd.Template("{% ssi hello parsed %}");
			t.is("Hello, <span>Bob</span>", template.render(context));

			template = new dd.Template("{% ssi hello %}");
			t.is("Hello, <span>{{ person }}</span>", template.render(context));

			template = new dd.Template('{% ssi "../../dojox/dtl/tests/templates/hello.html" parsed %}');
			t.is("Hello, <span>Bob</span>", template.render(context));

			template = new dd.Template('{% for person in people %}{% ssi hello parsed %} {% endfor %}');
			t.is("Hello, <span>Charles</span> Hello, <span>Ralph</span> Hello, <span>Julia</span> ", template.render(context));
		},
		function test_tag_templatetag(t){
			var dd = dojox.dtl;

			var template = new dd.Template("{% templatetag openblock %}");
			t.is("{%", template.render());
			template = new dd.Template("{% templatetag closeblock %}");
			t.is("%}", template.render());
			template = new dd.Template("{% templatetag openvariable %}");
			t.is("{{", template.render());
			template = new dd.Template("{% templatetag closevariable %}");
			t.is("}}", template.render());
			template = new dd.Template("{% templatetag openbrace %}");
			t.is("{", template.render());
			template = new dd.Template("{% templatetag closebrace %}");
			t.is("}", template.render());
			template = new dd.Template("{% templatetag opencomment %}");
			t.is("{#", template.render());
			template = new dd.Template("{% templatetag closecomment %}");
			t.is("#}", template.render());
		},
		function test_tag_widthratio(t){
			var dd = dojox.dtl;

			var context = new dd.Context({
				this_value: 175,
				max_value: 200
			});

			var template = new dd.Template('<img src="bar.gif" height="10" width="{% widthratio this_value max_value 100 %}" />');
			t.is('<img src="bar.gif" height="10" width="88" />', template.render(context));
		},
		function test_tag_with(t){
			var dd = dojox.dtl;

			var context = new dd.Context({
				person: {
					someSqlMethod: function(){
						return 4815162342;
					}
				}
			});

			var template = new dd.Template('{% with person.someSqlMethod as total %}{{ total }} object{{ total|pluralize }}{% endwith %}')
			t.is("4815162342 objects", template.render(context));
		}
	]
);

}