[stringtemplate-interest] Problem with newline chars

Andrew Goodnough Andrew.Goodnough at wicourts.gov
Thu Sep 22 08:38:25 PDT 2005


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==

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20050922/8e39d880/attachment.html


More information about the stringtemplate-interest mailing list