|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.norecess.antlr.Assert
public class Assert
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).
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
.
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)
.
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.
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 |
---|
public static void assertToken(String message, int expectedType, String expectedText, PostScan postScan)
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.public static void assertToken(String message, int expectedChannel, int expectedType, String expectedText, PostScan postScan)
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.public static void assertToken(int expectedType, String expectedText, PostScan postScan)
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.public static void assertToken(int expectedChannel, int expectedType, String expectedText, PostScan postScan)
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.public static void assertToken(String message, int expectedType, String expectedText, org.antlr.runtime.Token token)
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.public static void assertToken(String message, int expectedChannel, int expectedType, String expectedText, org.antlr.runtime.Token token)
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.public static void assertToken(int expectedType, String expectedText, org.antlr.runtime.Token token)
expectedType
- the expected type of the token.expectedText
- the expected text of the token.token
- the token to assert.public static void assertToken(int expectedChannel, int expectedType, String expectedText, org.antlr.runtime.Token token)
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.public static void refuteToken(int refutedType, PostScan postScan)
refuteToken(MyLexer.INTEGER, myTester.scanInput("x"));
refutedType
- the type the token should not be.postScan
- the result of scanning input with the tester.public static void assertTree(int rootType, String preorder, PostParse postParse)
rootType
- the type of the root of the tree.preorder
- the preorder traversal of the tree.postParse
- a helper class when using ANTLRTester
.public static void assertTree(int rootType, String preorder, org.antlr.runtime.tree.Tree tree)
rootType
- the type of the root of the tree.preorder
- the preorder traversal of the tree.tree
- an ANTLR tree to assert on.public static void assertTree(String message, int rootType, String preorder, PostParse postParse)
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
.public static void assertTree(String message, int rootType, String preorder, org.antlr.runtime.tree.Tree tree)
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.public static String preorder(org.antlr.runtime.tree.Tree tree)
There are two degenerate cases:
""
is returned when tree
itself
is null
.""
is returned when tree
is nil.
tree
- the tree to traverse.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |