Passing parameter to parser rule in GUnit
I have a parser rule in ANTLR that takes a parameter.
tincture [Tinctures tinctures] returns [Tincture tincture]
: { String tinctureName = ""; }
( COLOUR { tinctureName = $COLOUR.text; }
| METAL { tinctureName = $METAL.text; }
| FUR { tinctureName = $FUR.text; }
)
{
try {
$tincture = tinctures.getTincture(tinctureName);
} catch (UnknownTinctureException e) {开发者_StackOverflow
throw new MyRecognitionException("Unknown tincture found.", e);
}
}
;
How can I write a test for this in GUnit?
I have successfully written tests for my Lexer rules as they do not have any parameters. If I write the following I get "java.lang.NoSuchMethodException: mypackage.BlazonParser.tincture()"
tincture
: "gules" OK
"sable" OK
"blah" FAIL
I can find very little documentation around GUnit apart from this page, but it hasn't covered this.
Looking at the official docs, it seems you can't provide a parameter to the rules you test, causing for the exception:
java.lang.NoSuchMethodException: mypackage.BlazonParser.tincture()
since tincture
expects a Tinctures
as parameter.
You could keep keep using gUnit, and use JUnit for those rules containing parameters. But mixing gUnit and JUnit is a bit messy, IMO, so personally, I simply use JUnit for all my ANTLR tests.
You could also have a look at aunit, a 3rd party ANTLR unit test suite. I haven't looked at it in detail, and it seems to be lacking in documentation, but still, it could be what you're looking for.
精彩评论