Testing grammars with JUnit

Testing a Tree Parser

Be sure to define a subclass of IANTLRFrontEnd and to declare an ANTLRTester in your test-case class. (See the usage page for further instructions.)

Also, test your lexer and parser first.

Making Assertions

Since the output of a tree parser is usally not an ANTLR object requiring further processing, you should use the normal assertX() methods of JUnit.

To do the tree parse, use treeParseAs(String):

assertEquals(new MyOwnIdentifier("foo"),   
    myTester.scanInput("foo").parseAs("expression").treeParseAs("program"));

This uses the same scanInput() from the lexer tests and parseAs() from the parser tests. treeParseAs() just takes it through the tree parser. "program" (in this case) is the name of the production from the tree parser to start with.