From fd9d707d95c667b226732d7fde59966271eb8d12 Mon Sep 17 00:00:00 2001 From: ericvergnaud Date: Sat, 7 Jun 2014 00:52:39 +0800 Subject: [PATCH] tests granularity --- .../antlr/v4/test/TestFullContextParsing.java | 20 ++- .../test/org/antlr/v4/test/TestLexerExec.java | 34 ++++- .../org/antlr/v4/test/TestParserExec.java | 127 ++++++++++-------- 3 files changed, 112 insertions(+), 69 deletions(-) diff --git a/tool/test/org/antlr/v4/test/TestFullContextParsing.java b/tool/test/org/antlr/v4/test/TestFullContextParsing.java index 2e4d1b093..75dae171e 100644 --- a/tool/test/org/antlr/v4/test/TestFullContextParsing.java +++ b/tool/test/org/antlr/v4/test/TestFullContextParsing.java @@ -62,7 +62,7 @@ public class TestFullContextParsing extends BaseTest { this.stderrDuringParse); } - @Test public void testCtxSensitiveDFA() { + public String testCtxSensitiveDFA(String input) { String grammar = "grammar T;\n"+ "s @after {dumpDFA();}\n" + @@ -73,8 +73,12 @@ public class TestFullContextParsing extends BaseTest { "ID : 'a'..'z'+ ;\n"+ "INT : '0'..'9'+ ;\n"+ "WS : (' '|'\\t'|'\\n')+ -> skip ;\n"; - String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", - "$ 34 abc", true); + return execParser("T.g4", grammar, "TParser", "TLexer", "s", input, true); + } + + @Test + public void testCtxSensitiveDFA1() { + String result = testCtxSensitiveDFA("$ 34 abc"); String expecting = "Decision 1:\n" + "s0-INT->s1\n" + @@ -83,10 +87,12 @@ public class TestFullContextParsing extends BaseTest { assertEquals("line 1:5 reportAttemptingFullContext d=1 (e), input='34abc'\n" + "line 1:2 reportContextSensitivity d=1 (e), input='34'\n", this.stderrDuringParse); - - result = execParser("T.g4", grammar, "TParser", "TLexer", "s", - "@ 34 abc", true); - expecting = + } + + @Test + public void testCtxSensitiveDFA2() { + String result = testCtxSensitiveDFA("@ 34 abc"); + String expecting = "Decision 1:\n" + "s0-INT->s1\n" + "s1-ID->:s2^=>1\n"; diff --git a/tool/test/org/antlr/v4/test/TestLexerExec.java b/tool/test/org/antlr/v4/test/TestLexerExec.java index 0bc17c38e..5dccd3433 100644 --- a/tool/test/org/antlr/v4/test/TestLexerExec.java +++ b/tool/test/org/antlr/v4/test/TestLexerExec.java @@ -224,7 +224,8 @@ public class TestLexerExec extends BaseTest { assertNull(stderrDuringParse); } - @Test public void testRecursiveLexerRuleRefWithWildcardStar() throws Exception { + @Test + public void testRecursiveLexerRuleRefWithWildcardStar1() throws Exception { String grammar = "lexer grammar L;\n"+ "CMT : '/*' (CMT | .)*? '*/' ;\n" + @@ -245,14 +246,24 @@ public class TestLexerExec extends BaseTest { "/* /*nested*/ */\n"); assertEquals(expecting, found); assertNull(stderrDuringParse); + } + + @Test + public void testRecursiveLexerRuleRefWithWildcardStar2() throws Exception { + String grammar = + "lexer grammar L;\n"+ + "CMT : '/*' (CMT | .)*? '*/' ;\n" + + "WS : (' '|'\\n')+ ;\n" + /*+ "ANY : .;"*/; + // stuff on end of comment doesn't match another rule - expecting = + String expecting = "[@0,0:8='/* ick */',<1>,1:0]\n" + "[@1,10:10='\\n',<2>,1:10]\n" + "[@2,11:36='/* /* */x\\n/* /*nested*/ */',<1>,2:0]\n" + "[@3,38:38='\\n',<2>,3:17]\n" + "[@4,39:38='',<-1>,4:18]\n"; - found = execLexer("L.g4", grammar, "L", + String found = execLexer("L.g4", grammar, "L", "/* ick */x\n" + "/* /* */x\n" + "/* /*nested*/ */x\n"); @@ -262,7 +273,8 @@ public class TestLexerExec extends BaseTest { "line 3:16 token recognition error at: 'x'\n", stderrDuringParse); } - @Test public void testRecursiveLexerRuleRefWithWildcardPlus() throws Exception { + @Test + public void testRecursiveLexerRuleRefWithWildcardPlus1() throws Exception { String grammar = "lexer grammar L;\n"+ "CMT : '/*' (CMT | .)+? '*/' ;\n" + @@ -283,14 +295,24 @@ public class TestLexerExec extends BaseTest { "/* /*nested*/ */\n"); assertEquals(expecting, found); assertNull(stderrDuringParse); + } + + @Test + public void testRecursiveLexerRuleRefWithWildcardPlus2() throws Exception { + String grammar = + "lexer grammar L;\n"+ + "CMT : '/*' (CMT | .)+? '*/' ;\n" + + "WS : (' '|'\\n')+ ;\n" + /*+ "ANY : .;"*/; + // stuff on end of comment doesn't match another rule - expecting = + String expecting = "[@0,0:8='/* ick */',<1>,1:0]\n" + "[@1,10:10='\\n',<2>,1:10]\n" + "[@2,11:36='/* /* */x\\n/* /*nested*/ */',<1>,2:0]\n" + "[@3,38:38='\\n',<2>,3:17]\n" + "[@4,39:38='',<-1>,4:18]\n"; - found = execLexer("L.g4", grammar, "L", + String found = execLexer("L.g4", grammar, "L", "/* ick */x\n" + "/* /* */x\n" + "/* /*nested*/ */x\n"); diff --git a/tool/test/org/antlr/v4/test/TestParserExec.java b/tool/test/org/antlr/v4/test/TestParserExec.java index 8cba297b3..05bff46ed 100644 --- a/tool/test/org/antlr/v4/test/TestParserExec.java +++ b/tool/test/org/antlr/v4/test/TestParserExec.java @@ -165,7 +165,7 @@ public class TestParserExec extends BaseTest { "ID : 'a'..'z'+ ;\n" + "WS : (' '|'\\n') -> channel(HIDDEN);\n"; - @Test public void testIfIfElseGreedyBinding() throws Exception { + @Test public void testIfIfElseGreedyBinding1() throws Exception { final String input = "if y if y x else x"; final String expectedInnerBound = "if y x else x\nif y if y x else x\n"; @@ -173,8 +173,14 @@ public class TestParserExec extends BaseTest { String found = execParser("T.g4", grammar, "TParser", "TLexer", "start", input, false); assertEquals(expectedInnerBound, found); - grammar = String.format(ifIfElseGrammarFormat, "('else' statement|)"); - found = execParser("T.g4", grammar, "TParser", "TLexer", "start", input, false); + } + + @Test public void testIfIfElseGreedyBinding2() throws Exception { + final String input = "if y if y x else x"; + final String expectedInnerBound = "if y x else x\nif y if y x else x\n"; + + String grammar = String.format(ifIfElseGrammarFormat, "('else' statement|)"); + String found = execParser("T.g4", grammar, "TParser", "TLexer", "start", input, false); assertEquals(expectedInnerBound, found); } @@ -191,36 +197,36 @@ public class TestParserExec extends BaseTest { assertEquals(expectedOuterBound, found); } - @Test public void testAStar() throws Exception { - String grammar = - "grammar T;\n" + - "a : ID* {System.out.println($text);} ;\n" + - "ID : 'a'..'z'+ ;\n" + - "WS : (' '|'\\n') -> skip ;\n"; + @Test public void testAStar() throws Exception { + String grammar = + "grammar T;\n" + + "a : ID* {System.out.println($text);} ;\n" + + "ID : 'a'..'z'+ ;\n" + + "WS : (' '|'\\n') -> skip ;\n"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", - "", false); - assertEquals("\n", found); - found = execParser("T.g4", grammar, "TParser", "TLexer", "a", - "a b c", false); - assertEquals("abc\n", found); - } + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", + "", false); + assertEquals("\n", found); + found = execParser("T.g4", grammar, "TParser", "TLexer", "a", + "a b c", false); + assertEquals("abc\n", found); + } - @Test public void testLL1OptionalBlock() throws Exception { - String grammar = - "grammar T;\n" + - "a : (ID|{}INT)? {System.out.println($text);} ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+ ;\n" + - "WS : (' '|'\\n') -> skip ;\n"; + @Test public void testLL1OptionalBlock() throws Exception { + String grammar = + "grammar T;\n" + + "a : (ID|{}INT)? {System.out.println($text);} ;\n" + + "ID : 'a'..'z'+ ;\n" + + "INT : '0'..'9'+ ;\n" + + "WS : (' '|'\\n') -> skip ;\n"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", - "", false); - assertEquals("\n", found); - found = execParser("T.g4", grammar, "TParser", "TLexer", "a", - "a", false); - assertEquals("a\n", found); - } + String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", + "", false); + assertEquals("\n", found); + found = execParser("T.g4", grammar, "TParser", "TLexer", "a", + "a", false); + assertEquals("a\n", found); + } // force complex decision @Test public void testAorAStar() throws Exception { @@ -273,7 +279,7 @@ public class TestParserExec extends BaseTest { * https://github.com/antlr/antlr4/issues/41 */ @Test - public void testOptional() throws Exception { + public void testOptional1() throws Exception { String grammar = "grammar T;\n" + "stat : ifstat | 'x';\n" + @@ -284,16 +290,46 @@ public class TestParserExec extends BaseTest { String found = execParser("T.g4", grammar, "TParser", "TLexer", "stat", "x", false); assertEquals("", found); assertNull(this.stderrDuringParse); + } + + @Test + public void testOptional2() throws Exception { + String grammar = + "grammar T;\n" + + "stat : ifstat | 'x';\n" + + "ifstat : 'if' stat ('else' stat)?;\n" + + "WS : [ \\n\\t]+ -> skip ;" + ; - found = execParser("T.g4", grammar, "TParser", "TLexer", "stat", "if x else x", false); + String found = execParser("T.g4", grammar, "TParser", "TLexer", "stat", "if x else x", false); assertEquals("", found); assertNull(this.stderrDuringParse); + } + + @Test + public void testOptional3() throws Exception { + String grammar = + "grammar T;\n" + + "stat : ifstat | 'x';\n" + + "ifstat : 'if' stat ('else' stat)?;\n" + + "WS : [ \\n\\t]+ -> skip ;" + ; - found = execParser("T.g4", grammar, "TParser", "TLexer", "stat", "if x", false); + String found = execParser("T.g4", grammar, "TParser", "TLexer", "stat", "if x", false); assertEquals("", found); assertNull(this.stderrDuringParse); - - found = execParser("T.g4", grammar, "TParser", "TLexer", "stat", "if if x else x", false); + } + + @Test + public void testOptional4() throws Exception { + String grammar = + "grammar T;\n" + + "stat : ifstat | 'x';\n" + + "ifstat : 'if' stat ('else' stat)?;\n" + + "WS : [ \\n\\t]+ -> skip ;" + ; + + String found = execParser("T.g4", grammar, "TParser", "TLexer", "stat", "if if x else x", false); assertEquals("", found); assertNull(this.stderrDuringParse); } @@ -458,25 +494,4 @@ public class TestParserExec extends BaseTest { assertEquals("", found); assertNull(stderrDuringParse); } - - /** - * This is a regression test for antlr/antlr4#561 "Issue with parser - * generation in 4.2.2" - * https://github.com/antlr/antlr4/issues/561 - */ - @Test public void testReferenceToATN() throws Exception { - String grammar = - "grammar T;\n" + - "a : (ID|ATN)* ATN? {System.out.println($text);} ;\n" + - "ID : 'a'..'z'+ ;\n" + - "ATN : '0'..'9'+;\n" + - "WS : (' '|'\\n') -> skip ;\n"; - - String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", - "", false); - assertEquals("\n", found); - found = execParser("T.g4", grammar, "TParser", "TLexer", "a", - "a 34 c", false); - assertEquals("a34c\n", found); - } }