org.norecess.antlr
Class Assert

java.lang.Object
  extended by org.norecess.antlr.Assert

public class Assert
extends Object

Implements various assertion methods for testing lexers, parsers, and tree parsers. The best way to access these methods is with a static import:

 import static org.norecess.antlr.Assert.*;
 

Testing methods come in two flavors: testing ANTLR structures directly and testing through a ANTLRTester (and the objects it creates).

Testing a Lexer

The easiest way to test the output of a lexer is with assertToken(int, String, PostScan) and assertToken(String, int, String, PostScan). The ANTLR tester does all the work:

 assertToken(MyOwnLexer.IDENTIFIER, "foo", myTester.scanInput("foo"));
 
This asserts the type and text of the token. Each of the assertion methods has an optional message parameter:
 assertToken("should scan 'foo' as an identifier", MyOwnLexer.IDENTIFIER, "foo",
         myTester.scanInput("foo"));
 
When testing this way, PostScan.getSingleToken() is called, and so the input is checked to make sure that there is only one token returned by the input.

assertToken(int, String, Token) will directly assert the type and text of an Token.

Testing a Parser

The easiest way to test the output of a parser is with assertTree(int, String, PostParse) and assertTree(String, int, String, PostParse).

 assertTree(MyOwnParser.EXPRESSION, "(+(1)(x))", myTester.scanInput("1 + x")
         .parseAs("expression"));
 
The first argument is the type of the AST returned by the parser; the second argument is a preorder traversal using the text of the tokens in the AST, fully parenthesized. You again use an ANTLRTester to generate the AST, going through ANTLRTester.scanInput(String) and PostScan.parseAs(String). The argument given to PostScan.parseAs(String) is the name of the rule to use from the parser.

assertTree(int, String, Tree) and assertTree(String, int, String, Tree) let you test directly against an ANTLR tree.

You can get your own preorder traversal (without any assertions) with preorder(Tree).

Testing a Tree Parser

Since the output of a tree parser is usally not an ANTLR object (or some other common object), you should use the normal assertEquals() methods of JUnit. To do the tree parse, use PostParse.treeParseAs(String):

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

"expression" is the name of the production from the parser that you want to use; "program" is the name of the production from the tree parser that you want to use.

Author:
Jeremy D. Frens

Method Summary
static void assertToken(int expectedChannel, int expectedType, String expectedText, PostScan postScan)
          Asserts the token produced by an ANTLR tester.
static void assertToken(int expectedChannel, int expectedType, String expectedText, org.antlr.runtime.Token token)
          Asserts properties of a token.
static void assertToken(int expectedType, String expectedText, PostScan postScan)
          Asserts the token produced by an ANTLR tester.
static void assertToken(int expectedType, String expectedText, org.antlr.runtime.Token token)
          Asserts properties of a token.
static void assertToken(String message, int expectedChannel, int expectedType, String expectedText, PostScan postScan)
          Asserts the token produced by an ANTLR tester.
static void assertToken(String message, int expectedChannel, int expectedType, String expectedText, org.antlr.runtime.Token token)
          Asserts properties of a token.
static void assertToken(String message, int expectedType, String expectedText, PostScan postScan)
          Asserts the token produced by an ANTLR tester.
static void assertToken(String message, int expectedType, String expectedText, org.antlr.runtime.Token token)
          Asserts properties of a token.
static void assertTree(int rootType, String preorder, PostParse postParse)
          Asserts a parse tree.
static void assertTree(int rootType, String preorder, org.antlr.runtime.tree.Tree tree)
          Asserts a parse tree.
static void assertTree(String message, int rootType, String preorder, PostParse postParse)
          Asserts a parse tree.
static void assertTree(String message, int rootType, String preorder, org.antlr.runtime.tree.Tree tree)
          Asserts a parse tree.
static String preorder(org.antlr.runtime.tree.Tree tree)
          Generates a preorder traversal of an ANTLR tree.
static void refuteToken(int refutedType, PostScan postScan)
          To "refute" a token means to assert that it cannot be of the specified type.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

assertToken

public static void assertToken(String message,
                               int expectedType,
                               String expectedText,
                               PostScan postScan)
Asserts the token produced by an ANTLR tester.

Parameters:
message - the message to display on failure.
expectedType - the expected type of the token.
expectedText - the expected text of the token.
postScan - the result of ANTLRTester.scanInput(String) which will produce the token to assert.

assertToken

public static void assertToken(String message,
                               int expectedChannel,
                               int expectedType,
                               String expectedText,
                               PostScan postScan)
Asserts the token produced by an ANTLR tester.

Parameters:
message - the message to display on failure.
expectedChannel - the channel the token should appear on.
expectedType - the expected type of the token.
expectedText - the expected text of the token.
postScan - the result of ANTLRTester.scanInput(String) which will produce the token to assert.

assertToken

public static void assertToken(int expectedType,
                               String expectedText,
                               PostScan postScan)
Asserts the token produced by an ANTLR tester.

Parameters:
expectedType - the expected type of the token.
expectedText - the expected text of the token.
postScan - the result of ANTLRTester.scanInput(String) which will produce the token to assert.

assertToken

public static void assertToken(int expectedChannel,
                               int expectedType,
                               String expectedText,
                               PostScan postScan)
Asserts the token produced by an ANTLR tester.

Parameters:
expectedChannel - the channel the token should appear on.
expectedType - the expected type of the token.
expectedText - the expected text of the token.
postScan - the result of ANTLRTester.scanInput(String) which will produce the token to assert.

assertToken

public static void assertToken(String message,
                               int expectedType,
                               String expectedText,
                               org.antlr.runtime.Token token)
Asserts properties of a token.

Parameters:
message - the message to display on failure.
expectedType - the expected type of the token.
expectedText - the expected text of the token.
token - the token to assert.

assertToken

public static void assertToken(String message,
                               int expectedChannel,
                               int expectedType,
                               String expectedText,
                               org.antlr.runtime.Token token)
Asserts properties of a token.

Parameters:
message - the message to display on failure.
expectedChannel - the channel the token should appear on.
expectedType - the expected type of the token.
expectedText - the expected text of the token.
token - the token to assert.

assertToken

public static void assertToken(int expectedType,
                               String expectedText,
                               org.antlr.runtime.Token token)
Asserts properties of a token.

Parameters:
expectedType - the expected type of the token.
expectedText - the expected text of the token.
token - the token to assert.

assertToken

public static void assertToken(int expectedChannel,
                               int expectedType,
                               String expectedText,
                               org.antlr.runtime.Token token)
Asserts properties of a token.

Parameters:
expectedChannel - the channel the token should appear on.
expectedType - the expected type of the token.
expectedText - the expected text of the token.
token - the token to assert.

refuteToken

public static void refuteToken(int refutedType,
                               PostScan postScan)
To "refute" a token means to assert that it cannot be of the specified type. This is useful, for example, when you want to assert that "x" will not be recognized as an integer. Eventually, it may be recognized as an identifier or some other token, but you just want to assert that it's definitely not an integer.
 refuteToken(MyLexer.INTEGER, myTester.scanInput("x"));
 

Parameters:
refutedType - the type the token should not be.
postScan - the result of scanning input with the tester.

assertTree

public static void assertTree(int rootType,
                              String preorder,
                              PostParse postParse)
Asserts a parse tree.

Parameters:
rootType - the type of the root of the tree.
preorder - the preorder traversal of the tree.
postParse - a helper class when using ANTLRTester.

assertTree

public static void assertTree(int rootType,
                              String preorder,
                              org.antlr.runtime.tree.Tree tree)
Asserts a parse tree.

Parameters:
rootType - the type of the root of the tree.
preorder - the preorder traversal of the tree.
tree - an ANTLR tree to assert on.

assertTree

public static void assertTree(String message,
                              int rootType,
                              String preorder,
                              PostParse postParse)
Asserts a parse tree.

Parameters:
message - the message to display on failure.
rootType - the type of the root of the tree.
preorder - the preorder traversal of the tree.
postParse - a helper class when using ANTLRTester.

assertTree

public static void assertTree(String message,
                              int rootType,
                              String preorder,
                              org.antlr.runtime.tree.Tree tree)
Asserts a parse tree.

Parameters:
message - the message to display on failure.
rootType - the type of the root of the tree.
preorder - the preorder traversal of the tree.
tree - an ANTLR tree to assert on.

preorder

public static String preorder(org.antlr.runtime.tree.Tree tree)
Generates a preorder traversal of an ANTLR tree. In general, each tree and subtree in the preorder output is parenthesized (even leaves). The text of the AST (or token) is used to get the string to add to the preorder traversal.

There are two degenerate cases:

Parameters:
tree - the tree to traverse.
Returns:
the preorder representation of the tree.