35
37
import java.text.SimpleDateFormat;
36
38
import java.util.*;
38
/** Test the various functionality of StringTemplate. Seems to run only
39
* on unix due to \r\n vs \n issue. David Scurrah says:
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.
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."
54
public class TestStringTemplate extends TestSuite {
55
final String newline = System.getProperty("line.separator");
57
public void runTests() throws Throwable {
58
TestRig.runAllTests(this.getClass(), this);
60
System.out.println("num obj.prop refs: "+ ASTExpr.totalObjPropRefs);
61
System.out.println("num obj.prop refs: "+ ASTExpr.totalObjPropComputations);
65
public TestStringTemplate() {
40
public class TestStringTemplate extends TestCase {
41
static final String newline = System.getProperty("line.separator");
68
43
static class ErrorBuffer implements StringTemplateErrorListener {
69
44
StringBuffer errorOutput = new StringBuffer(500);
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.
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.
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.
94
public static void assertEquals(String expected, String actual)
96
// Replace all \r\n with \n for consistent use.
98
expected.replaceAll("\r\n", "\n"),
99
actual.replaceAll("\r\n", "\n")
104
expected.replaceAll("\r\n", "\n"),
105
actual.replaceAll("\r\n", "\n")
107
} catch (AssertionFailedError e) {
108
StringBuffer fullText = new StringBuffer();
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)
120
System.err.println(fullText.toString());
99
127
public void testInterfaceFileFormat() throws Exception {
101
129
"interface test;" +newline+
238
266
st.setAttribute("x", "foo");
240
268
String expecting = "*foo*";
241
assertEqual(st.toString(), expecting);
269
assertEquals(expecting, st.toString());
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)
280
"group superG;" +newline+
281
"bold(item) ::= <<*<item>*>>;\n"+newline;
282
writeFile(tmpdir, "superG.stg", superGroup);
285
"group testG : superG;" +newline+
286
"main(x) ::= \"<bold(x)>\""+newline;
288
writeFile(tmpdir, "testG.stg", templates);
290
StringTemplateGroup group =
291
new StringTemplateGroup(new FileReader(tmpdir+"/testG.stg"),
293
StringTemplate st = group.getInstanceOf("main");
294
st.setAttribute("x", "foo");
296
String expecting = "*foo*";
297
assertEquals(expecting, st.toString());
244
300
public void testMissingInterfaceTemplate() 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());
339
395
StringTemplate a = group.getInstanceOf("t");
340
396
expecting = "literal template";
341
assertEqual(a.toString(), expecting);
397
assertEquals(expecting,a.toString());
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());
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());
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());
370
426
StringTemplate a = group.getInstanceOf("t");
371
427
expecting = "literal} template";
372
assertEqual(a.toString(), expecting);
428
assertEquals(expecting,a.toString());
375
431
/** Check syntax and setAttribute-time errors */
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);
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);
691
748
public void testRegionOverride() throws Exception {
934
991
String result = errors.toString();
935
992
String expecting = "missing region r <@end> tag";
936
assertEqual(result, expecting);
993
assertEquals(expecting, result);
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);
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());
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());
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());
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());
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());
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());
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());
1155
1212
public void testMissingEndDelimiter() throws Exception {
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());
1186
1242
public void testNullTemplateApplication() 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;
1219
result = t.toString();
1221
1274
catch (IllegalArgumentException iae) {
1222
1275
error = iae.getMessage();
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);
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());
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());
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);
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());
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());
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());
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,
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);
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);
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());
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());
1366
1420
public void testApplyingTemplateFromDiskWithPrecompiledIF()
1367
1421
throws Exception
1369
String newline = System.getProperty("line.separator");
1423
// Create a temporary working directory
1424
File tmpDir = new File(System.getProperty("java.io.tmpdir"));
1426
int counter = (new Random()).nextInt() & 65535;;
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();
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);
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);
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());
1390
1457
StringTemplate a = group.getInstanceOf("page");
1391
1458
a.setAttribute("member", new Connector());
1445
1515
"\t// end of a body"+newline+
1447
1517
//System.out.println(m);
1448
assertEqual(m.toString(), expecting);
1518
assertEquals(expecting, m.toString());
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());
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());
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());
1477
1547
public void testApplyAnonymousTemplateToSingleValuedAttribute() 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());
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());
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");
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 ");
1620
1692
t = t.getInstanceOf();
1621
1693
t.setAttribute("b", new Boolean(false));
1622
assertEqual(t.toString(), " y");
1694
assertEquals(" y", t.toString());
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 =
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());
1853
1918
public void testEscapesOutsideExpressions() throws Exception {
1869
1934
e.setAttribute("title", "sample");
1870
1935
String expecting = "foo";
1871
assertEqual(e.toString(), expecting);
1936
assertEquals(expecting, e.toString());
1873
1938
e = e.getInstanceOf();
1874
1939
expecting = "bar";
1875
assertEqual(e.toString(), expecting);
1940
assertEquals(expecting, e.toString());
1943
public void testElseIfClause() throws Exception {
1944
StringTemplate e = new StringTemplate(
1947
"$elseif(y)$"+newline +
1951
e.setAttribute("y", "yep");
1952
String expecting = "bar";
1953
assertEquals(expecting, e.toString());
1956
public void testElseIfClause2() throws Exception {
1957
StringTemplate e = new StringTemplate(
1960
"$elseif(y)$"+newline +
1962
"$elseif(z)$"+newline +
1966
e.setAttribute("z", "yep");
1967
String expecting = "blort";
1968
assertEquals(expecting, e.toString());
1971
public void testElseIfClauseAndElse() throws Exception {
1972
StringTemplate e = new StringTemplate(
1975
"$elseif(y)$"+newline +
1977
"$elseif(z)$"+newline +
1983
String expecting = "blort";
1984
assertEquals(expecting, e.toString());
1878
1987
public void testNestedIF() throws Exception {
1879
1988
StringTemplate e = new StringTemplate(
1880
1989
"$if(title)$"+newline +
1890
1999
e.setAttribute("title", "sample");
1891
2000
String expecting = "foo";
1892
assertEqual(e.toString(), expecting);
2001
assertEquals(expecting, e.toString());
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());
1899
2008
e = e.getInstanceOf();
1900
2009
expecting = "blort";
1901
assertEqual(e.toString(), expecting);
2010
assertEquals(expecting, e.toString());
1904
2013
public void testEmbeddedMultiLineIF() throws Exception {
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());
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());
2183
2292
public void testSuperTemplateRef()
2641
2750
String expecting ="Foo "+newline+"\t bar"+newline;
2642
2751
String result = st.toString();
2643
assertEqual(result, expecting);
2752
assertEquals(expecting, result);
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);
2651
st = new StringTemplate(
2652
"Foo$\\ $bar$\\n$");
2653
expecting ="Foo bar"+newline;
2654
result = st.toString();
2655
assertEqual(result, expecting);
2758
assertEquals(expecting, result);
2760
st = new StringTemplate(
2761
"Foo$\\ $bar$\\n$");
2762
expecting ="Foo bar"+newline;
2763
result = st.toString();
2764
assertEquals(expecting, result);
2767
public void testUnicodeLiterals() throws Exception {
2768
StringTemplate st = new StringTemplate(
2769
"Foo <\\uFEA5\\n\\u00C2> bar" +newline,
2770
AngleBracketTemplateLexer.class
2772
String expecting ="Foo \ufea5"+newline+"\u00C2 bar"+newline;
2773
String result = st.toString();
2774
assertEquals(expecting, result);
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);
2782
st = new StringTemplate(
2783
"Foo$\\ $bar$\\n$");
2784
expecting ="Foo bar"+newline;
2785
result = st.toString();
2786
assertEquals(expecting, result);
2658
2790
public void testEmptyIteratedValueGetsSeparator() throws Exception {
2659
2791
StringTemplateGroup group =
3104
3239
SimpleDateFormat f = new SimpleDateFormat ("MM/dd/yyyy");
3105
3240
return f.format(((Calendar)o).getTime());
3242
public String toString(Object o, String formatString) {
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());
3252
public String toString(Object o, String formatString) {
3253
SimpleDateFormat f = new SimpleDateFormat (formatString);
3254
return f.format(((Calendar)o).getTime());
3258
public class StringRenderer implements AttributeRenderer {
3259
public String toString(Object o) {
3262
public String toString(Object o, String formatString) {
3263
if ( formatString.equals("upper") ) {
3264
return ((String)o).toUpperCase();
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);
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);
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);
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);
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();
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);
3121
3335
public void testEmbeddedRendererSeesEnclosing() 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);
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
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);
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
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);
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
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);
3517
* Test that a map can have only the default entry.
3519
* Bug ref: JIRA bug ST-15 (Fixed)
3521
public void testMapDefaultStringAsKey() throws Exception {
3523
"group test;" +newline+
3524
"typeInit ::= [\"default\":\"foo\"] "+newline+
3525
"var(type,name) ::= \"<type> <name> = <typeInit.(type)>;\""+newline
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);
3538
* Test that a map can return a <b>string</b> with the word: default.
3540
* Bug ref: JIRA bug ST-15 (Fixed)
3542
public void testMapDefaultIsDefaultString() throws Exception {
3544
"group test;" +newline+
3545
"map ::= [default: \"default\"] "+newline+
3546
"t1() ::= \"<map.(1)>\""+newline
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);
3302
3556
public void testMapViaEnclosingTemplates() throws Exception {
3303
3557
String templates =
3304
3558
"group test;" +newline+
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);
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.
3380
3639
public void test8BitEuroChars() throws Exception {
3381
3640
StringTemplate e = new StringTemplate(
3384
e = e.getInstanceOf();
3385
String expecting = "Danish: � char";
3386
assertEqual(e.toString(), expecting);
3641
"Danish: \u0143 char"
3643
e = e.getInstanceOf();
3644
String expecting = "Danish: \u0143 char";
3645
assertEquals(expecting, e.toString());
3648
public void test16BitUnicodeChar() throws Exception {
3649
StringTemplate e = new StringTemplate(
3650
"DINGBAT CIRCLED SANS-SERIF DIGIT ONE: \u2780"
3652
e = e.getInstanceOf();
3653
String expecting = "DINGBAT CIRCLED SANS-SERIF DIGIT ONE: \u2780";
3654
assertEquals(expecting, e.toString());
3389
3657
public void testFirstOp() 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());
3912
public void testMultipleRefsToListAttribute() throws Exception {
3914
"group test;" +newline+
3915
"f(x) ::= \"<x> <x>\""+newline
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());
3644
3926
public void testApplyTemplateWithSingleFormalArgs() 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());
4012
public void testFirstWithListOfMaps() throws Exception {
4013
StringTemplate e = new StringTemplate(
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());
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());
4033
public void testFirstWithListOfMaps2() throws Exception {
4034
StringTemplate e = new StringTemplate(
4035
"$first(maps):{ $it.Ter$ }$"
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());
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());
3730
4054
public void testJustCat() 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());
4231
public void testParallelAttributeIterationWithNullValue() throws Exception {
4232
StringTemplate e = new StringTemplate(
4233
"$names,phones,salaries:{n,p,s | $n$@$p$: $s$\n}$"
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());
3907
4249
public void testParallelAttributeIterationHasI() 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());
3967
4309
public void testParallelAttributeIterationWithMissingArgs() 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());
4134
4472
public void testNoDotsInAttributeNames() throws Exception {
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));
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));
4203
4541
public void testFortranLineWrap() throws Exception {
4233
4571
" 1,6,32,5,6,77,4,9,\n" +
4234
4572
" 20,2,1,4,63,9,20,2,\n" +
4236
assertEqual(a.toString(30), expecting);
4574
assertEquals(expecting, a.toString(30));
4239
4577
public void testLineWrapEdgeCase() 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" +
4484
assertEqual(top.toString(40), expecting);
4822
assertEquals(expecting, top.toString(40));
4487
4825
public void testEscapeEscape() 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());
5002
public void testLengthOpWithMap() throws Exception {
5003
StringTemplate e = new StringTemplate(
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());
5016
public void testLengthOpWithSet() throws Exception {
5017
StringTemplate e = new StringTemplate(
5020
e = e.getInstanceOf();
5021
Set m = new HashSet();
5025
e.setAttribute("names", m);
5026
String expecting = "3";
5027
assertEquals(expecting, e.toString());
4664
5030
public void testLengthOpNull() 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());
5090
public void testStripOpOfListOfListsWithNulls() throws Exception {
5091
StringTemplate e = new StringTemplate(
5092
"$strip(data):{list | $strip(list)$}; separator=\",\"$"
5094
e = e.getInstanceOf();
5095
List data = new ArrayList();
5096
List dataOne = new ArrayList();
5101
List dataTwo = new ArrayList();
5107
e.setAttribute("data", data);
5108
String expecting = "Himom,Hidad"; // nulls are skipped
5109
assertEquals(expecting, e.toString());
4724
5112
public void testStripOpOfSingleAlt() throws Exception {
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());
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.
5213
* Bug ref: JIRA bug ST-2
5215
public void testGroupTrailingSemiColon() throws Exception {
5218
"group test;" +newline+
5219
"t1()::=\"R1\"; "+newline+
5220
"t2() ::= \"R2\""+newline
5222
StringTemplateGroup group =
5223
new StringTemplateGroup(new StringReader(templates));
5225
StringTemplate st = group.getInstanceOf("t1");
5226
assertEquals("R1", st.toString());
5228
st = group.getInstanceOf("t2");
5229
assertEquals("R2", st.toString());
5231
fail("A parse error should have been generated");
5232
//} catch (ParseError??) {
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\""
5243
StringTemplateGroup superGroup = new StringTemplateGroup(
5244
new StringReader(superGroupString), AngleBracketTemplateLexer.class);
5245
String subGroupString =
5247
"a(x) ::= \"<if(x)><super.a()><endif>\"" + newline +
5248
"b(x) ::= \"<if(x)><else><super.b()><endif>\"" + newline +
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());
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=\"\"$"
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());
4820
5276
public static void writeFile(String dir, String fileName, String content) {
4822
5278
File f = new File(dir, fileName);