diff --git a/tool/test/org/antlr/v4/test/TestXPath.java b/tool/test/org/antlr/v4/test/TestXPath.java index 1e98a0122..f36864807 100644 --- a/tool/test/org/antlr/v4/test/TestXPath.java +++ b/tool/test/org/antlr/v4/test/TestXPath.java @@ -46,6 +46,9 @@ public class TestXPath extends BaseTest { "INT : [0-9]+ ; // match integers\n" + "NEWLINE:'\\r'? '\\n' -> skip; // return newlines to parser (is end-statement signal)\n" + "WS : [ \\t]+ -> skip ; // toss out whitespace\n"; + public static final String SAMPLE_PROGRAM = + "def f(x,y) { x = 3+4; y; ; }\n" + + "def g(x) { return 1+2*x; }\n"; @Test public void testValidPaths() throws Exception { boolean ok = @@ -53,9 +56,6 @@ public class TestXPath extends BaseTest { "ExprLexer", false); assertTrue(ok); - String input = - "def f(x,y) { x = 3+4; y; ; }\n" + - "def g(x) { return 1+2*x; }\n"; String xpath[] = { "/prog/func", // all funcs under prog at root "/prog/*", // all children of prog at root @@ -102,7 +102,7 @@ public class TestXPath extends BaseTest { }; for (int i=0; i nodes = getNodeStrings(input, xpath[i], "prog", "ExprParser", "ExprLexer"); + List nodes = getNodeStrings(SAMPLE_PROGRAM, xpath[i], "prog", "ExprParser", "ExprLexer"); String result = nodes.toString(); assertEquals("path "+xpath[i]+" failed", expected[i], result); } @@ -114,13 +114,10 @@ public class TestXPath extends BaseTest { "ExprLexer", false); assertTrue(ok); - String input = - "def f(x,y) { x = 3+4; y; ; }\n" + - "def g(x) { return 1+2*x; }\n"; String path = "&"; String expected = "Invalid tokens or characters at index 0 in path '&'"; - testError(input, path, expected, "prog", "ExprParser", "ExprLexer"); + testError(SAMPLE_PROGRAM, path, expected, "prog", "ExprParser", "ExprLexer"); } @Test public void testWeirdChar2() throws Exception { @@ -129,13 +126,10 @@ public class TestXPath extends BaseTest { "ExprLexer", false); assertTrue(ok); - String input = - "def f(x,y) { x = 3+4; y; ; }\n" + - "def g(x) { return 1+2*x; }\n"; String path = "//w&e/"; String expected = "Invalid tokens or characters at index 3 in path '//w&e/'"; - testError(input, path, expected, "prog", "ExprParser", "ExprLexer"); + testError(SAMPLE_PROGRAM, path, expected, "prog", "ExprParser", "ExprLexer"); } @Test public void testBadSyntax() throws Exception { @@ -144,13 +138,10 @@ public class TestXPath extends BaseTest { "ExprLexer", false); assertTrue(ok); - String input = - "def f(x,y) { x = 3+4; y; ; }\n" + - "def g(x) { return 1+2*x; }\n"; String path = "///"; String expected = "/ at index 2 isn't a valid rule name"; - testError(input, path, expected, "prog", "ExprParser", "ExprLexer"); + testError(SAMPLE_PROGRAM, path, expected, "prog", "ExprParser", "ExprLexer"); } @Test public void testMissingWordAtEnd() throws Exception { @@ -159,13 +150,10 @@ public class TestXPath extends BaseTest { "ExprLexer", false); assertTrue(ok); - String input = - "def f(x,y) { x = 3+4; y; ; }\n" + - "def g(x) { return 1+2*x; }\n"; String path = "//"; String expected = "Missing path element at end of path"; - testError(input, path, expected, "prog", "ExprParser", "ExprLexer"); + testError(SAMPLE_PROGRAM, path, expected, "prog", "ExprParser", "ExprLexer"); } @Test public void testBadTokenName() throws Exception { @@ -174,13 +162,10 @@ public class TestXPath extends BaseTest { "ExprLexer", false); assertTrue(ok); - String input = - "def f(x,y) { x = 3+4; y; ; }\n" + - "def g(x) { return 1+2*x; }\n"; String path = "//Ick"; String expected = "Ick at index 2 isn't a valid token name"; - testError(input, path, expected, "prog", "ExprParser", "ExprLexer"); + testError(SAMPLE_PROGRAM, path, expected, "prog", "ExprParser", "ExprLexer"); } @Test public void testBadRuleName() throws Exception { @@ -189,13 +174,10 @@ public class TestXPath extends BaseTest { "ExprLexer", false); assertTrue(ok); - String input = - "def f(x,y) { x = 3+4; y; ; }\n" + - "def g(x) { return 1+2*x; }\n"; String path = "/prog/ick"; String expected = "ick at index 6 isn't a valid rule name"; - testError(input, path, expected, "prog", "ExprParser", "ExprLexer"); + testError(SAMPLE_PROGRAM, path, expected, "prog", "ExprParser", "ExprLexer"); } protected void testError(String input, String path, String expected,