diff options
Diffstat (limited to 'tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/parser/AbstractXpand3NodeParserTest.java')
-rw-r--r-- | tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/parser/AbstractXpand3NodeParserTest.java | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/parser/AbstractXpand3NodeParserTest.java b/tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/parser/AbstractXpand3NodeParserTest.java new file mode 100644 index 00000000..619f05a1 --- /dev/null +++ b/tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/parser/AbstractXpand3NodeParserTest.java @@ -0,0 +1,131 @@ +package org.eclipse.xpand3.parser; + +import java.util.List; + +import junit.framework.TestCase; + +import org.eclipse.xpand3.node.CompositeNode; +import org.eclipse.xpand3.node.LeafNode; +import org.eclipse.xpand3.node.LexedToken; +import org.eclipse.xpand3.node.Node; + +public abstract class AbstractXpand3NodeParserTest extends TestCase { + + public static final char LG = '\u00AB'; + public static final char RG = '\u00BB'; + + public static final String LGS = String.valueOf(LG); + public static final String RGS = String.valueOf(RG); + + public AbstractXpand3NodeParserTest() { + super(); + } + + public AbstractXpand3NodeParserTest(String name) { + super(name); + } + + protected CompositeNode checkIsRule(Node node, String ruleName, + int numChildren) { + assertTrue(node instanceof CompositeNode); + assertEquals(ruleName, ((CompositeNode) node).getRule()); + assertEquals(numChildren, ((CompositeNode) node).getChildren().size()); + return (CompositeNode) node; + } + + private LeafNode checkIsToken(Node node, String tokenText) { + assertTrue(node instanceof LeafNode); + LexedToken token = ((LeafNode) node).getToken(); + assertNotNull(token); + assertEquals(tokenText, token.getText()); + return (LeafNode) node; + } + + private Node getChild(Node parent, int index) { + assertNotNull(parent); + assertTrue(parent instanceof CompositeNode); + List<Node> children = ((CompositeNode) parent).getChildren(); + assertTrue(children.size() > index); + Node child = children.get(index); + return child; + } + + protected CompositeNode checkChildIsRule(Node parent, int index, + String ruleName, int numChildren) { + return checkIsRule(getChild(parent, index), ruleName, numChildren); + } + + protected CompositeNode checkChildIsSimpleType(Node parent, int index, + String identifierName) { + CompositeNode simpleType = checkChildIsRule(parent, index, + "r_simpleType", 1); + checkChildIsIdentifier(simpleType, 0, identifierName); + return simpleType; + } + + protected CompositeNode checkChildIsCollectionType(Node parent, int index, + String identifierName) { + CompositeNode collectionType = checkChildIsRule(parent, index, + "r_collectionType", 1); + checkChildIsToken(collectionType, 0, identifierName); + return collectionType; + } + + protected CompositeNode checkChildIsIdentifier(CompositeNode parent, + int index, String identifierName) { + CompositeNode identifier = checkChildIsRule(parent, index, + "r_identifier", 1); + checkChildIsToken(identifier, 0, identifierName); + return identifier; + } + + protected CompositeNode checkChildIsScopedType(Node parent, int index, + String scopeName, String identifierName) { + CompositeNode scopedType = checkChildIsRule(parent, index, + "r_simpleType", 3); + checkChildIsIdentifier(scopedType, 0, scopeName); + checkChildIsToken(scopedType, 1, "::"); + checkChildIsIdentifier(scopedType, 2, identifierName); + return scopedType; + } + + protected CompositeNode checkChildIsNumberLiteral(Node parent, int index, + String value) { + CompositeNode numberLiteral = checkChildIsRule(parent, index, + "r_numberLiteral", 1); + checkChildIsToken(numberLiteral, 0, value); + return numberLiteral; + } + + protected CompositeNode checkChildIsStringLiteral(Node parent, int index, + String value) { + CompositeNode stringLiteral = checkChildIsRule(parent, index, + "r_stringLiteral", 1); + checkChildIsToken(stringLiteral, 0, value); + return stringLiteral; + } + + protected CompositeNode checkChildIsTrueLiteral(Node parent, int index) { + CompositeNode booleanLiteral = checkChildIsRule(parent, index, + "r_booleanLiteral", 1); + checkChildIsToken(booleanLiteral, 0, "true"); + return booleanLiteral; + } + + protected CompositeNode checkChildIsFalseLiteral(Node parent, int index) { + CompositeNode booleanLiteral = checkChildIsRule(parent, index, + "r_booleanLiteral", 1); + checkChildIsToken(booleanLiteral, 0, "false"); + return booleanLiteral; + } + + protected LeafNode checkChildIsToken(Node parent, int index, + String tokenText) { + return checkIsToken(getChild(parent, index), tokenText); + } + + protected String tag(final String str) { + return LG + str + RG; + } + +}
\ No newline at end of file |