[stringtemplate-interest] unit tests for line wrapping
Terence Parr
parrt at cs.usfca.edu
Sun May 28 14:15:37 PDT 2006
Howdy, the following line wrap unit tests "work". Can all interested
parties, please examine them to see if the functionality matches your
expectations?
I line up wrapped lines derived from expressions to the start of the
expression. For template literal chunks, they just wrap back to char
position 0.
The last testLineWrapInNestedExpr test is hard to think about, but is
pretty groovy. It treats each array as a single attribute value so
that the outer template doesn't do the indent. This is proper I
believe. Once the first array is dumped, the column is 0 and so the
second array has more elements per line. Far out.
This implementation complicated StringTemplateWriter interface and
auto-indent writer, but most people will never see this stuff.
Should be no speed hit. I also switched from a stack to an arraylist
for the stack of indentation given my experience with Stack
recently. Might even go faster!
Ok, where was I? Oh, right. ANTLR v3 DFA state machine table
generation for which I need auto-indent. Gotta love parallel, demand-
driven development. Off to the pool to exercise...
I will wait to hear feedback before pushing all of this...
Thanks!
Ter
----------------
public void testLineWrap() throws Exception {
String templates =
"group test;" +newline+
"array(values) ::= <<int[] a = { <values; separator=\",
\"> };>>"+new
StringTemplateGroup group =
new StringTemplateGroup(new StringReader(templates));
StringTemplate a = group.getInstanceOf("array");
a.setAttribute("values",
new int[]
{3,9,20,2,1,4,6,32,5,6,77,888,2,1,6,32,5,6,77,
4,9,20,2,1,4,63,9,20,2,1,4,6,32,5,6,77,6,32,5,6,77,
3,9,20,2,1,4,6,32,5,6,77,888,1,6,32,5});
String expecting =
"int[] a = { 3,9,20,2,1,4,6,32,5,6,77,888,\n" +
" 2,1,6,32,5,6,77,4,9,20,2,1,4,\n" +
" 63,9,20,2,1,4,6,32,5,6,77,6,\n" +
" 32,5,6,77,3,9,20,2,1,4,6,32,\n" +
" 5,6,77,888,1,6,32,5 };";
assertEqual(a.toString(40), expecting);
}
public void testLineWrapEdgeCase() throws Exception {
String templates =
"group test;" +newline+
"duh(chars) ::= \"<chars>\""+newline;
StringTemplateGroup group =
new StringTemplateGroup(new StringReader(templates));
StringTemplate a = group.getInstanceOf("duh");
a.setAttribute("chars", new String[] {"a","b","c","d","e"});
// lineWidth==3 implies that we can have 3 characters at most
String expecting =
"abc\n"+
"de";
assertEqual(a.toString(3), expecting);
}
public void testLineWrapLastCharIsNewline() throws Exception {
String templates =
"group test;" +newline+
"duh(chars) ::= \"<chars>\""+newline;
StringTemplateGroup group =
new StringTemplateGroup(new StringReader(templates));
StringTemplate a = group.getInstanceOf("duh");
a.setAttribute("chars", new String[] {"a","b","\n","d","e"});
// don't do \n if it's last element anyway
String expecting =
"ab\n"+
"de";
assertEqual(a.toString(3), expecting);
}
public void testLineWrapCharAfterWrapIsNewline() throws Exception {
String templates =
"group test;" +newline+
"duh(chars) ::= \"<chars>\""+newline;
StringTemplateGroup group =
new StringTemplateGroup(new StringReader(templates));
StringTemplate a = group.getInstanceOf("duh");
a.setAttribute("chars", new String[] {"a","b","c","\n","d","e"});
// Once we wrap, we must dump chars as we see them. A newline
right
// after a wrap is just an "unfortunate" event. People will expect
// a newline if it's in the data.
String expecting =
"abc\n" +
"\n" +
"de";
assertEqual(a.toString(3), expecting);
}
public void testLineWrapDueToLiteral() throws Exception {
String templates =
"group test;" +newline+
"m(args,body) ::= <<public void foo(<args; separator=\",
\">) throws
StringTemplateGroup group =
new StringTemplateGroup(new StringReader(templates));
StringTemplate a = group.getInstanceOf("m");
a.setAttribute("args",
new String[] {"a", "b", "c"});
a.setAttribute("body", "i=3;");
// make it wrap because of ") throws Ick { " literal
int n = "public void foo(a, b, c".length();
String expecting =
"public void foo(a, b, c\n" +
") throws Ick { i=3; }";
assertEqual(a.toString(n), expecting);
}
public void testLineWrapInNestedExpr() throws Exception {
String templates =
"group test;" +newline+
"top(arrays) ::= <<Arrays: <arrays>done>>"+newline+
"array(values) ::= <<int[] a = { <values; separator=\",
\"> };<\\n\\>
StringTemplateGroup group =
new StringTemplateGroup(new StringReader(templates));
StringTemplate top = group.getInstanceOf("top");
StringTemplate a = group.getInstanceOf("array");
a.setAttribute("values",
new int[]
{3,9,20,2,1,4,6,32,5,6,77,888,2,1,6,32,5,6,77,
4,9,20,2,1,4,63,9,20,2,1,4,6,32,5,6,77,6,32,5,6,77,
3,9,20,2,1,4,6,32,5,6,77,888,1,6,32,5});
top.setAttribute("arrays", a);
top.setAttribute("arrays", a); // add twice
String expecting =
"Arrays: int[] a = { 3,9,20,2,1,4,6,32,5,\n" +
" 6,77,888,2,1,6,32,5,\n" +
" 6,77,4,9,20,2,1,4,63,\n" +
" 9,20,2,1,4,6,32,5,6,\n" +
" 77,6,32,5,6,77,3,9,20,\n" +
" 2,1,4,6,32,5,6,77,888,\n" +
" 1,6,32,5 };\n" +
"int[] a = { 3,9,20,2,1,4,6,32,5,6,77,888,\n" +
" 2,1,6,32,5,6,77,4,9,20,2,1,4,\n" +
" 63,9,20,2,1,4,6,32,5,6,77,6,\n" +
" 32,5,6,77,3,9,20,2,1,4,6,32,\n" +
" 5,6,77,888,1,6,32,5 };\n" +
"done";
assertEqual(top.toString(40), expecting);
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20060528/aaf5558b/attachment-0001.html
More information about the stringtemplate-interest
mailing list