diff --git a/tool/test/org/antlr/v4/test/TestParseTreeMatcher.java b/tool/test/org/antlr/v4/test/TestParseTreeMatcher.java index dbb1a8dcc..cb92c8c4b 100644 --- a/tool/test/org/antlr/v4/test/TestParseTreeMatcher.java +++ b/tool/test/org/antlr/v4/test/TestParseTreeMatcher.java @@ -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(" = ;") ); - System.out.println( p.split(" = ") ); - System.out.println( p.split(" = ") ); - System.out.println( p.split("") ); - System.out.println(p.split("\\ foo")); - System.out.println(p.split("foo \\ bar ")); -// System.out.println( p.split(">expr<") ); + assertEquals("[ID, ' = ', expr, ' ;']", p.split(" = ;").toString()); + assertEquals("[' ', ID, ' = ', expr]", p.split(" = ").toString()); + assertEquals("[ID, ' = ', expr]", p.split(" = ").toString()); + assertEquals("[expr]", p.split("").toString()); + assertEquals("[' foo']", p.split("\\ foo").toString()); + assertEquals("['foo bar ', tag]", p.split("foo \\ bar ").toString()); + } + @Test public void testDelimiters() throws Exception { + ParseTreePatternMatcher p = new ParseTreePatternMatcher(); p.setDelimiters("<<", ">>", "$"); - System.out.println(p.split("<> = <> ;$<< ick $>>")); + String result = p.split("<> = <> ;$<< 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(" >"); + } + catch (IllegalArgumentException iae) { + result = iae.getMessage(); + } + String expected = "missing start tag in pattern: >"; + assertEquals(expected, result); } @Test public void testTokenizingPattern() throws Exception { @@ -44,7 +85,7 @@ public class TestParseTreeMatcher extends BaseTest { List tokens = p.tokenizePattern(" = ;"); 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 = " = ;"; 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); + } }