forked from jasder/antlr
Merge pull request #591 from sharwell/xpath-tests
Simplify common code in XPath tests
This commit is contained in:
commit
1a56da626f
|
@ -46,6 +46,9 @@ public class TestXPath extends BaseTest {
|
||||||
"INT : [0-9]+ ; // match integers\n" +
|
"INT : [0-9]+ ; // match integers\n" +
|
||||||
"NEWLINE:'\\r'? '\\n' -> skip; // return newlines to parser (is end-statement signal)\n" +
|
"NEWLINE:'\\r'? '\\n' -> skip; // return newlines to parser (is end-statement signal)\n" +
|
||||||
"WS : [ \\t]+ -> skip ; // toss out whitespace\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 {
|
@Test public void testValidPaths() throws Exception {
|
||||||
boolean ok =
|
boolean ok =
|
||||||
|
@ -53,9 +56,6 @@ public class TestXPath extends BaseTest {
|
||||||
"ExprLexer", false);
|
"ExprLexer", false);
|
||||||
assertTrue(ok);
|
assertTrue(ok);
|
||||||
|
|
||||||
String input =
|
|
||||||
"def f(x,y) { x = 3+4; y; ; }\n" +
|
|
||||||
"def g(x) { return 1+2*x; }\n";
|
|
||||||
String xpath[] = {
|
String xpath[] = {
|
||||||
"/prog/func", // all funcs under prog at root
|
"/prog/func", // all funcs under prog at root
|
||||||
"/prog/*", // all children of prog at root
|
"/prog/*", // all children of prog at root
|
||||||
|
@ -102,7 +102,7 @@ public class TestXPath extends BaseTest {
|
||||||
};
|
};
|
||||||
|
|
||||||
for (int i=0; i<xpath.length; i++) {
|
for (int i=0; i<xpath.length; i++) {
|
||||||
List<String> nodes = getNodeStrings(input, xpath[i], "prog", "ExprParser", "ExprLexer");
|
List<String> nodes = getNodeStrings(SAMPLE_PROGRAM, xpath[i], "prog", "ExprParser", "ExprLexer");
|
||||||
String result = nodes.toString();
|
String result = nodes.toString();
|
||||||
assertEquals("path "+xpath[i]+" failed", expected[i], result);
|
assertEquals("path "+xpath[i]+" failed", expected[i], result);
|
||||||
}
|
}
|
||||||
|
@ -114,13 +114,10 @@ public class TestXPath extends BaseTest {
|
||||||
"ExprLexer", false);
|
"ExprLexer", false);
|
||||||
assertTrue(ok);
|
assertTrue(ok);
|
||||||
|
|
||||||
String input =
|
|
||||||
"def f(x,y) { x = 3+4; y; ; }\n" +
|
|
||||||
"def g(x) { return 1+2*x; }\n";
|
|
||||||
String path = "&";
|
String path = "&";
|
||||||
String expected = "Invalid tokens or characters at index 0 in 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 {
|
@Test public void testWeirdChar2() throws Exception {
|
||||||
|
@ -129,13 +126,10 @@ public class TestXPath extends BaseTest {
|
||||||
"ExprLexer", false);
|
"ExprLexer", false);
|
||||||
assertTrue(ok);
|
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 path = "//w&e/";
|
||||||
String expected = "Invalid tokens or characters at index 3 in 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 {
|
@Test public void testBadSyntax() throws Exception {
|
||||||
|
@ -144,13 +138,10 @@ public class TestXPath extends BaseTest {
|
||||||
"ExprLexer", false);
|
"ExprLexer", false);
|
||||||
assertTrue(ok);
|
assertTrue(ok);
|
||||||
|
|
||||||
String input =
|
|
||||||
"def f(x,y) { x = 3+4; y; ; }\n" +
|
|
||||||
"def g(x) { return 1+2*x; }\n";
|
|
||||||
String path = "///";
|
String path = "///";
|
||||||
String expected = "/ at index 2 isn't a valid rule name";
|
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 {
|
@Test public void testMissingWordAtEnd() throws Exception {
|
||||||
|
@ -159,13 +150,10 @@ public class TestXPath extends BaseTest {
|
||||||
"ExprLexer", false);
|
"ExprLexer", false);
|
||||||
assertTrue(ok);
|
assertTrue(ok);
|
||||||
|
|
||||||
String input =
|
|
||||||
"def f(x,y) { x = 3+4; y; ; }\n" +
|
|
||||||
"def g(x) { return 1+2*x; }\n";
|
|
||||||
String path = "//";
|
String path = "//";
|
||||||
String expected = "Missing path element at end of 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 {
|
@Test public void testBadTokenName() throws Exception {
|
||||||
|
@ -174,13 +162,10 @@ public class TestXPath extends BaseTest {
|
||||||
"ExprLexer", false);
|
"ExprLexer", false);
|
||||||
assertTrue(ok);
|
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 path = "//Ick";
|
||||||
String expected = "Ick at index 2 isn't a valid token name";
|
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 {
|
@Test public void testBadRuleName() throws Exception {
|
||||||
|
@ -189,13 +174,10 @@ public class TestXPath extends BaseTest {
|
||||||
"ExprLexer", false);
|
"ExprLexer", false);
|
||||||
assertTrue(ok);
|
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 path = "/prog/ick";
|
||||||
String expected = "ick at index 6 isn't a valid rule name";
|
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,
|
protected void testError(String input, String path, String expected,
|
||||||
|
|
Loading…
Reference in New Issue