index.js 27.7 KB
Newer Older
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
/*
 *  Copyright 2011 Twitter, Inc.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

var Hogan = Hogan || require('../lib/hogan')
  , doc = this["document"]

function testScanTextNoTags() {
  var text = "<h2>hi</h2>";
  var tokens = Hogan.scan(text);
  is(tokens.length, 1, "One token");
  is(tokens[0]+'', text, "text is equal to first token");
}

function testScanOneTag() {
  var text = "{{hmm}}";
  var tokens = Hogan.scan(text);
  is(tokens.length, 1, "One token");
  is(tokens[0].n, "hmm", "First token content is variable name.");
}

function testScanMultipleTags() {
  var text = "asdf{{hmm}}asdf2{{hmm2}}asdf3";
  var tokens = Hogan.scan(text);
  is(tokens.length, 5, "3 text tokens, 2 tag tokens.");
  is(tokens[0]+'', "asdf", "first token is text");
  is(tokens[1].n, "hmm", "second token is tag");
  is(tokens[1].tag, "_v", "second token is a variable");
  is(tokens[2]+'', "asdf2", "third token is text");
  is(tokens[3].n, "hmm2", "fourth token is tag");
  is(tokens[3].tag, "_v", "fourth token is a variable");
  is(tokens[4]+'', "asdf3", "Fifth token is text");
}

function testScanSectionOpen() {
  var text = "{{#hmm}}";
  var tokens = Hogan.scan(text);
  is(tokens.length, 1, "One token");
  is(tokens[0].n, "hmm", "First token content is variable name.");
  is(tokens[0].tag, "#", "First token is a section.");
}

function testScanSectionClose() {
  var text = "{{/hmm}}";
  var tokens = Hogan.scan(text);
  is(tokens.length, 1, "One token");
  is(tokens[0].n, "hmm", "First token content is variable name.");
  is(tokens[0].tag, "/", "First token is a section.");
}

function testScanSection() {
  var text = "{{#hmm}}{{/hmm}}";
  var tokens = Hogan.scan(text);
  is(tokens.length, 2, "One token");
  is(tokens[0].n, "hmm", "First token content is variable name.");
  is(tokens[0].tag, "#", "First token is a section.");
  is(tokens[1].n, "hmm", "Second token content is variable name.");
  is(tokens[1].tag, "/", "Second token is a section.");
}

function testScanSectionInContent() {
  var text = "abc{{#hmm}}def{{/hmm}}ghi";
  var tokens = Hogan.scan(text);
  is(tokens.length, 5, "3 text tokens, 2 tag tokens.");
  is(tokens[0]+'', "abc", "first token is text");
  is(tokens[1].n, "hmm", "second token is tag");
  is(tokens[1].tag, "#", "second token is a variable");
  is(tokens[2]+'', "def", "third token is text");
  is(tokens[3].n, "hmm", "fourth token is tag");
  is(tokens[3].tag, "/", "fourth token is a variable");
  is(tokens[4]+'', "ghi", "Fifth token is text");
}

function testScanNegativeSection() {
  var text = "{{^hmm}}{{/hmm}}";
  var tokens = Hogan.scan(text);
  is(tokens.length, 2, "One token");
  is(tokens[0].n, "hmm", "First token content is variable name.");
  is(tokens[0].tag, "^", "First token is a negative section.");
  is(tokens[1].n, "hmm", "First token content is variable name.");
  is(tokens[1].tag, "/", "Second token is a section.");
}

function testScanPartial() {
  var text = "{{>hmm}}";
  var tokens = Hogan.scan(text);
  is(tokens.length, 1, "One token");
  is(tokens[0].n, "hmm", "First token content is variable name.");
  is(tokens[0].tag, ">", "First token is a partial.");
}


function testScanBackwardPartial() {
  var text = "{{<hmm}}";
  var tokens = Hogan.scan(text);
  is(tokens.length, 1, "One token");
  is(tokens[0].n, "hmm", "First token content is variable name.");
  is(tokens[0].tag, "<", "First token is a backward partial.");
}

function testScanAmpersandNoEscapeTag() {
  var text = "{{&hmm}}";
  var tokens = Hogan.scan(text);
  is(tokens.length, 1, "One token");
  is(tokens[0].n, "hmm", "First token content is variable name.");
  is(tokens[0].tag, "&", "First token is an ampersand no-escape.");
}

function testScanTripleStache() {
  var text = "{{{hmm}}}";
  var tokens = Hogan.scan(text);
  is(tokens.length, 1, "One token");
  is(tokens[0].n, "hmm", "First token content is variable name.");
  is(tokens[0].tag, "{", "First token is a triple-stache.");
}

function testScanSectionWithTripleStacheInside() {
  var text = "a{{#yo}}b{{{hmm}}}c{{/yo}}d";
  var tokens = Hogan.scan(text);
  is(tokens.length, 7, "One token");
  is(tokens[0]+'', "a", "First token content is correct text.");
  is(tokens[1].n, "yo", "Second token content is correct text.");
  is(tokens[1].tag, "#", "Second token is a section.");
  is(tokens[2]+'', "b", "Third token content is correct text.");
  is(tokens[3].n, "hmm", "Fourth token content is correct text.");
  is(tokens[3].tag, "{", "Fourth token is a triple stache.");
  is(tokens[4]+'', "c", "Fifth token content is correct text.");
  is(tokens[5].n, "yo", "Sixth token content is correct text.");
  is(tokens[5].tag, "/", "Sixth token is a close.");
  is(tokens[6]+'', "d", "Seventh token content is correct text.");
}

function testScanSetDelimiter() {
  var text = "a{{=<% %>=}}b";
  var tokens = Hogan.scan(text);
  is(tokens.length, 2, "change delimiter doesn't appear as token.");
  is(tokens[0]+'', "a", "text before change delimiter is processed.");
  is(tokens[1]+'', "b", "text after change delimiter is processed.");
}

function testScanResetDelimiter() {
  var text = "a{{=<% %>=}}b<%hmm%>c<%={{ }}=%>d{{hmm}}";
  var tokens = Hogan.scan(text);
  is(tokens.length, 6, "8 tokens, delimiter changes don't count.");
  is(tokens[0]+'', "a", "first token is correct.");
  is(tokens[1]+'', "b", "third token is correct.");
  is(tokens[2].tag, "_v", "third token is correct tag.");
  is(tokens[2].n, "hmm", "third token is correct name.");
  is(tokens[3]+'', "c", "fifth token is correct.");
  is(tokens[4]+'', "d", "seventh token is correct.");
  is(tokens[5].tag, "_v", "eighth token is correct tag.");
  is(tokens[5].n, "hmm", "eighth token is correct name.");
}

function testSingleCharDelimiter() {
  var text = '({{foo}} {{=[ ]=}}[text])';
  var tokens = Hogan.scan(text);

  var t = Hogan.compile(text);
  s = t.render({foo: "bar", text: 'It worked!'});
  is(s, '(bar It worked!)', "Hogan substitution worked after custom delimiters.");
}

function testSetDelimiterWithWhitespace() {
  var text = "{{= | | =}}|foo|";
  var t = Hogan.compile(text);
  s = t.render({foo: "bar"});
  is(s, 'bar', "custom delimiters with whitespace works.")
}

function testParseBasic() {
  var text = "test";
  var tree = Hogan.parse(Hogan.scan(text));
  is(tree.length, 1, "one parse node");
  is(tree[0]+'', "test", "text is correct");
}

function testParseVariables() {
  var text = "test{{foo}}test!{{bar}}test!!{{baz}}test!!!";
  var tree = Hogan.parse(Hogan.scan(text));
  is(tree.length, 7, "one parse node");
  is(tree[0]+'', "test", "first text is correct");
  is(tree[2]+'', "test!", "second text is correct")
  is(tree[4]+'', "test!!", "third text is correct")
  is(tree[6]+'', "test!!!", "last text is correct")
  is(tree[1].n, "foo", "first var is correct");
  is(tree[3].n, "bar", "second var is correct");
  is(tree[5].n, "baz", "third var is correct");
}

function testParseSection() {
  var text = "a{{#foo}}b{{/foo}}c";
  var tree = Hogan.parse(Hogan.scan(text));
  is(tree.length, 3, "three nodes at base");
  is(tree[0]+'', "a", "correct text in first node");
  is(tree[1].hasOwnProperty('nodes'), true, "second node is a section");
  is(tree[1].tag, '#', "second node is a section");
  is(tree[1].n, "foo", "correct name for section");
  is(tree[1].nodes[0]+'', "b", "correct text in section");
  is(tree[2]+'', "c", "correct text in last node");
}

function testParseIndexes() {
  var text = "abc{{#foo}}asdf{{bar}}asdf{{/foo}}def";
  var tree = Hogan.parse(Hogan.scan(text));
  is(text.substring(tree[1].i, tree[1].end), "asdf{{bar}}asdf", "section text indexes are correct");
}

function testParseNegativeSection() {
  var text = "a{{^foo}}b{{/foo}}c";
  var tree = Hogan.parse(Hogan.scan(text));

  is(tree.length, 3, "three nodes at base");
  is(tree[0]+'', "a", "correct text in first node");
  is(tree[1].hasOwnProperty('nodes'), true, "second node is a section");
  is(tree[1].tag, '^', "second node is a negative section");
  is(tree[1].n, "foo", "correct name for section");
  is(tree[1].nodes[0]+'', "b", "correct text in section");
  is(tree[2]+'', "c", "correct text in last node");
}

function testParseNestedSections() {
  var text = "{{#bar}}{{#foo}}c{{/foo}}{{/bar}}"
  var tree = Hogan.parse(Hogan.scan(text));

  is(tree.length, 1, "one node at base");
  is(tree[0].tag, "#", "open section is first node");
  is(tree[0].n, "bar", "first section name is 'bar'");
  is(tree[0].nodes.length, 1, "first section contains one node.");
  is(tree[0].nodes[0].n, "foo", "correct name for nested section");
  is(tree[0].nodes[0].nodes[0]+'', "c", "correct text in nested section");
}

function testMissingClosingTag() {
  var text = "a{{#foo}}bc";
  var msg = '';
  try {
    var tree = Hogan.parse(Hogan.scan(text));
  } catch (e) {
    msg = e.message;
  }
  is(msg, "missing closing tag: foo", "Error is generated");
}

function testBadNesting() {
  var text = "a{{#foo}}{{#bar}}b{{/foo}}{{/bar}}c";
  var msg = '';
  try {
    var tree = Hogan.parse(Hogan.scan(text));
  } catch (e) {
    msg = e.message;
  }
  is(msg, "Nesting error: bar vs. foo", "Error is generated");
}

function testBasicOutput() {
  var text = "test";
  var t = Hogan.compile(text);
  is(t.render(), text, "template renders one text node");
}

function testBasicOutputAsString() {
  var text = "test";
  var textFunc = Hogan.compile(text, true);
  is(textFunc, "function(context, partials){this.buffer.push('test');};", "template renders correct text function.");
}

function testOneVariable() {
  var text = "test {{foo}} test";
  var t = Hogan.compile(text);
  var s = t.render({foo:'bar'});
  is(s, "test bar test", "basic variable substitution works.");
}

function testOneVariableAsString() {
  var text = "test {{foo}} test";
  var funcText = Hogan.compile(text, true);
  is(funcText, "function(context, partials){this.buffer.push('test ');\nthis.buffer.push(this.find('foo', context));\nthis.buffer.push(' test');};",
     "Function text is correct with variable substitution.");
}

function testRenderWithWhitespace() {
  var text = "{{ string }}";
  var t = Hogan.compile(text);
  is(t.render({string: "---" }), "---", "tags with whitespace render correctly.");
}

function testRenderWithWhitespaceAroundTripleStache() {
  var text = "  {{{string}}}\n";
  var t = Hogan.compile(text);
  is(t.render({string: "---" }), "  ---\n", "triple stache surrounded by whitespace render correctly.");
}

function testRenderWithWhitespaceAroundAmpersand() {
  var text = "  {{& string }}\n";
  var t = Hogan.compile(text);
  is(t.render({string: "---" }), "  ---\n", "ampersand surrounded by whitespace render correctly.");
}

function testMultipleVariables() {
  var text = "test {{foo}} test {{bar}} test {{baz}} test {{foo}} test";
  var t = Hogan.compile(text);
  var s = t.render({foo:'42', bar: '43', baz: '44'});
  is(s, "test 42 test 43 test 44 test 42 test", "all variables render correctly.");
}

function testNumberValues() {
  var text = "integer: {{foo}} float: {{bar}} negative: {{baz}}";
  var t = Hogan.compile(text);
  var s = t.render({foo: 42, bar: 42.42, baz: -42});
  is(s, "integer: 42 float: 42.42 negative: -42", "numbers render correctly");
}

function testObjectRender() {
  var text = "object: {{foo}}";
  var t = Hogan.compile(text);
  var s = t.render({foo: {}});
  is(s, "object: [object Object]", "objects render default toString.");
}

function testObjectToStringRender() {
  var text = "object: {{foo}}";
  var t = Hogan.compile(text);
  var s = t.render({foo: {toString: function(){ return "yo!"}}});
  is(s, "object: yo!", "objects render supplied toString.");
}

function testArrayRender() {
  var text = "array: {{foo}}";
  var t = Hogan.compile(text);
  var s = t.render({foo: ["a","b","c"]});
  is(s, "array: a,b,c", "arrays render default toString.");
}

function testEscaping() {
  var text = "{{foo}}";
  var t = Hogan.compile(text);
  var s = t.render();
  var s = t.render({foo: "< > <div> \' \" &"});
  is(s, "&lt; &gt; &lt;div&gt; &#39; &quot; &amp;", "input correctly escaped.");

  var ec ={ "'": "&#39;", '"': "&quot;", "<": "&lt;", ">": "&gt;", "&": "&amp;"}
  for (var char in ec) {
    var s = t.render({foo: char + " just me"});
    is(s, ec[char] + " just me", "input correctly escaped.");
  }

}

function testMustacheInjection() {
  var text = "{{foo}}";
  var t = Hogan.compile(text);
  s = t.render({foo:"{{{<42}}}"})
  is(s, "{{{&lt;42}}}", "Can't inject mustache");
}

function testTripleStache() {
  var text = "{{{foo}}}";
  var t = Hogan.compile(text);
  var s = t.render({foo: "< > <div> \' \" &"});
  is(s, "< > <div> \' \" &", "input correctly not-escaped.");
}

function testAmpNoEscaping() {
  var text = "{{&foo}}";
  var t = Hogan.compile(text);
  var s = t.render({foo: "< > <div> \' \" &"});
  is(s, "< > <div> \' \" &", "input correctly not-escaped.");
}

function testPartial() {
  var partialText = "this is text from the partial--the magic number {{foo}} is from a variable";
  var p = Hogan.compile(partialText);

  var text = "This template contains a partial ({{>testPartial}})."
  var t = Hogan.compile(text);

  var s = t.render({foo: 42}, {testPartial: p});
  is(s, "This template contains a partial (this is text from the partial--the magic number 42 is from a variable).", "partials work");
}

function testNestedPartials() {
  var partialText = "this is text from the partial--the magic number {{foo}} is from a variable";
  var p = Hogan.compile(partialText);

  var partialText2 = "This template contains a partial ({{>testPartial}})."
  var p2 = Hogan.compile(partialText2);

  var text = "This template contains a partial that contains a partial [{{>testPartial2}}]."
  var t = Hogan.compile(text);

  var s = t.render({foo: 42}, {testPartial: p, testPartial2: p2});
  is(s, "This template contains a partial that contains a partial [This template contains a partial (this is text from the partial--the magic number 42 is from a variable).].", "nested partials work");
}

function testNegativeSection() {
  var text = "This template {{^foo}}BOO {{/foo}}contains an inverted section."
  var t = Hogan.compile(text);
  var s = t.render();
  is(s, "This template BOO contains an inverted section.", "inverted sections with no context work");

  s = t.render({foo:[]});
  is(s, "This template BOO contains an inverted section.", "inverted sections with empty list context work");

  s = t.render({ foo:false });
  is(s, "This template BOO contains an inverted section.", "inverted sections with false context work");

  s = t.render({foo:''});
  is(s, "This template contains an inverted section.", "inverted sections with empty string context work");

  s = t.render({foo:true});
  is(s, "This template contains an inverted section.", "inverted sections with true context work");

  s = t.render({foo: function() { return false; }});
  is(s, "This template BOO contains an inverted section.", "inverted sections with false returning method in context work");
}

function testSectionElision() {
  var text = "This template {{#foo}}BOO {{/foo}}contains a section."
  var t = Hogan.compile(text);
  var s = t.render();
  is(s, "This template contains a section.", "sections with no context work");

  s = t.render({foo:[]});
  is(s, "This template contains a section.", "sections with empty list context work");

  s = t.render({foo:false});
  is(s, "This template contains a section.", "sections with false context work");
}

function testSectionObjectContext() {
  var text = "This template {{#foo}}{{bar}} {{/foo}}contains a section."
  var t = Hogan.compile(text);
  var s = t.render({foo:{bar:42}});
  is(s, "This template 42 contains a section.", "sections with object context work");
}

function testSectionArrayContext() {
  var text = "This template {{#foo}}{{bar}} {{/foo}}contains a section."
  var t = Hogan.compile(text);
  var s = t.render({foo:[{bar:42}, {bar:43}, {bar:44}]});
  is(s, "This template 42 43 44 contains a section.", "sections with object ctx and array values work");
}

function testFalsyVariableNoRender() {
  var text = "I ({{cannot}}) be seen!";
  var t = Hogan.compile(text);
  var s = t.render();
  is(s, "I () be seen!", "missing value doesn't render.");
}

function testSectionExtensions() {
  var text = "Test {{_//|__foo}}bar{{/foo}}";
  var options = {sectionTags:[{o:'_//|__foo', c:'foo'}]};
  var tree = Hogan.parse(Hogan.scan(text), options);
  is(tree[1].tag, "#", "_//|__foo node transformed to section");
  is(tree[1].n, "_//|__foo", "_//|__foo node transformed to section");

  var t = Hogan.compile(text, options );
  var s = t.render({'_//|__foo':true});
  is(s, "Test bar", "Custom sections work");
}

function testMisnestedSectionExtensions() {
  var text = "Test {{__foo}}bar{{/bar}}";
  var options = {sectionTags:[{o:'__foo', c:'foo'}, {o:'__bar', c:'bar'}]};
  var msg = '';
  try {
    var tree = Hogan.parse(Hogan.scan(text), options);
  } catch (e) {
    msg = e.message;
  }
  is(msg, "Nesting error: __foo vs. bar", "Error is generated");
}

function testNestedSection() {
  var text = "{{#foo}}{{#bar}}{{baz}}{{/bar}}{{/foo}}";
  var t = Hogan.compile(text);
  var s = t.render({foo: 42, bar: 42, baz:42});
  is(s, "42", "can reach up context stack");
}

function testDottedNames() {
  var text = '"{{person.name}}" == "{{#person}}{{name}}{{/person}}"';
  var t = Hogan.compile(text);
  var s = t.render({person:{name:'Joe'}});
  is(s, '"Joe" == "Joe"', "dotted names work");
}

function testImplicitIterator() {
  var text = '{{#stuff}} {{.}} {{/stuff}}';
  var t = Hogan.compile(text);
  var s = t.render({stuff:[42,43,44]});
  is(s, " 42  43  44 ", "implicit iterators work");
}

function testPartialsAndDelimiters() {
  var text = '{{>include}}*\n{{= | | =}}\n*|>include|';
  var partialText = ' .{{value}}. ';
  var partial = Hogan.compile(partialText);
  var t = Hogan.compile(text);
  var s = t.render({value:"yes"}, {'include':partial});
  is(s, " .yes. *\n* .yes. ", "partials work around delimiters");
}

function testStringPartials() {
  var text = "foo{{>mypartial}}baz";
  var partialText = " bar ";
  var t = Hogan.compile(text);
  var s = t.render({}, {'mypartial': partialText});
  is(s, "foo bar baz", "string partial works.");
}

function testMissingPartials() {
  var text = "foo{{>mypartial}} bar";
  var t = Hogan.compile(text);
  var s = t.render({});
  is(s, "foo bar", "missing partial works.");
}

function testIndentedStandaloneComment() {
  var text = 'Begin.\n {{! Indented Comment Block! }}\nEnd.';
  var t = Hogan.compile(text);
  var s = t.render();
  is(s, 'Begin.\nEnd.', "Standalone comment blocks are removed.");
}

function testNewLineBetweenDelimiterChanges() {
  var data = { section: true, data: 'I got interpolated.' };
  var text = '\n{{#section}}\n {{data}}\n |data|\n{{/section}}x\n\n{{= | | =}}\n|#section|\n {{data}}\n |data|\n|/section|';
  var t = Hogan.compile(text);
  var s = t.render(data);
  is(s, '\n I got interpolated.\n |data|\nx\n\n {{data}}\n I got interpolated.\n', 'render correct')
}

function testMustacheJSApostrophe() {
  var text = '{{apos}}{{control}}';
  var t = Hogan.compile(text);
  var s = t.render({'apos':"'", 'control':"X"});
  is(s, '&#39;X', 'Apostrophe is escaped.');
}

function testMustacheJSArrayOfImplicitPartials() {
  var text = 'Here is some stuff!\n{{#numbers}}\n{{>partial}}\n{{/numbers}}\n';
  var partialText = '{{.}}\n';
  var t = Hogan.compile(text);
  var s = t.render({numbers:[1,2,3,4]}, {partial: partialText});
  is(s, 'Here is some stuff!\n1\n2\n3\n4\n', 'Partials with implicit iterators work.');
}

function testMustacheJSArrayOfPartials() {
  var text = 'Here is some stuff!\n{{#numbers}}\n{{>partial}}\n{{/numbers}}\n';
  var partialText = '{{i}}\n';
  var t = Hogan.compile(text);
  var s = t.render({numbers:[{i:1},{i:2},{i:3},{i:4}]}, {partial: partialText});
  is(s, 'Here is some stuff!\n1\n2\n3\n4\n', 'Partials with arrays work.');
}

function testMustacheJSArrayOfStrings() {
  var text = '{{#strings}}{{.}} {{/strings}}';
  var t = Hogan.compile(text);
  var s = t.render({strings:['foo', 'bar', 'baz']});
  is(s, 'foo bar baz ', 'array of strings works with implicit iterators.');
}

function testMustacheJSUndefinedString() {
  var text = 'foo{{bar}}baz';
  var t = Hogan.compile(text);
  var s = t.render({bar:undefined});
  is(s, 'foobaz', 'undefined value does not render.');
}

function testMustacheJSTripleStacheAltDelimiter() {
  var text = '{{=<% %>=}}<% foo %> {{foo}} <%{bar}%> {{{bar}}}';
  var t = Hogan.compile(text);
  var s = t.render({foo:'yeah', bar:'hmm'});
  is(s, 'yeah {{foo}} hmm {{{bar}}}', 'triple stache inside alternate delimiter works.');
}

/* shootout benchmark tests */

function testShootOutString() {
  var text = "Hello World!";
  var expected = "Hello World!"
  var t = Hogan.compile(text)
  var s = t.render({})
  is(s, expected, "Shootout String compiled correctly");
}

function testShootOutReplace() {
  var text = "Hello {{name}}! You have {{count}} new messages.";
  var expected = "Hello Mick! You have 30 new messages.";
  var t = Hogan.compile(text)
  var s = t.render({ name: "Mick", count: 30 })
  is(s, expected, "Shootout Replace compiled correctly");
}

function testShootOutArray() {
  var text = "{{#names}}{{name}}{{/names}}";
  var expected = "MoeLarryCurlyShemp";
  var t = Hogan.compile(text);
  var s = t.render({ names: [{name: "Moe"}, {name: "Larry"}, {name: "Curly"}, {name: "Shemp"}] })
  is(s, expected, "Shootout Array compiled correctly");
}

function testShootOutObject() {
  var text = "{{#person}}{{name}}{{age}}{{/person}}";
  var expected = "Larry45";
  var t = Hogan.compile(text)
  var s = t.render({ person: { name: "Larry", age: 45 } })
  is(s, expected, "Shootout Object compiled correctly");
}

function testShootOutPartial() {
  var text = "{{#peeps}}{{>replace}}{{/peeps}}";
  var t = Hogan.compile(text);
  var partial = Hogan.compile(" Hello {{name}}! You have {{count}} new messages.");
  var s = t.render({ peeps: [{name: "Moe", count: 15}, {name: "Larry", count: 5}, {name: "Curly", count: 2}] }, { replace: partial });
  var expected = " Hello Moe! You have 15 new messages. Hello Larry! You have 5 new messages. Hello Curly! You have 2 new messages.";
  is(s, expected, "Shootout Partial compiled correctly");
}

function testShootOutRecurse() {
  var text = "{{name}}{{#kids}}{{>recursion}}{{/kids}}";
  var t = Hogan.compile(text);
  var partial = Hogan.compile("{{name}}{{#kids}}{{>recursion}}{{/kids}}");
  var s = t.render({
                name: '1',
                kids: [
                  {
                    name: '1.1',
                    kids: [
                      { name: '1.1.1', kids: [] }
                    ]
                  }
                ]
              }, { recursion: partial });
  var expected = "11.11.1.1";
  is(s, expected, "Shootout Recurse compiled correctly");
}

function testShootOutFilter() {
  var text = "{{#filter}}foo {{bar}}{{/filter}}";
  var t = Hogan.compile(text);
  var s = t.render({
    filter: function() {
      return function(text, render) {
        return render(text).toUpperCase();
      }
    },
    bar: "bar"
  });
  var expected = "FOO BAR"
  is(s, expected, "Shootout Filter compiled correctly");
}

function testShootOutComplex() {
  var text =
    "<h1>{{header}}</h1>" +
    "{{#hasItems}}" +
    "<ul>" +
      "{{#items}}" +
        "{{#current}}" +
          "<li><strong>{{name}}</strong></li>" +
        "{{/current}}" +
        "{{^current}}" +
          "<li><a href=\"{{url}}\">{{name}}</a></li>" +
        "{{/current}}" +
      "{{/items}}" +
    "</ul>" +
    "{{/hasItems}}" +
    "{{^hasItems}}" +
      "<p>The list is empty.</p>" +
    "{{/hasItems}}";

  var expected = "<h1>Colors</h1><ul><li><strong>red</strong></li><li><a href=\"#Green\">green</a></li><li><a href=\"#Blue\">blue</a></li></ul>";
  var t = Hogan.compile(text)
  var s = t.render({
     header: function() {
       return "Colors";
     },
     items: [
       {name: "red", current: true, url: "#Red"},
       {name: "green", current: false, url: "#Green"},
       {name: "blue", current: false, url: "#Blue"}
     ],
     hasItems: function() {
       return this.items.length !== 0;
     },
     empty: function() {
       return this.items.length === 0;
     }
  })

  is(s, expected, "Shootout Complex compiled correctly");
}

function testRenderOutput() {
  if (doc) return
  var fs = require('fs');
  var inPath = 'test/templates';
  var outPath = 'test/html';

  fs.readdirSync(inPath).forEach(function (file) {
    var i = fs.readFileSync([inPath, file].join('/'), 'utf-8');
    var t = Hogan.compile(i);
    var r = t.render({});
    var o = fs.readFileSync([outPath, file].join('/').replace(/mustache$/, 'html')).toString();
    is(r === o, true, file + ' should correctly render html')
  })
}

function testDefaultRenderImpl() {
  var ht = new Hogan.Template();
  is(ht.render() === '', true, 'default renderImpl returns an array.');
}


function appendText(el, text) {
  var textNode = document.createTextNode(text);
  el.appendChild(textNode);
  el.appendChild(document.createElement('br'));
}

if (!this["output"]) {
  var output = function (s) {
    return doc ? appendText(doc.getElementById('console'), s) : console.log(s);
  };
}
var passed = 0;
var failed = 0;

function is(got, expected, msg) {
  if (got === expected) {
    output("OK:   " + msg);
    ++passed;
  } else {
    output("FAIL: " + msg);
    output("Expected |" + expected + "|");
    output("     Got |" + got + "|");
    ++failed;
  }
}

function complete() {
  output("\nTests Complete");
  output("--------------");
  output("Passed: " + passed);
  output("Failed: " + failed);
  output("\n");
}

function runTests() {
  output("Tests Starting");
  output("--------------");
  testScanTextNoTags();
  testScanOneTag();
  testScanMultipleTags();
  testScanSectionOpen();
  testScanSectionClose();
  testScanSection();
  testScanSectionInContent();
  testScanNegativeSection();
  testScanPartial();
  testScanBackwardPartial();
  testScanAmpersandNoEscapeTag();
  testScanTripleStache();
  testScanSectionWithTripleStacheInside();
  testScanSetDelimiter();
  testScanResetDelimiter();
  testSetDelimiterWithWhitespace();
  testSingleCharDelimiter();
  testParseBasic();
  testParseVariables();
  testParseSection();
  testParseIndexes();
  testParseNegativeSection();
  testParseNestedSections();
  testMissingClosingTag();
  testBadNesting();
  testBasicOutput();
 //testBasicOutputAsString();
  testOneVariable();
 //testOneVariableAsString();
  testMultipleVariables();
  testNumberValues();
  testObjectRender();
  testObjectToStringRender();
  testArrayRender();
  testEscaping();
  testMustacheInjection();
  testTripleStache();
  testAmpNoEscaping();
  testPartial();
  testNestedPartials();
  testNegativeSection();
  testSectionElision();
  testSectionObjectContext();
  testSectionArrayContext();
  testRenderWithWhitespace();
  testRenderWithWhitespaceAroundTripleStache();
  testRenderWithWhitespaceAroundAmpersand();
  testFalsyVariableNoRender();
  testRenderOutput();
  testDefaultRenderImpl();
  testSectionExtensions();
  testMisnestedSectionExtensions();
  testNestedSection();
  testShootOutString();
  testShootOutReplace();
  testShootOutArray();
  testShootOutObject();
  testShootOutPartial();
  testShootOutRecurse();
  testShootOutFilter();
  testShootOutComplex();
  testDottedNames();
  testImplicitIterator();
  testPartialsAndDelimiters();
  testStringPartials();
  testMissingPartials();
  testIndentedStandaloneComment();
  testNewLineBetweenDelimiterChanges();
  testMustacheJSApostrophe();
  testMustacheJSArrayOfImplicitPartials();
  testMustacheJSArrayOfPartials();
  testMustacheJSArrayOfStrings();
  testMustacheJSUndefinedString();
  testMustacheJSTripleStacheAltDelimiter();
  complete();
}

if (doc) {
  window.onload = runTests;
} else {
  runTests();
}