This commit is contained in:
Terence Parr 2013-09-04 17:25:14 -07:00
parent 4b87f84b99
commit d2f7ba8339
1 changed files with 69 additions and 28 deletions

View File

@ -12,18 +12,59 @@ import static org.junit.Assert.assertTrue;
public class TestParseTreeMatcher extends BaseTest {
@Test public void testChunking() throws Exception {
// tests
ParseTreePatternMatcher p = new ParseTreePatternMatcher();
System.out.println( p.split("<ID> = <expr> ;") );
System.out.println( p.split(" <ID> = <expr>") );
System.out.println( p.split("<ID> = <expr>") );
System.out.println( p.split("<expr>") );
System.out.println(p.split("\\<x\\> foo"));
System.out.println(p.split("foo \\<x\\> bar <tag>"));
// System.out.println( p.split(">expr<") );
assertEquals("[ID, ' = ', expr, ' ;']", p.split("<ID> = <expr> ;").toString());
assertEquals("[' ', ID, ' = ', expr]", p.split(" <ID> = <expr>").toString());
assertEquals("[ID, ' = ', expr]", p.split("<ID> = <expr>").toString());
assertEquals("[expr]", p.split("<expr>").toString());
assertEquals("['<x> foo']", p.split("\\<x\\> foo").toString());
assertEquals("['foo <x> bar ', tag]", p.split("foo \\<x\\> bar <tag>").toString());
}
@Test public void testDelimiters() throws Exception {
ParseTreePatternMatcher p = new ParseTreePatternMatcher();
p.setDelimiters("<<", ">>", "$");
System.out.println(p.split("<<ID>> = <<expr>> ;$<< ick $>>"));
String result = p.split("<<ID>> = <<expr>> ;$<< ick $>>").toString();
assertEquals("[ID, ' = ', expr, ' ;<< ick >>']", result);
}
@Test public void testInvertedTags() throws Exception {
ParseTreePatternMatcher p = new ParseTreePatternMatcher();
String result = null;
try {
p.split(">expr<");
}
catch (IllegalArgumentException iae) {
result = iae.getMessage();
}
String expected = "tag delimiters out of order in pattern: >expr<";
assertEquals(expected, result);
}
@Test public void testUnclosedTag() throws Exception {
ParseTreePatternMatcher p = new ParseTreePatternMatcher();
String result = null;
try {
p.split("<expr hi mom");
}
catch (IllegalArgumentException iae) {
result = iae.getMessage();
}
String expected = "unterminated tag in pattern: <expr hi mom";
assertEquals(expected, result);
}
@Test public void testExtraClose() throws Exception {
ParseTreePatternMatcher p = new ParseTreePatternMatcher();
String result = null;
try {
p.split("<expr> >");
}
catch (IllegalArgumentException iae) {
result = iae.getMessage();
}
String expected = "missing start tag in pattern: <expr> >";
assertEquals(expected, result);
}
@Test public void testTokenizingPattern() throws Exception {
@ -44,7 +85,7 @@ public class TestParseTreeMatcher extends BaseTest {
List<? extends Token> tokens = p.tokenizePattern("<ID> = <expr> ;");
String results = tokens.toString();
String expected = "[ID:3, [@-1,1:1='=',<1>,1:1], expr:1, [@-1,0:0=';',<2>,1:0]]";
String expected = "[ID:3, [@-1,1:1='=',<1>,1:1], expr:1, [@-1,1:1=';',<2>,1:1]]";
assertEquals(expected, results);
}
@ -71,24 +112,6 @@ public class TestParseTreeMatcher extends BaseTest {
assertEquals(expected, results);
}
public void checkPatternMatch(String grammarName, String grammar, String startRule,
String input, String pattern,
String parserName, String lexerName)
throws Exception
{
boolean ok =
rawGenerateAndBuildRecognizer(grammarName, grammar, parserName, lexerName, false);
assertTrue(ok);
ParseTree result = execParser(startRule, input, parserName, lexerName);
ParseTreePatternMatcher p =
new ParseTreePatternMatcher(loadLexerClassFromTempDir(lexerName),
loadParserClassFromTempDir(parserName));
boolean matches = p.matches(result, startRule, pattern);
assertTrue(matches);
}
@Test public void testIDNodeMatches() throws Exception {
String grammar =
"grammar T;\n" +
@ -114,4 +137,22 @@ public class TestParseTreeMatcher extends BaseTest {
String pattern = "<ID> = <expr> ;";
checkPatternMatch("T.g4", grammar, "s", input, pattern, "TParser", "TLexer");
}
public void checkPatternMatch(String grammarName, String grammar, String startRule,
String input, String pattern,
String parserName, String lexerName)
throws Exception
{
boolean ok =
rawGenerateAndBuildRecognizer(grammarName, grammar, parserName, lexerName, false);
assertTrue(ok);
ParseTree result = execParser(startRule, input, parserName, lexerName);
ParseTreePatternMatcher p =
new ParseTreePatternMatcher(loadLexerClassFromTempDir(lexerName),
loadParserClassFromTempDir(parserName));
boolean matches = p.matches(result, startRule, pattern);
assertTrue(matches);
}
}