Merge pull request #611 from ericvergnaud/Improved-tests-granularity

Improved tests granularity
This commit is contained in:
Terence Parr 2014-06-06 15:09:56 -07:00
commit 0460e98bc9
3 changed files with 85 additions and 21 deletions

View File

@ -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";

View File

@ -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='<EOF>',<-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='<EOF>',<-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");

View File

@ -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);
}
@ -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);
}