1 package org.apache.velocity.test;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.velocity.runtime.parser.node.ASTTextblock;
23
24
25
26
27 public class TextblockTestCase extends BaseTestCase
28 {
29
30
31 private static final String START = ASTTextblock.START;
32 private static final String END = ASTTextblock.END;
33 private static final String PARTIAL_START = START.substring(0, START.length() - 1);
34 private static final String PARTIAL_END = END.substring(1, END.length());
35 private static final String END_OF_START = START.substring(START.length() - 1, START.length());
36 private static final String START_OF_END = END.substring(0, 1);
37
38 public TextblockTestCase(String name)
39 {
40 super(name);
41
42 }
43
44 public String textblock(String s)
45 {
46 return START + s + END;
47 }
48
49 public void assertTextblockEvalEquals(String s) throws Exception
50 {
51 assertEvalEquals(s, textblock(s));
52 }
53
54
55
56
57 public void testTextblockAjaxcode() throws Exception
58 {
59 String s = "var myId = 'someID';$('#test).append($.template('<div id=\"${myId}\"></div>').apply({myId: myId}));";
60 assertEvalEquals(s + " 123", textblock(s)+" #foreach($i in [1..3])$i#end");
61 }
62
63 public void testLooseTextblockEnd() throws Exception
64 {
65
66
67 assertEvalEquals(END+" ", END+" ");
68 }
69
70 public void testTextblockStartInTextblock() throws Exception
71 {
72 assertTextblockEvalEquals(START);
73 }
74
75 public void testTextblockEndBetweenTwoTextblockHalves() throws Exception
76 {
77
78
79
80
81
82 assertEvalEquals(" "+END+" ", textblock(" ")+END+" "+textblock(" "));
83 }
84
85 public void testZerolengthTextblock() throws Exception
86 {
87 assertTextblockEvalEquals("");
88 }
89
90 public void testTextblockInsideForeachLoop() throws Exception
91 {
92 String s = "var myId = 'someID';$('#test).append($.template('<div id=\"${myId}\"></div>').apply({myId: myId}));";
93 assertEvalEquals("1 "+s+"2 "+s+"3 "+s, "#foreach($i in [1..3])$i "+ textblock(s) + "#end");
94 }
95
96 public void testSingleHashInsideTextblock() throws Exception
97 {
98 assertTextblockEvalEquals(" # ");
99 }
100
101 public void testDollarInsideTextblock() throws Exception
102 {
103 assertTextblockEvalEquals("$");
104 }
105
106 public void testTextblockInsideComment() throws Exception
107 {
108 String s = "FOOBAR";
109 assertEvalEquals("", "#* comment "+textblock(s) + " *#");
110 }
111
112 public void testPartialStartEndTokensInsideTextblock() throws Exception
113 {
114 assertTextblockEvalEquals(PARTIAL_START+"foo"+PARTIAL_END);
115 }
116
117 public void testDupeTokenChars() throws Exception
118 {
119 assertTextblockEvalEquals(END_OF_START+START_OF_END);
120 assertTextblockEvalEquals(END_OF_START+END_OF_START+START_OF_END+START_OF_END);
121 assertTextblockEvalEquals(END_OF_START+END_OF_START+"#"+START_OF_END+START_OF_END);
122 }
123
124
125
126
127 public void testServerSideIncludeEscaping() throws Exception
128 {
129 assertTextblockEvalEquals("<!--#include file=\"wisdom.inc\"--> ");
130 }
131
132
133
134
135 public void testLineCommentInsideTextblock() throws Exception
136 {
137 assertTextblockEvalEquals("##x");
138 }
139
140 }