[stringtemplate-interest] Problem with newline chars
stefan
stefan at amiq.ro
Thu Sep 22 09:11:15 PDT 2005
Hello,
I haven't run the test yet, but my verification experience tells me that
you can do some string filtering before compare the two strings. The ideea is
to bring the two strings to common acceptable form, as long as it doesn't
affects the test.
There are noises that can be ignored like the newline character or number
of new lines, number of spaces etc, as long as you DO NOT TEST THEM.
For example the assertEquals method would be applied for filtered
...........................
assertEquals(SomeFilter.filterNewLine(expected)
,SomeFilter.filterNewLine(actual));
...........................
And the class would look like :
...............................
public class SomeFilter {
public static String filterNewLine(String input) {
// code to replace newlines with some other character or a LF
// eventually code to reduce *spaces to 1 space if you need
such reduction
return res;
}
}
...............................
A similar filtering is implemented in http://sourceforge.net/projects/fasper.
I hope it helps,
Stefan.
On Thursday 22 September 2005 18:38, Andrew Goodnough wrote:
> First of all, let me say that I am able to get my desired output with
> StringTemplate so I'm basically happy, but...(there's always a but)...
>
> I'm unable to create a test which verifies my output in an automated way.
> The test is included below along with the template files. The problem
> comes in the testDouble() method where I am creating two "userTypes". The
> expected and actual string output is visually identical but the test fails
> due to a difference in newline characters. It appears that StringTemplate
> is returning an ASCII '10' which is CR, while my test is expecting a '13'
> or LF just after the first output of the printUserType template.
>
> Any ideas? Maybe there's a simpler way to accomplish what I want too, I
> don't know.
>
> Andy
>
> ==test.st=====
> <userTypes; separator="\n\n">
>
> GO
>
> ==end test.st==
>
> ==printUserType.st=====
> --USERTYPE <userType>
> EXEC sp_addtype '<userType>' char(5)
> ==end printUserType.st==
>
> ==Test.java==
> import java.io.IOException;
> import java.io.StringReader;
>
> import junit.framework.TestCase;
>
> import org.antlr.stringtemplate.StringTemplate;
> import org.antlr.stringtemplate.StringTemplateGroup;
> import org.antlr.stringtemplate.language.AngleBracketTemplateLexer;
>
> public class Test extends TestCase {
>
> private static final String NEWLINE =
> System.getProperty("line.separator"); //private static final String NEWLINE
> = "\n";
>
>
> protected void setUp() throws Exception {
> super.setUp();
> }
>
> protected void tearDown() throws Exception {
> super.tearDown();
> }
>
> /*
> * Test method testSingle
> */
> public void testSingle() throws Exception {
> StringTemplateGroup templates = new StringTemplateGroup("myGroup",
> ".", AngleBracketTemplateLexer.class); StringTemplate fileTpl =
> templates.getInstanceOf("test");
>
> StringTemplate userTypeTpl =
> templates.getInstanceOf("printUserType");
> userTypeTpl.setAttribute("userType", "NewTypeT");
> fileTpl.setAttribute("userTypes", userTypeTpl);
>
> String expected =
> "--USERTYPE NewTypeT" + NEWLINE +
> "EXEC sp_addtype 'NewTypeT' char(5)" + NEWLINE +
> NEWLINE +
> "GO";
>
> String actual = fileTpl.toString();
>
> assertEquals(expected, actual);
> }
>
> /*
> * Test method testDouble
> */
> public void testDouble() throws Exception {
> StringTemplateGroup templates = new StringTemplateGroup("myGroup",
> ".", AngleBracketTemplateLexer.class); StringTemplate fileTpl =
> templates.getInstanceOf("test");
>
> StringTemplate userTypeTpl =
> templates.getInstanceOf("printUserType");
> userTypeTpl.setAttribute("userType", "NewTypeT");
> fileTpl.setAttribute("userTypes", userTypeTpl);
>
> userTypeTpl = templates.getInstanceOf("printUserType");
> userTypeTpl.setAttribute("userType", "AnotherTypeT");
> fileTpl.setAttribute("userTypes", userTypeTpl);
>
> String expected =
> "--USERTYPE NewTypeT" + NEWLINE +
> "EXEC sp_addtype 'NewTypeT' char(5)" + NEWLINE +
> NEWLINE +
> "--USERTYPE AnotherTypeT" + NEWLINE +
> "EXEC sp_addtype 'AnotherTypeT' char(5)" + NEWLINE +
> NEWLINE +
> "GO";
>
> String actual = fileTpl.toString();
>
> /* Shows that newline is the culprit
> char[] expectedchars = expected.toCharArray();
> char[] actualchars = actual.toCharArray();
> for (int i = 0; i < expectedchars.length; i++) {
> System.out.println("Comparing " + expectedchars[i] + " to " +
> actualchars[i]); assertEquals(expectedchars[i],actualchars[i]);
> }
> */
>
> /* Shows ASCII 13 vs. ASCII 10
> StringReader expectedReader = new StringReader(expected);
> StringReader actualReader = new StringReader(actual);
> int exp = 0;
> int act = 0;
> while ((exp = expectedReader.read()) != -1) {
> act = actualReader.read();
> System.out.println("Comparing " + exp + " to " + act);
> assertEquals(exp,act);
> }
> */
>
> assertEquals(expected, actual);
> }
>
> }
> ==End Test.java==
More information about the stringtemplate-interest
mailing list