summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsefftinge2008-02-29 03:07:01 (EST)
committer sefftinge2008-02-29 03:07:01 (EST)
commitb824cfe46e51515a7bb7b13bba1178a11ab457fd (patch)
tree29e42bb39f68eb4aa882ca1824a80a3ba460496e
parent414f80b1ea4304b70bc28e5bb76d93dc2efac705 (diff)
downloadorg.eclipse.xpand-b824cfe46e51515a7bb7b13bba1178a11ab457fd.zip
org.eclipse.xpand-b824cfe46e51515a7bb7b13bba1178a11ab457fd.tar.gz
org.eclipse.xpand-b824cfe46e51515a7bb7b13bba1178a11ab457fd.tar.bz2
More tests
-rw-r--r--tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/parser/AbstractXpand3NodeParserTest.java131
-rw-r--r--tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/parser/StatementParserTest.java149
-rw-r--r--tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/parser/Xpand3MigratedNodeParserTest.java253
-rw-r--r--tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/parser/Xpand3ScannerTest.java59
4 files changed, 443 insertions, 149 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 0000000..619f05a
--- /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
diff --git a/tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/parser/StatementParserTest.java b/tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/parser/StatementParserTest.java
new file mode 100644
index 0000000..5db199e
--- /dev/null
+++ b/tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/parser/StatementParserTest.java
@@ -0,0 +1,149 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 2007 committers of openArchitectureWare and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * committers of openArchitectureWare - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.xpand3.parser;
+
+import org.antlr.runtime.ANTLRStringStream;
+import org.antlr.runtime.CommonTokenStream;
+import org.eclipse.xpand3.node.CompositeNode;
+import org.eclipse.xpand3.node.Node;
+import org.eclipse.xpand3.node.NodeUtil;
+
+/**
+ * @author Jan Kšhnlein
+ */
+public class StatementParserTest extends AbstractXpand3NodeParserTest {
+ private CompositeNode parse(String s) throws Exception {
+ System.out.println(s);
+ ANTLRStringStream stream = new ANTLRStringStream(s);
+ Xpand3Lexer lexer = new Xpand3Lexer(stream);
+ CommonTokenStream tokenStream = new CommonTokenStream(lexer);
+ AbstractXpand3NodeParser xpand3NodeParser = new Xpand3NodeParser(
+ tokenStream);
+ xpand3NodeParser.r_file();
+ Node rootNode = xpand3NodeParser.getRootNode();
+ if (rootNode == null) {
+ System.out.println("Nothing parsed.");
+ } else {
+ System.out.println(NodeUtil.toString(rootNode));
+ }
+ return (CompositeNode) rootNode;
+ }
+
+ public final void testEmptyTemplate() throws Exception {
+ Node expr = parse("");
+ assertNull(expr);
+ }
+
+ public final void testSimpleDefine() throws Exception {
+ Node expr = parse(tag("DEFINE test FOR ecore::EClass")
+ + tag("ENDDEFINE"));
+ checkIsRule(expr, "r_definition", 8);
+ checkChildIsToken(expr, 0, LGS);
+ checkChildIsToken(expr, 1, "DEFINE");
+ checkChildIsToken(expr, 3, "FOR");
+ checkChildIsToken(expr, 6, "ENDEFINE");
+ checkChildIsToken(expr, 7, RGS);
+
+ }
+ //
+ // public final void testDoubleDefine() throws Exception {
+ // final Template t = parse(tag("DEFINE test FOR ecore::EClass") +
+ // tag("ENDDEFINE")
+ // + tag("DEFINE test2(String txt) FOR ecore::EClass") + tag("ENDDEFINE"));
+ // assertEquals(2, t.getDefinitions().length);
+ // }
+ //
+ // public final void testMoreComplexDefine() throws Exception {
+ // final Template t = parse(tag("DEFINE test(ecore::EPackage a,String b) FOR
+ // ecore::EClass")
+ // + tag("FILE name+\".txt\"") + "Text und so " + tag("name") + tag("FOREACH
+ // eAllattributes AS attr")
+ // + "Attribute : " + tag("attr.name") + tag("ENDFOREACH") + tag("ENDFILE")
+ // +
+ // tag("ENDDEFINE"));
+ // assertEquals(1, t.getDefinitions().length);
+ // final Definition def = (Definition) t.getDefinitions()[0];
+ // assertEquals("test", def.getName());
+ // assertEquals(2, def.getParams().length);
+ // DeclaredParameter param = def.getParams()[0];
+ // assertEquals("a", param.getName().getValue());
+ // assertEquals("ecore::EPackage", param.getType().getValue());
+ // param = def.getParams()[1];
+ // assertEquals("b", param.getName().getValue());
+ // assertEquals("String", param.getType().getValue());
+ // assertEquals("ecore::EClass", def.getType().getValue());
+ // List<Statement> statements = Arrays.asList(def.getBody());
+ // assertEquals(3, statements.size());
+ // final FileStatement f = (FileStatement) statements.get(1);
+ // final Expression concat = f.getTargetFileName();
+ // assertNotNull(concat);
+ // statements = Arrays.asList(f.getBody());
+ // assertEquals(5, statements.size());
+ // assertEquals("Text und so ", ((TextStatement)
+ // statements.get(0)).getValue());
+ // }
+ //
+ // public final void testImportDeclaration() throws Exception {
+ // Template t;
+ // t = parse(tag("IMPORT org::eclipse::xtend") + tag("IMPORT
+ // org::eclipse::xtend::xpand")
+ // + tag("DEFINE test FOR ecore::EClass") + tag("ENDDEFINE"));
+ // assertEquals(1, t.getDefinitions().length);
+ // assertEquals(2, t.getImports().length);
+ //
+ // }
+ //
+ // public final void testFileStatement() throws Exception {
+ // final Template t = parse(tag("DEFINE test FOR ecore::EClass") + tag("FILE
+ // \"test.txt\" ONCE") + tag("ENDFILE")
+ // + tag("ENDDEFINE"));
+ // assertEquals(1, t.getDefinitions().length);
+ // final FileStatement file = (FileStatement) ((Definition)
+ // t.getDefinitions()[0]).getBody()[1];
+ // assertEquals("ONCE", file.getMode().getValue());
+ // }
+ //
+ // public final void testIfStatement() throws Exception {
+ // final Template t = parse(tag("DEFINE test FOR ecore::EClass") + tag("IF
+ // !true") + tag("ELSEIF false")
+ // + tag("ELSE") + tag("ENDIF") + tag("ENDDEFINE"));
+ // assertEquals(1, t.getDefinitions().length);
+ //
+ // final IfStatement ifSt = (IfStatement) ((Definition)
+ // t.getDefinitions()[0]).getBody()[1];
+ // assertNotNull(ifSt);
+ // assertNotNull(ifSt.getCondition());
+ // assertNotNull(ifSt.getElseIf());
+ // assertNotNull(ifSt.getElseIf().getCondition());
+ // assertNotNull(ifSt.getElseIf().getElseIf());
+ // assertNull(ifSt.getElseIf().getElseIf().getCondition());
+ // }
+ //
+ // public void testLocation() throws Exception {
+ // String defineStart = tag("DEFINE test FOR ecore::EClass");
+ // String ifStmnt = tag("IF !true") + tag("ELSEIF false")
+ // + tag("ELSE") + tag("ENDIF");
+ // String string = defineStart + ifStmnt + tag("ENDDEFINE");
+ // final Template t = parse(string);
+ // assertEquals(0,t.getStart());
+ // assertEquals(string.length(),t.getEnd());
+ //
+ // Definition xpandDefinition = (Definition) t.getDefinitions()[0];
+ // assertEquals(1,xpandDefinition.getStart());
+ // assertEquals(string.length()-1,xpandDefinition.getEnd());
+ //
+ // Statement statement = xpandDefinition.getBody()[1];
+ // assertEquals(defineStart.length()+1,statement.getStart());
+ // assertEquals(defineStart.length()+ifStmnt.length()-1,statement.getEnd());
+ // }
+
+} \ No newline at end of file
diff --git a/tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/parser/Xpand3MigratedNodeParserTest.java b/tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/parser/Xpand3MigratedNodeParserTest.java
index 8611c56..cd20dba 100644
--- a/tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/parser/Xpand3MigratedNodeParserTest.java
+++ b/tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/parser/Xpand3MigratedNodeParserTest.java
@@ -1,79 +1,19 @@
package org.eclipse.xpand3.parser;
-import java.util.List;
-import junit.framework.TestCase;
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CommonTokenStream;
import org.eclipse.xpand3.node.CompositeNode;
-import org.eclipse.xpand3.node.LeafNode;
-import org.eclipse.xpand3.node.LexedToken;
import org.eclipse.xpand3.node.Node;
import org.eclipse.xpand3.node.NodeUtil;
-public class Xpand3MigratedNodeParserTest extends TestCase {
-
- private CompositeNode parse(String s) throws Exception {
- ANTLRStringStream stream = new ANTLRStringStream(s);
- Xpand3Lexer lexer = new Xpand3Lexer(stream);
- CommonTokenStream tokenStream = new CommonTokenStream(lexer);
- AbstractXpand3NodeParser xpand3NodeParser = new Xpand3NodeParser(
- tokenStream);
- xpand3NodeParser.test_expression();
- Node rootNode = xpand3NodeParser.getRootNode();
- System.out.println(NodeUtil.toString(rootNode));
- // System.out.println(NodeUtil.serialize(rootNode));
- return (CompositeNode) rootNode;
- }
-
- private 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 CompositeNode checkChildIsRule(Node parent, int index,
- String ruleName, int numChildren) {
- return checkIsRule(getChild(parent, index), ruleName, numChildren);
- }
-
- private CompositeNode checkChildIsIdentifier(Node parent, int index,
- String identifierName) {
- CompositeNode st = checkChildIsRule(parent, index, "r_simpleType", 1);
- CompositeNode i = checkChildIsRule(st, 0, "r_identifier", 1);
- checkChildIsToken(i, 0, identifierName);
- return st;
- }
-
- 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 LeafNode checkChildIsToken(Node parent, int index, String tokenText) {
- return checkIsToken(getChild(parent, index), tokenText);
- }
-
- 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;
- }
+public class Xpand3MigratedNodeParserTest extends AbstractXpand3NodeParserTest {
public final void testSimple() throws Exception {
CompositeNode parse = parse("true == null");
CompositeNode cn = checkIsRule(parse, "r_relationalExpression", 3);
- CompositeNode bl = checkChildIsRule(cn, 0, "r_booleanLiteral", 1);
- checkChildIsToken(bl, 0, "true");
+ checkChildIsTrueLiteral(cn, 0);
checkChildIsToken(cn, 1, "==");
CompositeNode nl = checkChildIsRule(cn, 2, "r_nullLiteral", 1);
checkChildIsToken(nl, 0, "null");
@@ -83,25 +23,21 @@ public class Xpand3MigratedNodeParserTest extends TestCase {
CompositeNode ie = checkIsRule(
parse("String.feature.test(true,{\"test\",\"hallo\"})"),
"r_infixExpression", 5);
- checkChildIsIdentifier(ie, 0, "String");
+ checkChildIsSimpleType(ie, 0, "String");
checkChildIsToken(ie, 1, ".");
- checkChildIsIdentifier(ie, 2, "feature");
+ checkChildIsSimpleType(ie, 2, "feature");
checkChildIsToken(ie, 3, ".");
CompositeNode fc = checkChildIsRule(ie, 4, "r_featureCall", 4);
- CompositeNode i3 = checkChildIsRule(fc, 0, "r_identifier", 1);
- checkChildIsToken(i3, 0, "test");
+ checkChildIsIdentifier(fc, 0, "test");
checkChildIsToken(fc, 1, "(");
CompositeNode pl = checkChildIsRule(fc, 2, "r_parameterList", 3);
- CompositeNode bl = checkChildIsRule(pl, 0, "r_booleanLiteral", 1);
- checkChildIsToken(bl, 0, "true");
+ checkChildIsTrueLiteral(pl, 0);
checkChildIsToken(pl, 1, ",");
CompositeNode ll = checkChildIsRule(pl, 2, "r_listLiteral", 5);
checkChildIsToken(ll, 0, "{");
- CompositeNode pe1 = checkChildIsRule(ll, 1, "r_stringLiteral", 1);
- checkChildIsToken(pe1, 0, "\"test\"");
+ checkChildIsStringLiteral(ll, 1, "\"test\"");
checkChildIsToken(ll, 2, ",");
- CompositeNode pe2 = checkChildIsRule(ll, 3, "r_stringLiteral", 1);
- checkChildIsToken(pe2, 0, "\"hallo\"");
+ checkChildIsStringLiteral(ll, 3, "\"hallo\"");
checkChildIsToken(ll, 4, "}");
checkChildIsToken(fc, 3, ")");
}
@@ -114,35 +50,31 @@ public class Xpand3MigratedNodeParserTest extends TestCase {
checkChildIsToken(pe, 0, "(");
CompositeNode re = checkChildIsRule(pe, 1, "r_relationalExpression", 3);
CompositeNode ie0 = checkChildIsRule(re, 0, "r_infixExpression", 3);
- checkChildIsIdentifier(ie0, 0, "client");
+ checkChildIsSimpleType(ie0, 0, "client");
checkChildIsToken(ie0, 1, ".");
- checkChildIsIdentifier(ie0, 2, "sIdent1");
+ checkChildIsSimpleType(ie0, 2, "sIdent1");
checkChildIsToken(re, 1, "!=");
CompositeNode nl = checkChildIsRule(re, 2, "r_nullLiteral", 1);
checkChildIsToken(nl, 0, "null");
checkChildIsToken(pe, 2, ")");
checkChildIsToken(ie, 1, "?");
CompositeNode ie1 = checkChildIsRule(ie, 2, "r_infixExpression", 3);
- checkChildIsIdentifier(ie1, 0, "client");
+ checkChildIsSimpleType(ie1, 0, "client");
checkChildIsToken(ie1, 1, ".");
- checkChildIsIdentifier(ie1, 2, "sIdent1");
+ checkChildIsSimpleType(ie1, 2, "sIdent1");
checkChildIsToken(ie, 3, ":");
- CompositeNode pe1 = checkChildIsRule(ie, 4, "r_stringLiteral", 1);
- checkChildIsToken(pe1, 0, "\"XXXXXXXX\"");
+ checkChildIsStringLiteral(ie, 4, "\"XXXXXXXX\"");
}
public void testIfExpression2() throws Exception {
Node expr = parse("if true then true else false");
checkIsRule(expr, "r_ifExpression", 6);
checkChildIsToken(expr, 0, "if");
- CompositeNode bl0 = checkChildIsRule(expr, 1, "r_booleanLiteral", 1);
- checkChildIsToken(bl0, 0, "true");
+ checkChildIsTrueLiteral(expr, 1);
checkChildIsToken(expr, 2, "then");
- CompositeNode bl1 = checkChildIsRule(expr, 3, "r_booleanLiteral", 1);
- checkChildIsToken(bl1, 0, "true");
+ checkChildIsTrueLiteral(expr, 3);
checkChildIsToken(expr, 4, "else");
- CompositeNode bl2 = checkChildIsRule(expr, 5, "r_booleanLiteral", 1);
- checkChildIsToken(bl2, 0, "false");
+ checkChildIsFalseLiteral(expr, 5);
}
public final void testEscaped() throws Exception {
@@ -164,77 +96,100 @@ public class Xpand3MigratedNodeParserTest extends TestCase {
CompositeNode ct = checkChildIsRule(expr, 1, "r_collectionType", 4);
checkChildIsToken(ct, 0, "List");
checkChildIsToken(ct, 1, "[");
- checkChildIsIdentifier(ct, 2, "InnerType");
+ checkChildIsSimpleType(ct, 2, "InnerType");
checkChildIsToken(ct, 3, "]");
checkChildIsToken(expr, 2, ")");
- checkChildIsIdentifier(expr, 3, "anExpr");
+ checkChildIsSimpleType(expr, 3, "anExpr");
}
public final void testSwitch() throws Exception {
Node expr = parse("switch { default : true }");
+ checkIsRule(expr, "r_switchExpression", 6);
+ checkChildIsToken(expr, 0, "switch");
+ checkChildIsToken(expr, 1, "{");
+ checkChildIsToken(expr, 2, "default");
+ checkChildIsToken(expr, 3, ":");
+ checkChildIsTrueLiteral(expr, 4);
+ checkChildIsToken(expr, 5, "}");
+
+ }
+
+ public final void testChainExpression() throws Exception {
+ Node expr = parse("1 -> 2 -> 3 -> 4");
+ checkIsRule(expr, "r_chainExpression", 7);
+ checkChildIsNumberLiteral(expr, 0, "1");
+ checkChildIsToken(expr, 1, "->");
+ checkChildIsNumberLiteral(expr, 2, "2");
+ checkChildIsToken(expr, 3, "->");
+ checkChildIsNumberLiteral(expr, 4, "3");
+ checkChildIsToken(expr, 5, "->");
+ checkChildIsNumberLiteral(expr, 6, "4");
+ }
+
+ public final void testPositionInfo() throws Exception {
+ Node expr = parse("\n\n\n1");
+ assertEquals(4, expr.line());
+ assertEquals(3, expr.start());
+ assertEquals(4, expr.end());
+ }
+
+ public final void testPositionInfo2() throws Exception {
+ Node exp = parse("/*\n\n\n*/1");
+ assertEquals(4, exp.line());
+ assertEquals(7, exp.start());
+ assertEquals(8, exp.end());
+ }
+ public final void testPositionInfo3() throws Exception {
+ Node expr = parse("'/*\n\n\n*/'+1");
+ assertEquals(1, expr.line());
+ assertEquals(0, expr.start());
+ assertEquals(11, expr.end());
+ checkIsRule(expr, "r_additiveExpression", 3);
+ CompositeNode sl = checkChildIsStringLiteral(expr, 0, "'/*\n\n\n*/'");
+ assertEquals(1, sl.line());
+ assertEquals(0, sl.start());
+ assertEquals(9, sl.end());
+ checkChildIsToken(expr, 1, "+");
+ CompositeNode il = checkChildIsNumberLiteral(expr, 2, "1");
+ assertEquals(4, il.line());
+ assertEquals(10, il.start());
+ assertEquals(11, il.end());
+ }
+
+ public final void testTypeLiterals() throws Exception {
+ Node expr = parse("{" + " Object,\n" + " String,\n" + "Collection,\n"
+ + " Set,\n" + " List,\n" + " oaw::Type,\n" + " oaw::Feature,\n"
+ + " oaw::Property\n}");
+ checkIsRule(expr, "r_listLiteral", 17);
+ checkChildIsToken(expr, 0, "{");
+ checkChildIsSimpleType(expr, 1, "Object");
+ checkChildIsToken(expr, 2, ",");
+ checkChildIsSimpleType(expr, 3, "String");
+ checkChildIsToken(expr, 4, ",");
+ checkChildIsCollectionType(expr, 5, "Collection");
+ checkChildIsToken(expr, 6, ",");
+ checkChildIsCollectionType(expr, 7, "Set");
+ checkChildIsToken(expr, 8, ",");
+ checkChildIsCollectionType(expr, 9, "List");
+ checkChildIsToken(expr, 10, ",");
+ checkChildIsScopedType(expr, 11, "oaw", "Type");
+ checkChildIsToken(expr, 12, ",");
+ checkChildIsScopedType(expr, 13, "oaw", "Feature");
+ checkChildIsToken(expr, 14, ",");
+ checkChildIsScopedType(expr, 15, "oaw", "Property");
+ checkChildIsToken(expr, 16, "}");
+ }
+
+ private CompositeNode parse(String s) throws Exception {
+ ANTLRStringStream stream = new ANTLRStringStream(s);
+ Xpand3Lexer lexer = new Xpand3Lexer(stream);
+ CommonTokenStream tokenStream = new CommonTokenStream(lexer);
+ AbstractXpand3NodeParser xpand3NodeParser = new Xpand3NodeParser(
+ tokenStream);
+ xpand3NodeParser.test_expression();
+ Node rootNode = xpand3NodeParser.getRootNode();
+ System.out.println(NodeUtil.toString(rootNode));
+ return (CompositeNode) rootNode;
}
- // assertNull(expr.getSwitchExpr());
- // assertTrue(expr.getCases().isEmpty());
- // assertEquals("true", ((BooleanLiteral)
- // expr.getDefaultExpr()).getLiteralValue().getValue());
- //
- // expr = (SwitchExpression) parse("switch (\"test\") { case \"horst\":
- // false default : true }");
- // assertEquals("\"test\"", ((StringLiteral)
- // expr.getSwitchExpr()).getLiteralValue().getValue());
- // final Case c = expr.getCases().get(0);
- // assertEquals("\"horst\"", ((StringLiteral)
- // c.getCondition()).getLiteralValue().getValue());
- // assertEquals("false", ((BooleanLiteral)
- // c.getThenPart()).getLiteralValue().getValue());
- //
- // assertEquals("true", ((BooleanLiteral)
- // expr.getDefaultExpr()).getLiteralValue().getValue());
- // }
- //
- // public final void testChainExpression() {
- // final ChainExpression expr = (ChainExpression) parse("1 -> 2 -> 3 -> 4");
- // assertEquals("4", expr.getNext().toString());
- // assertEquals("1->2->3", expr.getFirst().toString());
- // }
- //
- // public final void testPositionInfo() {
- // final Expression exp = parse("\n\n\n1");
- // assertEquals(4, exp.getLine());
- // assertEquals(3, exp.getStart());
- // assertEquals(4, exp.getEnd());
- //
- // }
- //
- // public final void testPositionInfo2() {
- // final Expression exp = parse("/*\n\n\n*/1");
- // assertEquals(4, exp.getLine());
- // assertEquals(7, exp.getStart());
- // assertEquals(8, exp.getEnd());
- // }
- //
- // public final void testPositionInfo3() {
- // final OperationCall exp = (OperationCall) parse("'/*\n\n\n*/'+1");
- // assertEquals(1, exp.getLine());
- // assertEquals(0, exp.getStart());
- // assertEquals(11, exp.getEnd());
- // final StringLiteral target = (StringLiteral) exp.getTarget();
- // assertEquals(1, target.getLine());
- // assertEquals(0, target.getStart());
- // assertEquals(9, target.getEnd());
- // final IntegerLiteral param = (IntegerLiteral) exp.getParams()[0];
- // assertEquals(4, param.getLine());
- // assertEquals(10, param.getStart());
- // assertEquals(11, param.getEnd());
- // }
- //
- // public final void testTypeLiterals() {
- // final Expression e = parse("{" + " Object,\n" + " String,\n" +
- // "Collection,\n" + " Set,\n" + " List,\n"
- // + " oaw::Type,\n" + " oaw::Feature,\n" + " oaw::Property\n}");
- //
- // assertNotNull(e);
- // }
- //
}
diff --git a/tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/parser/Xpand3ScannerTest.java b/tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/parser/Xpand3ScannerTest.java
new file mode 100644
index 0000000..f16d83e
--- /dev/null
+++ b/tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/parser/Xpand3ScannerTest.java
@@ -0,0 +1,59 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 2007 committers of openArchitectureWare and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * committers of openArchitectureWare - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.xpand3.parser;
+
+import junit.framework.TestCase;
+
+import org.antlr.runtime.ANTLRStringStream;
+import org.antlr.runtime.RecognitionException;
+
+/**
+ * @author Sven Efftinge (http://www.efftinge.de)
+ * @author Arno Haase
+ */
+public class Xpand3ScannerTest extends TestCase {
+
+ public Xpand3Lexer scan(final String txt) {
+ return new Xpand3Lexer(new ANTLRStringStream(txt)) {
+ @Override
+ public void reportError(RecognitionException e) {
+ throw new RuntimeException(e);
+ }
+ };
+ }
+
+ public final void testString() throws Exception {
+ test("\"test\"");
+ test("\"test\"");
+ test("\"te\nst\"");
+ test("\"te\\nst\"");
+ test("'test'");
+ test("'test'");
+ test("'te\nst'");
+ test("'te\\nst'");
+ try {
+ test("'unterminated");
+ fail();
+ } catch (final Exception e) {
+ // expected
+ }
+ }
+
+ public void testIdentifier() throws Exception {
+ System.out.println(scan("sortBy").nextToken().getType());
+ System.out.println(scan("^sortBy").nextToken().getType());
+ }
+
+ private void test(final String txt) throws Exception {
+ assertEquals(txt, scan(txt).nextToken().getText());
+ }
+}