Search in sources :

Example 1 with Model

use of org.eclipse.xtext.testlanguages.actionLang.Model in project xtext-core by eclipse.

the class AntlrParserTest method testParseWithoutActionCall.

/**
 * Model: '<code>myID</code>'
 * The following parse tree is expected:
 * <pre>
 * CompositeNode (GrammarElement: Rule[Model], Element: null)
 *  |
 * CompositeNode (GrammarElement: RuleCall[Child], Element: Child[myID])
 *  |
 * LeafNode (GrammarElement: RuleCall[ID], Element: null)
 * </pre>
 */
@Test
public void testParseWithoutActionCall() throws Exception {
    XtextResource resource = helper.getResourceFromString("myID");
    assertTrue(resource.getErrors().toString(), resource.getErrors().isEmpty());
    Model model = (Model) resource.getContents().get(0);
    assertNotNull("model", model);
    assertEquals(model.eClass().getName(), ActionLangPackage.Literals.CHILD, model.eClass());
    ICompositeNode node = NodeModelUtils.getNode(model);
    assertNotNull("node", node);
    assertEquals("node.grammarElement", grammarAccess.getModelAccess().getChildParserRuleCall_0(), node.getGrammarElement());
    assertEquals(node.getChildren().toString(), 1, Iterables.size(node.getChildren()));
    INode childNode = node.getFirstChild();
    assertTrue(childNode.toString(), childNode instanceof ILeafNode);
    assertEquals("childNode.grammarElement", grammarAccess.getChildAccess().getNameIDTerminalRuleCall_0(), childNode.getGrammarElement());
    assertFalse("childNode.element", childNode.hasDirectSemanticElement());
    ICompositeNode rootNode = resource.getParseResult().getRootNode();
    assertNotNull("rootNode", rootNode);
    assertEquals("rootNode.grammarElement", grammarAccess.getModelRule(), rootNode.getGrammarElement());
    assertEquals(rootNode.getChildren().toString(), 1, Iterables.size(rootNode.getChildren()));
    assertEquals("node is child of rootNode", rootNode, node.getParent());
    assertFalse("rootNode.element", rootNode.hasDirectSemanticElement());
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) Model(org.eclipse.xtext.testlanguages.actionLang.Model) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) XtextResource(org.eclipse.xtext.resource.XtextResource) Test(org.junit.Test)

Example 2 with Model

use of org.eclipse.xtext.testlanguages.actionLang.Model in project xtext-core by eclipse.

the class AntlrParserTest method testParseWithActionCall.

/**
 * Model: '<code>myID otherID</code>'
 * The following parse tree is expected:
 * <pre>
 * CompositeNode (GrammarElement: Action, Element: Parent[left: Child[myID], right: Child[otherID]])
 *  |________________________________________________________________________,
 * CompositeNode (GrammarElement: Rule[Model], Element: null)               CompositeNode (GrammarElement: RuleCall[Child], Element: Child[otherID])
 *  |                                                                        |________________,
 * CompositeNode (GrammarElement: RuleCall[Child], Element: Child[myID])    LeafNode (WS)    LeafNode (GrammarElement: RuleCall[ID], Element: null)
 *  |
 * LeafNode (GrammarElement: RuleCall[ID], Element: null)
 * </pre>
 */
@Test
public void testParseWithActionCall() throws Exception {
    XtextResource resource = helper.getResourceFromString("myID otherID");
    assertTrue(resource.getErrors().toString(), resource.getErrors().isEmpty());
    Model model = (Model) resource.getContents().get(0);
    assertNotNull("model", model);
    assertEquals(model.eClass().getName(), ActionLangPackage.Literals.PARENT, model.eClass());
    ICompositeNode node = NodeModelUtils.getNode(model);
    assertNotNull("node", node);
    assertNotNull("node", node);
    assertEquals("node.grammarElement", grammarAccess.getModelAccess().getParentLeftAction_1_0(), node.getGrammarElement());
    assertEquals(node.getChildren().toString(), 2, Iterables.size(node.getChildren()));
    ICompositeNode firstChildNode = (ICompositeNode) node.getFirstChild();
    assertEquals("firstChildNode.grammarElement", grammarAccess.getModelRule(), firstChildNode.getGrammarElement());
    assertFalse("firstChildNode.element", firstChildNode.hasDirectSemanticElement());
    assertEquals(firstChildNode.getChildren().toString(), 1, Iterables.size(firstChildNode.getChildren()));
    INode childNode = firstChildNode.getFirstChild();
    assertTrue(childNode.toString(), childNode instanceof ICompositeNode);
    assertEquals("childNode.grammarElement", grammarAccess.getModelAccess().getChildParserRuleCall_0(), childNode.getGrammarElement());
    assertEquals("childNode.element", ((Parent) model).getLeft(), childNode.getSemanticElement());
    assertEquals(((ICompositeNode) childNode).getChildren().toString(), 1, Iterables.size(((ICompositeNode) childNode).getChildren()));
    assertEquals("childNode.children[0].grammarElement", grammarAccess.getChildAccess().getNameIDTerminalRuleCall_0(), ((ICompositeNode) childNode).getFirstChild().getGrammarElement());
    ICompositeNode secondChildNode = (ICompositeNode) node.getFirstChild().getNextSibling();
    assertEquals("childNode.grammarElement", grammarAccess.getModelAccess().getRightChildParserRuleCall_1_1_0(), secondChildNode.getGrammarElement());
    assertEquals("childNode.element", ((Parent) model).getRight(), secondChildNode.getSemanticElement());
    INode otherChildNode = secondChildNode.getFirstChild().getNextSibling();
    assertTrue(otherChildNode.toString(), otherChildNode instanceof ILeafNode);
    assertEquals("otherChildNode.grammarElement", grammarAccess.getChildAccess().getNameIDTerminalRuleCall_0(), otherChildNode.getGrammarElement());
    ICompositeNode rootNode = resource.getParseResult().getRootNode();
    assertNotNull("rootNode", rootNode);
    assertEquals("rootNode.grammarElement", grammarAccess.getModelAccess().getParentLeftAction_1_0(), rootNode.getGrammarElement());
    assertEquals(node, rootNode);
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) Model(org.eclipse.xtext.testlanguages.actionLang.Model) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) XtextResource(org.eclipse.xtext.resource.XtextResource) Test(org.junit.Test)

Aggregations

ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)2 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)2 INode (org.eclipse.xtext.nodemodel.INode)2 XtextResource (org.eclipse.xtext.resource.XtextResource)2 Model (org.eclipse.xtext.testlanguages.actionLang.Model)2 Test (org.junit.Test)2