I've got a fairly complicated antlr4 grammar that utilizes a visitor pattern. I'd like to test parts of the visitor. What's a good way to test individual visit rules?
My visitor has tons of rules like this that I want to test:
@Override
public Object visitQux(ExclParser.QuxContext ctx) {
return visitChildren(ctx);
}
And my test code basically is the following:
PrintStream ps = new PrintStream(stdError, false /* autoFlush */, "UTF-8")
ANTLRInputStream input = new ANTLRInputStream(is);
MyLexer lexer = new MyLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
MyParser parser = new MyParser(tokens);
parser.removeErrorListeners();
MyErrorListener errorListener = new MyErrorListener(ps, filename);
parser.addErrorListener(errorListener);
MyVisitor visitor = new MyVisitor();
visitor.setParser(filename, parser, errorListener);
ParseTree tree = parser.qux(); // <--- This is the line I want to vary.
Object result = visitor.visit(tree);
assertThat(describeExpectation(), result, equalTo(this.expectedOutput));
Ideally I would be able to test any visitor using a parameterized test. But to get the parse tree I want to visit (parser.qux) I can't specify any variant of qux() in a table because parser.qux() is not static.
Any thoughts?
Reflection may be the right answer here:
is a suitable replacement for: