~ludovicc/uj/stringtemplate

« back to all changes in this revision

Viewing changes to src/org/antlr/stringtemplate/test/TestStringTemplate.java

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2008-02-26 15:20:55 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080226152055-dyloofp410km3kei
Tags: 3.1-3
Added junit to Build-Depends-Indep. Closes #467605.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
*/
28
28
package org.antlr.stringtemplate.test;
29
29
 
 
30
import junit.framework.Assert;
 
31
import junit.framework.TestCase;
30
32
import org.antlr.stringtemplate.*;
31
33
import org.antlr.stringtemplate.language.AngleBracketTemplateLexer;
32
34
import org.antlr.stringtemplate.language.DefaultTemplateLexer;
35
37
import java.text.SimpleDateFormat;
36
38
import java.util.*;
37
39
 
38
 
/** Test the various functionality of StringTemplate. Seems to run only
39
 
 *  on unix due to \r\n vs \n issue.  David Scurrah says:
40
 
 *
41
 
 * "I don't think you were necessarily sloppy with your newlines, but Java make it very difficult to be consistant.
42
 
The stringtemplate library used unix end of lines for writing toString methods and the like,
43
 
while the testing was using the system local end of line. The other problem with end of lines was any template
44
 
file used in the testing will also have a specific end of line ( this case unix) and when read into a string that can the unique problem
45
 
of having end of line unix and local system end of line in the on  line.
46
 
 
47
 
My solution was not very elegant but I think it required the least changes and only to the testing.
48
 
I simply converted all strings to use unix end of line characters inside the assertTrue and then compared them.
49
 
The only other problem I found was writing a file out to the /tmp directory won't work on windows so I used the
50
 
system property  java.io.tmpdir to get a temp directory."
51
 
 
52
 
 * I'll fix later.
53
 
 */
54
 
public class TestStringTemplate extends TestSuite {
55
 
    final String newline = System.getProperty("line.separator");
56
 
 
57
 
        public void runTests() throws Throwable {
58
 
                TestRig.runAllTests(this.getClass(), this);
59
 
                /*
60
 
                System.out.println("num obj.prop refs: "+ ASTExpr.totalObjPropRefs);
61
 
                System.out.println("num obj.prop refs: "+ ASTExpr.totalObjPropComputations);
62
 
                */
63
 
        }
64
 
 
65
 
        public TestStringTemplate() {
66
 
    }
 
40
public class TestStringTemplate extends TestCase {
 
41
    static final String newline = System.getProperty("line.separator");
67
42
 
68
43
        static class ErrorBuffer implements StringTemplateErrorListener {
69
44
                StringBuffer errorOutput = new StringBuffer(500);
96
71
                }
97
72
        }
98
73
 
 
74
    /**
 
75
     * A fix for the String Template tests so that they run on unix and windows.
 
76
     * This is done by ignoring \r\n vs \n differences.
 
77
     * <p>
 
78
     * This method simply removes any \r\n and replaces them with \n. Please not the 
 
79
     * order of the parameters. Expected is the "fixed" item and actual is the string
 
80
     * generated by the test code.
 
81
     * <p>
 
82
     * David Scurrah says:<br>
 
83
     * "I don't think you were necessarily sloppy with your newlines, but Java make
 
84
     * it very difficult to be consistant. The stringtemplate library used unix end
 
85
     * of lines for writing toString methods and the like, while the testing was 
 
86
     * using the system local end of line. The other problem with end of lines was 
 
87
     * any template file used in the testing will also have a specific end of line 
 
88
     * (this case unix) and when read into a string that can the unique problem
 
89
     * of having end of line unix and local system end of line in the on  line.
 
90
     * <p> 
 
91
     * @param expected
 
92
     * @param actual
 
93
     */
 
94
    public static void assertEquals(String expected, String actual) 
 
95
    {
 
96
        // Replace all \r\n with \n for consistent use.
 
97
                Assert.assertEquals(
 
98
                                expected.replaceAll("\r\n", "\n"),
 
99
                                actual.replaceAll("\r\n", "\n")
 
100
                );
 
101
/*
 
102
        try {
 
103
            Assert.assertEquals(
 
104
                    expected.replaceAll("\r\n", "\n"),
 
105
                    actual.replaceAll("\r\n", "\n")
 
106
            );
 
107
        } catch (AssertionFailedError e) {
 
108
            StringBuffer fullText = new StringBuffer();
 
109
            
 
110
            fullText.append("Assertion Equals details: ").append(newline)
 
111
                    .append("----")          .append(newline)
 
112
                    .append("Expected:")     .append(newline)
 
113
                    .append(expected)        .append(newline)
 
114
                    .append("----")          .append(newline)
 
115
                    .append("Actual:")       .append(newline)
 
116
                    .append(actual)          .append(newline)
 
117
                    .append("----");
 
118
            
 
119
            e.printStackTrace();
 
120
            System.err.println(fullText.toString());
 
121
            
 
122
            throw e;
 
123
        }
 
124
        */
 
125
    }
 
126
 
99
127
        public void testInterfaceFileFormat() throws Exception {
100
128
                String groupI =
101
129
                                "interface test;" +newline+
110
138
                        "t();\n" +
111
139
                        "bold(item);\n" +
112
140
                        "optional duh(a, b, c);\n";
113
 
                assertEqual(I.toString(), expecting);
 
141
                assertEquals(expecting,I.toString());
114
142
        }
115
143
 
116
144
        public void testNoGroupLoader() throws Exception {
126
154
 
127
155
                writeFile(tmpdir, "testG.stg", templates);
128
156
 
129
 
                StringTemplateGroup group =
 
157
                /*StringTemplateGroup group =*/
130
158
                                new StringTemplateGroup(new FileReader(tmpdir+"/testG.stg"), errors);
131
159
 
132
160
                String expecting = "no group loader registered";
133
 
                assertEqual(errors.toString(), expecting);
 
161
                assertEquals(expecting,errors.toString());
134
162
        }
135
163
 
136
164
        public void testCannotFindInterfaceFile() throws Exception {
147
175
 
148
176
                writeFile(tmpdir, "testG.stg", templates);
149
177
 
150
 
                StringTemplateGroup group =
 
178
                /*StringTemplateGroup group =*/
151
179
                                new StringTemplateGroup(new FileReader(tmpdir+"/testG.stg"), errors);
152
180
 
153
181
                String expecting = "no such interface file blort.sti";
154
 
                assertEqual(errors.toString(), expecting);
 
182
                assertEquals(expecting,errors.toString());
155
183
        }
156
184
 
157
185
        public void testMultiDirGroupLoading() throws Exception {
182
210
                        "bold(item) ::= <<foo>>\n" +
183
211
                        "duh(a,b,c) ::= <<foo>>\n" +
184
212
                        "t() ::= <<foo>>\n";
185
 
                assertEqual(group.toString(), expecting);
 
213
                assertEquals(expecting,group.toString());
186
214
        }
187
215
 
188
216
        public void testGroupSatisfiesSingleInterface() throws Exception {
205
233
 
206
234
                writeFile(tmpdir, "testG.stg", templates);
207
235
 
208
 
                StringTemplateGroup group =
 
236
                /*StringTemplateGroup group =*/
209
237
                                new StringTemplateGroup(new FileReader(tmpdir+"/testG.stg"), errors);
210
238
 
211
239
                String expecting = ""; // should be no errors
212
 
                assertEqual(errors.toString(), expecting);
 
240
                assertEquals(expecting,errors.toString());
213
241
        }
214
242
 
215
243
        public void testGroupExtendsSuperGroup() throws Exception {
238
266
                st.setAttribute("x", "foo");
239
267
 
240
268
                String expecting = "*foo*";
241
 
                assertEqual(st.toString(), expecting);
 
269
                assertEquals(expecting, st.toString());
 
270
        }
 
271
 
 
272
        public void testGroupExtendsSuperGroupWithAngleBrackets() throws Exception {
 
273
                // this also tests the group loader
 
274
                StringTemplateErrorListener errors = new ErrorBuffer();
 
275
                String tmpdir = System.getProperty("java.io.tmpdir");
 
276
                StringTemplateGroup.registerGroupLoader(
 
277
                        new PathGroupLoader(tmpdir,errors)
 
278
                );
 
279
                String superGroup =
 
280
                                "group superG;" +newline+
 
281
                                "bold(item) ::= <<*<item>*>>;\n"+newline;
 
282
                writeFile(tmpdir, "superG.stg", superGroup);
 
283
 
 
284
                String templates =
 
285
                        "group testG : superG;" +newline+
 
286
                        "main(x) ::= \"<bold(x)>\""+newline;
 
287
 
 
288
                writeFile(tmpdir, "testG.stg", templates);
 
289
 
 
290
                StringTemplateGroup group =
 
291
                                new StringTemplateGroup(new FileReader(tmpdir+"/testG.stg"),
 
292
                                                                                errors);
 
293
                StringTemplate st = group.getInstanceOf("main");
 
294
                st.setAttribute("x", "foo");
 
295
 
 
296
                String expecting = "*foo*";
 
297
                assertEquals(expecting, st.toString());
242
298
        }
243
299
 
244
300
        public void testMissingInterfaceTemplate() throws Exception {
260
316
 
261
317
                writeFile(tmpdir, "testG.stg", templates);
262
318
 
263
 
                StringTemplateGroup group =
 
319
                /*StringTemplateGroup group =*/
264
320
                                new StringTemplateGroup(new FileReader(tmpdir+"/testG.stg"), errors);
265
321
 
266
322
                String expecting = "group testG does not satisfy interface testI: missing templates [bold]";
267
 
                assertEqual(errors.toString(), expecting);
 
323
                assertEquals(expecting, errors.toString());
268
324
        }
269
325
 
270
326
        public void testMissingOptionalInterfaceTemplate() throws Exception {
286
342
 
287
343
                writeFile(tmpdir, "testG.stg", templates);
288
344
 
289
 
                StringTemplateGroup group =
 
345
                /*StringTemplateGroup group =*/
290
346
                                new StringTemplateGroup(new FileReader(tmpdir+"/testG.stg"), errors);
291
347
 
292
348
                String expecting = ""; // should be NO errors
293
 
                assertEqual(errors.toString(), expecting);
 
349
                assertEquals(expecting, errors.toString());
294
350
        }
295
351
 
296
352
        public void testMismatchedInterfaceTemplate() throws Exception {
313
369
 
314
370
                writeFile(tmpdir, "testG.stg", templates);
315
371
 
316
 
                StringTemplateGroup group =
 
372
                /*StringTemplateGroup group =*/
317
373
                                new StringTemplateGroup(new FileReader(tmpdir+"/testG.stg"), errors);
318
374
 
319
375
                String expecting = "group testG does not satisfy interface testI: mismatched arguments on these templates [optional duh(a, b, c)]";
320
 
                assertEqual(errors.toString(), expecting);
 
376
                assertEquals(expecting,errors.toString());
321
377
        }
322
378
 
323
379
        public void testGroupFileFormat() throws Exception {
334
390
                                "bold(item) ::= <<<b>$item$</b>>>" +newline+
335
391
                                "duh() ::= <<xx>>" +newline+
336
392
                                "t() ::= <<literal template>>"+newline;
337
 
                assertEqual(group.toString(), expecting);
 
393
                assertEquals(expecting,group.toString());
338
394
 
339
395
                StringTemplate a = group.getInstanceOf("t");
340
396
                expecting = "literal template";
341
 
                assertEqual(a.toString(), expecting);
 
397
                assertEquals(expecting,a.toString());
342
398
 
343
399
                StringTemplate b = group.getInstanceOf("bold");
344
400
                b.setAttribute("item", "dork");
345
401
                expecting = "<b>dork</b>";
346
 
                assertEqual(b.toString(), expecting);
 
402
                assertEquals(expecting,b.toString());
347
403
        }
348
404
 
349
405
        public void testEscapedTemplateDelimiters() throws Exception {
360
416
                                "bold(item) ::= <<<b>$item$</b>>>" +newline+
361
417
                                "duh() ::= <<xx>>" +newline+
362
418
                                "t() ::= <<$\"literal\":{a|$a$\\}}$ template>>"+newline;
363
 
                assertEqual(group.toString(), expecting);
 
419
                assertEquals(expecting,group.toString());
364
420
 
365
421
                StringTemplate b = group.getInstanceOf("bold");
366
422
                b.setAttribute("item", "dork");
367
423
                expecting = "<b>dork</b>";
368
 
                assertEqual(b.toString(), expecting);
 
424
                assertEquals(expecting,b.toString());
369
425
 
370
426
                StringTemplate a = group.getInstanceOf("t");
371
427
                expecting = "literal} template";
372
 
                assertEqual(a.toString(), expecting);
 
428
                assertEquals(expecting,a.toString());
373
429
        }
374
430
 
375
431
    /** Check syntax and setAttribute-time errors */
395
451
            error = e.getMessage();
396
452
        }
397
453
        String expecting = "no such attribute: foo in template context [t]";
398
 
        assertEqual(error, expecting);
 
454
        assertEquals(expecting,error);
399
455
 
400
456
        // check setting known arg
401
457
        a = group.getInstanceOf("t2");
413
469
                "b() ::= \"y\"" +newline+
414
470
                "a() ::= \"z\"" +newline;
415
471
        StringTemplateErrorListener errors = new ErrorBuffer();
416
 
        StringTemplateGroup group =
 
472
        /*StringTemplateGroup group =*/
417
473
                new StringTemplateGroup(new StringReader(templates), errors);
418
474
        String expecting = "redefinition of template: a";
419
 
        assertEqual(errors.toString(), expecting);
 
475
        assertEquals(expecting,errors.toString());
420
476
    }
421
477
 
422
478
    public void testMissingInheritedAttribute() throws Exception {
450
506
                                                                                DefaultTemplateLexer.class);
451
507
        StringTemplate t = group.getInstanceOf("page");
452
508
        String expecting = "<font face=Times>my body</font>";
453
 
        assertEqual(t.toString(), expecting);
 
509
        assertEquals(expecting, t.toString());
454
510
    }
455
511
 
456
512
    public void testUndefinedArgumentAssignment() throws Exception {
471
527
            error = iae.getMessage();
472
528
        }
473
529
        String expecting = "template body has no such attribute: font in template context [page <invoke body arg context>]";
474
 
        assertEqual(error, expecting);
 
530
        assertEquals(expecting, error);
475
531
    }
476
532
 
477
533
    public void testFormalArgumentAssignmentInApply() throws Exception {
485
541
        StringTemplate t = group.getInstanceOf("page");
486
542
        t.setAttribute("name", "Ter");
487
543
        String expecting = "<font face=Times><b>Ter</b></font>";
488
 
        assertEqual(t.toString(), expecting);
 
544
        assertEquals(expecting, t.toString());
489
545
    }
490
546
 
491
547
    public void testUndefinedArgumentAssignmentInApply() throws Exception {
507
563
            error = iae.getMessage();
508
564
        }
509
565
        String expecting = "template bold has no such attribute: font in template context [page <invoke bold arg context>]";
510
 
        assertEqual(error, expecting);
 
566
        assertEquals(expecting,error);
511
567
    }
512
568
 
513
569
    public void testUndefinedAttributeReference() throws Exception {
527
583
            error = iae.getMessage();
528
584
        }
529
585
        String expecting = "no such attribute: name in template context [page bold]";
530
 
        assertEqual(error, expecting);
 
586
        assertEquals(expecting,error);
531
587
    }
532
588
 
533
589
    public void testUndefinedDefaultAttributeReference() throws Exception {
547
603
            error = nse.getMessage();
548
604
        }
549
605
        String expecting = "no such attribute: it in template context [page bold]";
550
 
        assertEqual(error, expecting);
 
606
        assertEquals(expecting,error);
551
607
    }
552
608
 
553
609
    public void testAngleBracketsWithGroupFile() throws Exception {
563
619
        StringTemplate t = group.getInstanceOf("a");
564
620
        t.setAttribute("s","Test");
565
621
        String expecting = "case 1 : Test break;";
566
 
        assertEqual(t.toString(), expecting);
 
622
        assertEquals(expecting, t.toString());
567
623
    }
568
624
 
569
625
    public void testAngleBracketsNoGroup() throws Exception {
573
629
        st.setAttribute("rules", "A");
574
630
        st.setAttribute("rules", "B");
575
631
        String expecting = "Tokens : A|B ;";
576
 
        assertEqual(st.toString(), expecting);
 
632
        assertEquals(expecting, st.toString());
577
633
    }
578
634
 
579
635
        public void testRegionRef() throws Exception {
586
642
                StringTemplate st = group.getInstanceOf("a");
587
643
                String result = st.toString();
588
644
                String expecting = "XY";
589
 
                assertEqual(result, expecting);
 
645
                assertEquals(expecting, result);
590
646
        }
591
647
 
592
648
        public void testEmbeddedRegionRef() throws Exception {
599
655
                StringTemplate st = group.getInstanceOf("a");
600
656
                String result = st.toString();
601
657
                String expecting = "XblortY";
602
 
                assertEqual(result, expecting);
 
658
                assertEquals(expecting, result);
603
659
        }
604
660
 
605
661
        public void testRegionRefAngleBrackets() throws Exception {
611
667
                StringTemplate st = group.getInstanceOf("a");
612
668
                String result = st.toString();
613
669
                String expecting = "XY";
614
 
                assertEqual(result, expecting);
 
670
                assertEquals(expecting, result);
615
671
        }
616
672
 
617
673
        public void testEmbeddedRegionRefAngleBrackets() throws Exception {
623
679
                StringTemplate st = group.getInstanceOf("a");
624
680
                String result = st.toString();
625
681
                String expecting = "XblortY";
626
 
                assertEqual(result, expecting);
 
682
                assertEquals(expecting, result);
627
683
        }
628
684
 
 
685
    // FIXME: This test fails due to inserted white space...
629
686
        public void testEmbeddedRegionRefWithNewlinesAngleBrackets() throws Exception {
630
687
                String templates =
631
688
                                "group test;" +newline+
638
695
                StringTemplate st = group.getInstanceOf("a");
639
696
                String result = st.toString();
640
697
                String expecting = "XblortY";
641
 
                assertEqual(result, expecting);
 
698
                assertEquals(expecting, result);
642
699
        }
643
700
 
644
701
        public void testRegionRefWithDefAngleBrackets() throws Exception {
651
708
                StringTemplate st = group.getInstanceOf("a");
652
709
                String result = st.toString();
653
710
                String expecting = "XfooY";
654
 
                assertEqual(result, expecting);
 
711
                assertEquals(expecting, result);
655
712
        }
656
713
 
657
714
        public void testRegionRefWithDefInConditional() throws Exception {
665
722
                st.setAttribute("v", "true");
666
723
                String result = st.toString();
667
724
                String expecting = "XAfooBY";
668
 
                assertEqual(result, expecting);
 
725
                assertEquals(expecting, result);
669
726
        }
670
727
 
671
728
        public void testRegionRefWithImplicitDefInConditional() throws Exception {
681
738
                st.setAttribute("v", "true");
682
739
                String result = st.toString();
683
740
                String expecting = "XAyoBY";
684
 
                assertEqual(result, expecting);
 
741
                assertEquals(expecting, result);
685
742
 
686
743
                String err_result = errors.toString();
687
744
                String err_expecting = "group test line 3: redefinition of template region: @a.r";
688
 
                assertEqual(err_result, err_expecting);
 
745
                assertEquals(err_expecting,err_result);
689
746
        }
690
747
 
691
748
        public void testRegionOverride() throws Exception {
708
765
                StringTemplate st = subGroup.getInstanceOf("a");
709
766
                String result = st.toString();
710
767
                String expecting = "XfooY";
711
 
                assertEqual(result, expecting);
 
768
                assertEquals(expecting, result);
712
769
        }
713
770
 
714
771
        public void testRegionOverrideRefSuperRegion() throws Exception {
731
788
                StringTemplate st = subGroup.getInstanceOf("a");
732
789
                String result = st.toString();
733
790
                String expecting = "XAfooBY";
734
 
                assertEqual(result, expecting);
 
791
                assertEquals(expecting, result);
735
792
        }
736
793
 
737
794
        public void testRegionOverrideRefSuperRegion3Levels() throws Exception {
776
833
                StringTemplate st = subSubGroup.getInstanceOf("a");
777
834
                String result = st.toString();
778
835
                String expecting = "Xfoo23Y";
779
 
                assertEqual(result, expecting);
 
836
                assertEquals(expecting, result);
780
837
        }
781
838
 
782
839
        public void testRegionOverrideRefSuperImplicitRegion() throws Exception {
798
855
                StringTemplate st = subGroup.getInstanceOf("a");
799
856
                String result = st.toString();
800
857
                String expecting = "XAfooY";
801
 
                assertEqual(result, expecting);
 
858
                assertEquals(expecting, result);
802
859
        }
803
860
 
804
861
        public void testEmbeddedRegionRedefError() throws Exception {
815
872
                st.toString();
816
873
                String result = errors.toString();
817
874
                String expecting = "group test line 2: redefinition of template region: @a.r";
818
 
                assertEqual(result, expecting);
 
875
                assertEquals(expecting, result);
819
876
        }
820
877
 
821
878
        public void testImplicitRegionRedefError() throws Exception {
833
890
                st.toString();
834
891
                String result = errors.toString();
835
892
                String expecting = "group test line 4: redefinition of template region: @a.r";
836
 
                assertEqual(result, expecting);
 
893
                assertEquals(expecting, result);
837
894
        }
838
895
 
839
896
        public void testImplicitOverriddenRegionRedefError() throws Exception {
855
912
                                                                                errors,
856
913
                                                                                group);
857
914
 
858
 
                StringTemplate st = subGroup.getInstanceOf("a");
 
915
                /*StringTemplate st =*/ subGroup.getInstanceOf("a");
859
916
                String result = errors.toString();
860
917
                String expecting = "group sub line 3: redefinition of template region: @a.r";
861
 
                assertEqual(result, expecting);
 
918
                assertEquals(expecting, result);
862
919
        }
863
920
 
864
921
        public void testUnknownRegionDefError() throws Exception {
875
932
                st.toString();
876
933
                String result = errors.toString();
877
934
                String expecting = "group test line 3: template a has no region called q";
878
 
                assertEqual(result, expecting);
 
935
                assertEquals(expecting, result);
879
936
        }
880
937
 
881
938
        public void testSuperRegionRefError() throws Exception {
896
953
                                                                                errors,
897
954
                                                                                group);
898
955
 
899
 
                StringTemplate st = subGroup.getInstanceOf("a");
 
956
                /*StringTemplate st =*/ subGroup.getInstanceOf("a");
900
957
                String result = errors.toString();
901
958
                String expecting = "template a has no region called q";
902
 
                assertEqual(result, expecting);
 
959
                assertEquals(expecting, result);
903
960
        }
904
961
 
905
962
        public void testMissingEndRegionError() throws Exception {
917
974
                st.toString();
918
975
                String result = errors.toString();
919
976
                String expecting = "missing region r $@end$ tag";
920
 
                assertEqual(result, expecting);
 
977
                assertEquals(expecting, result);
921
978
        }
922
979
 
923
980
        public void testMissingEndRegionErrorAngleBrackets() throws Exception {
933
990
                st.toString();
934
991
                String result = errors.toString();
935
992
                String expecting = "missing region r <@end> tag";
936
 
                assertEqual(result, expecting);
 
993
                assertEquals(expecting, result);
937
994
        }
938
995
 
939
996
    public void testSimpleInheritance() throws Exception {
940
997
                // make a bold template in the super group that you can inherit from sub
941
998
                StringTemplateGroup supergroup = new StringTemplateGroup("super");
942
999
                StringTemplateGroup subgroup = new StringTemplateGroup("sub");
943
 
                StringTemplate bold = supergroup.defineTemplate("bold", "<b>$it$</b>");
 
1000
                /*StringTemplate bold =*/ supergroup.defineTemplate("bold", "<b>$it$</b>");
944
1001
                subgroup.setSuperGroup(supergroup);
945
1002
                StringTemplateErrorListener errors = new ErrorBuffer();
946
1003
                subgroup.setErrorListener(errors);
948
1005
                StringTemplate duh = new StringTemplate(subgroup, "$name:bold()$");
949
1006
                duh.setAttribute("name", "Terence");
950
1007
                String expecting = "<b>Terence</b>";
951
 
                assertEqual(duh.toString(), expecting);
 
1008
                assertEquals(expecting,duh.toString());
952
1009
        }
953
1010
 
954
1011
        public void testOverrideInheritance() throws Exception {
964
1021
                StringTemplate duh = new StringTemplate(subgroup, "$name:bold()$");
965
1022
                duh.setAttribute("name", "Terence");
966
1023
                String expecting = "<strong>Terence</strong>";
967
 
                assertEqual(duh.toString(), expecting);
 
1024
                assertEquals(expecting,duh.toString());
968
1025
        }
969
1026
 
970
1027
        public void testMultiLevelInheritance() throws Exception {
982
1039
                StringTemplate duh = new StringTemplate(level2, "$name:bold()$");
983
1040
                duh.setAttribute("name", "Terence");
984
1041
                String expecting = "<b>Terence</b>";
985
 
                assertEqual(duh.toString(), expecting);
 
1042
                assertEquals(expecting,duh.toString());
986
1043
        }
987
1044
 
988
1045
        public void testComplicatedInheritance() throws Exception {
1012
1069
                StringTemplate st = sub.getInstanceOf("decls");
1013
1070
                String expecting = "DSL";
1014
1071
                String result = st.toString();
1015
 
                assertEqual(result, expecting);
 
1072
                assertEquals(expecting, result);
1016
1073
        }
1017
1074
 
1018
1075
        public void test3LevelSuperRef() throws Exception {
1043
1100
                StringTemplate st = subSubGroup.getInstanceOf("r");
1044
1101
                String result = st.toString();
1045
1102
                String expecting = "foo23";
1046
 
                assertEqual(result, expecting);
 
1103
                assertEquals(expecting, result);
1047
1104
        }
1048
1105
 
1049
1106
        public void testExprInParens() throws Exception {
1051
1108
                // Use a template group so we can specify the start/stop chars
1052
1109
                StringTemplateGroup group =
1053
1110
                        new StringTemplateGroup("dummy", ".");
1054
 
                StringTemplate bold = group.defineTemplate("bold", "<b>$it$</b>");
 
1111
                /*StringTemplate bold =*/ group.defineTemplate("bold", "<b>$it$</b>");
1055
1112
                StringTemplate duh = new StringTemplate(group, "$(\"blort: \"+(list)):bold()$");
1056
1113
                duh.setAttribute("list", "a");
1057
1114
                duh.setAttribute("list", "b");
1058
1115
                duh.setAttribute("list", "c");
1059
1116
                // System.out.println(duh);
1060
1117
                String expecting = "<b>blort: abc</b>";
1061
 
                assertEqual(duh.toString(), expecting);
 
1118
                assertEquals(expecting, duh.toString());
1062
1119
        }
1063
1120
 
1064
1121
    public void testMultipleAdditions() throws Exception {
1073
1130
        duh.setAttribute("ID", "3321");
1074
1131
        duh.setAttribute("foo", "fubar");
1075
1132
        String expecting = "<a href=\"/member/view?ID=3321&x=yfubar\"><b>the title</b></a>";
1076
 
        assertEqual(duh.toString(), expecting);
 
1133
        assertEquals(expecting, duh.toString());
1077
1134
    }
1078
1135
 
1079
1136
    public void testCollectionAttributes() throws Exception {
1080
1137
        StringTemplateGroup group =
1081
1138
                new StringTemplateGroup("test");
1082
 
        StringTemplate bold = group.defineTemplate("bold", "<b>$it$</b>");
 
1139
        /*StringTemplate bold =*/ group.defineTemplate("bold", "<b>$it$</b>");
1083
1140
        StringTemplate t =
1084
1141
            new StringTemplate(group, "$data$, $data:bold()$, "+
1085
1142
                                      "$list:bold():bold()$, $array$, $a2$, $a3$, $a4$");
1100
1157
        //System.out.println(t);
1101
1158
        String expecting="123, <b>1</b><b>2</b><b>3</b>, "+
1102
1159
            "<b><b>a</b></b><b><b>b</b></b><b><b>c</b></b>, xy, 1020, 1.21.3, 8.79.2";
1103
 
        assertEqual(t.toString(), expecting);
 
1160
        assertEquals(expecting, t.toString());
1104
1161
    }
1105
1162
 
1106
1163
    public void testParenthesizedExpression() throws Exception {
1107
1164
        StringTemplateGroup group =
1108
1165
                new StringTemplateGroup("test");
1109
 
        StringTemplate bold = group.defineTemplate("bold", "<b>$it$</b>");
 
1166
        /*StringTemplate bold =*/ group.defineTemplate("bold", "<b>$it$</b>");
1110
1167
        StringTemplate t = new StringTemplate(group, "$(f+l):bold()$");
1111
1168
        t.setAttribute("f", "Joe");
1112
1169
        t.setAttribute("l", "Schmoe");
1113
1170
        //System.out.println(t);
1114
1171
        String expecting="<b>JoeSchmoe</b>";
1115
 
        assertEqual(t.toString(), expecting);
 
1172
        assertEquals(expecting, t.toString());
1116
1173
    }
1117
1174
 
1118
1175
        public void testApplyTemplateNameExpression() throws Exception {
1119
1176
        StringTemplateGroup group =
1120
1177
                new StringTemplateGroup("test");
1121
 
        StringTemplate bold = group.defineTemplate("foobar", "foo$attr$bar");
 
1178
        /* StringTemplate bold =*/ group.defineTemplate("foobar", "foo$attr$bar");
1122
1179
        StringTemplate t = new StringTemplate(group, "$data:(name+\"bar\")()$");
1123
1180
        t.setAttribute("data", "Ter");
1124
1181
        t.setAttribute("data", "Tom");
1125
1182
        t.setAttribute("name", "foo");
1126
1183
        //System.out.println(t);
1127
1184
        String expecting="fooTerbarfooTombar";
1128
 
        assertEqual(t.toString(), expecting);
 
1185
        assertEquals(expecting, t.toString());
1129
1186
    }
1130
1187
 
1131
1188
        public void testApplyTemplateNameTemplateEval() throws Exception {
1132
1189
        StringTemplateGroup group =
1133
1190
                new StringTemplateGroup("test");
1134
 
                StringTemplate foobar = group.defineTemplate("foobar", "foo$it$bar");
1135
 
                StringTemplate a = group.defineTemplate("a", "$it$bar");
 
1191
                /*StringTemplate foobar =*/ group.defineTemplate("foobar", "foo$it$bar");
 
1192
                /*StringTemplate a =*/ group.defineTemplate("a", "$it$bar");
1136
1193
        StringTemplate t = new StringTemplate(group, "$data:(\"foo\":a())()$");
1137
1194
        t.setAttribute("data", "Ter");
1138
1195
        t.setAttribute("data", "Tom");
1139
1196
        //System.out.println(t);
1140
1197
        String expecting="fooTerbarfooTombar";
1141
 
        assertEqual(t.toString(), expecting);
 
1198
        assertEquals(expecting, t.toString());
1142
1199
    }
1143
1200
 
1144
1201
    public void testTemplateNameExpression() throws Exception {
1145
1202
        StringTemplateGroup group =
1146
1203
                new StringTemplateGroup("test");
1147
 
        StringTemplate foo = group.defineTemplate("foo", "hi there!");
 
1204
        /*StringTemplate foo =*/ group.defineTemplate("foo", "hi there!");
1148
1205
        StringTemplate t = new StringTemplate(group, "$(name)()$");
1149
1206
        t.setAttribute("name", "foo");
1150
1207
        //System.out.println(t);
1151
1208
        String expecting="hi there!";
1152
 
        assertEqual(t.toString(), expecting);
 
1209
        assertEquals(expecting, t.toString());
1153
1210
    }
1154
1211
 
1155
1212
    public void testMissingEndDelimiter() throws Exception {
1157
1214
                new StringTemplateGroup("test");
1158
1215
                StringTemplateErrorListener errors = new ErrorBuffer();
1159
1216
        group.setErrorListener(errors);
1160
 
        StringTemplate t = new StringTemplate(group, "stuff $a then more junk etc...");
 
1217
        /*StringTemplate t =*/ new StringTemplate(group, "stuff $a then more junk etc...");
1161
1218
        String expectingError="problem parsing template 'anonymous': line 1:31: expecting '$', found '<EOF>'";
1162
1219
        //System.out.println("error: '"+errors+"'");
1163
1220
        //System.out.println("expecting: '"+expectingError+"'");
1172
1229
        t.setAttribute("a", "Terence");
1173
1230
        t.setAttribute("b", "Terence");
1174
1231
        t.setAttribute("cc", "Terence"); // oops...should be 'c'
1175
 
        final String newline = System.getProperty("line.separator");
1176
1232
                StringTemplateErrorListener errors = new ErrorBuffer();
1177
1233
        group.setErrorListener(errors);
1178
1234
        String expectingError="anonymous: set but not used: cc";
1179
 
        String result = t.toString();    // result is irrelevant
 
1235
        /*String result =*/ t.toString();    // result is irrelevant
1180
1236
        //System.out.println("result error: '"+errors+"'");
1181
1237
        //System.out.println("expecting: '"+expectingError+"'");
1182
1238
        StringTemplate.setLintMode(false);
1183
 
        assertEqual(errors.toString(), expectingError);
 
1239
        assertEquals(expectingError,errors.toString());
1184
1240
    }
1185
1241
 
1186
1242
    public void testNullTemplateApplication() throws Exception {
1190
1246
        group.setErrorListener(errors);
1191
1247
        StringTemplate t = new StringTemplate(group, "$names:bold(x=it)$");
1192
1248
        t.setAttribute("names", "Terence");
1193
 
        //System.out.println(t);
1194
 
        String expecting=null;
1195
 
                String result = null;
 
1249
        
1196
1250
                String error = null;
1197
1251
                try {
1198
 
                        result = t.toString();
 
1252
                        t.toString();
1199
1253
                }
1200
1254
                catch (IllegalArgumentException iae) {
1201
1255
                        error = iae.getMessage();
1202
1256
                }
1203
 
                assertEqual(error, "Can't find template bold.st; context is [anonymous]");
 
1257
        String expecting = "Can't find template bold.st; context is [anonymous]; group hierarchy is [test]" ;
 
1258
                assertEquals(expecting,error);
1204
1259
    }
1205
1260
 
1206
1261
    public void testNullTemplateToMultiValuedApplication() throws Exception {
1212
1267
        t.setAttribute("names", "Terence");
1213
1268
        t.setAttribute("names", "Tom");
1214
1269
        //System.out.println(t);
1215
 
        String expecting=null; // bold not found...empty string
1216
 
                String result = null;
1217
1270
                String error = null;
1218
1271
                try {
1219
 
                        result = t.toString();
 
1272
                        t.toString();
1220
1273
                }
1221
1274
                catch (IllegalArgumentException iae) {
1222
1275
                        error = iae.getMessage();
1223
1276
                }
1224
 
                assertEqual(error, "Can't find template bold.st; context is [anonymous]");
 
1277
        String expecting = "Can't find template bold.st; context is [anonymous]; group hierarchy is [test]"; // bold not found...empty string
 
1278
                assertEquals(expecting,error);
1225
1279
    }
1226
1280
 
1227
1281
    public void testChangingAttrValueTemplateApplicationToVector() throws Exception {
1228
1282
        StringTemplateGroup group =
1229
1283
                new StringTemplateGroup("test");
1230
 
        StringTemplate bold = group.defineTemplate("bold", "<b>$x$</b>");
 
1284
        /*StringTemplate bold =*/ group.defineTemplate("bold", "<b>$x$</b>");
1231
1285
        StringTemplate t = new StringTemplate(group, "$names:bold(x=it)$");
1232
1286
        t.setAttribute("names", "Terence");
1233
1287
        t.setAttribute("names", "Tom");
1234
1288
        //System.out.println("'"+t.toString()+"'");
1235
1289
        String expecting="<b>Terence</b><b>Tom</b>";
1236
 
        assertEqual(t.toString(), expecting);
 
1290
        assertEquals(expecting, t.toString());
1237
1291
    }
1238
1292
 
1239
1293
    public void testChangingAttrValueRepeatedTemplateApplicationToVector() throws Exception {
1240
1294
        StringTemplateGroup group =
1241
1295
                new StringTemplateGroup("dummy", ".");
1242
 
        StringTemplate bold = group.defineTemplate("bold", "<b>$item$</b>");
1243
 
        StringTemplate italics = group.defineTemplate("italics", "<i>$it$</i>");
 
1296
        /*StringTemplate bold =*/ group.defineTemplate("bold", "<b>$item$</b>");
 
1297
        /*StringTemplate italics =*/ group.defineTemplate("italics", "<i>$it$</i>");
1244
1298
        StringTemplate members =
1245
1299
                new StringTemplate(group, "$members:bold(item=it):italics(it=it)$");
1246
1300
        members.setAttribute("members", "Jim");
1248
1302
        members.setAttribute("members", "Ashar");
1249
1303
        //System.out.println("members="+members);
1250
1304
        String expecting = "<i><b>Jim</b></i><i><b>Mike</b></i><i><b>Ashar</b></i>";
1251
 
        assertEqual(members.toString(), expecting);
 
1305
        assertEquals(expecting,members.toString());
1252
1306
    }
1253
1307
 
1254
1308
    public void testAlternatingTemplateApplication() throws Exception {
1255
1309
        StringTemplateGroup group =
1256
1310
                new StringTemplateGroup("dummy", ".");
1257
 
        StringTemplate listItem = group.defineTemplate("listItem", "<li>$it$</li>");
1258
 
        StringTemplate bold = group.defineTemplate("bold", "<b>$it$</b>");
1259
 
        StringTemplate italics = group.defineTemplate("italics", "<i>$it$</i>");
 
1311
        /*StringTemplate listItem =*/ group.defineTemplate("listItem", "<li>$it$</li>");
 
1312
        /*StringTemplate bold =*/ group.defineTemplate("bold", "<b>$it$</b>");
 
1313
        /*StringTemplate italics =*/ group.defineTemplate("italics", "<i>$it$</i>");
1260
1314
        StringTemplate item =
1261
1315
                new StringTemplate(group, "$item:bold(),italics():listItem()$");
1262
1316
        item.setAttribute("item", "Jim");
1264
1318
        item.setAttribute("item", "Ashar");
1265
1319
        //System.out.println("ITEM="+item);
1266
1320
        String expecting = "<li><b>Jim</b></li><li><i>Mike</i></li><li><b>Ashar</b></li>";
1267
 
        assertEqual(item.toString(), expecting);
 
1321
        assertEquals(item.toString(), expecting);
1268
1322
    }
1269
1323
 
1270
1324
    public void testExpressionAsRHSOfAssignment() throws Exception {
1271
1325
        StringTemplateGroup group =
1272
1326
                new StringTemplateGroup("test");
1273
 
        StringTemplate hostname = group.defineTemplate("hostname", "$machine$.jguru.com");
1274
 
        StringTemplate bold = group.defineTemplate("bold", "<b>$x$</b>");
 
1327
        /*StringTemplate hostname =*/ group.defineTemplate("hostname", "$machine$.jguru.com");
 
1328
        /*StringTemplate bold =*/ group.defineTemplate("bold", "<b>$x$</b>");
1275
1329
        StringTemplate t = new StringTemplate(group, "$bold(x=hostname(machine=\"www\"))$");
1276
1330
        String expecting="<b>www.jguru.com</b>";
1277
 
        assertEqual(t.toString(), expecting);
 
1331
        assertEquals(expecting, t.toString());
1278
1332
    }
1279
1333
 
1280
1334
    public void testTemplateApplicationAsRHSOfAssignment() throws Exception {
1281
1335
        StringTemplateGroup group =
1282
1336
                new StringTemplateGroup("test");
1283
 
        StringTemplate hostname = group.defineTemplate("hostname", "$machine$.jguru.com");
1284
 
        StringTemplate bold = group.defineTemplate("bold", "<b>$x$</b>");
1285
 
        StringTemplate italics = group.defineTemplate("italics", "<i>$it$</i>");
 
1337
        /*StringTemplate hostname =*/ group.defineTemplate("hostname", "$machine$.jguru.com");
 
1338
        /*StringTemplate bold =*/ group.defineTemplate("bold", "<b>$x$</b>");
 
1339
        /*StringTemplate italics =*/ group.defineTemplate("italics", "<i>$it$</i>");
1286
1340
        StringTemplate t = new StringTemplate(group, "$bold(x=hostname(machine=\"www\"):italics())$");
1287
1341
        String expecting="<b><i>www.jguru.com</i></b>";
1288
 
        assertEqual(t.toString(), expecting);
 
1342
        assertEquals(expecting, t.toString());
1289
1343
    }
1290
1344
 
1291
1345
    public void testParameterAndAttributeScoping() throws Exception {
1292
1346
        StringTemplateGroup group =
1293
1347
                new StringTemplateGroup("test");
1294
 
        StringTemplate italics = group.defineTemplate("italics", "<i>$x$</i>");
1295
 
        StringTemplate bold = group.defineTemplate("bold", "<b>$x$</b>");
 
1348
        /*StringTemplate italics =*/ group.defineTemplate("italics", "<i>$x$</i>");
 
1349
        /*StringTemplate bold =*/ group.defineTemplate("bold", "<b>$x$</b>");
1296
1350
        StringTemplate t = new StringTemplate(group, "$bold(x=italics(x=name))$");
1297
1351
        t.setAttribute("name", "Terence");
1298
1352
        //System.out.println(t);
1299
1353
        String expecting="<b><i>Terence</i></b>";
1300
 
        assertEqual(t.toString(), expecting);
 
1354
        assertEquals(expecting, t.toString());
1301
1355
    }
1302
1356
 
1303
1357
    public void testComplicatedSeparatorExpr() throws Exception {
1304
1358
        StringTemplateGroup group =
1305
1359
                new StringTemplateGroup("test");
1306
 
        StringTemplate bold = group.defineTemplate("bulletSeparator", "</li>$foo$<li>");
 
1360
        /*StringTemplate bold =*/ group.defineTemplate("bulletSeparator", "</li>$foo$<li>");
1307
1361
        // make separator a complicated expression with args passed to included template
1308
1362
        StringTemplate t =
1309
1363
            new StringTemplate(group,
1313
1367
        t.setAttribute("name", "Mel");
1314
1368
        //System.out.println(t);
1315
1369
        String expecting = "<ul>Ter</li> <li>&nbsp;Tom</li> <li>&nbsp;Mel</ul>";
1316
 
        assertEqual(t.toString(), expecting);
 
1370
        assertEquals(expecting, t.toString());
1317
1371
    }
1318
1372
 
1319
1373
    public void testAttributeRefButtedUpAgainstEndifAndWhitespace() throws Exception {
1323
1377
                                              "$if (!firstName)$$email$$endif$");
1324
1378
        a.setAttribute("email", "parrt@jguru.com");
1325
1379
        String expecting = "parrt@jguru.com";
1326
 
        assertEqual(a.toString(), expecting);
 
1380
        assertEquals(a.toString(), expecting);
1327
1381
    }
1328
1382
 
1329
1383
        public void testStringCatenationOnSingleValuedAttributeViaTemplateLiteral() throws Exception {
1330
1384
                StringTemplateGroup group =
1331
1385
                                new StringTemplateGroup("test");
1332
 
                StringTemplate bold = group.defineTemplate("bold", "<b>$it$</b>");
 
1386
                /*StringTemplate bold =*/ group.defineTemplate("bold", "<b>$it$</b>");
1333
1387
                //StringTemplate a = new StringTemplate(group, "$\" Parr\":bold()$");
1334
1388
                StringTemplate b = new StringTemplate(group, "$bold(it={$name$ Parr})$");
1335
1389
                //a.setAttribute("name", "Terence");
1336
1390
                b.setAttribute("name", "Terence");
1337
1391
                String expecting = "<b>Terence Parr</b>";
1338
 
                //assertEqual(a.toString(), expecting);
1339
 
                assertEqual(b.toString(), expecting);
 
1392
                //assertEquals(a.toString(), expecting);
 
1393
                assertEquals(b.toString(), expecting);
1340
1394
        }
1341
1395
 
1342
1396
        public void testStringCatenationOpOnArg() throws Exception {
1343
1397
                StringTemplateGroup group =
1344
1398
                                new StringTemplateGroup("test");
1345
 
                StringTemplate bold = group.defineTemplate("bold", "<b>$it$</b>");
 
1399
                /*StringTemplate bold =*/ group.defineTemplate("bold", "<b>$it$</b>");
1346
1400
                StringTemplate b = new StringTemplate(group, "$bold(it=name+\" Parr\")$");
1347
1401
                //a.setAttribute("name", "Terence");
1348
1402
                b.setAttribute("name", "Terence");
1349
1403
                String expecting = "<b>Terence Parr</b>";
1350
 
                //assertEqual(a.toString(), expecting);
1351
 
                assertEqual(b.toString(), expecting);
 
1404
                //assertEquals(expecting, a.toString());
 
1405
                assertEquals(expecting, b.toString());
1352
1406
        }
1353
1407
 
1354
1408
        public void testStringCatenationOpOnArgWithEqualsInString() throws Exception {
1355
1409
                StringTemplateGroup group =
1356
1410
                                new StringTemplateGroup("test");
1357
 
                StringTemplate bold = group.defineTemplate("bold", "<b>$it$</b>");
 
1411
                /*StringTemplate bold =*/ group.defineTemplate("bold", "<b>$it$</b>");
1358
1412
                StringTemplate b = new StringTemplate(group, "$bold(it=name+\" Parr=\")$");
1359
1413
                //a.setAttribute("name", "Terence");
1360
1414
                b.setAttribute("name", "Terence");
1361
1415
                String expecting = "<b>Terence Parr=</b>";
1362
 
                //assertEqual(a.toString(), expecting);
1363
 
                assertEqual(b.toString(), expecting);
 
1416
        //assertEquals(expecting, a.toString());
 
1417
        assertEquals(expecting, b.toString());
1364
1418
        }
1365
1419
 
1366
1420
    public void testApplyingTemplateFromDiskWithPrecompiledIF()
1367
1421
            throws Exception
1368
1422
    {
1369
 
        String newline = System.getProperty("line.separator");
 
1423
        // Create a temporary working directory
 
1424
        File tmpDir = new File(System.getProperty("java.io.tmpdir"));
 
1425
        File tmpWorkDir;
 
1426
        int counter = (new Random()).nextInt() & 65535;;
 
1427
        do { 
 
1428
            counter++;
 
1429
            StringBuffer name = new StringBuffer("st-junit-");
 
1430
            name.append(counter);
 
1431
            tmpWorkDir = new File(tmpDir, name.toString());            
 
1432
        } while (tmpWorkDir.exists());        
 
1433
        tmpWorkDir.mkdirs();
 
1434
        
1370
1435
        // write the template files first to /tmp
1371
 
        FileWriter fw = new FileWriter("/tmp/page.st");
 
1436
        File pageFile = new File(tmpWorkDir,"page.st");
 
1437
        FileWriter fw = new FileWriter(pageFile);
1372
1438
        fw.write("<html><head>"+newline);
1373
1439
        //fw.write("  <title>PeerScope: $title$</title>"+newline);
1374
1440
        fw.write("</head>"+newline);
1378
1444
        fw.write("</head>"+newline);
1379
1445
        fw.close();
1380
1446
 
1381
 
        fw = new FileWriter("/tmp/terse.st");
 
1447
        File terseFile = new File(tmpWorkDir,"terse.st");
 
1448
        fw = new FileWriter(terseFile);
1382
1449
        fw.write("$it.firstName$ $it.lastName$ (<tt>$it.email$</tt>)"+newline);
1383
1450
        fw.close();
1384
1451
 
1385
1452
        // specify a template to apply to an attribute
1386
1453
        // Use a template group so we can specify the start/stop chars
1387
1454
        StringTemplateGroup group =
1388
 
                new StringTemplateGroup("dummy", "/tmp");
 
1455
                new StringTemplateGroup("dummy", tmpWorkDir.toString());
1389
1456
 
1390
1457
        StringTemplate a = group.getInstanceOf("page");
1391
1458
        a.setAttribute("member", new Connector());
1396
1463
                "</body>"+newline+
1397
1464
                "</head>";
1398
1465
        //System.out.println("'"+a+"'");
1399
 
        assertEqual(a.toString(), expecting);
 
1466
        assertEquals(expecting, a.toString());
 
1467
 
 
1468
        // Cleanup the temp folder.
 
1469
        pageFile.delete();
 
1470
        terseFile.delete();
 
1471
        tmpWorkDir.delete();
1400
1472
    }
1401
1473
 
1402
1474
    public void testMultiValuedAttributeWithAnonymousTemplateUsingIndexVariableI()
1411
1483
                                                                   "}$");
1412
1484
        t.setAttribute("names", "Terence");
1413
1485
        t.setAttribute("names", "Jim");
1414
 
        t.setAttribute("names", "Sriram");
1415
 
        String newline = System.getProperty("line.separator");
 
1486
        t.setAttribute("names", "Sriram");        
1416
1487
        //System.out.println(t);
1417
1488
        String expecting =
1418
1489
                " List:"+newline+
1421
1492
                "<br>1. Terence"+newline+
1422
1493
                "<br>2. Jim"+newline+
1423
1494
                "<br>3. Sriram"+newline;
1424
 
        assertEqual(t.toString(), expecting);
 
1495
        assertEquals(expecting, t.toString());
1425
1496
    }
1426
1497
 
1427
1498
    public void testFindTemplateInCLASSPATH() throws Exception {
1436
1507
        m.setAttribute("returnType", "void");
1437
1508
        m.setAttribute("statements", "i=1;"); // body inherits these from method
1438
1509
        m.setAttribute("statements", "x=i;");
1439
 
        String newline = System.getProperty("line.separator");
1440
1510
        String expecting =
1441
1511
                "public void foobar() {"+newline+
1442
1512
                "\t// start of a body"+newline+
1445
1515
                "\t// end of a body"+newline+
1446
1516
                "}";
1447
1517
                //System.out.println(m);
1448
 
        assertEqual(m.toString(), expecting);
 
1518
        assertEquals(expecting, m.toString());        
1449
1519
    }
1450
1520
 
1451
1521
    public void testApplyTemplateToSingleValuedAttribute() throws Exception {
1452
1522
        StringTemplateGroup group =
1453
1523
                new StringTemplateGroup("test");
1454
 
        StringTemplate bold = group.defineTemplate("bold", "<b>$x$</b>");
 
1524
        /*StringTemplate bold =*/ group.defineTemplate("bold", "<b>$x$</b>");
1455
1525
        StringTemplate name = new StringTemplate(group, "$name:bold(x=name)$");
1456
1526
        name.setAttribute("name", "Terence");
1457
 
        assertEqual(name.toString(), "<b>Terence</b>");
 
1527
        assertEquals("<b>Terence</b>",name.toString());
1458
1528
    }
1459
1529
 
1460
1530
    public void testStringLiteralAsAttribute() throws Exception {
1461
1531
        StringTemplateGroup group =
1462
1532
                new StringTemplateGroup("test");
1463
 
        StringTemplate bold = group.defineTemplate("bold", "<b>$it$</b>");
 
1533
        /*StringTemplate bold =*/ group.defineTemplate("bold", "<b>$it$</b>");
1464
1534
        StringTemplate name = new StringTemplate(group, "$\"Terence\":bold()$");
1465
 
        assertEqual(name.toString(), "<b>Terence</b>");
 
1535
        assertEquals("<b>Terence</b>",name.toString());
1466
1536
    }
1467
1537
 
1468
1538
    public void testApplyTemplateToSingleValuedAttributeWithDefaultAttribute() throws Exception {
1469
1539
        StringTemplateGroup group =
1470
1540
                new StringTemplateGroup("test");
1471
 
        StringTemplate bold = group.defineTemplate("bold", "<b>$it$</b>");
 
1541
        /*StringTemplate bold =*/ group.defineTemplate("bold", "<b>$it$</b>");
1472
1542
        StringTemplate name = new StringTemplate(group, "$name:bold()$");
1473
1543
        name.setAttribute("name", "Terence");
1474
 
        assertEqual(name.toString(), "<b>Terence</b>");
 
1544
        assertEquals("<b>Terence</b>",name.toString());
1475
1545
    }
1476
1546
 
1477
1547
    public void testApplyAnonymousTemplateToSingleValuedAttribute() throws Exception {
1482
1552
        StringTemplate item =
1483
1553
                new StringTemplate(group, "$item:{<li>$it$</li>}$");
1484
1554
        item.setAttribute("item", "Terence");
1485
 
        assertEqual(item.toString(), "<li>Terence</li>");
 
1555
        assertEquals("<li>Terence</li>",item.toString());
1486
1556
    }
1487
1557
 
1488
1558
    public void testApplyAnonymousTemplateToMultiValuedAttribute() throws Exception {
1499
1569
        item.setAttribute("item", "Jim");
1500
1570
        item.setAttribute("item", "John");
1501
1571
        list.setAttribute("items", item); // nested template
1502
 
        assertEqual(list.toString(), "<ul><li>Terence</li>,<li>Jim</li>,<li>John</li></ul>");
 
1572
        String expecting = "<ul><li>Terence</li>,<li>Jim</li>,<li>John</li></ul>";
 
1573
        assertEquals(expecting,list.toString());
1503
1574
    }
1504
1575
 
1505
1576
    public void testApplyAnonymousTemplateToAggregateAttribute() throws Exception {
1511
1582
        String expecting =
1512
1583
                "Parr, Ter"+newline +
1513
1584
                "Burns, Tom"+newline;
1514
 
        assertEqual(st.toString(), expecting);
 
1585
        assertEquals(expecting, st.toString());
1515
1586
    }
1516
1587
 
1517
1588
    public void testRepeatedApplicationOfTemplateToSingleValuedAttribute() throws Exception {
1518
1589
        StringTemplateGroup group =
1519
1590
                new StringTemplateGroup("dummy", ".");
1520
 
        StringTemplate search = group.defineTemplate("bold", "<b>$it$</b>");
 
1591
        /*StringTemplate search =*/ group.defineTemplate("bold", "<b>$it$</b>");
1521
1592
        StringTemplate item =
1522
1593
                new StringTemplate(group, "$item:bold():bold()$");
1523
1594
        item.setAttribute("item", "Jim");
1524
 
        assertEqual(item.toString(), "<b><b>Jim</b></b>");
 
1595
        assertEquals("<b><b>Jim</b></b>", item.toString());
1525
1596
    }
1526
1597
 
1527
1598
    public void testRepeatedApplicationOfTemplateToMultiValuedAttributeWithSeparator() throws Exception {
1528
1599
        StringTemplateGroup group =
1529
1600
                new StringTemplateGroup("dummy", ".");
1530
 
        StringTemplate search = group.defineTemplate("bold", "<b>$it$</b>");
 
1601
        /*StringTemplate search =*/ group.defineTemplate("bold", "<b>$it$</b>");
1531
1602
        StringTemplate item =
1532
1603
                new StringTemplate(group, "$item:bold():bold(); separator=\",\"$");
1533
1604
        item.setAttribute("item", "Jim");
1535
1606
        item.setAttribute("item", "Ashar");
1536
1607
        // first application of template must yield another vector!
1537
1608
        //System.out.println("ITEM="+item);
1538
 
        assertEqual(item.toString(), "<b><b>Jim</b></b>,<b><b>Mike</b></b>,<b><b>Ashar</b></b>");
 
1609
        String expecting = "<b><b>Jim</b></b>,<b><b>Mike</b></b>,<b><b>Ashar</b></b>";
 
1610
        assertEquals(item.toString(), expecting);
1539
1611
    }
1540
1612
 
1541
1613
    // ### NEED A TEST OF obj ASSIGNED TO ARG?
1553
1625
        // uncomment next line to make "DISTINCT" appear in output
1554
1626
        // query.setAttribute("distince", "DISTINCT");
1555
1627
        // System.out.println(query);
1556
 
        assertEqual(query.toString(), "SELECT  name, email FROM User;");
 
1628
        assertEquals("SELECT  name, email FROM User;",query.toString());
1557
1629
    }
1558
1630
 
1559
1631
    public void testSingleValuedAttributes() throws Exception {
1563
1635
        query.setAttribute("column", "name");
1564
1636
        query.setAttribute("table", "User");
1565
1637
        // System.out.println(query);
1566
 
        assertEqual(query.toString(), "SELECT name FROM User;");
 
1638
        assertEquals("SELECT name FROM User;",query.toString());
1567
1639
    }
1568
1640
 
1569
1641
        public void testIFTemplate() throws Exception {
1576
1648
                t.setAttribute("column", "name");
1577
1649
                t.setAttribute("cond", "true");
1578
1650
                t.setAttribute("id", "231");
1579
 
                assertEqual(t.toString(), "SELECT name FROM PERSON WHERE ID=231;");
 
1651
                assertEquals("SELECT name FROM PERSON WHERE ID=231;",t.toString());
1580
1652
        }
1581
1653
 
1582
1654
        public void testIFCondWithParensTemplate() throws Exception {
1590
1662
                t.setAttribute("map", map);
1591
1663
                t.setAttribute("prop", "x");
1592
1664
                t.setAttribute("type", "int");
1593
 
                assertEqual(t.toString(), "int x=0;");
 
1665
                assertEquals("int x=0;",t.toString());
1594
1666
        }
1595
1667
 
1596
1668
        public void testIFCondWithParensDollarDelimsTemplate() throws Exception {
1604
1676
                t.setAttribute("map", map);
1605
1677
                t.setAttribute("prop", "x");
1606
1678
                t.setAttribute("type", "int");
1607
 
                assertEqual(t.toString(), "int x=0;");
 
1679
                assertEquals("int x=0;",t.toString());
1608
1680
        }
1609
1681
 
1610
1682
        /** As of 2.0, you can test a boolean value */
1615
1687
                        new StringTemplate(group,
1616
1688
                                          "$if(b)$x$endif$ $if(!b)$y$endif$");
1617
1689
                t.setAttribute("b", new Boolean(true));
1618
 
                assertEqual(t.toString(), "x ");
 
1690
                assertEquals(t.toString(), "x ");
1619
1691
 
1620
1692
                t = t.getInstanceOf();
1621
1693
                t.setAttribute("b", new Boolean(false));
1622
 
                assertEqual(t.toString(), " y");
 
1694
                assertEquals(" y", t.toString());
1623
1695
        }
1624
1696
 
1625
 
    public void testNestedIFTemplate() throws Exception {
1626
 
        String newline = System.getProperty("line.separator");
 
1697
    public void testNestedIFTemplate() throws Exception {        
1627
1698
        StringTemplateGroup group =
1628
1699
            new StringTemplateGroup("dummy", ".", AngleBracketTemplateLexer.class);
1629
1700
        StringTemplate t =
1642
1713
                "ackfoo"+newline+
1643
1714
                "stuff"+newline+
1644
1715
                "junk";
1645
 
        assertEqual(t.toString(), expecting);
 
1716
        assertEquals(expecting, t.toString());
1646
1717
    }
1647
1718
 
1648
1719
    public class Connector {
1669
1740
    public void testObjectPropertyReference() throws Exception {
1670
1741
        StringTemplateGroup group =
1671
1742
                new StringTemplateGroup("dummy", ".");
1672
 
        String newline = System.getProperty("line.separator");
1673
1743
        StringTemplate t =
1674
1744
                new StringTemplate(
1675
1745
                        group,
1683
1753
                "<b>Name: Terence Parr</b><br>"+newline+
1684
1754
                "<b>Email: parrt@jguru.com</b><br>"+newline+
1685
1755
                "Superhero by night...";
1686
 
        assertEqual(t.toString(), expecting);
 
1756
        assertEquals(expecting, t.toString());
1687
1757
    }
1688
1758
 
1689
1759
    public void testApplyRepeatedAnonymousTemplateWithForeignTemplateRefToMultiValuedAttribute() throws Exception {
1698
1768
        "{$it$<br>\n}$|end");
1699
1769
        duh.setAttribute("p", new Connector());
1700
1770
        duh.setAttribute("p", new Connector2());
1701
 
        String newline = System.getProperty("line.separator");
1702
1771
        //System.out.println(duh);
1703
1772
        String expecting = "start|<a href=\"/member/view?ID=1\"><b>Terence</b></a> <br>"+newline+
1704
1773
            "<a href=\"/member/view?ID=2\"><b>Tom</b></a> canEdit<br>"+newline+
1705
1774
            "|end";
1706
 
        assertEqual(duh.toString(), expecting);
 
1775
        assertEquals(expecting,duh.toString());
1707
1776
    }
1708
1777
 
1709
1778
        public static class Tree {
1748
1817
                root.addChild(new Tree("e"));
1749
1818
                tree.setAttribute("it", root);
1750
1819
                String expecting = "( a b ( c d ) e )";
1751
 
                assertEqual(tree.toString(), expecting);
 
1820
                assertEquals(expecting, tree.toString());
1752
1821
        }
1753
1822
 
1754
1823
    public void testNestedAnonymousTemplates() throws Exception {
1755
1824
        StringTemplateGroup group =
1756
1825
                new StringTemplateGroup("dummy", ".");
1757
 
        String newline = System.getProperty("line.separator");
1758
1826
        StringTemplate t =
1759
1827
                new StringTemplate(
1760
1828
                        group,
1769
1837
            "<i>" + newline +
1770
1838
            "<b>parrt</b>" + newline +
1771
1839
            "</i>" + newline;
1772
 
        assertEqual(t.toString(), expecting);
 
1840
        assertEquals(expecting, t.toString());
1773
1841
    }
1774
1842
 
1775
1843
    public void testAnonymousTemplateAccessToEnclosingAttributes() throws Exception {
1776
1844
        StringTemplateGroup group =
1777
1845
                new StringTemplateGroup("dummy", ".");
1778
 
        String newline = System.getProperty("line.separator");
1779
1846
        StringTemplate t =
1780
1847
                new StringTemplate(
1781
1848
                        group,
1791
1858
            "<i>" + newline +
1792
1859
            "<b>parrt, tombu</b>" + newline +
1793
1860
            "</i>" + newline;
1794
 
        assertEqual(t.toString(), expecting);
 
1861
        assertEquals(expecting, t.toString());
1795
1862
    }
1796
1863
 
1797
1864
    public void testNestedAnonymousTemplatesAgain() throws Exception {
1798
1865
 
1799
1866
        StringTemplateGroup group =
1800
1867
                new StringTemplateGroup("dummy", ".");
1801
 
        String newline = System.getProperty("line.separator");
1802
1868
        StringTemplate t =
1803
1869
                new StringTemplate(
1804
1870
                        group,
1812
1878
                "<table>" + newline +
1813
1879
                "<tr><td><b>parrt</b></td></tr><tr><td><b>tombu</b></td></tr>" + newline +
1814
1880
                "</table>" + newline;
1815
 
        assertEqual(t.toString(), expecting);
 
1881
        assertEquals(expecting, t.toString());
1816
1882
    }
1817
1883
 
1818
1884
        public void testEscapes() throws Exception {
1819
1885
                StringTemplateGroup group =
1820
 
                                new StringTemplateGroup("dummy", ".");
1821
 
                String newline = System.getProperty("line.separator");
 
1886
                                new StringTemplateGroup("dummy", ".");          
1822
1887
                group.defineTemplate("foo", "$x$ && $it$");
1823
1888
                StringTemplate t =
1824
1889
                                new StringTemplate(
1843
1908
                //System.out.println("u is '"+u.toString()+"'");
1844
1909
                //System.out.println("v is '"+v.toString()+"'");
1845
1910
                String expecting = "dog\"\" && ick";
1846
 
                assertEqual(t.toString(), expecting);
 
1911
                assertEquals(expecting, t.toString());
1847
1912
                expecting = "dog\"g && ick";
1848
 
                assertEqual(u.toString(), expecting);
 
1913
                assertEquals(expecting,u.toString());
1849
1914
                expecting = "{dog}\" && ick is cool";
1850
 
                assertEqual(v.toString(), expecting);
 
1915
                assertEquals(expecting,v.toString());
1851
1916
        }
1852
1917
 
1853
1918
    public void testEscapesOutsideExpressions() throws Exception {
1855
1920
        b.setAttribute("a", "Ter");
1856
1921
        String expecting ="It\\'s ok...$; \\'hi\\', Ter";
1857
1922
        String result = b.toString();
1858
 
        assertEqual(result, expecting);
 
1923
        assertEquals(expecting, result);
1859
1924
    }
1860
1925
 
1861
1926
    public void testElseClause() throws Exception {
1868
1933
            );
1869
1934
        e.setAttribute("title", "sample");
1870
1935
        String expecting = "foo";
1871
 
        assertEqual(e.toString(), expecting);
 
1936
        assertEquals(expecting, e.toString());
1872
1937
 
1873
1938
        e = e.getInstanceOf();
1874
1939
        expecting = "bar";
1875
 
        assertEqual(e.toString(), expecting);
 
1940
        assertEquals(expecting, e.toString());
1876
1941
    }
1877
1942
 
 
1943
        public void testElseIfClause() throws Exception {
 
1944
                StringTemplate e = new StringTemplate(
 
1945
                                "$if(x)$"+newline +
 
1946
                                "foo"+newline +
 
1947
                                "$elseif(y)$"+newline +
 
1948
                                "bar"+newline +
 
1949
                                "$endif$"
 
1950
                        );
 
1951
                e.setAttribute("y", "yep");
 
1952
                String expecting = "bar";
 
1953
                assertEquals(expecting, e.toString());
 
1954
        }
 
1955
 
 
1956
        public void testElseIfClause2() throws Exception {
 
1957
                StringTemplate e = new StringTemplate(
 
1958
                                "$if(x)$"+newline +
 
1959
                                "foo"+newline +
 
1960
                                "$elseif(y)$"+newline +
 
1961
                                "bar"+newline +
 
1962
                                "$elseif(z)$"+newline +
 
1963
                                "blort"+newline +
 
1964
                                "$endif$"
 
1965
                        );
 
1966
                e.setAttribute("z", "yep");
 
1967
                String expecting = "blort";
 
1968
                assertEquals(expecting, e.toString());
 
1969
        }
 
1970
 
 
1971
        public void testElseIfClauseAndElse() throws Exception {
 
1972
                StringTemplate e = new StringTemplate(
 
1973
                                "$if(x)$"+newline +
 
1974
                                "foo"+newline +
 
1975
                                "$elseif(y)$"+newline +
 
1976
                                "bar"+newline +
 
1977
                                "$elseif(z)$"+newline +
 
1978
                                "z"+newline +
 
1979
                                "$else$"+newline +
 
1980
                                "blort"+newline +
 
1981
                                "$endif$"
 
1982
                        );
 
1983
                String expecting = "blort";
 
1984
                assertEquals(expecting, e.toString());
 
1985
        }
 
1986
 
1878
1987
        public void testNestedIF() throws Exception {
1879
1988
                StringTemplate e = new StringTemplate(
1880
1989
                                "$if(title)$"+newline +
1889
1998
                        );
1890
1999
                e.setAttribute("title", "sample");
1891
2000
                String expecting = "foo";
1892
 
                assertEqual(e.toString(), expecting);
 
2001
                assertEquals(expecting, e.toString());
1893
2002
 
1894
2003
                e = e.getInstanceOf();
1895
2004
                e.setAttribute("header", "more");
1896
2005
                expecting = "bar";
1897
 
                assertEqual(e.toString(), expecting);
 
2006
                assertEquals(expecting, e.toString());
1898
2007
 
1899
2008
                e = e.getInstanceOf();
1900
2009
                expecting = "blort";
1901
 
                assertEqual(e.toString(), expecting);
 
2010
                assertEquals(expecting, e.toString());
1902
2011
        }
1903
2012
 
1904
2013
        public void testEmbeddedMultiLineIF() throws Exception {
1918
2027
                String expecting =
1919
2028
                        "begin"+newline+
1920
2029
                        "stuff";
1921
 
                assertEqual(main.toString(), expecting);
 
2030
                assertEquals(expecting, main.toString());
1922
2031
 
1923
2032
                main = new StringTemplate(group, "$sub$");
1924
2033
                sub = sub.getInstanceOf();
1926
2035
                expecting =
1927
2036
                        "begin"+newline+
1928
2037
                        "blort";
1929
 
                assertEqual(main.toString(), expecting);
 
2038
                assertEquals(expecting, main.toString());
1930
2039
        }
1931
2040
 
1932
2041
    public void testSimpleIndentOfAttributeList()
1950
2059
                "  Terence"+newline+
1951
2060
                "  Jim"+newline+
1952
2061
                "  Sriram";
1953
 
        assertEqual(t.toString(), expecting);
 
2062
        assertEquals(expecting, t.toString());
1954
2063
    }
1955
2064
 
1956
2065
    public void testIndentOfMultilineAttributes()
1979
2088
                "  Sriram"+newline+
1980
2089
                "  is"+newline+
1981
2090
                "  cool";
1982
 
        assertEqual(t.toString(), expecting);
 
2091
        assertEquals(expecting, t.toString());
1983
2092
    }
1984
2093
 
1985
2094
        public void testIndentOfMultipleBlankLines()
2001
2110
                                "  Terence"+newline+
2002
2111
                                ""+newline+ // no indent on blank line
2003
2112
                                "  is a maniac";
2004
 
                assertEqual(t.toString(), expecting);
 
2113
                assertEquals(expecting, t.toString());
2005
2114
        }
2006
2115
 
2007
2116
    public void testIndentBetweenLeftJustifiedLiterals()
2029
2138
                "  Jim"+newline+
2030
2139
                "  Sriram"+newline+
2031
2140
                "after";
2032
 
        assertEqual(t.toString(), expecting);
 
2141
        assertEquals(expecting, t.toString());
2033
2142
    }
2034
2143
 
2035
2144
    public void testNestedIndent()
2079
2188
                "\t  z=4;"+newline+
2080
2189
                "\t}"+newline+
2081
2190
                "}";
2082
 
        assertEqual(t.toString(), expecting);
 
2191
        assertEquals(expecting, t.toString());
2083
2192
    }
2084
2193
 
2085
2194
        public void testAlternativeWriter() throws Exception {
2115
2224
                StringTemplate name = new StringTemplate(group, "$name:bold(x=name)$");
2116
2225
                name.setAttribute("name", "Terence");
2117
2226
                name.write(w);
2118
 
                assertEqual(buf.toString(), "<b>Terence</b>");
 
2227
                assertEquals("<b>Terence</b>", buf.toString());
2119
2228
        }
2120
2229
 
2121
2230
        public void testApplyAnonymousTemplateToMapAndSet() throws Exception {
2127
2236
                m.put("c", "3");
2128
2237
                st.setAttribute("items", m);
2129
2238
                String expecting = "<li>1</li><li>3</li><li>2</li>";
2130
 
                assertEqual(st.toString(), expecting);
 
2239
                assertEquals(expecting, st.toString());
2131
2240
 
2132
2241
                st = st.getInstanceOf();
2133
2242
                Set s = new HashSet();
2136
2245
                s.add("3");
2137
2246
                st.setAttribute("items", s);
2138
2247
                expecting = "<li>3</li><li>2</li><li>1</li>";
2139
 
                assertEqual(st.toString(), expecting);
 
2248
                assertEquals(expecting, st.toString());
2140
2249
        }
2141
2250
 
2142
2251
        public void testDumpMapAndSet() throws Exception {
2148
2257
                m.put("c", "3");
2149
2258
                st.setAttribute("items", m);
2150
2259
                String expecting = "1,3,2";
2151
 
                assertEqual(st.toString(), expecting);
 
2260
                assertEquals(expecting, st.toString());
2152
2261
 
2153
2262
                st = st.getInstanceOf();
2154
2263
                Set s = new HashSet();
2157
2266
                s.add("3");
2158
2267
                st.setAttribute("items", s);
2159
2268
                expecting = "3,2,1";
2160
 
                assertEqual(st.toString(), expecting);
 
2269
                assertEquals(expecting, st.toString());
2161
2270
        }
2162
2271
 
2163
2272
        public class Connector3 {
2172
2281
                                new StringTemplate("$x.values:{<li>$it$</li>}$");
2173
2282
                st.setAttribute("x", new Connector3());
2174
2283
                String expecting = "<li>1</li><li>2</li><li>3</li>";
2175
 
                assertEqual(st.toString(), expecting);
 
2284
                assertEquals(expecting, st.toString());
2176
2285
 
2177
2286
                st = new StringTemplate("$x.stuff:{<li>$it$</li>}$");
2178
2287
                st.setAttribute("x", new Connector3());
2179
2288
                expecting = "<li>1</li><li>2</li>";
2180
 
                assertEqual(st.toString(), expecting);
 
2289
                assertEquals(expecting, st.toString());
2181
2290
        }
2182
2291
 
2183
2292
    public void testSuperTemplateRef()
2193
2302
        StringTemplate st = subGroup.getInstanceOf("page");
2194
2303
        String expecting =
2195
2304
                "Helvetica and Times:text";
2196
 
        assertEqual(st.toString(), expecting);
 
2305
        assertEquals(expecting, st.toString());
2197
2306
    }
2198
2307
 
2199
2308
    public void testApplySuperTemplateRef()
2209
2318
        st.setAttribute("name", "Ter");
2210
2319
        String expecting =
2211
2320
                "<b>Ter</b>";
2212
 
        assertEqual(st.toString(), expecting);
 
2321
        assertEquals(expecting, st.toString());
2213
2322
    }
2214
2323
 
2215
2324
    public void testLazyEvalOfSuperInApplySuperTemplateRef()
2239
2348
                        error = iae.getMessage();
2240
2349
                }
2241
2350
                String expectingError = "base has no super group; invalid template: super.bold";
2242
 
                assertEqual(error, expectingError);
 
2351
                assertEquals(expectingError, error);
2243
2352
    }
2244
2353
 
2245
2354
    public void testTemplatePolymorphism()
2259
2368
        st.setAttribute("name", "Ter");
2260
2369
        String expecting =
2261
2370
                "<strong>Ter</strong>";
2262
 
        assertEqual(st.toString(), expecting);
 
2371
        assertEquals(expecting, st.toString());
2263
2372
    }
2264
2373
 
2265
2374
    public void testListOfEmbeddedTemplateSeesEnclosingAttributes() throws Exception {
2282
2391
        outputST.setAttribute("items", bodyST2);
2283
2392
        outputST.setAttribute("items", bodyST3);
2284
2393
        String expecting = "page: thatstuffthatstuffthatstuff";
2285
 
        assertEqual(outputST.toString(), expecting);
 
2394
        assertEquals(expecting, outputST.toString());
2286
2395
    }
2287
2396
 
2288
2397
    public void testInheritArgumentFromRecursiveTemplateApplication() throws Exception {
2300
2409
        String expecting = "IF true then IF true then ";
2301
2410
        String result = b.toString();
2302
2411
        //System.err.println("result='"+result+"'");
2303
 
        assertEqual(result, expecting);
 
2412
        assertEquals(expecting, result);
2304
2413
    }
2305
2414
 
2306
2415
 
2332
2441
        // before I render.
2333
2442
        String errors = "";
2334
2443
        try {
2335
 
            String result = b.toString();
 
2444
            /*String result =*/ b.toString();
2336
2445
        }
2337
2446
        catch (IllegalStateException ise) {
2338
2447
            errors = ise.getMessage();
2340
2449
        //System.err.println("errors="+errors+"'");
2341
2450
        //System.err.println("expecting="+expectingError+"'");
2342
2451
        StringTemplate.setLintMode(false);
2343
 
        assertEqual(errors, expectingError);
 
2452
        assertEquals(expectingError, errors);
2344
2453
    }
2345
2454
 
2346
2455
 
2359
2468
        String expecting ="{{}}";
2360
2469
        String result = b.toString();
2361
2470
        //System.err.println(result);
2362
 
        assertEqual(result, expecting);
 
2471
        assertEquals(expecting, result);
2363
2472
    }
2364
2473
 
2365
2474
 
2375
2484
        b.setAttribute("name", "Ter");
2376
2485
        String expecting ="name is Ter";
2377
2486
        String result = b.toString();
2378
 
        assertEqual(result, expecting);
 
2487
        assertEquals(expecting, result);
2379
2488
    }
2380
2489
 
2381
2490
    public void testTemplateGetPropertyGetsAttribute() throws Exception {
2410
2519
                                "public void g(int arg);" +newline+
2411
2520
                "public void f() {i=1;}"+newline+
2412
2521
                "public void g(int arg) {y=1;}";
2413
 
        assertEqual(b.toString(), expecting);
 
2522
        assertEquals(expecting,b.toString());
2414
2523
    }
2415
2524
 
2416
2525
    public static class Decl {
2439
2548
                //System.out.println("f='"+f+"'");
2440
2549
                String expecting = "int i = 0;" +newline+
2441
2550
                                "int[] a = null;";
2442
 
                assertEqual(f.toString(), expecting);
 
2551
                assertEquals(expecting, f.toString());
2443
2552
        }
2444
2553
 
2445
2554
        public void testIndirectTemplateApplication() throws Exception {
2457
2566
                StringTemplate f = group.getInstanceOf("test");
2458
2567
                f.setAttribute("name", "first");
2459
2568
                String expecting = "the first";
2460
 
                assertEqual(f.toString(), expecting);
 
2569
                assertEquals(expecting, f.toString());
2461
2570
        }
2462
2571
 
2463
2572
        public void testIndirectTemplateWithArgsApplication() throws Exception {
2475
2584
                StringTemplate f = group.getInstanceOf("test");
2476
2585
                f.setAttribute("name", "first");
2477
2586
                String expecting = "the first: foo";
2478
 
                assertEqual(f.toString(), expecting);
 
2587
                assertEquals(f.toString(), expecting);
2479
2588
        }
2480
2589
 
2481
2590
        public void testNullIndirectTemplateApplication() throws Exception {
2493
2602
                f.setAttribute("names", "me");
2494
2603
                f.setAttribute("names", "you");
2495
2604
                String expecting = "";
2496
 
                assertEqual(f.toString(), expecting);
 
2605
                assertEquals(expecting, f.toString());
2497
2606
        }
2498
2607
 
2499
2608
        public void testNullIndirectTemplate() throws Exception {
2511
2620
                StringTemplate f = group.getInstanceOf("test");
2512
2621
                //f.setAttribute("name", "first");
2513
2622
                String expecting = "";
2514
 
                assertEqual(f.toString(), expecting);
 
2623
                assertEquals(expecting, f.toString());
2515
2624
        }
2516
2625
 
2517
2626
        public void testHashMapPropertyFetch() throws Exception {
2522
2631
                String results = a.toString();
2523
2632
                //System.out.println(results);
2524
2633
                String expecting = "Terence";
2525
 
                assertEqual(results, expecting);
 
2634
                assertEquals(expecting, results);
2526
2635
        }
2527
2636
 
2528
2637
        public void testHashMapPropertyFetchEmbeddedStringTemplate() throws Exception {
2534
2643
                String results = a.toString();
2535
2644
                //System.out.println(results);
2536
2645
                String expecting = "embedded refers to ST rocks";
2537
 
                assertEqual(results, expecting);
 
2646
                assertEquals(expecting, results);
2538
2647
        }
2539
2648
 
2540
2649
        public void testEmbeddedComments() throws Exception {
2543
2652
                                );
2544
2653
                String expecting ="Foo bar"+newline;
2545
2654
                String result = st.toString();
2546
 
                assertEqual(result, expecting);
 
2655
                assertEquals(expecting, result);
2547
2656
 
2548
2657
                st = new StringTemplate(
2549
2658
                                "Foo $! ignore" +newline+
2552
2661
                                );
2553
2662
                expecting ="Foo "+newline+"bar"+newline;
2554
2663
                result = st.toString();
2555
 
                assertEqual(result, expecting);
 
2664
                assertEquals(expecting, result);
2556
2665
 
2557
2666
                st = new StringTemplate(
2558
2667
                                "$! start of line $ and $! ick" +newline+
2560
2669
                                );
2561
2670
                expecting ="boo"+newline;
2562
2671
                result = st.toString();
2563
 
                assertEqual(result, expecting);
 
2672
                assertEquals(expecting, result);
2564
2673
 
2565
2674
                st = new StringTemplate(
2566
2675
                        "$! start of line !$" +newline+
2570
2679
                );
2571
2680
                expecting ="boo"+newline;
2572
2681
                result = st.toString();
2573
 
                assertEqual(result, expecting);
 
2682
                assertEquals(expecting, result);
2574
2683
 
2575
2684
                st = new StringTemplate(
2576
2685
                        "$! back !$$! to back !$" +newline+ // can't detect; leaves \n
2579
2688
                );
2580
2689
                expecting =newline+"boo"+newline;
2581
2690
                result = st.toString();
2582
 
                assertEqual(result, expecting);
 
2691
                assertEquals(expecting, result);
2583
2692
        }
2584
2693
 
2585
2694
        public void testEmbeddedCommentsAngleBracketed() throws Exception {
2589
2698
                                );
2590
2699
                String expecting ="Foo bar"+newline;
2591
2700
                String result = st.toString();
2592
 
                assertEqual(result, expecting);
 
2701
                assertEquals(expecting, result);
2593
2702
 
2594
2703
                st = new StringTemplate(
2595
2704
                                "Foo <! ignore" +newline+
2599
2708
                                );
2600
2709
                expecting ="Foo "+newline+"bar"+newline;
2601
2710
                result = st.toString();
2602
 
                assertEqual(result, expecting);
 
2711
                assertEquals(expecting, result);
2603
2712
 
2604
2713
                st = new StringTemplate(
2605
2714
                                "<! start of line $ and <! ick" +newline+
2608
2717
                                );
2609
2718
                expecting ="boo"+newline;
2610
2719
                result = st.toString();
2611
 
                assertEqual(result, expecting);
 
2720
                assertEquals(expecting, result);
2612
2721
 
2613
2722
                st = new StringTemplate(
2614
2723
                        "<! start of line !>" +
2620
2729
                expecting ="boo"+newline;
2621
2730
                result = st.toString();
2622
2731
                //System.out.println(result);
2623
 
                assertEqual(result, expecting);
 
2732
                assertEquals(expecting, result);
2624
2733
 
2625
2734
                st = new StringTemplate(
2626
2735
                        "<! back !><! to back !>" +newline+ // can't detect; leaves \n
2630
2739
                );
2631
2740
                expecting =newline+"boo"+newline;
2632
2741
                result = st.toString();
2633
 
                assertEqual(result, expecting);
 
2742
                assertEquals(expecting, result);
2634
2743
        }
2635
2744
 
2636
2745
        public void testCharLiterals() throws Exception {
2640
2749
                                );
2641
2750
                String expecting ="Foo "+newline+"\t bar"+newline;
2642
2751
                String result = st.toString();
2643
 
                assertEqual(result, expecting);
 
2752
                assertEquals(expecting, result);
2644
2753
 
2645
2754
                st = new StringTemplate(
2646
2755
                                "Foo $\\n$$\\t$ bar" +newline);
2647
2756
                expecting ="Foo "+newline+"\t bar"+newline;
2648
2757
                result = st.toString();
2649
 
                assertEqual(result, expecting);
2650
 
 
2651
 
                st = new StringTemplate(
2652
 
                                "Foo$\\ $bar$\\n$");
2653
 
                expecting ="Foo bar"+newline;
2654
 
                result = st.toString();
2655
 
                assertEqual(result, expecting);
2656
 
        }
 
2758
                assertEquals(expecting, result);
 
2759
 
 
2760
                st = new StringTemplate(
 
2761
                                "Foo$\\ $bar$\\n$");
 
2762
                expecting ="Foo bar"+newline;
 
2763
                result = st.toString();
 
2764
                assertEquals(expecting, result);
 
2765
        }
 
2766
 
 
2767
        public void testUnicodeLiterals() throws Exception {
 
2768
                StringTemplate st = new StringTemplate(
 
2769
                                "Foo <\\uFEA5\\n\\u00C2> bar" +newline,
 
2770
                                AngleBracketTemplateLexer.class
 
2771
                                );
 
2772
                String expecting ="Foo \ufea5"+newline+"\u00C2 bar"+newline;
 
2773
                String result = st.toString();
 
2774
                assertEquals(expecting, result);
 
2775
 
 
2776
                st = new StringTemplate(
 
2777
                                "Foo $\\uFEA5\\n\\u00C2$ bar" +newline);
 
2778
                expecting ="Foo \ufea5"+newline+"\u00C2 bar"+newline;
 
2779
                result = st.toString();
 
2780
                assertEquals(expecting, result);
 
2781
 
 
2782
                st = new StringTemplate(
 
2783
                                "Foo$\\ $bar$\\n$");
 
2784
                expecting ="Foo bar"+newline;
 
2785
                result = st.toString();
 
2786
                assertEquals(expecting, result);
 
2787
        }
 
2788
 
2657
2789
 
2658
2790
        public void testEmptyIteratedValueGetsSeparator() throws Exception {
2659
2791
                StringTemplateGroup group =
2670
2802
                // empty values get separator still
2671
2803
                String expecting="Terence,,,Tom,Frank,";
2672
2804
                String result = t.toString();
2673
 
                assertEqual(result, expecting);
 
2805
                assertEquals(expecting, result);
2674
2806
        }
2675
2807
 
2676
2808
        public void testEmptyIteratedConditionalValueGetsSeparator() throws Exception {
2687
2819
                // empty conditional values get no separator
2688
2820
                String expecting="Terence,,Frank,";
2689
2821
                String result = t.toString();
2690
 
                assertEqual(result, expecting);
 
2822
                assertEquals(expecting, result);
2691
2823
        }
2692
2824
 
2693
2825
        public void testEmptyIteratedConditionalWithElseValueGetsSeparator() throws Exception {
2704
2836
                // empty conditional values get no separator
2705
2837
                String expecting="Terence,,Frank,";
2706
2838
                String result = t.toString();
2707
 
                assertEqual(result, expecting);
 
2839
                assertEquals(expecting, result);
2708
2840
        }
2709
2841
 
2710
2842
        public void testWhiteSpaceAtEndOfTemplate() throws Exception {
2722
2854
                        "Terence parrt@jguru.comTom tombu@jguru.com";
2723
2855
                String result = pageST.toString();
2724
2856
                //System.out.println("'"+result+"'");
2725
 
                assertEqual(result, expecting);
 
2857
                assertEquals(expecting, result);
2726
2858
        }
2727
2859
 
2728
2860
        static class Duh {
2741
2873
                t.setAttribute("duh", new Duh());
2742
2874
                String expecting="begin\nend\n";
2743
2875
                String result = t.toString();
2744
 
                assertEqual(result, expecting);
 
2876
                assertEquals(expecting, result);
2745
2877
        }
2746
2878
 
2747
2879
        public void testNullListGetsNoOutput() throws Exception {
2756
2888
                //t.setAttribute("users", new Duh());
2757
2889
                String expecting="begin\nend\n";
2758
2890
                String result = t.toString();
2759
 
                assertEqual(result, expecting);
 
2891
                assertEquals(expecting, result);
2760
2892
        }
2761
2893
 
2762
2894
        public void testEmptyListGetsNoOutput() throws Exception {
2771
2903
                t.setAttribute("users", new ArrayList());
2772
2904
                String expecting="begin\nend\n";
2773
2905
                String result = t.toString();
2774
 
                assertEqual(result, expecting);
 
2906
                assertEquals(expecting, result);
2775
2907
        }
2776
2908
 
2777
2909
        public void testEmptyListNoIteratorGetsNoOutput() throws Exception {
2786
2918
                t.setAttribute("users", new ArrayList());
2787
2919
                String expecting="begin\nend\n";
2788
2920
                String result = t.toString();
2789
 
                assertEqual(result, expecting);
 
2921
                assertEquals(expecting, result);
2790
2922
        }
2791
2923
 
2792
2924
        public void testEmptyExprAsFirstLineGetsNoOutput() throws Exception {
2800
2932
                        "end\n");
2801
2933
                String expecting="end\n";
2802
2934
                String result = t.toString();
2803
 
                assertEqual(result, expecting);
 
2935
                assertEquals(expecting, result);
2804
2936
        }
2805
2937
 
2806
2938
        public void testSizeZeroOnLineByItselfGetsNoOutput() throws Exception {
2816
2948
                        "end\n");
2817
2949
                String expecting="begin\nend\n";
2818
2950
                String result = t.toString();
2819
 
                assertEqual(result, expecting);
 
2951
                assertEquals(expecting, result);
2820
2952
        }
2821
2953
 
2822
2954
        public void testSizeZeroOnLineWithIndentGetsNoOutput() throws Exception {
2832
2964
                        "end\n");
2833
2965
                String expecting="begin\nend\n";
2834
2966
                String result = t.toString();
2835
 
                assertEqual(result, expecting);
 
2967
                assertEquals(expecting, result);
2836
2968
        }
2837
2969
 
2838
2970
        public void testSimpleAutoIndent() throws Exception {
2850
2982
                        "       Terence\n" +
2851
2983
                        "       Frank\n" +
2852
2984
                        "}";
2853
 
                assertEqual(results, expecting);
 
2985
                assertEquals(results, expecting);
2854
2986
        }
2855
2987
 
2856
2988
        public void testComputedPropertyName() throws Exception {
2864
2996
                t.setAttribute("propName", "type");
2865
2997
                String expecting="variable property type=int";
2866
2998
                String result = t.toString();
2867
 
                assertEqual(errors.toString(), "");
2868
 
                assertEqual(result, expecting);
 
2999
                assertEquals("", errors.toString());
 
3000
                assertEquals(expecting, result);
2869
3001
        }
2870
3002
 
2871
3003
        public void testNonNullButEmptyIteratorTestsFalse() throws Exception {
2878
3010
                t.setAttribute("users", new LinkedList());
2879
3011
                String expecting="";
2880
3012
                String result = t.toString();
2881
 
                assertEqual(result, expecting);
 
3013
                assertEquals(expecting, result);
2882
3014
        }
2883
3015
 
2884
3016
        public void testDoNotInheritAttributesThroughFormalArgs() throws Exception {
2896
3028
                String expecting = "x=y; // ";
2897
3029
                String result = b.toString();
2898
3030
                //System.err.println("result='"+result+"'");
2899
 
                assertEqual(result, expecting);
 
3031
                assertEquals(expecting, result);
2900
3032
        }
2901
3033
 
2902
3034
        public void testArgEvaluationContext() throws Exception {
2917
3049
                String expecting = "x=y; // foo";
2918
3050
                String result = b.toString();
2919
3051
                //System.err.println("result='"+result+"'");
2920
 
                assertEqual(result, expecting);
 
3052
                assertEquals(expecting, result);
2921
3053
        }
2922
3054
 
2923
3055
        public void testPassThroughAttributes() throws Exception {
2933
3065
                String expecting = "x=y; // foo";
2934
3066
                String result = b.toString();
2935
3067
                //System.err.println("result='"+result+"'");
2936
 
                assertEqual(result, expecting);
 
3068
                assertEquals(expecting, result);
2937
3069
        }
2938
3070
 
2939
3071
        public void testPassThroughAttributes2() throws Exception {
2951
3083
                String expecting = "x=34; // foo";
2952
3084
                String result = b.toString();
2953
3085
                //System.err.println("result='"+result+"'");
2954
 
                assertEqual(result, expecting);
 
3086
                assertEquals(expecting, result);
2955
3087
        }
2956
3088
 
2957
3089
        public void testDefaultArgument() throws Exception {
2969
3101
                String expecting = "x=99; // foo";
2970
3102
                String result = b.toString();
2971
3103
                //System.err.println("result='"+result+"'");
2972
 
                assertEqual(result, expecting);
 
3104
                assertEquals(expecting, result);
2973
3105
        }
2974
3106
 
2975
3107
        public void testDefaultArgument2() throws Exception {
2984
3116
                String expecting = "x=99; // foo";
2985
3117
                String result = b.toString();
2986
3118
                //System.err.println("result='"+result+"'");
2987
 
                assertEqual(result, expecting);
 
3119
                assertEquals(expecting, result);
2988
3120
        }
2989
3121
 
2990
3122
        public void testDefaultArgumentAsTemplate() throws Exception {
3003
3135
                String expecting = "x=foo; // foo";
3004
3136
                String result = b.toString();
3005
3137
                //System.err.println("result='"+result+"'");
3006
 
                assertEqual(result, expecting);
 
3138
                assertEquals(expecting, result);
3007
3139
        }
3008
3140
 
3009
3141
        public void testDefaultArgumentAsTemplate2() throws Exception {
3022
3154
                String expecting = "x= [foo] ; // foo";
3023
3155
                String result = b.toString();
3024
3156
                //System.err.println("result='"+result+"'");
3025
 
                assertEqual(result, expecting);
 
3157
                assertEquals(expecting, result);
3026
3158
        }
3027
3159
 
3028
3160
        public void testDoNotUseDefaultArgument() throws Exception {
3039
3171
                b.setAttribute("name", "foo");
3040
3172
                String expecting = "x=34; // foo";
3041
3173
                String result = b.toString();
3042
 
                assertEqual(result, expecting);
 
3174
                assertEquals(expecting, result);
3043
3175
        }
3044
3176
 
3045
3177
        public void testArgumentsAsTemplates() throws Exception {
3057
3189
                b.setAttribute("size", "34");
3058
3190
                String expecting = "x=34;";
3059
3191
                String result = b.toString();
3060
 
                assertEqual(result, expecting);
 
3192
                assertEquals(expecting, result);
3061
3193
        }
3062
3194
 
3063
3195
        public void testArgumentsAsTemplatesDefaultDelimiters() throws Exception {
3076
3208
                b.setAttribute("size", "34");
3077
3209
                String expecting = "x=34;";
3078
3210
                String result = b.toString();
3079
 
                assertEqual(result, expecting);
 
3211
                assertEquals(expecting, result);
3080
3212
        }
3081
3213
 
3082
3214
        public void testDefaultArgsWhenNotInvoked() throws Exception {
3089
3221
                StringTemplate b = group.getInstanceOf("b");
3090
3222
                String expecting = ".foo.";
3091
3223
                String result = b.toString();
3092
 
                assertEqual(result, expecting);
 
3224
                assertEquals(expecting, result);
3093
3225
        }
3094
3226
 
3095
3227
        public class DateRenderer implements AttributeRenderer {
3097
3229
                        SimpleDateFormat f = new SimpleDateFormat ("yyyy.MM.dd");
3098
3230
                        return f.format(((Calendar)o).getTime());
3099
3231
                }
 
3232
                public String toString(Object o, String formatString) {
 
3233
                        return toString(o);
 
3234
                }
3100
3235
        }
3101
3236
 
3102
3237
        public class DateRenderer2 implements AttributeRenderer {
3104
3239
                        SimpleDateFormat f = new SimpleDateFormat ("MM/dd/yyyy");
3105
3240
                        return f.format(((Calendar)o).getTime());
3106
3241
                }
 
3242
                public String toString(Object o, String formatString) {
 
3243
                        return toString(o);
 
3244
                }
 
3245
        }
 
3246
 
 
3247
        public class DateRenderer3 implements AttributeRenderer {
 
3248
                public String toString(Object o) {
 
3249
                        SimpleDateFormat f = new SimpleDateFormat ("MM/dd/yyyy");
 
3250
                        return f.format(((Calendar)o).getTime());
 
3251
                }
 
3252
                public String toString(Object o, String formatString) {
 
3253
                        SimpleDateFormat f = new SimpleDateFormat (formatString);
 
3254
                        return f.format(((Calendar)o).getTime());
 
3255
                }
 
3256
        }
 
3257
 
 
3258
        public class StringRenderer implements AttributeRenderer {
 
3259
                public String toString(Object o) {
 
3260
                        return (String)o;
 
3261
                }
 
3262
                public String toString(Object o, String formatString) {
 
3263
                        if ( formatString.equals("upper") ) {
 
3264
                                return ((String)o).toUpperCase();
 
3265
                        }
 
3266
                        return toString(o);
 
3267
                }
3107
3268
        }
3108
3269
 
3109
3270
        public void testRendererForST() throws Exception {
3115
3276
                st.registerRenderer(GregorianCalendar.class, new DateRenderer());
3116
3277
                String expecting = "date: 2005.07.05";
3117
3278
                String result = st.toString();
3118
 
                assertEqual(result, expecting);
 
3279
                assertEquals(expecting, result);
 
3280
        }
 
3281
 
 
3282
        public void testRendererWithFormat() throws Exception {
 
3283
                StringTemplate st =new StringTemplate(
 
3284
                                "date: <created; format=\"yyyy.MM.dd\">",
 
3285
                                AngleBracketTemplateLexer.class);
 
3286
                st.setAttribute("created",
 
3287
                                                new GregorianCalendar(2005, 07-1, 05));
 
3288
                st.registerRenderer(GregorianCalendar.class, new DateRenderer3());
 
3289
                String expecting = "date: 2005.07.05";
 
3290
                String result = st.toString();
 
3291
                assertEquals(expecting, result);
 
3292
        }
 
3293
 
 
3294
        public void testRendererWithFormatAndList() throws Exception {
 
3295
                StringTemplate st =new StringTemplate(
 
3296
                                "The names: <names; format=\"upper\">",
 
3297
                                AngleBracketTemplateLexer.class);
 
3298
                st.setAttribute("names", "ter");
 
3299
                st.setAttribute("names", "tom");
 
3300
                st.setAttribute("names", "sriram");
 
3301
                st.registerRenderer(String.class, new StringRenderer());
 
3302
                String expecting = "The names: TERTOMSRIRAM";
 
3303
                String result = st.toString();
 
3304
                assertEquals(expecting, result);
 
3305
        }
 
3306
 
 
3307
        public void testRendererWithFormatAndSeparator() throws Exception {
 
3308
                StringTemplate st =new StringTemplate(
 
3309
                                "The names: <names; separator=\" and \", format=\"upper\">",
 
3310
                                AngleBracketTemplateLexer.class);
 
3311
                st.setAttribute("names", "ter");
 
3312
                st.setAttribute("names", "tom");
 
3313
                st.setAttribute("names", "sriram");
 
3314
                st.registerRenderer(String.class, new StringRenderer());
 
3315
                String expecting = "The names: TER and TOM and SRIRAM";
 
3316
                String result = st.toString();
 
3317
                assertEquals(expecting, result);
 
3318
        }
 
3319
 
 
3320
        public void testRendererWithFormatAndSeparatorAndNull() throws Exception {
 
3321
                StringTemplate st =new StringTemplate(
 
3322
                                "The names: <names; separator=\" and \", null=\"n/a\", format=\"upper\">",
 
3323
                                AngleBracketTemplateLexer.class);
 
3324
                List names = new ArrayList();
 
3325
                names.add("ter");
 
3326
                names.add(null);
 
3327
                names.add("sriram");
 
3328
                st.setAttribute("names", names);
 
3329
                st.registerRenderer(String.class, new StringRenderer());
 
3330
                String expecting = "The names: TER and N/A and SRIRAM";
 
3331
                String result = st.toString();
 
3332
                assertEquals(expecting, result);
3119
3333
        }
3120
3334
 
3121
3335
        public void testEmbeddedRendererSeesEnclosing() throws Exception {
3133
3347
                outer.registerRenderer(GregorianCalendar.class, new DateRenderer());
3134
3348
                String expecting = "X: date: 2005.07.05";
3135
3349
                String result = outer.toString();
3136
 
                assertEqual(result, expecting);
 
3350
                assertEquals(expecting, result);
3137
3351
        }
3138
3352
 
3139
3353
        public void testRendererForGroup() throws Exception {
3149
3363
                group.registerRenderer(GregorianCalendar.class, new DateRenderer());
3150
3364
                String expecting = "date: 2005.07.05";
3151
3365
                String result = st.toString();
3152
 
                assertEqual(result, expecting);
 
3366
                assertEquals(expecting, result);
3153
3367
        }
3154
3368
 
3155
3369
        public void testOverriddenRenderer() throws Exception {
3166
3380
                st.registerRenderer(GregorianCalendar.class, new DateRenderer2());
3167
3381
                String expecting = "date: 07/05/2005";
3168
3382
                String result = st.toString();
3169
 
                assertEqual(result, expecting);
 
3383
                assertEquals(expecting, result);
3170
3384
        }
3171
3385
 
3172
3386
        public void testMap() throws Exception {
3182
3396
                st.setAttribute("name", "x");
3183
3397
                String expecting = "int x = 0;";
3184
3398
                String result = st.toString();
3185
 
                assertEqual(result, expecting);
 
3399
                assertEquals(expecting, result);
3186
3400
        }
3187
3401
 
3188
3402
        public void testMapValuesAreTemplates() throws Exception {
3199
3413
                st.setAttribute("name", "x");
3200
3414
                String expecting = "int x = 0L;";
3201
3415
                String result = st.toString();
3202
 
                assertEqual(result, expecting);
 
3416
                assertEquals(expecting, result);
3203
3417
        }
3204
3418
 
3205
3419
        public void testMapMissingDefaultValueIsEmpty() throws Exception {
3216
3430
                st.setAttribute("name", "x");
3217
3431
                String expecting = "double x = ;"; // weird, but tests default value is key
3218
3432
                String result = st.toString();
3219
 
                assertEqual(result, expecting);
 
3433
                assertEquals(expecting, result);
3220
3434
        }
3221
3435
 
3222
3436
        public void testMapHiddenByFormalArg() throws Exception {
3232
3446
                st.setAttribute("name", "x");
3233
3447
                String expecting = "int x = ;";
3234
3448
                String result = st.toString();
3235
 
                assertEqual(result, expecting);
 
3449
                assertEquals(expecting, result);
3236
3450
        }
3237
3451
 
3238
3452
        public void testMapEmptyValueAndAngleBracketStrings() throws Exception {
3248
3462
                st.setAttribute("name", "x");
3249
3463
                String expecting = "float x = ;";
3250
3464
                String result = st.toString();
3251
 
                assertEqual(result, expecting);
 
3465
                assertEquals(expecting, result);
3252
3466
        }
3253
3467
 
3254
3468
        public void testMapDefaultValue() throws Exception {
3255
3469
                String templates =
3256
3470
                                "group test;" +newline+
3257
 
                                "typeInit ::= [\"int\":\"0\", \"default\":\"null\"] "+newline+
 
3471
                                "typeInit ::= [\"int\":\"0\", default:\"null\"] "+newline+
3258
3472
                                "var(type,name) ::= \"<type> <name> = <typeInit.(type)>;\""+newline
3259
3473
                                ;
3260
3474
                StringTemplateGroup group =
3264
3478
                st.setAttribute("name", "x");
3265
3479
                String expecting = "UserRecord x = null;";
3266
3480
                String result = st.toString();
3267
 
                assertEqual(result, expecting);
 
3481
                assertEquals(expecting, result);
3268
3482
        }
3269
3483
 
3270
3484
        public void testMapEmptyDefaultValue() throws Exception {
3271
3485
                String templates =
3272
3486
                                "group test;" +newline+
3273
 
                                "typeInit ::= [\"int\":\"0\", \"default\":] "+newline+
 
3487
                                "typeInit ::= [\"int\":\"0\", default:] "+newline+
3274
3488
                                "var(type,name) ::= \"<type> <name> = <typeInit.(type)>;\""+newline
3275
3489
                                ;
3276
3490
                StringTemplateGroup group =
3280
3494
                st.setAttribute("name", "x");
3281
3495
                String expecting = "UserRecord x = ;";
3282
3496
                String result = st.toString();
3283
 
                assertEqual(result, expecting);
 
3497
                assertEquals(expecting, result);
3284
3498
        }
3285
3499
 
3286
 
        public void testMapEmptyDefaultValueIsKey() throws Exception {
 
3500
        public void testMapDefaultValueIsKey() throws Exception {
3287
3501
                String templates =
3288
3502
                                "group test;" +newline+
3289
 
                                "typeInit ::= [\"int\":\"0\", \"default\":key] "+newline+
 
3503
                                "typeInit ::= [\"int\":\"0\", default:key] "+newline+
3290
3504
                                "var(type,name) ::= \"<type> <name> = <typeInit.(type)>;\""+newline
3291
3505
                                ;
3292
3506
                StringTemplateGroup group =
3296
3510
                st.setAttribute("name", "x");
3297
3511
                String expecting = "UserRecord x = UserRecord;";
3298
3512
                String result = st.toString();
3299
 
                assertEqual(result, expecting);
 
3513
                assertEquals(expecting, result);
3300
3514
        }
3301
3515
 
 
3516
    /**
 
3517
     * Test that a map can have only the default entry.
 
3518
     * <p>
 
3519
     * Bug ref: JIRA bug ST-15 (Fixed)
 
3520
     */
 
3521
    public void testMapDefaultStringAsKey() throws Exception {
 
3522
        String templates =
 
3523
                "group test;" +newline+
 
3524
                "typeInit ::= [\"default\":\"foo\"] "+newline+
 
3525
                "var(type,name) ::= \"<type> <name> = <typeInit.(type)>;\""+newline
 
3526
                ;
 
3527
        StringTemplateGroup group =
 
3528
                new StringTemplateGroup(new StringReader(templates));
 
3529
        StringTemplate st = group.getInstanceOf("var");
 
3530
        st.setAttribute("type", "default");
 
3531
        st.setAttribute("name", "x");
 
3532
        String expecting = "default x = foo;";
 
3533
        String result = st.toString();
 
3534
        assertEquals(expecting, result);
 
3535
    }
 
3536
    
 
3537
    /**
 
3538
     * Test that a map can return a <b>string</b> with the word: default.
 
3539
     * <p>
 
3540
     * Bug ref: JIRA bug ST-15 (Fixed)
 
3541
     */
 
3542
    public void testMapDefaultIsDefaultString() throws Exception {
 
3543
        String templates =
 
3544
                "group test;" +newline+
 
3545
                "map ::= [default: \"default\"] "+newline+
 
3546
                "t1() ::= \"<map.(1)>\""+newline                
 
3547
                ;
 
3548
        StringTemplateGroup group =
 
3549
                new StringTemplateGroup(new StringReader(templates));
 
3550
        StringTemplate st = group.getInstanceOf("t1");
 
3551
        String expecting = "default";
 
3552
        String result = st.toString();        
 
3553
        assertEquals(expecting, result);
 
3554
    }    
 
3555
 
3302
3556
        public void testMapViaEnclosingTemplates() throws Exception {
3303
3557
                String templates =
3304
3558
                                "group test;" +newline+
3313
3567
                st.setAttribute("name", "x");
3314
3568
                String expecting = "int x = 0;";
3315
3569
                String result = st.toString();
3316
 
                assertEqual(result, expecting);
 
3570
                assertEquals(expecting, result);
3317
3571
        }
3318
3572
 
3319
3573
        public void testMapViaEnclosingTemplates2() throws Exception {
3332
3586
                interm.setAttribute("stuff", var);
3333
3587
                String expecting = "int x = 0;";
3334
3588
                String result = interm.toString();
3335
 
                assertEqual(result, expecting);
 
3589
                assertEquals(expecting, result);
3336
3590
        }
3337
3591
 
3338
3592
        public void testEmptyGroupTemplate() throws Exception {
3345
3599
                StringTemplate a = group.getInstanceOf("foo");
3346
3600
                String expecting = "";
3347
3601
                String result = a.toString();
3348
 
                assertEqual(result, expecting);
 
3602
                assertEquals(expecting, result);
3349
3603
        }
3350
3604
 
3351
3605
        public void testEmptyStringAndEmptyAnonTemplateAsParameterUsingAngleBracketLexer() throws Exception {
3359
3613
                StringTemplate a = group.getInstanceOf("top");
3360
3614
                String expecting = "a=, b=";
3361
3615
                String result = a.toString();
3362
 
                assertEqual(result, expecting);
 
3616
                assertEquals(expecting, result);
3363
3617
        }
3364
3618
 
3365
3619
        public void testEmptyStringAndEmptyAnonTemplateAsParameterUsingDollarLexer() throws Exception {
3374
3628
                StringTemplate a = group.getInstanceOf("top");
3375
3629
                String expecting = "a=, b=";
3376
3630
                String result = a.toString();
3377
 
                assertEqual(result, expecting);
 
3631
                assertEquals(expecting, result);
3378
3632
        }
3379
3633
 
 
3634
    /**
 
3635
     *  FIXME: Dannish does not work if typed directly in with default file
 
3636
     *  encoding on windows. The character needs to be escaped as bellow.
 
3637
     *  Please correct to escape the correct charcter.
 
3638
     */
3380
3639
        public void test8BitEuroChars() throws Exception {
3381
3640
                StringTemplate e = new StringTemplate(
3382
 
                                "Danish: � char"
3383
 
                        );
3384
 
                e = e.getInstanceOf();
3385
 
                String expecting = "Danish: � char";
3386
 
                assertEqual(e.toString(), expecting);
 
3641
                                "Danish: \u0143 char"
 
3642
                        );
 
3643
                e = e.getInstanceOf();
 
3644
                String expecting = "Danish: \u0143 char";
 
3645
                assertEquals(expecting, e.toString());
 
3646
        }
 
3647
 
 
3648
        public void test16BitUnicodeChar() throws Exception {
 
3649
                StringTemplate e = new StringTemplate(
 
3650
                                "DINGBAT CIRCLED SANS-SERIF DIGIT ONE: \u2780"
 
3651
                        );
 
3652
                e = e.getInstanceOf();
 
3653
                String expecting = "DINGBAT CIRCLED SANS-SERIF DIGIT ONE: \u2780";
 
3654
                assertEquals(expecting, e.toString());
3387
3655
        }
3388
3656
 
3389
3657
        public void testFirstOp() throws Exception {
3395
3663
                e.setAttribute("names", "Tom");
3396
3664
                e.setAttribute("names", "Sriram");
3397
3665
                String expecting = "Ter";
3398
 
                assertEqual(e.toString(), expecting);
 
3666
                assertEquals(expecting, e.toString());
3399
3667
        }
3400
3668
 
3401
3669
        public void testRestOp() throws Exception {
3407
3675
                e.setAttribute("names", "Tom");
3408
3676
                e.setAttribute("names", "Sriram");
3409
3677
                String expecting = "Tom, Sriram";
3410
 
                assertEqual(e.toString(), expecting);
 
3678
                assertEquals(expecting, e.toString());
3411
3679
        }
3412
3680
 
3413
3681
        public void testLastOp() throws Exception {
3419
3687
                e.setAttribute("names", "Tom");
3420
3688
                e.setAttribute("names", "Sriram");
3421
3689
                String expecting = "Sriram";
3422
 
                assertEqual(e.toString(), expecting);
 
3690
                assertEquals(expecting, e.toString());
3423
3691
        }
3424
3692
 
3425
3693
        public void testCombinedOp() throws Exception {
3434
3702
                e.setAttribute("yours", "a");
3435
3703
                e.setAttribute("yours", "b");
3436
3704
                String expecting = "1, b";
3437
 
                assertEqual(e.toString(), expecting);
 
3705
                assertEquals(expecting, e.toString());
3438
3706
        }
3439
3707
 
3440
3708
        public void testCatListAndSingleAttribute() throws Exception {
3448
3716
                e.setAttribute("mine", "3");
3449
3717
                e.setAttribute("yours", "a");
3450
3718
                String expecting = "1, 2, 3, a";
3451
 
                assertEqual(e.toString(), expecting);
 
3719
                assertEquals(expecting, e.toString());
3452
3720
        }
3453
3721
 
3454
3722
        public void testCatListAndEmptyAttributes() throws Exception {
3465
3733
                e.setAttribute("mine", "3");
3466
3734
                e.setAttribute("yours", "a");
3467
3735
                String expecting = "1, 2, 3, a";
3468
 
                assertEqual(e.toString(), expecting);
 
3736
                assertEquals(expecting, e.toString());
3469
3737
        }
3470
3738
 
3471
3739
        public void testNestedOp() throws Exception {
3477
3745
                e.setAttribute("names", "Tom");
3478
3746
                e.setAttribute("names", "Sriram");
3479
3747
                String expecting = "Tom";
3480
 
                assertEqual(e.toString(), expecting);
 
3748
                assertEquals(expecting, e.toString());
3481
3749
        }
3482
3750
 
3483
3751
        public void testFirstWithOneAttributeOp() throws Exception {
3487
3755
                e = e.getInstanceOf();
3488
3756
                e.setAttribute("names", "Ter");
3489
3757
                String expecting = "Ter";
3490
 
                assertEqual(e.toString(), expecting);
 
3758
                assertEquals(expecting, e.toString());
3491
3759
        }
3492
3760
 
3493
3761
        public void testLastWithOneAttributeOp() throws Exception {
3497
3765
                e = e.getInstanceOf();
3498
3766
                e.setAttribute("names", "Ter");
3499
3767
                String expecting = "Ter";
3500
 
                assertEqual(e.toString(), expecting);
 
3768
                assertEquals(expecting, e.toString());
3501
3769
        }
3502
3770
 
3503
3771
        public void testLastWithLengthOneListAttributeOp() throws Exception {
3507
3775
                e = e.getInstanceOf();
3508
3776
                e.setAttribute("names", new ArrayList() {{add("Ter");}});
3509
3777
                String expecting = "Ter";
3510
 
                assertEqual(e.toString(), expecting);
 
3778
                assertEquals(expecting, e.toString());
3511
3779
        }
3512
3780
 
3513
3781
        public void testRestWithOneAttributeOp() throws Exception {
3517
3785
                e = e.getInstanceOf();
3518
3786
                e.setAttribute("names", "Ter");
3519
3787
                String expecting = "";
3520
 
                assertEqual(e.toString(), expecting);
 
3788
                assertEquals(expecting, e.toString());
3521
3789
        }
3522
3790
 
3523
3791
        public void testRestWithLengthOneListAttributeOp() throws Exception {
3527
3795
                e = e.getInstanceOf();
3528
3796
                e.setAttribute("names", new ArrayList() {{add("Ter");}});
3529
3797
                String expecting = "";
3530
 
                assertEqual(e.toString(), expecting);
 
3798
                assertEquals(expecting, e.toString());
3531
3799
        }
3532
3800
 
3533
3801
        public void testRepeatedRestOp() throws Exception {
3538
3806
                e.setAttribute("names", "Ter");
3539
3807
                e.setAttribute("names", "Tom");
3540
3808
                String expecting = "Tom, Tom";
3541
 
                assertEqual(e.toString(), expecting);
 
3809
                assertEquals(expecting, e.toString());
3542
3810
        }
3543
3811
 
3544
3812
        /** If an iterator is sent into ST, it must be cannot be reset after each
3561
3829
                names.add("Tom");
3562
3830
                e.setAttribute("names", names.iterator());
3563
3831
                String expecting = "TerTom, ";  // This does not give TerTom twice!!
3564
 
                assertEqual(e.toString(), expecting);
 
3832
                assertEquals(expecting, e.toString());
3565
3833
        }
3566
3834
 
3567
 
        /** BUG!  Fix this.  Iterator is not reset from first to second $x$
 
3835
        /** FIXME: BUG! Iterator is not reset from first to second $x$
3568
3836
         *  Either reset the iterator or pass an attribute that knows to get
3569
3837
         *  the iterator each time.  Seems like first, tail do not
3570
3838
         *  have same problem as they yield objects.
3584
3852
                e.setAttribute("names", "Ter");
3585
3853
                e.setAttribute("names", "Tom");
3586
3854
                String expecting = "Tom, Tom";
3587
 
                assertEqual(e.toString(), expecting);
 
3855
                assertEquals(expecting, e.toString());
3588
3856
        }
3589
3857
 
3590
3858
        public void testIncomingLists() throws Exception {
3595
3863
                e.setAttribute("names", "Ter");
3596
3864
                e.setAttribute("names", "Tom");
3597
3865
                String expecting = "Tom, Tom";
3598
 
                assertEqual(e.toString(), expecting);
 
3866
                assertEquals(expecting, e.toString());
3599
3867
        }
3600
3868
 
3601
3869
        public void testIncomingListsAreNotModified() throws Exception {
3609
3877
                e.setAttribute("names", names);
3610
3878
                e.setAttribute("names", "Sriram");
3611
3879
                String expecting = "Ter, Tom, Sriram";
3612
 
                assertEqual(e.toString(), expecting);
 
3880
                assertEquals(expecting, e.toString());
3613
3881
 
3614
 
                assertEqual(names.size(), 2);
 
3882
                assertEquals(names.size(), 2);
3615
3883
        }
3616
3884
 
3617
3885
        public void testIncomingListsAreNotModified2() throws Exception {
3625
3893
                e.setAttribute("names", "Sriram"); // single element first now
3626
3894
                e.setAttribute("names", names);
3627
3895
                String expecting = "Sriram, Ter, Tom";
3628
 
                assertEqual(e.toString(), expecting);
 
3896
                assertEquals(expecting, e.toString());
3629
3897
 
3630
 
                assertEqual(names.size(), 2);
 
3898
                assertEquals(names.size(), 2);
3631
3899
        }
3632
3900
 
3633
3901
        public void testIncomingArraysAreOk() throws Exception {
3638
3906
                e.setAttribute("names", new String[] {"Ter","Tom"});
3639
3907
                e.setAttribute("names", "Sriram");
3640
3908
                String expecting = "Ter, Tom, Sriram";
3641
 
                assertEqual(e.toString(), expecting);
 
3909
                assertEquals(expecting, e.toString());
 
3910
        }
 
3911
 
 
3912
        public void testMultipleRefsToListAttribute() throws Exception {
 
3913
                String templates =
 
3914
                                "group test;" +newline+
 
3915
                                "f(x) ::= \"<x> <x>\""+newline
 
3916
                                ;
 
3917
                StringTemplateGroup group =
 
3918
                                new StringTemplateGroup(new StringReader(templates));
 
3919
                StringTemplate e = group.getInstanceOf("f");
 
3920
                e.setAttribute("x", "Ter");
 
3921
                e.setAttribute("x", "Tom");
 
3922
                String expecting = "TerTom TerTom";
 
3923
                assertEquals(expecting, e.toString());
3642
3924
        }
3643
3925
 
3644
3926
        public void testApplyTemplateWithSingleFormalArgs() throws Exception {
3654
3936
                e.setAttribute("names", "Tom");
3655
3937
                String expecting = "*Ter*, *Tom* ";
3656
3938
                String result = e.toString();
3657
 
                assertEqual(result, expecting);
 
3939
                assertEquals(expecting, result);
3658
3940
        }
3659
3941
 
3660
3942
        public void testApplyTemplateWithNoFormalArgs() throws Exception {
3671
3953
                e.setAttribute("names", "Tom");
3672
3954
                String expecting = "*Ter*, *Tom* ";
3673
3955
                String result = e.toString();
3674
 
                assertEqual(result, expecting);
 
3956
                assertEquals(expecting, result);
3675
3957
        }
3676
3958
 
3677
3959
        public void testAnonTemplateArgs() throws Exception {
3682
3964
                e.setAttribute("names", "Ter");
3683
3965
                e.setAttribute("names", "Tom");
3684
3966
                String expecting = "Ter, Tom";
3685
 
                assertEqual(e.toString(), expecting);
 
3967
                assertEquals(expecting, e.toString());
3686
3968
        }
3687
3969
 
3688
3970
        public void testAnonTemplateWithArgHasNoITArg() throws Exception {
3700
3982
                        error = nse.getMessage();
3701
3983
                }
3702
3984
                String expecting = "no such attribute: it in template context [anonymous anonymous]";
3703
 
                assertEqual(error, expecting);
 
3985
                assertEquals(error, expecting);
3704
3986
        }
3705
3987
 
3706
3988
        public void testAnonTemplateArgs2() throws Exception {
3711
3993
                e.setAttribute("names", "Ter");
3712
3994
                e.setAttribute("names", "Tom");
3713
3995
                String expecting = "_.Ter._, _.Tom._";
3714
 
                assertEqual(e.toString(), expecting);
 
3996
                assertEquals(expecting, e.toString());
3715
3997
        }
3716
3998
 
3717
3999
        public void testFirstWithCatAttribute() throws Exception {
3724
4006
                e.setAttribute("phones", "1");
3725
4007
                e.setAttribute("phones", "2");
3726
4008
                String expecting = "Ter";
3727
 
                assertEqual(e.toString(), expecting);
 
4009
                assertEquals(expecting, e.toString());
 
4010
        }
 
4011
 
 
4012
        public void testFirstWithListOfMaps() throws Exception {
 
4013
                StringTemplate e = new StringTemplate(
 
4014
                                "$first(maps).Ter$"
 
4015
                        );
 
4016
                e = e.getInstanceOf();
 
4017
                final Map m1 = new HashMap();
 
4018
                final Map m2 = new HashMap();
 
4019
                m1.put("Ter", "x5707");
 
4020
                e.setAttribute("maps", m1);
 
4021
                m2.put("Tom", "x5332");
 
4022
                e.setAttribute("maps", m2);
 
4023
                String expecting = "x5707";
 
4024
                assertEquals(expecting, e.toString());
 
4025
 
 
4026
                e = e.getInstanceOf();
 
4027
                List list = new ArrayList() {{add(m1); add(m2);}};
 
4028
                e.setAttribute("maps", list);
 
4029
                expecting = "x5707";
 
4030
                assertEquals(expecting, e.toString());
 
4031
        }
 
4032
 
 
4033
        public void testFirstWithListOfMaps2() throws Exception {
 
4034
                StringTemplate e = new StringTemplate(
 
4035
                                "$first(maps):{ $it.Ter$ }$"
 
4036
                        );
 
4037
                e = e.getInstanceOf();
 
4038
                final Map m1 = new HashMap();
 
4039
                final Map m2 = new HashMap();
 
4040
                m1.put("Ter", "x5707");
 
4041
                e.setAttribute("maps", m1);
 
4042
                m2.put("Tom", "x5332");
 
4043
                e.setAttribute("maps", m2);
 
4044
                String expecting = "x5707";
 
4045
                assertEquals(expecting, e.toString());
 
4046
 
 
4047
                e = e.getInstanceOf();
 
4048
                List list = new ArrayList() {{add(m1); add(m2);}};
 
4049
                e.setAttribute("maps", list);
 
4050
                expecting = "x5707";
 
4051
                assertEquals(expecting, e.toString());
3728
4052
        }
3729
4053
 
3730
4054
        public void testJustCat() throws Exception {
3737
4061
                e.setAttribute("phones", "1");
3738
4062
                e.setAttribute("phones", "2");
3739
4063
                String expecting = "TerTom12";
3740
 
                assertEqual(e.toString(), expecting);
 
4064
                assertEquals(expecting, e.toString());
3741
4065
        }
3742
4066
 
3743
4067
        public void testCat2Attributes() throws Exception {
3750
4074
                e.setAttribute("phones", "1");
3751
4075
                e.setAttribute("phones", "2");
3752
4076
                String expecting = "Ter, Tom, 1, 2";
3753
 
                assertEqual(e.toString(), expecting);
 
4077
                assertEquals(expecting, e.toString());
3754
4078
        }
3755
4079
 
3756
4080
        public void testCat2AttributesWithApply() throws Exception {
3763
4087
                e.setAttribute("phones", "1");
3764
4088
                e.setAttribute("phones", "2");
3765
4089
                String expecting = "Ter.Tom.1.2.";
3766
 
                assertEqual(e.toString(), expecting);
 
4090
                assertEquals(expecting, e.toString());
3767
4091
        }
3768
4092
 
3769
4093
        public void testCat3Attributes() throws Exception {
3778
4102
                e.setAttribute("salaries", "big");
3779
4103
                e.setAttribute("salaries", "huge");
3780
4104
                String expecting = "Ter, Tom, 1, 2, big, huge";
3781
 
                assertEqual(e.toString(), expecting);
 
4105
                assertEquals(expecting, e.toString());
3782
4106
        }
3783
4107
 
3784
4108
        public void testListAsTemplateArgument() throws Exception {
3797
4121
                e.setAttribute("phones", "2");
3798
4122
                String expecting = "*Ter**Tom**1**2*";
3799
4123
                String result = e.toString();
3800
 
                assertEqual(result, expecting);
 
4124
                assertEquals(expecting, result);
3801
4125
        }
3802
4126
 
3803
4127
        public void testSingleExprTemplateArgument() throws Exception {
3813
4137
                e.setAttribute("name", "Ter");
3814
4138
                String expecting = "*Ter*";
3815
4139
                String result = e.toString();
3816
 
                assertEqual(result, expecting);
 
4140
                assertEquals(expecting, result);
3817
4141
        }
3818
4142
 
3819
4143
        public void testSingleExprTemplateArgumentInApply() throws Exception {
3834
4158
                e.setAttribute("x", "ick");
3835
4159
                String expecting = "*ick**ick*";
3836
4160
                String result = e.toString();
3837
 
                assertEqual(result, expecting);
 
4161
                assertEquals(expecting, result);
3838
4162
        }
3839
4163
 
3840
4164
        public void testSoleFormalTemplateArgumentInMultiApply() throws Exception {
3852
4176
                e.setAttribute("names", "Tom");
3853
4177
                String expecting = "*Ter*_Tom_";
3854
4178
                String result = e.toString();
3855
 
                assertEqual(result, expecting);
 
4179
                assertEquals(expecting, result);
3856
4180
        }
3857
4181
 
3858
4182
        public void testSingleExprTemplateArgumentError() throws Exception {
3867
4191
                                                AngleBracketTemplateLexer.class, errors);
3868
4192
                StringTemplate e = group.getInstanceOf("test");
3869
4193
                e.setAttribute("name", "Ter");
3870
 
                String result = e.toString();
 
4194
                /*String result =*/ e.toString();
3871
4195
                String expecting = "template bold must have exactly one formal arg in template context [test <invoke bold arg context>]";
3872
 
                assertEqual(errors.toString(), expecting);
 
4196
                assertEquals(errors.toString(), expecting);
3873
4197
        }
3874
4198
 
3875
4199
        public void testInvokeIndirectTemplateWithSingleFormalArgs() throws Exception {
3886
4210
                e.setAttribute("arg", "Ter");
3887
4211
                String expecting = "_Ter_";
3888
4212
                String result = e.toString();
3889
 
                assertEqual(result, expecting);
 
4213
                assertEquals(expecting, result);
3890
4214
        }
3891
4215
 
3892
4216
        public void testParallelAttributeIteration() throws Exception {
3901
4225
                e.setAttribute("salaries", "big");
3902
4226
                e.setAttribute("salaries", "huge");
3903
4227
                String expecting = "Ter@1: big"+newline+"Tom@2: huge"+newline;
3904
 
                assertEqual(e.toString(), expecting);
 
4228
                assertEquals(expecting, e.toString());
 
4229
        }
 
4230
 
 
4231
        public void testParallelAttributeIterationWithNullValue() throws Exception {
 
4232
                StringTemplate e = new StringTemplate(
 
4233
                                "$names,phones,salaries:{n,p,s | $n$@$p$: $s$\n}$"
 
4234
                        );
 
4235
                e = e.getInstanceOf();
 
4236
                e.setAttribute("names", "Ter");
 
4237
                e.setAttribute("names", "Tom");
 
4238
                e.setAttribute("names", "Sriram");
 
4239
                e.setAttribute("phones", new ArrayList() {{add("1"); add(null); add("3");}});
 
4240
                e.setAttribute("salaries", "big");
 
4241
                e.setAttribute("salaries", "huge");
 
4242
                e.setAttribute("salaries", "enormous");
 
4243
                String expecting = "Ter@1: big"+newline+
 
4244
                                                   "Tom@: huge"+newline+
 
4245
                                                   "Sriram@3: enormous"+newline;
 
4246
                assertEquals(expecting, e.toString());
3905
4247
        }
3906
4248
 
3907
4249
        public void testParallelAttributeIterationHasI() throws Exception {
3916
4258
                e.setAttribute("salaries", "big");
3917
4259
                e.setAttribute("salaries", "huge");
3918
4260
                String expecting = "0. Ter@1: big"+newline+"1. Tom@2: huge"+newline;
3919
 
                assertEqual(e.toString(), expecting);
 
4261
                assertEquals(expecting, e.toString());
3920
4262
        }
3921
4263
 
3922
4264
        public void testParallelAttributeIterationWithDifferentSizes() throws Exception {
3931
4273
                e.setAttribute("phones", "2");
3932
4274
                e.setAttribute("salaries", "big");
3933
4275
                String expecting = "Ter@1: big, Tom@2: , Sriram@: ";
3934
 
                assertEqual(e.toString(), expecting);
 
4276
                assertEquals(expecting, e.toString());
3935
4277
        }
3936
4278
 
3937
4279
        public void testParallelAttributeIterationWithSingletons() throws Exception {
3943
4285
                e.setAttribute("phones", "1");
3944
4286
                e.setAttribute("salaries", "big");
3945
4287
                String expecting = "Ter@1: big";
3946
 
                assertEqual(e.toString(), expecting);
 
4288
                assertEquals(expecting, e.toString());
3947
4289
        }
3948
4290
 
3949
4291
        public void testParallelAttributeIterationWithMismatchArgListSizes() throws Exception {
3959
4301
                e.setAttribute("phones", "2");
3960
4302
                e.setAttribute("salaries", "big");
3961
4303
                String expecting = "Ter@1, Tom@2";
3962
 
                assertEqual(e.toString(), expecting);
 
4304
                assertEquals(expecting, e.toString());
3963
4305
                String errorExpecting = "number of arguments [n, p] mismatch between attribute list and anonymous template in context [anonymous]";
3964
 
                assertEqual(errors.toString(), errorExpecting);
 
4306
                assertEquals(errorExpecting, errors.toString());
3965
4307
        }
3966
4308
 
3967
4309
        public void testParallelAttributeIterationWithMissingArgs() throws Exception {
3976
4318
                e.setAttribute("salaries", "big");
3977
4319
                e.toString(); // generate the error
3978
4320
                String errorExpecting = "missing arguments in anonymous template in context [anonymous]";
3979
 
                assertEqual(errors.toString(), errorExpecting);
 
4321
                assertEquals(errorExpecting, errors.toString());
3980
4322
        }
3981
4323
 
3982
4324
        public void testParallelAttributeIterationWithDifferentSizesTemplateRefInsideToo() throws Exception {
3996
4338
                p.setAttribute("phones", "2");
3997
4339
                p.setAttribute("salaries", "big");
3998
4340
                String expecting = "Ter@1: big, Tom@2: n/a, Sriram@n/a: n/a";
3999
 
                assertEqual(p.toString(), expecting);
 
4341
                assertEquals(expecting, p.toString());
4000
4342
        }
4001
4343
 
4002
4344
        public void testAnonTemplateOnLeftOfApply() throws Exception {
4004
4346
                                "${foo}:{($it$)}$"
4005
4347
                        );
4006
4348
                String expecting = "(foo)";
4007
 
                assertEqual(e.toString(), expecting);
 
4349
                assertEquals(expecting, e.toString());
4008
4350
        }
4009
4351
 
4010
4352
        public void testOverrideThroughConditional() throws Exception {
4028
4370
                StringTemplate b = subgroup.getInstanceOf("body");
4029
4371
                String expecting ="bar";
4030
4372
                String result = b.toString();
4031
 
                assertEqual(result, expecting);
 
4373
                assertEquals(expecting, result);
4032
4374
        }
4033
4375
 
4034
4376
        public static class NonPublicProperty {
4045
4387
 
4046
4388
                st.setAttribute("x", o);
4047
4389
                String expecting = "9:34";
4048
 
                assertEqual(st.toString(), expecting);
 
4390
                assertEquals(expecting, st.toString());
4049
4391
        }
4050
4392
 
4051
4393
        public void testIndexVar() throws Exception {
4052
4394
                StringTemplateGroup group =
4053
4395
                                new StringTemplateGroup("dummy", ".");
4054
 
                String newline = System.getProperty("line.separator");
4055
4396
                StringTemplate t =
4056
4397
                                new StringTemplate(
4057
4398
                                                group,
4062
4403
                String expecting =
4063
4404
                        "1. parrt" +newline+
4064
4405
                        "2. tombu";
4065
 
                assertEqual(t.toString(), expecting);
 
4406
                assertEquals(expecting, t.toString());
4066
4407
        }
4067
4408
 
4068
4409
        public void testIndex0Var() throws Exception {
4069
4410
                StringTemplateGroup group =
4070
4411
                                new StringTemplateGroup("dummy", ".");
4071
 
                String newline = System.getProperty("line.separator");
4072
4412
                StringTemplate t =
4073
4413
                                new StringTemplate(
4074
4414
                                                group,
4079
4419
                String expecting =
4080
4420
                        "0. parrt" +newline+
4081
4421
                        "1. tombu";
4082
 
                assertEqual(t.toString(), expecting);
 
4422
                assertEquals(expecting, t.toString());
4083
4423
        }
4084
4424
 
4085
4425
        public void testIndexVarWithMultipleExprs() throws Exception {
4086
4426
                StringTemplateGroup group =
4087
4427
                                new StringTemplateGroup("dummy", ".");
4088
 
                String newline = System.getProperty("line.separator");
4089
4428
                StringTemplate t =
4090
4429
                                new StringTemplate(
4091
4430
                                                group,
4098
4437
                String expecting =
4099
4438
                        "1. parrt@x5707" +newline+
4100
4439
                        "2. tombu@x5000";
4101
 
                assertEqual(t.toString(), expecting);
 
4440
                assertEquals(expecting, t.toString());
4102
4441
        }
4103
4442
 
4104
4443
        public void testIndex0VarWithMultipleExprs() throws Exception {
4105
4444
                StringTemplateGroup group =
4106
4445
                                new StringTemplateGroup("dummy", ".");
4107
 
                String newline = System.getProperty("line.separator");
4108
4446
                StringTemplate t =
4109
4447
                                new StringTemplate(
4110
4448
                                                group,
4117
4455
                String expecting =
4118
4456
                        "0. parrt@x5707" +newline+
4119
4457
                        "1. tombu@x5000";
4120
 
                assertEqual(t.toString(), expecting);
 
4458
                assertEquals(expecting, t.toString());
4121
4459
        }
4122
4460
 
4123
4461
        public void testArgumentContext() throws Exception {
4126
4464
                StringTemplateGroup group =
4127
4465
                                new StringTemplateGroup("test");
4128
4466
                StringTemplate main = group.defineTemplate("main", "$foo(t={Hi, $name$}, name=\"parrt\")$");
4129
 
                StringTemplate foo = group.defineTemplate("foo", "$t$");
 
4467
                /*StringTemplate foo =*/ group.defineTemplate("foo", "$t$");
4130
4468
                String expecting="Hi, parrt";
4131
 
                assertEqual(main.toString(), expecting);
 
4469
                assertEquals(expecting, main.toString());
4132
4470
        }
4133
4471
 
4134
4472
        public void testNoDotsInAttributeNames() throws Exception {
4142
4480
                        error = e.getMessage();
4143
4481
                }
4144
4482
                String expecting = "cannot have '.' in attribute names";
4145
 
                assertEqual(error, expecting);
 
4483
                assertEquals(expecting,error);
4146
4484
        }
4147
4485
 
4148
4486
        public void testNoDotsInTemplateNames() throws Exception {
4150
4488
                String templates =
4151
4489
                                "group test;" +newline+
4152
4490
                                "a.b() ::= <<foo>>"+newline;
4153
 
                String error = null;
4154
 
                        StringTemplateGroup group =
 
4491
 
 
4492
                        /*StringTemplateGroup group =*/
4155
4493
                                new StringTemplateGroup(new StringReader(templates),
4156
4494
                                                                                DefaultTemplateLexer.class,
4157
4495
                                                                                errors);
4176
4514
                        "2,1,6,32,5,6,77,4,9,20,2,1,4,63,9,20,2,1,\n" +
4177
4515
                        "4,6,32,5,6,77,6,32,5,6,77,3,9,20,2,1,4,6,\n" +
4178
4516
                        "32,5,6,77,888,1,6,32,5 };";
4179
 
                assertEqual(a.toString(40), expecting);
 
4517
                assertEquals(expecting,a.toString(40));
4180
4518
        }
4181
4519
 
4182
4520
        public void testLineWrapAnchored() throws Exception {
4197
4535
                        "            63,9,20,2,1,4,6,32,5,6,77,6,\n" +
4198
4536
                        "            32,5,6,77,3,9,20,2,1,4,6,32,\n" +
4199
4537
                        "            5,6,77,888,1,6,32,5 };";
4200
 
                assertEqual(a.toString(40), expecting);
 
4538
                assertEquals(expecting, a.toString(40));
4201
4539
        }
4202
4540
 
4203
4541
        public void testFortranLineWrap() throws Exception {
4213
4551
                String expecting =
4214
4552
                        "       FUNCTION line( a,b,c,d,\n" +
4215
4553
                        "      ce,f )";
4216
 
                assertEqual(a.toString(30), expecting);
 
4554
                assertEquals(expecting, a.toString(30));
4217
4555
        }
4218
4556
 
4219
4557
        public void testLineWrapWithDiffAnchor() throws Exception {
4233
4571
                        "            1,6,32,5,6,77,4,9,\n" +
4234
4572
                        "            20,2,1,4,63,9,20,2,\n" +
4235
4573
                        "            1,4,6 };";
4236
 
                assertEqual(a.toString(30), expecting);
 
4574
                assertEquals(expecting, a.toString(30));
4237
4575
        }
4238
4576
 
4239
4577
        public void testLineWrapEdgeCase() throws Exception {
4249
4587
                String expecting =
4250
4588
                        "abc\n"+
4251
4589
                        "de";
4252
 
                assertEqual(a.toString(3), expecting);
 
4590
                assertEquals(expecting, a.toString(3));
4253
4591
        }
4254
4592
 
4255
4593
        public void testLineWrapLastCharIsNewline() throws Exception {
4265
4603
                String expecting =
4266
4604
                        "ab\n"+
4267
4605
                        "de";
4268
 
                assertEqual(a.toString(3), expecting);
 
4606
                assertEquals(expecting,a.toString(3));
4269
4607
        }
4270
4608
 
4271
4609
        public void testLineWrapCharAfterWrapIsNewline() throws Exception {
4284
4622
                        "abc\n" +
4285
4623
                        "\n" +
4286
4624
                        "de";
4287
 
                assertEqual(a.toString(3), expecting);
 
4625
                assertEquals(expecting, a.toString(3));
4288
4626
        }
4289
4627
 
4290
4628
        public void testLineWrapForAnonTemplate() throws Exception {
4300
4638
                        "![1][2][3]\n" + // width=9 is the 3 char; don't break til after ]
4301
4639
                        "[4][5][6]\n" +
4302
4640
                        "[7][8][9]!";
4303
 
                assertEqual(a.toString(9), expecting);
 
4641
                assertEquals(expecting,a.toString(9));
4304
4642
        }
4305
4643
 
4306
4644
        public void testLineWrapForAnonTemplateAnchored() throws Exception {
4316
4654
                        "![1][2][3]\n" +
4317
4655
                        " [4][5][6]\n" +
4318
4656
                        " [7][8][9]!";
4319
 
                assertEqual(a.toString(9), expecting);
 
4657
                assertEquals(expecting, a.toString(9));
4320
4658
        }
4321
4659
 
4322
4660
        public void testLineWrapForAnonTemplateComplicatedWrap() throws Exception {
4337
4675
                        "  ![5][6]!+\n" +
4338
4676
                        "  ![7][8]!+\n" +
4339
4677
                        "  ![9]!.";
4340
 
                assertEqual(t.toString(9), expecting);
 
4678
                assertEquals(expecting,t.toString(9));
4341
4679
        }
4342
4680
 
4343
4681
        public void testIndentBeyondLineWidth() throws Exception {
4356
4694
                        "    c\n" +
4357
4695
                        "    d\n" +
4358
4696
                        "    e";
4359
 
                assertEqual(a.toString(2), expecting);
 
4697
                assertEquals(expecting, a.toString(2));
4360
4698
        }
4361
4699
 
4362
4700
        public void testIndentedExpr() throws Exception {
4374
4712
                        "    cd\n" +
4375
4713
                        "    e";
4376
4714
                // width=4 spaces + 2 char.
4377
 
                assertEqual(a.toString(6), expecting);
 
4715
                assertEquals(expecting, a.toString(6));
4378
4716
        }
4379
4717
 
4380
4718
        public void testNestedIndentedExpr() throws Exception {
4394
4732
                        "    cd\n" +
4395
4733
                        "    e!";
4396
4734
                // width=4 spaces + 2 char.
4397
 
                assertEqual(top.toString(6), expecting);
 
4735
                assertEquals(expecting, top.toString(6));
4398
4736
        }
4399
4737
 
4400
4738
        public void testNestedWithIndentAndTrackStartOfExpr() throws Exception {
4414
4752
                        "  x: ab\n" +
4415
4753
                        "     cd\n" +
4416
4754
                        "     e!";
4417
 
                assertEqual(top.toString(7), expecting);
 
4755
                assertEquals(expecting, top.toString(7));
4418
4756
        }
4419
4757
 
4420
4758
        public void testLineDoesNotWrapDueToLiteral() throws Exception {
4432
4770
                int n = "public void foo(a, b, c".length();
4433
4771
                String expecting =
4434
4772
                        "public void foo(a, b, c) throws Ick { i=3; }";
4435
 
                assertEqual(a.toString(n), expecting);
 
4773
                assertEquals(expecting, a.toString(n));
4436
4774
        }
4437
4775
 
4438
4776
        public void testSingleValueWrap() throws Exception {
4448
4786
                String expecting =
4449
4787
                        "{ \n"+
4450
4788
                        "  i=3; }";
4451
 
                assertEqual(m.toString(2), expecting);
 
4789
                assertEquals(expecting, m.toString(2));
4452
4790
        }
4453
4791
 
4454
4792
        public void testLineWrapInNestedExpr() throws Exception {
4481
4819
                        "            32,5,6,77,3,9,20,2,1,4,6,32,\n" +
4482
4820
                        "            5,6,77,888,1,6,32,5 };\n" +
4483
4821
                        "done";
4484
 
                assertEqual(top.toString(40), expecting);
 
4822
                assertEquals(expecting, top.toString(40));
4485
4823
        }
4486
4824
 
4487
4825
        public void testEscapeEscape() throws Exception {
4491
4829
                t.setAttribute("v", "Joe");
4492
4830
                //System.out.println(t);
4493
4831
                String expecting="\\Joe";
4494
 
                assertEqual(t.toString(), expecting);
 
4832
                assertEquals(expecting, t.toString());
4495
4833
        }
4496
4834
 
4497
4835
        public void testEscapeEscapeNestedAngle() throws Exception {
4501
4839
                t.setAttribute("v", "Joe");
4502
4840
                //System.out.println(t);
4503
4841
                String expecting="\\Joe";
4504
 
                assertEqual(t.toString(), expecting);
 
4842
                assertEquals(expecting, t.toString());
4505
4843
        }
4506
4844
 
4507
4845
        public void testListOfIntArrays() throws Exception {
4517
4855
                t.setAttribute("data", data);
4518
4856
                //System.out.println(t);
4519
4857
                String expecting="[1,2,3][10,20,30]";
4520
 
                assertEqual(t.toString(), expecting);
 
4858
                assertEquals(expecting, t.toString());
4521
4859
        }
4522
4860
 
4523
4861
        // Test null option
4529
4867
                        group.defineTemplate("t", "<data; null=\"0\">");
4530
4868
                //System.out.println(t);
4531
4869
                String expecting="0";
4532
 
                assertEqual(t.toString(), expecting);
 
4870
                assertEquals(expecting, t.toString());
4533
4871
        }
4534
4872
 
4535
4873
        public void testNullOptionHasEmptyNullValue() throws Exception {
4542
4880
                data.add(new Integer(1));
4543
4881
                t.setAttribute("data", data);
4544
4882
                String expecting=", 1";
4545
 
                assertEqual(t.toString(), expecting);
 
4883
                assertEquals(expecting, t.toString());
4546
4884
        }
4547
4885
 
4548
4886
        public void testNullOptionSingleNullValueInList() throws Exception {
4555
4893
                t.setAttribute("data", data);
4556
4894
                //System.out.println(t);
4557
4895
                String expecting="0";
4558
 
                assertEqual(t.toString(), expecting);
 
4896
                assertEquals(expecting, t.toString());
4559
4897
        }
4560
4898
 
4561
4899
        public void testNullValueInList() throws Exception {
4573
4911
                t.setAttribute("data", data);
4574
4912
                //System.out.println(t);
4575
4913
                String expecting="-1, 1, -1, 3, 4, -1";
4576
 
                assertEqual(t.toString(), expecting);
 
4914
                assertEquals(expecting, t.toString());
4577
4915
        }
4578
4916
 
4579
4917
        public void testNullValueInListNoNullOption() throws Exception {
4591
4929
                t.setAttribute("data", data);
4592
4930
                //System.out.println(t);
4593
4931
                String expecting="1, 3, 4";
4594
 
                assertEqual(t.toString(), expecting);
 
4932
                assertEquals(expecting, t.toString());
4595
4933
        }
4596
 
 
4597
 
        public void testNullValueInListWithTemplateApply() throws Exception {
 
4934
        
 
4935
    public void testNullValueInListWithTemplateApply() throws Exception {
4598
4936
                StringTemplateGroup group =
4599
4937
                                new StringTemplateGroup("test", AngleBracketTemplateLexer.class);
4600
4938
                StringTemplate t =
4607
4945
                data.add(null);
4608
4946
                t.setAttribute("data", data);
4609
4947
                String expecting="0, -1, 2, -1";
4610
 
                assertEqual(t.toString(), expecting);
 
4948
                assertEquals(expecting, t.toString());
4611
4949
        }
4612
4950
 
4613
4951
        public void testNullValueInListWithTemplateApplyNullFirstValue() throws Exception {
4623
4961
                data.add(new Integer(2));
4624
4962
                t.setAttribute("data", data);
4625
4963
                String expecting="-1, 0, -1, 2";
4626
 
                assertEqual(t.toString(), expecting);
 
4964
                assertEquals(expecting, t.toString());
4627
4965
        }
4628
4966
 
4629
4967
        public void testNullSingleValueInListWithTemplateApply() throws Exception {
4636
4974
                data.add(null);
4637
4975
                t.setAttribute("data", data);
4638
4976
                String expecting="-1";
4639
 
                assertEqual(t.toString(), expecting);
 
4977
                assertEquals(expecting, t.toString());
4640
4978
        }
4641
4979
 
4642
4980
        public void testNullSingleValueWithTemplateApply() throws Exception {
4646
4984
                        group.defineTemplate("t", "<data:array(); null=\"-1\", separator=\", \">");
4647
4985
                group.defineTemplate("array", "<it>");
4648
4986
                String expecting="-1";
4649
 
                assertEqual(t.toString(), expecting);
 
4987
                assertEquals(expecting, t.toString());
4650
4988
        }
4651
4989
 
4652
4990
        public void testLengthOp() throws Exception {
4658
4996
                e.setAttribute("names", "Tom");
4659
4997
                e.setAttribute("names", "Sriram");
4660
4998
                String expecting = "3";
4661
 
                assertEqual(e.toString(), expecting);
 
4999
                assertEquals(expecting, e.toString());
 
5000
        }
 
5001
 
 
5002
        public void testLengthOpWithMap() throws Exception {
 
5003
                StringTemplate e = new StringTemplate(
 
5004
                                "$length(names)$"
 
5005
                        );
 
5006
                e = e.getInstanceOf();
 
5007
                Map m = new HashMap();
 
5008
                m.put("Tom", "foo");
 
5009
                m.put("Sriram", "foo");
 
5010
                m.put("Doug", "foo");
 
5011
                e.setAttribute("names", m);
 
5012
                String expecting = "3";
 
5013
                assertEquals(expecting, e.toString());
 
5014
        }
 
5015
 
 
5016
        public void testLengthOpWithSet() throws Exception {
 
5017
                StringTemplate e = new StringTemplate(
 
5018
                                "$length(names)$"
 
5019
                        );
 
5020
                e = e.getInstanceOf();
 
5021
                Set m = new HashSet();
 
5022
                m.add("Tom");
 
5023
                m.add("Sriram");
 
5024
                m.add("Doug");
 
5025
                e.setAttribute("names", m);
 
5026
                String expecting = "3";
 
5027
                assertEquals(expecting, e.toString());
4662
5028
        }
4663
5029
 
4664
5030
        public void testLengthOpNull() throws Exception {
4668
5034
                e = e.getInstanceOf();
4669
5035
                e.setAttribute("names", null);
4670
5036
                String expecting = "0";
4671
 
                assertEqual(e.toString(), expecting);
 
5037
                assertEquals(expecting, e.toString());
4672
5038
        }
4673
5039
 
4674
5040
        public void testLengthOpSingleValue() throws Exception {
4678
5044
                e = e.getInstanceOf();
4679
5045
                e.setAttribute("names", "Ter");
4680
5046
                String expecting = "1";
4681
 
                assertEqual(e.toString(), expecting);
 
5047
                assertEquals(expecting, e.toString());
4682
5048
        }
4683
5049
 
4684
5050
        public void testLengthOpPrimitive() throws Exception {
4688
5054
                e = e.getInstanceOf();
4689
5055
                e.setAttribute("ints", new int[] {1,2,3,4} );
4690
5056
                String expecting = "4";
4691
 
                assertEqual(e.toString(), expecting);
 
5057
                assertEquals(expecting, e.toString());
4692
5058
        }
4693
5059
 
4694
5060
        public void testLengthOpOfListWithNulls() throws Exception {
4703
5069
                data.add(null);
4704
5070
                e.setAttribute("data", data);
4705
5071
                String expecting = "4"; // nulls are counted
4706
 
                assertEqual(e.toString(), expecting);
 
5072
                assertEquals(expecting, e.toString());
4707
5073
        }
4708
5074
 
4709
5075
        public void testStripOpOfListWithNulls() throws Exception {
4718
5084
                data.add(null);
4719
5085
                e.setAttribute("data", data);
4720
5086
                String expecting = "Himom"; // nulls are skipped
4721
 
                assertEqual(e.toString(), expecting);
 
5087
                assertEquals(expecting, e.toString());
 
5088
        }
 
5089
 
 
5090
        public void testStripOpOfListOfListsWithNulls() throws Exception {
 
5091
                StringTemplate e = new StringTemplate(
 
5092
                                "$strip(data):{list | $strip(list)$}; separator=\",\"$"
 
5093
                        );
 
5094
                e = e.getInstanceOf();
 
5095
                List data = new ArrayList();
 
5096
                List dataOne = new ArrayList();
 
5097
                dataOne.add("Hi");
 
5098
                dataOne.add("mom");
 
5099
                data.add(dataOne);
 
5100
                data.add(null);
 
5101
                List dataTwo = new ArrayList();
 
5102
                dataTwo.add("Hi");
 
5103
                dataTwo.add(null);
 
5104
                dataTwo.add("dad");
 
5105
                dataTwo.add(null);
 
5106
                data.add(dataTwo);
 
5107
                e.setAttribute("data", data);
 
5108
                String expecting = "Himom,Hidad"; // nulls are skipped
 
5109
                assertEquals(expecting, e.toString());
4722
5110
        }
4723
5111
 
4724
5112
        public void testStripOpOfSingleAlt() throws Exception {
4728
5116
                e = e.getInstanceOf();
4729
5117
                e.setAttribute("data", "hi");
4730
5118
                String expecting = "hi"; // nulls are skipped
4731
 
                assertEqual(e.toString(), expecting);
 
5119
                assertEquals(expecting, e.toString());
4732
5120
        }
4733
5121
 
4734
5122
        public void testStripOpOfNull() throws Exception {
4737
5125
                        );
4738
5126
                e = e.getInstanceOf();
4739
5127
                String expecting = ""; // nulls are skipped
4740
 
                assertEqual(e.toString(), expecting);
 
5128
                assertEquals(expecting, e.toString());
4741
5129
        }
4742
5130
 
4743
5131
        public void testLengthOpOfStrippedListWithNulls() throws Exception {
4752
5140
                data.add(null);
4753
5141
                e.setAttribute("data", data);
4754
5142
                String expecting = "2"; // nulls are counted
4755
 
                assertEqual(e.toString(), expecting);
 
5143
                assertEquals(expecting, e.toString());
4756
5144
        }
4757
5145
 
4758
5146
        public void testLengthOpOfStrippedListWithNullsFrontAndBack() throws Exception {
4774
5162
                data.add(null);
4775
5163
                e.setAttribute("data", data);
4776
5164
                String expecting = "2"; // nulls are counted
4777
 
                assertEqual(e.toString(), expecting);
 
5165
                assertEquals(expecting, e.toString());
4778
5166
        }
4779
5167
 
4780
5168
        public void testMapKeys() throws Exception {
4787
5175
                map.put("int","0");
4788
5176
                map.put("float","0.0");
4789
5177
                t.setAttribute("aMap", map);
4790
 
                assertEqual(t.toString(), "int:0, float:0.0");
 
5178
                assertEquals("int:0, float:0.0",t.toString());
4791
5179
        }
4792
5180
 
4793
5181
        public void testMapValues() throws Exception {
4800
5188
                map.put("int","0");
4801
5189
                map.put("float","0.0");
4802
5190
                t.setAttribute("aMap", map);
4803
 
                assertEqual(t.toString(), "0, 0.0");
 
5191
                assertEquals("0, 0.0", t.toString());
4804
5192
        }
4805
5193
 
4806
5194
        /** Use when super.attr name is implemented
4813
5201
                main.setAttribute("name", "tombu");
4814
5202
                StringTemplate foo = group.defineTemplate("foo", "$t$");
4815
5203
                String expecting="Hi, parrt";
4816
 
                assertEqual(main.toString(), expecting);
 
5204
                assertEquals(expecting, main.toString());
4817
5205
        }
4818
5206
         */
4819
5207
 
 
5208
    /**
 
5209
     * Check what happens when a semicolon is  appended to a single line template
 
5210
     * Should fail with a parse error(?) and not a missing template error.
 
5211
     * FIXME: This should generate a warning or error about that semi colon.
 
5212
     * <p>
 
5213
     * Bug ref: JIRA bug ST-2
 
5214
     */
 
5215
    public void testGroupTrailingSemiColon() throws Exception {
 
5216
        //try {
 
5217
            String templates =
 
5218
                    "group test;" +newline+
 
5219
                    "t1()::=\"R1\"; "+newline+
 
5220
                    "t2() ::= \"R2\""+newline                
 
5221
                    ;
 
5222
            StringTemplateGroup group =
 
5223
                    new StringTemplateGroup(new StringReader(templates));
 
5224
            
 
5225
            StringTemplate st = group.getInstanceOf("t1");
 
5226
            assertEquals("R1", st.toString());
 
5227
            
 
5228
            st = group.getInstanceOf("t2");
 
5229
            assertEquals("R2", st.toString());
 
5230
            
 
5231
            fail("A parse error should have been generated");
 
5232
        //} catch (ParseError??) {            
 
5233
        //}
 
5234
    }
 
5235
 
 
5236
        public void testSuperReferenceInIfClause() throws Exception {
 
5237
                String superGroupString =
 
5238
                        "group super;" + newline +
 
5239
                        "a(x) ::= \"super.a\"" + newline +
 
5240
                        "b(x) ::= \"<c()>super.b\"" + newline +
 
5241
                        "c() ::= \"super.c\""
 
5242
                        ;
 
5243
                StringTemplateGroup superGroup = new StringTemplateGroup(
 
5244
                        new StringReader(superGroupString), AngleBracketTemplateLexer.class);
 
5245
                String subGroupString =
 
5246
                        "group sub;\n" +
 
5247
                        "a(x) ::= \"<if(x)><super.a()><endif>\"" + newline +
 
5248
                        "b(x) ::= \"<if(x)><else><super.b()><endif>\"" + newline +
 
5249
                        "c() ::= \"sub.c\""
 
5250
                        ;
 
5251
                StringTemplateGroup subGroup = new StringTemplateGroup(
 
5252
                        new StringReader(subGroupString), AngleBracketTemplateLexer.class);
 
5253
                subGroup.setSuperGroup(superGroup);
 
5254
                StringTemplate a = subGroup.getInstanceOf("a");
 
5255
                a.setAttribute("x", "foo");
 
5256
                assertEquals("super.a", a.toString());
 
5257
                StringTemplate b = subGroup.getInstanceOf("b");
 
5258
                assertEquals("sub.csuper.b", b.toString());
 
5259
                StringTemplate c = subGroup.getInstanceOf("c");
 
5260
                assertEquals("sub.c", c.toString());
 
5261
        }
 
5262
 
 
5263
        /** Added feature for ST-21 */
 
5264
        public void testListLiteralWithEmptyElements() throws Exception {
 
5265
                StringTemplate e = new StringTemplate(
 
5266
                                "$[\"Ter\",,\"Jesse\"]:{n | $i$:$n$}; separator=\", \", null=\"\"$"
 
5267
                        );
 
5268
                e = e.getInstanceOf();
 
5269
                e.setAttribute("names", "Ter");
 
5270
                e.setAttribute("phones", "1");
 
5271
                e.setAttribute("salaries", "big");
 
5272
                String expecting = "1:Ter, 2:, 3:Jesse";
 
5273
                assertEquals(expecting, e.toString());
 
5274
        }
 
5275
 
4820
5276
        public static void writeFile(String dir, String fileName, String content) {
4821
5277
                try {
4822
5278
                        File f = new File(dir, fileName);