From 9ce5efc570129ad4d9e5b9fbd41fbe539fe70453 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Sun, 25 Jan 2015 21:42:24 -0600 Subject: [PATCH] TestListeners for Java is working --- .../antlr4/runtime/test/templates/Index.stg | 1 + .../test/templates/Listeners/Basic.stg | 26 +- .../test/templates/Listeners/Index.stg | 12 + .../runtime/test/templates/Listeners/LR.stg | 27 +++ .../test/templates/Listeners/LRWithLabels.stg | 26 ++ .../test/templates/Listeners/RuleGetters.stg | 14 ++ .../templates/Listeners/RuleGetters_1.stg | 10 + .../templates/Listeners/RuleGetters_2.stg | 10 + .../test/templates/Listeners/TokenGetters.stg | 14 ++ .../templates/Listeners/TokenGetters_1.stg | 10 + .../templates/Listeners/TokenGetters_2.stg | 10 + .../org/antlr/v4/test/rt/gen/Generator.java | 38 --- .../antlr/v4/test/rt/java/TestListeners.java | 228 ------------------ 13 files changed, 159 insertions(+), 267 deletions(-) rename tool/test/org/antlr/v4/test/rt/gen/grammars/Listeners/Basic.st => antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/Basic.stg (54%) create mode 100644 antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/Index.stg rename tool/test/org/antlr/v4/test/rt/gen/grammars/Listeners/LR.st => antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/LR.stg (55%) rename tool/test/org/antlr/v4/test/rt/gen/grammars/Listeners/LRWithLabels.st => antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/LRWithLabels.stg (58%) rename tool/test/org/antlr/v4/test/rt/gen/grammars/Listeners/RuleGetters.st => antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/RuleGetters.stg (71%) create mode 100644 antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/RuleGetters_1.stg create mode 100644 antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/RuleGetters_2.stg rename tool/test/org/antlr/v4/test/rt/gen/grammars/Listeners/TokenGetters.st => antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/TokenGetters.stg (68%) create mode 100644 antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/TokenGetters_1.stg create mode 100644 antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/TokenGetters_2.stg delete mode 100644 tool/test/org/antlr/v4/test/rt/java/TestListeners.java diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Index.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Index.stg index ae5518f62..56baedd70 100644 --- a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Index.stg +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Index.stg @@ -1,6 +1,7 @@ TestFolders ::= [ "CompositeLexers": [], "LexerExec": [], + "Listeners": [], "ParserErrors": [], "ParseTrees": [], "SemPredEvalLexer": [], diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/Listeners/Basic.st b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/Basic.stg similarity index 54% rename from tool/test/org/antlr/v4/test/rt/gen/grammars/Listeners/Basic.st rename to antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/Basic.stg index 589fbd7f7..5016926c8 100644 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/Listeners/Basic.st +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/Basic.stg @@ -1,3 +1,26 @@ +TestType() ::= "Parser" + +Options ::= [ + "Debug": false +] + +Grammar ::= [ + "T": {} +] + +Input() ::= "1 2" + +Rule() ::= "s" + +Output() ::= << +(a 1 2) +1 +2<\n> +>> + +Errors() ::= "" + +grammar(grammarName) ::= << grammar ; @parser::header { @@ -20,4 +43,5 @@ MULT: '*' ; ADD : '+' ; INT : [0-9]+ ; ID : [a-z]+ ; -WS : [ \t\n]+ -> skip ; \ No newline at end of file +WS : [ \t\n]+ -> skip ; +>> diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/Index.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/Index.stg new file mode 100644 index 000000000..ebaed53d8 --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/Index.stg @@ -0,0 +1,12 @@ +//TestFolders ::= [ +//] + +TestTemplates ::= [ + "Basic": [], + "TokenGetters_1": [], + "TokenGetters_2": [], + "RuleGetters_1": [], + "RuleGetters_2": [], + "LR": [], + "LRWithLabels": [] +] diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/Listeners/LR.st b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/LR.stg similarity index 55% rename from tool/test/org/antlr/v4/test/rt/gen/grammars/Listeners/LR.st rename to antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/LR.stg index 03bec4568..ee877709d 100644 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/Listeners/LR.st +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/LR.stg @@ -1,3 +1,29 @@ +TestType() ::= "Parser" + +Options ::= [ + "Debug": false +] + +Grammar ::= [ + "T": {} +] + +Input() ::= "1+2*3" + +Rule() ::= "s" + +Output() ::= << +(e (e 1) + (e (e 2) * (e 3))) +1 +2 +3 +2 3 2 +1 2 1<\n> +>> + +Errors() ::= "" + +grammar(grammarName) ::= << grammar ; @parser::header { @@ -22,3 +48,4 @@ ADD : '+' ; INT : [0-9]+ ; ID : [a-z]+ ; WS : [ \t\n]+ -> skip ; +>> diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/Listeners/LRWithLabels.st b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/LRWithLabels.stg similarity index 58% rename from tool/test/org/antlr/v4/test/rt/gen/grammars/Listeners/LRWithLabels.st rename to antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/LRWithLabels.stg index 635088bd2..0800fef9d 100644 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/Listeners/LRWithLabels.st +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/LRWithLabels.stg @@ -1,3 +1,28 @@ +TestType() ::= "Parser" + +Options ::= [ + "Debug": false +] + +Grammar ::= [ + "T": {} +] + +Input() ::= "1(2,3)" + +Rule() ::= "s" + +Output() ::= << +(e (e 1) ( (eList (e 2) , (e 3)) )) +1 +2 +3 +1 [13 6]<\n> +>> + +Errors() ::= "" + +grammar(grammarName) ::= << grammar ; @parser::header { @@ -22,3 +47,4 @@ ADD : '+' ; INT : [0-9]+ ; ID : [a-z]+ ; WS : [ \t\n]+ -> skip ; +>> diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/Listeners/RuleGetters.st b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/RuleGetters.stg similarity index 71% rename from tool/test/org/antlr/v4/test/rt/gen/grammars/Listeners/RuleGetters.st rename to antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/RuleGetters.stg index 3eff8165d..4984abf3f 100644 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/Listeners/RuleGetters.st +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/RuleGetters.stg @@ -1,3 +1,16 @@ +TestType() ::= "Parser" + +Options ::= [ + "Debug": false +] + +Grammar ::= [ + "T": {} +] + +Rule() ::= "s" + +grammar(grammarName) ::= << grammar ; @parser::header { @@ -22,3 +35,4 @@ ADD : '+' ; INT : [0-9]+ ; ID : [a-z]+ ; WS : [ \t\n]+ -> skip ; +>> diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/RuleGetters_1.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/RuleGetters_1.stg new file mode 100644 index 000000000..3064fb5dd --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/RuleGetters_1.stg @@ -0,0 +1,10 @@ +import "RuleGetters.stg" + +Input() ::= "1 2" + +Output() ::= << +(a (b 1) (b 2)) +1 2 1<\n> +>> + +Errors() ::= "" diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/RuleGetters_2.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/RuleGetters_2.stg new file mode 100644 index 000000000..c1ce57406 --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/RuleGetters_2.stg @@ -0,0 +1,10 @@ +import "RuleGetters.stg" + +Input() ::= "abc" + +Output() ::= << +(a (b abc)) +abc<\n> +>> + +Errors() ::= "" diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/Listeners/TokenGetters.st b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/TokenGetters.stg similarity index 68% rename from tool/test/org/antlr/v4/test/rt/gen/grammars/Listeners/TokenGetters.st rename to antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/TokenGetters.stg index 3d89d2b2e..a58dcb307 100644 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/Listeners/TokenGetters.st +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/TokenGetters.stg @@ -1,3 +1,16 @@ +TestType() ::= "Parser" + +Options ::= [ + "Debug": false +] + +Grammar ::= [ + "T": {} +] + +Rule() ::= "s" + +grammar(grammarName) ::= << grammar ; @parser::header { @@ -21,3 +34,4 @@ ADD : '+' ; INT : [0-9]+ ; ID : [a-z]+ ; WS : [ \t\n]+ -> skip ; +>> diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/TokenGetters_1.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/TokenGetters_1.stg new file mode 100644 index 000000000..d8a36e6e2 --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/TokenGetters_1.stg @@ -0,0 +1,10 @@ +import "TokenGetters.stg" + +Input() ::= "1 2" + +Output() ::= << +(a 1 2) +1 2 [1, 2]<\n> +>> + +Errors() ::= "" diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/TokenGetters_2.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/TokenGetters_2.stg new file mode 100644 index 000000000..e01d01e5f --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Listeners/TokenGetters_2.stg @@ -0,0 +1,10 @@ +import "TokenGetters.stg" + +Input() ::= "abc" + +Output() ::= << +(a abc) +[@0,0:2='abc',\<4>,1:0]<\n> +>> + +Errors() ::= "" diff --git a/tool/test/org/antlr/v4/test/rt/gen/Generator.java b/tool/test/org/antlr/v4/test/rt/gen/Generator.java index 0fe82eec0..fdfd06eca 100644 --- a/tool/test/org/antlr/v4/test/rt/gen/Generator.java +++ b/tool/test/org/antlr/v4/test/rt/gen/Generator.java @@ -154,48 +154,10 @@ public class Generator { list.add(buildFullContextParsing()); list.add(buildLeftRecursion()); list.add(buildLexerErrors()); - list.add(buildListeners()); list.add(buildParserExec()); return list; } - private JUnitTestFile buildListeners() throws Exception { - JUnitTestFile file = new JUnitTestFile("Listeners"); - file.addParserTest(input, "Basic", "T", "s", - "1 2", - "(a 1 2)\n" + "1\n" + "2\n", - null); - file.addParserTests(input, "TokenGetters", "T", "s", - "1 2", - "(a 1 2)\n" + - "1 2 [1, 2]\n", - "abc", - "(a abc)\n" + - "[@0,0:2='abc',<4>,1:0]\n"); - file.addParserTests(input, "RuleGetters", "T", "s", - "1 2", - "(a (b 1) (b 2))\n" + - "1 2 1\n", - "abc", - "(a (b abc))\n" + - "abc\n"); - file.addParserTest(input, "LR", "T", "s", - "1+2*3", - "(e (e 1) + (e (e 2) * (e 3)))\n" + - "1\n" + - "2\n" + - "3\n" + - "2 3 2\n" + - "1 2 1\n", - null); - file.addParserTest(input, "LRWithLabels", "T", "s", - "1(2,3)", - "(e (e 1) ( (eList (e 2) , (e 3)) ))\n" + - "1\n" + "2\n" + "3\n" + "1 [13 6]\n", - null); - return file; - } - private JUnitTestFile buildLexerErrors() throws Exception { JUnitTestFile file = new JUnitTestFile("LexerErrors"); file.addLexerTest(input, "InvalidCharAtStart", "L", diff --git a/tool/test/org/antlr/v4/test/rt/java/TestListeners.java b/tool/test/org/antlr/v4/test/rt/java/TestListeners.java deleted file mode 100644 index e44d19ccb..000000000 --- a/tool/test/org/antlr/v4/test/rt/java/TestListeners.java +++ /dev/null @@ -1,228 +0,0 @@ -package org.antlr.v4.test.rt.java; - -import org.junit.Test; -import static org.junit.Assert.*; - -public class TestListeners extends BaseTest { - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testBasic() throws Exception { - String grammar = "grammar T;\n" + - "@parser::header {\n" + - "}\n" + - "\n" + - "@parser::members {\n" + - "public static class LeafListener extends TBaseListener {\n" + - " public void visitTerminal(TerminalNode node) {\n" + - " System.out.println(node.getSymbol().getText());\n" + - " }\n" + - "}\n" + - "}\n" + - "\n" + - "s\n" + - "@after {\n" + - "System.out.println($ctx.r.toStringTree(this));\n" + - "ParseTreeWalker walker = new ParseTreeWalker();\n" + - "walker.walk(new LeafListener(), $ctx.r);\n" + - "}\n" + - " : r=a ;\n" + - "a : INT INT\n" + - " | ID\n" + - " ;\n" + - "MULT: '*' ;\n" + - "ADD : '+' ;\n" + - "INT : [0-9]+ ;\n" + - "ID : [a-z]+ ;\n" + - "WS : [ \\t\\n]+ -> skip ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "1 2", false); - assertEquals("(a 1 2)\n1\n2\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - String testTokenGetters(String input) throws Exception { - String grammar = "grammar T;\n" + - "@parser::header {\n" + - "}\n" + - "\n" + - "@parser::members {\n" + - "public static class LeafListener extends TBaseListener {\n" + - " public void exitA(TParser.AContext ctx) {\n" + - " if (ctx.getChildCount()==2) \n" + - " System.out.printf(\"%s %s %s\",ctx.INT(0).getSymbol().getText(),\n" + - " ctx.INT(1).getSymbol().getText(),ctx.INT());\n" + - " else\n" + - " System.out.println(ctx.ID().getSymbol());\n" + - " }\n" + - "}\n" + - "}\n" + - "\n" + - "s\n" + - "@after {\n" + - "System.out.println($ctx.r.toStringTree(this));\n" + - "ParseTreeWalker walker = new ParseTreeWalker();\n" + - "walker.walk(new LeafListener(), $ctx.r);\n" + - "}\n" + - " : r=a ;\n" + - "a : INT INT\n" + - " | ID\n" + - " ;\n" + - "MULT: '*' ;\n" + - "ADD : '+' ;\n" + - "INT : [0-9]+ ;\n" + - "ID : [a-z]+ ;\n" + - "WS : [ \\t\\n]+ -> skip ;"; - return execParser("T.g4", grammar, "TParser", "TLexer", "s", input, false); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testTokenGetters_1() throws Exception { - String found = testTokenGetters("1 2"); - assertEquals("(a 1 2)\n1 2 [1, 2]\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testTokenGetters_2() throws Exception { - String found = testTokenGetters("abc"); - assertEquals("(a abc)\n[@0,0:2='abc',<4>,1:0]\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - String testRuleGetters(String input) throws Exception { - String grammar = "grammar T;\n" + - "@parser::header {\n" + - "}\n" + - "\n" + - "@parser::members {\n" + - "public static class LeafListener extends TBaseListener {\n" + - " public void exitA(TParser.AContext ctx) {\n" + - " if (ctx.getChildCount()==2) {\n" + - " System.out.printf(\"%s %s %s\",ctx.b(0).start.getText(),\n" + - " ctx.b(1).start.getText(),ctx.b().get(0).start.getText());\n" + - " } else \n" + - " System.out.println(ctx.b(0).start.getText());\n" + - " }\n" + - "}\n" + - "}\n" + - "\n" + - "s\n" + - "@after {\n" + - "System.out.println($ctx.r.toStringTree(this));\n" + - "ParseTreeWalker walker = new ParseTreeWalker();\n" + - "walker.walk(new LeafListener(), $ctx.r);\n" + - "}\n" + - " : r=a ;\n" + - "a : b b // forces list\n" + - " | b // a list still\n" + - " ;\n" + - "b : ID | INT;\n" + - "MULT: '*' ;\n" + - "ADD : '+' ;\n" + - "INT : [0-9]+ ;\n" + - "ID : [a-z]+ ;\n" + - "WS : [ \\t\\n]+ -> skip ;"; - return execParser("T.g4", grammar, "TParser", "TLexer", "s", input, false); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testRuleGetters_1() throws Exception { - String found = testRuleGetters("1 2"); - assertEquals("(a (b 1) (b 2))\n1 2 1\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testRuleGetters_2() throws Exception { - String found = testRuleGetters("abc"); - assertEquals("(a (b abc))\nabc\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testLR() throws Exception { - String grammar = "grammar T;\n" + - "@parser::header {\n" + - "}\n" + - "\n" + - "@parser::members {\n" + - "public static class LeafListener extends TBaseListener {\n" + - " public void exitE(TParser.EContext ctx) {\n" + - " if (ctx.getChildCount()==3) {\n" + - " System.out.printf(\"%s %s %s\\n\",ctx.e(0).start.getText(),\n" + - " ctx.e(1).start.getText(), ctx.e().get(0).start.getText());\n" + - " } else \n" + - " System.out.println(ctx.INT().getSymbol().getText());\n" + - " }\n" + - "}\n" + - "}\n" + - "\n" + - "s\n" + - "@after {\n" + - "System.out.println($ctx.r.toStringTree(this));\n" + - "ParseTreeWalker walker = new ParseTreeWalker();\n" + - "walker.walk(new LeafListener(), $ctx.r);\n" + - "}\n" + - " : r=e ;\n" + - "e : e op='*' e\n" + - " | e op='+' e\n" + - " | INT\n" + - " ;\n" + - "MULT: '*' ;\n" + - "ADD : '+' ;\n" + - "INT : [0-9]+ ;\n" + - "ID : [a-z]+ ;\n" + - "WS : [ \\t\\n]+ -> skip ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "1+2*3", false); - assertEquals("(e (e 1) + (e (e 2) * (e 3)))\n1\n2\n3\n2 3 2\n1 2 1\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testLRWithLabels() throws Exception { - String grammar = "grammar T;\n" + - "@parser::header {\n" + - "}\n" + - "\n" + - "@parser::members {\n" + - "public static class LeafListener extends TBaseListener {\n" + - " public void exitCall(TParser.CallContext ctx) {\n" + - " System.out.printf(\"%s %s\",ctx.e().start.getText(),ctx.eList());\n" + - " }\n" + - " public void exitInt(TParser.IntContext ctx) {\n" + - " System.out.println(ctx.INT().getSymbol().getText());\n" + - " }\n" + - "}\n" + - "}\n" + - "\n" + - "s\n" + - "@after {\n" + - "System.out.println($ctx.r.toStringTree(this));\n" + - "ParseTreeWalker walker = new ParseTreeWalker();\n" + - "walker.walk(new LeafListener(), $ctx.r);\n" + - "}\n" + - " : r=e ;\n" + - "e : e '(' eList ')' # Call\n" + - " | INT # Int\n" + - " ;\n" + - "eList : e (',' e)* ;\n" + - "MULT: '*' ;\n" + - "ADD : '+' ;\n" + - "INT : [0-9]+ ;\n" + - "ID : [a-z]+ ;\n" + - "WS : [ \\t\\n]+ -> skip ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "1(2,3)", false); - assertEquals("(e (e 1) ( (eList (e 2) , (e 3)) ))\n1\n2\n3\n1 [13 6]\n", found); - assertNull(this.stderrDuringParse); - } - - -} \ No newline at end of file