-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_beard.js
More file actions
672 lines (604 loc) · 18.1 KB
/
test_beard.js
File metadata and controls
672 lines (604 loc) · 18.1 KB
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
var sys = require('sys'),
fs = require('fs'),
Template = require('./beard').Template;
// comment tag
exports.testOneLineComment = function(test) {
test.expect(1);
var code = '{# This is a one line comment. #}';
var template = new Template(code);
test.equals(template.render(), '');
test.done();
};
exports.testMultiLineComment = function(test) {
test.expect(1);
var code = '{#\n'+
'This is a comment\n'+
'that has a # in it\n'+
'as well as a } and a }}.\n'+
'#}';
var template = new Template(code);
test.equals(template.render(), '');
test.done();
};
// variable tag
exports.testSemicolonInVariable = function(test) {
test.expect(1);
var code = '{{hello;there}}',
template = new Template(code);
test.equals(template.render(), code);
test.done();
};
exports.testPlainVariable = function(test) {
test.expect(1);
var code ='{{knownVariable}}';
var template = new Template(code);
test.equals(template.render({knownVariable: 'hello'}), 'hello');
test.done();
};
exports.testMultipleVariables = function(test) {
test.expect(1);
var code ='{{a}}{{b}}';
var template = new Template(code);
test.equals(template.render({a: 'a'}, {b: 'b'}), 'ab');
test.done();
};
exports.testUnknownVariable = function(test) {
test.expect(1);
var code = '{{unknownVariable}}';
var template = new Template(code);
// must throw VariablenotFound
try {
template.render({});
} catch(e) {
test.equals(e.name, 'VariableNotFound');
}
test.done();
};
exports.testVariableWithKnownFormatter = function(test) {
test.expect(1);
var code = '{{variable|knownFormatter}}';
var template = new Template(code, {
getFormatter: function(name) {
if (name == 'knownFormatter') {
return function(variable, context, parameters) {
// just put quotes around the variable as a test
var output = '"'+variable+'"';
return output
}
}
}
});
test.equals(template.render({variable: 'Hello'}), '"Hello"');
test.done();
};
exports.testVariableWithUnknownFormatter = function(test) {
test.expect(1);
var code = '{{variable|unknownFormatter}}';
// must throw FormatternotFound
try {
var template = new Template(code);
} catch(e) {
test.equals(e.name, 'FormatterNotFound');
}
test.done();
};
// if tag
exports.testPlainIf = function(test) {
test.expect(3);
var code = '{{if a}}Hello{{/if}}';
var template = new Template(code);
test.equals(template.render({a: true}), 'Hello');
test.equals(template.render({a: false}), '');
test.equals(template.render({}), '');
test.done();
};
exports.testIfElse = function(test) {
test.expect(3);
var code = '{{if a}}A{{else}}Not A{{/if}}';
var template = new Template(code);
test.equals(template.render({a: true}), 'A');
test.equals(template.render({a: false}), 'Not A');
test.equals(template.render({}), 'Not A');
test.done();
};
exports.testIfMissingEnd = function(test) {
test.expect(1);
var code = '{{if a}}';
// must throw TemplateSyntaxError
try {
var template = new Template(code);
} catch(e) {
test.equals(e.name, 'TemplateSyntaxError');
}
test.done();
};
// ifnot tag
exports.testPlainIfNot = function(test) {
test.expect(3);
var code = '{{if not a}}Hello{{/if}}';
var template = new Template(code);
test.equals(template.render({a: false}), 'Hello');
test.equals(template.render({a: true}), '');
test.equals(template.render({}), 'Hello');
test.done();
};
exports.testIfNotElse = function(test) {
test.expect(3);
var code = '{{if not a}}Not A{{else}}A{{/if}}';
var template = new Template(code);
test.equals(template.render({a: true}), 'A');
test.equals(template.render({a: false}), 'Not A');
test.equals(template.render({}), 'Not A');
test.done();
};
exports.testIfNotMissingEnd = function(test) {
test.expect(1);
var code = '{{if not a}}';
// must throw TemplateSyntaxError
try {
var template = new Template(code);
} catch(e) {
test.equals(e.name, 'TemplateSyntaxError');
}
test.done();
};
// loop tag
exports.testLoopWithoutItem = function(test) {
test.expect(2);
var code = '{{loop items}}\n'+
'{{counter0}}.{{counter1}}.{{if first}}First{{/if}}{{if last}}Last{{/if}}.'+
'{{if odd}}Odd{{/if}}{{if even}}Even{{/if}}.{{item.name}}\n'+
'{{/loop}}';
var template = new Template(code);
var output = template.render({
items: [
{name: 'John'},
{name: 'Paul'},
{name: 'George'},
{name: 'Ringo'}
]
});
// check that we're actually looping
// also check First, Last, Even, Odd, Counter0, Counter1
var firstFound = (output.indexOf('0.1.First.Even.John') != -1);
var lastFound = (output.indexOf('3.4.Last.Odd.Ringo') != -1);
test.ok(firstFound);
test.ok(lastFound);
test.done();
};
exports.testLoopWithItem = function(test) {
test.expect(2);
var code = '{{loop items as banana}}\n'+
'{{counter0}}.{{counter1}}.{{if first}}First{{/if}}{{if last}}Last{{/if}}.'+
'{{if odd}}Odd{{/if}}{{if even}}Even{{/if}}.{{banana.name}}\n'+
'{{/loop}}';
var template = new Template(code);
var output = template.render({
items: [
{name: 'John'},
{name: 'Paul'},
{name: 'George'},
{name: 'Ringo'}
]
});
// check that we're actually looping
// also check First, Last, Even, Odd, Counter0, Counter1
var firstFound = (output.indexOf('0.1.First.Even.John') != -1);
var lastFound = (output.indexOf('3.4.Last.Odd.Ringo') != -1);
test.ok(firstFound);
test.ok(lastFound);
test.done();
};
exports.testLoopNonIterable = function(test) {
test.expect(1);
var code = '{{loop items}}{{/loop}}';
var template = new Template(code);
try {
template.render({items: 1});
} catch(e) {
test.equals(e.name, 'VariableNotIterable');
}
test.done();
};
exports.testLoopMissingEnd = function(test) {
test.expect(1);
var code = '{{loop items}}';
// must throw TemplateSyntaxError
try {
var template = new Template(code);
} catch(e) {
test.equals(e.name, 'TemplateSyntaxError');
}
test.done();
};
// Override tag
exports.testUnknownSnippet = function(test) {
test.expect(1);
var code = '{{override unknown}}{{/override}}';
// must throw UnknownOverrideTag
try {
var template = new Template(code);
} catch(e) {
test.equals(e.name, 'UnknownOverrideTag');
}
test.done();
};
exports.testIgnoreOverride = function(test) {
test.expect(1);
var code = '{{override SingleProduct}}Hello{{/override}}';
var template = new Template(code, {
allowedSnippets: ['SingleProduct']
});
test.equals(template.render({}), '');
test.done();
};
exports.testRenderOverrideOnly = function(test) {
test.expect(1);
var code = 'This will not render\n'+
'{{override SingleProduct}}Hello{{/override}}\n'+
'This won\'t render either.';
var template = new Template(code, {
allowedSnippets: ['SingleProduct']
});
test.equals(template.renderSnippet({}, 'SingleProduct'), 'Hello');
test.done();
};
exports.testRenderUnknownSnippet = function(test) {
test.expect(1);
var code = '';
var template = new Template(code);
try {
template.renderSnippet({}, 'SingleProduct');
} catch(e) {
test.equals(e.name, 'UnknownSnippet')
}
test.done();
};
exports.testMultipleSameSnippet = function(test) {
test.expect(1);
var code = '{{override SingleProduct}}{{/override}}\n'+
'{{override SingleProduct}}{{/override}}\n';
try {
var template = new Template(code, {
allowedSnippets: ['SingleProduct']
});
} catch(e) {
test.equals(e.name, 'DuplicateOverrideTags');
}
test.done();
};
exports.testNestedOverrideTags = function(test) {
test.expect(1);
var code = '{{override SingleProduct}}\n'+
'{{override Products}}{{/override}}\n'+
'{{/override}}';
try {
var template = new Template(code, {
allowedSnippets: ['SingleProduct', 'Products']
});
} catch(e) {
test.equals(e.name, 'NestedOverrideTags');
}
test.done();
};
// Settings tags
exports.testSettingsWithInvalidSubtags = function(test) {
test.expect(1);
var code = "{{colour Background}}{{if something}}Err!{{/if}}{{/colour}}";
try {
var template = new Template(code);
} catch(e) {
test.equals(e.name, 'InvalidNestedTag');
}
test.done();
};
exports.testStyleWithInvalidSubtags = function(test) {
test.expect(1);
var code = "{{style Logo}}{{if something}}Err!{{/if}}{{/style}}";
try {
var template = new Template(code);
} catch(e) {
test.equals(e.name, 'InvalidNestedTag');
}
test.done();
};
exports.testStyleWithoutSubsets = function(test) {
test.expect(4);
var code = "{{style Logo}}{{/style}}";
var template = new Template(code),
settings = template.extractSettings();
test.equals(settings.length, 1);
var style = settings[0];
test.equals(style.subsets.length, 2);
test.equals(style.subsets[0], "b");
test.equals(style.subsets[1], "f");
test.done();
};
exports.testStyleWithValidSubsets = function(test) {
test.expect(3);
var code = "{{style:b Background}}{{/style}}";
var template = new Template(code),
settings = template.extractSettings();
test.equals(settings.length, 1);
var style = settings[0];
test.equals(style.subsets.length, 1);
test.equals(style.subsets[0], "b");
test.done();
};
exports.testStyleWithInvalidSubsets = function(test) {
test.expect(1);
var code = "{{style:rawr Invalid}}{{/style}}";
try {
var template = new Template(code);
} catch(e) {
test.equals(e.name, 'InvalidSubsets');
}
test.done();
};
exports.testTopLevelVariants = function(test) {
test.expect(1);
var code = "{{variant Something}}";
try {
var template = new Template(code);
} catch(e) {
test.equals(e.name, "TemplateSyntaxError");
}
test.done();
};
exports.testNonStyleWithVariants = function(test) {
test.expect(1);
var code = "{{if someVar}}{{variant Normal}}Err!{{/variant}}{{/if}}";
try {
var template = new Template(code);
} catch(e) {
test.equals(e.name, 'TemplateSyntaxError');
}
test.done();
};
exports.testStyleWithVariants = function(test) {
test.expect(19);
var code = "{{style:bf Nav | Global, utility and in-page.}}\n"+
"{{variant Normal | The default.}}color: #aaa;{{/variant}}\n"+
"{{variant Hover}}color: #333;{{/variant}}\n"+
"{{variant Selected}}color: #000;{{/variant}}\n"+
"{{/style}}";
/*
console.log("about to parse the template!");
try {
var template = new Template(code);
console.log("successfully parsed the template!");
} catch(e) {
console.log("template parse error!");
console.log(e);
}
*/
var template = new Template(code),
settings = template.extractSettings();
test.equals(settings.length, 1);
var style = settings[0];
test.equals(style.label, "Nav");
test.equals(style.help, "Global, utility and in-page.");
test.equals(style.type, "Style");
test.equals(style.isFile, false);
test.equals(style.variableName, "nav");
test.equals(style.defaultValue, undefined);
test.equals(style.options, undefined);
test.equals(style.variants.length, 3);
test.equals(style.subsets.length, 2);
test.equals(style.subsets[0], 'b');
test.equals(style.subsets[1], 'f');
var v1 = style.variants[0],
v2 = style.variants[1],
v3 = style.variants[2];
test.equals(v1.label, "Normal");
test.equals(v1.variableName, "normal");
test.equals(v1.help, "The default.");
test.equals(v1.defaultValue, "color: #aaa;");
test.equals(v2.label, "Hover");
test.equals(v2.help, "");
test.equals(v3.label, "Selected");
test.done();
};
exports.testChoiceSettings = function(test) {
test.expect(7);
var code = "{{choice Sidebar Position}}Left|Right{{/choice}}",
template = new Template(code),
settings = template.extractSettings();
test.equals(settings.length, 1);
var choice = settings[0];
test.equals(choice.defaultValue, "left");
test.equals(choice.options.length, 2);
test.equals(choice.options[0].label, "Left");
test.equals(choice.options[0].value, "left");
test.equals(choice.options[1].label, "Right");
test.equals(choice.options[1].value, "right");
test.done();
};
exports.testMutipleSettingsWithSameName = function(test) {
// multiple sizes with the same name
test.expect(1);
var code = "{{size Small}}100W{{/size}}{{size Small}}200W{{/size}}",
template;
try {
template = new Template(code);
} catch(e) {
test.equals(e.name, "DuplicateNameError");
}
test.done();
};
/*
exports.testMutipleSizesWithSameValue = function(test) {
test.expect(1);
var code = "{{size Small}}100W{{/size}}{{size Medium}}100W{{/size}}",
template;
try {
template = new Template(code);
} catch(e) {
test.equals(e.name, "DuplicateSizeError");
}
test.done();
};
*/
exports.testImageAndLogoWithSameName = function(test) {
// image and logo with the same name
test.expect(1);
var code = "{{logo Logo}}{{/logo}}{{image Logo}}{{/image}}",
template;
try {
template = new Template(code);
} catch(e) {
test.equals(e.name, "DuplicateNameError");
}
test.done();
};
exports.testFontSizeWithWrongUnit = function(test) {
// FontSize with wrong unit
test.expect(1);
var code = "{{fontsize Text}}2em{{/fontsize}}",
template;
try {
template = new Template(code);
} catch(e) {
test.equals(e.name, "FontSizeDefaultError");
}
test.done();
};
exports.testFontSizeOutOfRange = function(test) {
// FontSize that's too small or too big
test.expect(1);
var code = "{{fontsize Text}}5px{{/fontsize}}",
template;
try {
template = new Template(code);
} catch(e) {
test.equals(e.name, "FontSizeDefaultError");
}
test.done();
};
exports.testFloatPixelFontSize = function(test) {
// pixel FontSize that's not an integer
test.expect(1);
var code = "{{fontsize Text}}12.5px{{/fontsize}}",
template;
try {
template = new Template(code);
} catch(e) {
test.equals(e.name, "FontSizeDefaultError");
}
test.done();
};
exports.testSizeRuleMissingOrientation = function(test) {
// image size that doesn't end with a valid character
test.expect(1);
var code = "{{size Small}}100{{/size}}",
template;
try {
template = new Template(code);
} catch(e) {
test.equals(e.name, "InvalidSizeError");
}
test.done();
};
exports.testSizeRuleTooSmall = function(test) {
// image size of zero
test.expect(1);
var code = "{{size Very Small}}0{{/size}}",
template;
try {
template = new Template(code);
} catch(e) {
test.equals(e.name, "InvalidSizeError");
}
test.done();
};
exports.testColourWithoutHash = function(test) {
// colour without #
test.expect(1);
var code = "{{colour Something Black}}000000{{/colour}}",
template;
try {
template = new Template(code);
} catch(e) {
test.equals(e.name, "ColourDefaultError");
}
test.done();
};
exports.testColourNotValid = function(test) {
// colour that's not 3 or 6 hex digits
test.expect(1);
var code = "{{colour Something Wrong}}#ff{{/colour}}",
template;
try {
template = new Template(code);
} catch(e) {
test.equals(e.name, "ColourDefaultError");
}
test.done();
};
exports.testInvalidBoolean = function(test) {
// boolean that's not true or false
test.expect(1);
var code = "{{boolean Will this work?}}No{{/boolean}}",
template;
try {
template = new Template(code);
} catch(e) {
test.equals(e.name, "BooleanDefaultError");
}
test.done();
};
// other tests
exports.testAlternativeStyleTags = function(test) {
test.expect(1);
var code = "__object.property__",
template = new Template(code),
output = template.render({
object: {
property: "hello"
}
});
test.equals(output, "hello");
test.done();
};
exports.testSpaciousTags = function(test) {
test.expect(3);
var code = '{{ if a }}A{{ else }}Not A{{ /if }}';
var template = new Template(code);
test.equals(template.render({a: true}), 'A');
test.equals(template.render({a: false}), 'Not A');
test.equals(template.render({}), 'Not A');
test.done();
};
exports.testUnbalancedTags = function(test) {
test.expect(1);
var code = '{{if a}}{{loop b}}{{/if}}{{/loop}}';
try {
var template = new Template(code);
} catch(e) {
test.equals(e.name, 'TemplateSyntaxError');
}
test.done();
};
exports.testBlockTagsAsVariables = function(test) {
test.expect(1);
var tags = ['Loop', 'If', 'Override', 'Size', 'Image', 'Logo', 'CSS',
'JS', 'Style', 'Variant', 'HTML', 'Colour', 'Boolean',
'Font', 'Choice'],
code = "",
expected = "",
context = {};
for (var i in tags) {
var varname = tags[i].toLowerCase();
code = code + '{{' + varname + '}}';
expected = expected + varname;
context[varname] = varname;
}
var template = new Template(code),
output = template.render(context);
test.equals(output, expected);
test.done();
};
// TODO: test these:
// hasSnippet
// replaceWidgets
// highlight