summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjkohnlein2008-03-07 11:20:07 (EST)
committerjkohnlein2008-03-07 11:20:07 (EST)
commit5543fc3f3d78d816f5b347ed047885163678265b (patch)
tree33439f796c621221d51f7e8a8e46d009f40d3e11
parente88b18456d0ce048c8341244e99366ed7f59fea4 (diff)
downloadorg.eclipse.xpand-5543fc3f3d78d816f5b347ed047885163678265b.zip
org.eclipse.xpand-5543fc3f3d78d816f5b347ed047885163678265b.tar.gz
org.eclipse.xpand-5543fc3f3d78d816f5b347ed047885163678265b.tar.bz2
First shot at treetest
-rw-r--r--plugins/org.eclipse.xpand3.parser/src/org/eclipse/xpand3/parser/node2ast/Node2AstTransformer.java4
-rw-r--r--tests/org.eclipse.xpand3.parser.tests/META-INF/MANIFEST.MF3
-rw-r--r--tests/org.eclipse.xpand3.parser.tests/src/org/eclipse/xpand3/parser/node2ast/Node2AstTest.java15
-rw-r--r--tests/org.eclipse.xpand3.parser.tests/src/org/eclipse/xpand3/parser/node2ast/TreetestInterpreterTest.java134
-rw-r--r--tests/org.eclipse.xpand3.parser.tests/src/org/eclipse/xpand3/parser/node2ast/test.ttst36
-rw-r--r--tests/org.eclipse.xpand3.parser.tests/src/org/eclipse/xpand3/parser/node2ast/test.txt2
6 files changed, 187 insertions, 7 deletions
diff --git a/plugins/org.eclipse.xpand3.parser/src/org/eclipse/xpand3/parser/node2ast/Node2AstTransformer.java b/plugins/org.eclipse.xpand3.parser/src/org/eclipse/xpand3/parser/node2ast/Node2AstTransformer.java
index 7cf76ae..c16c1d0 100644
--- a/plugins/org.eclipse.xpand3.parser/src/org/eclipse/xpand3/parser/node2ast/Node2AstTransformer.java
+++ b/plugins/org.eclipse.xpand3.parser/src/org/eclipse/xpand3/parser/node2ast/Node2AstTransformer.java
@@ -163,8 +163,8 @@ public class Node2AstTransformer extends Xpand3nodeSwitch<SyntaxElement> {
}
}
- private OperationCall createFeatureCall(SimpleTypeNode stn) {
- OperationCall oc = expressionFactory.createOperationCall();
+ private FeatureCall createFeatureCall(SimpleTypeNode stn) {
+ FeatureCall oc = expressionFactory.createFeatureCall();
setPosition(oc, stn);
oc.setName((Identifier) caseSimpleTypeNode(stn));
return oc;
diff --git a/tests/org.eclipse.xpand3.parser.tests/META-INF/MANIFEST.MF b/tests/org.eclipse.xpand3.parser.tests/META-INF/MANIFEST.MF
index 9b66c47..d52e44d 100644
--- a/tests/org.eclipse.xpand3.parser.tests/META-INF/MANIFEST.MF
+++ b/tests/org.eclipse.xpand3.parser.tests/META-INF/MANIFEST.MF
@@ -6,4 +6,5 @@ Bundle-Version: 1.0.0
Bundle-RequiredExecutionEnvironment: J2SE-1.5
Require-Bundle: org.eclipse.xpand3.parser;bundle-version="1.0.0",
org.junit;bundle-version="3.8.2",
- org.eclipse.tmf.common.runtime;bundle-version="1.0.0"
+ org.eclipse.tmf.common.runtime;bundle-version="1.0.0",
+ org.eclipse.tmf.common.treetest;bundle-version="1.0.0"
diff --git a/tests/org.eclipse.xpand3.parser.tests/src/org/eclipse/xpand3/parser/node2ast/Node2AstTest.java b/tests/org.eclipse.xpand3.parser.tests/src/org/eclipse/xpand3/parser/node2ast/Node2AstTest.java
index 87b68e9..a627387 100644
--- a/tests/org.eclipse.xpand3.parser.tests/src/org/eclipse/xpand3/parser/node2ast/Node2AstTest.java
+++ b/tests/org.eclipse.xpand3.parser.tests/src/org/eclipse/xpand3/parser/node2ast/Node2AstTest.java
@@ -4,16 +4,27 @@ import junit.framework.TestCase;
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CommonTokenStream;
+import org.eclipse.emf.ecore.EPackage;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import org.eclipse.tmf.common.node.Node;
import org.eclipse.tmf.common.node.NodeUtil;
+import org.eclipse.tmf.common.treetest.TreetestPackage;
import org.eclipse.xpand3.SyntaxElement;
-import org.eclipse.xpand3.declaration.Extension;
import org.eclipse.xpand3.parser.Xpand3NodeLexer;
import org.eclipse.xpand3.parser.Xpand3NodeParser;
public class Node2AstTest extends TestCase {
private SyntaxElement parseAndTransform(String s) throws Exception {
+ ClassLoader classLoader = getClass().getClassLoader();
+ Thread.currentThread().setContextClassLoader(classLoader);
+ Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
+ "ecore", new XMIResourceFactoryImpl());
+
+ EPackage treePackage = TreetestPackage.eINSTANCE;
+ EPackage.Registry.INSTANCE.put(treePackage.getNsURI(), treePackage);
+
System.out.println(s);
ANTLRStringStream stream = new ANTLRStringStream(s);
Xpand3NodeLexer lexer = new Xpand3NodeLexer(stream);
@@ -33,6 +44,6 @@ public class Node2AstTest extends TestCase {
public void testExpression() throws Exception {
String expr = "foo(Object this, Object that):\n\tthis.toString()==that.toString();";
SyntaxElement ast = parseAndTransform(expr);
- assertTrue(ast instanceof Extension);
+
}
}
diff --git a/tests/org.eclipse.xpand3.parser.tests/src/org/eclipse/xpand3/parser/node2ast/TreetestInterpreterTest.java b/tests/org.eclipse.xpand3.parser.tests/src/org/eclipse/xpand3/parser/node2ast/TreetestInterpreterTest.java
new file mode 100644
index 0000000..f32983b
--- /dev/null
+++ b/tests/org.eclipse.xpand3.parser.tests/src/org/eclipse/xpand3/parser/node2ast/TreetestInterpreterTest.java
@@ -0,0 +1,134 @@
+package org.eclipse.xpand3.parser.node2ast;
+
+import java.io.InputStream;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.antlr.runtime.ANTLRStringStream;
+import org.antlr.runtime.CommonTokenStream;
+import org.antlr.runtime.RecognitionException;
+import org.eclipse.emf.common.util.EList;
+import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EPackage;
+import org.eclipse.emf.ecore.EStructuralFeature;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
+import org.eclipse.tmf.common.node.Node;
+import org.eclipse.tmf.common.treetest.Body;
+import org.eclipse.tmf.common.treetest.Expression;
+import org.eclipse.tmf.common.treetest.NodeRef;
+import org.eclipse.tmf.common.treetest.PropertyCheck;
+import org.eclipse.tmf.common.treetest.PropertyList;
+import org.eclipse.tmf.common.treetest.Test;
+import org.eclipse.tmf.common.treetest.TestSpec;
+import org.eclipse.tmf.common.treetest.TreetestPackage;
+import org.eclipse.tmf.common.treetest.parser.XtextParser;
+import org.eclipse.xpand3.SyntaxElement;
+import org.eclipse.xpand3.parser.Xpand3NodeLexer;
+import org.eclipse.xpand3.parser.Xpand3NodeParser;
+
+public class TreetestInterpreterTest extends TestCase {
+
+ @Override
+ protected void setUp() throws Exception {
+ ClassLoader classLoader = getClass().getClassLoader();
+ Thread.currentThread().setContextClassLoader(classLoader);
+ Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
+ "ecore", new XMIResourceFactoryImpl());
+
+ EPackage treePackage = TreetestPackage.eINSTANCE;
+ EPackage.Registry.INSTANCE.put(treePackage.getNsURI(), treePackage);
+ }
+
+ public void testTree() throws RecognitionException {
+ test("org/eclipse/xpand3/parser/node2ast/test.tree");
+ }
+
+ private void test(String testFileName) throws RecognitionException {
+ ClassLoader classLoader = getClass().getClassLoader();
+ InputStream testFile = classLoader.getResourceAsStream(testFileName);
+ XtextParser treeSpecParser = new XtextParser(testFile);
+ TestSpec testSpec = (TestSpec) treeSpecParser.getRootNode()
+ .getModelElement();
+ System.out.println("Loading test spec from " + testSpec.getName());
+ EList<Test> tests = testSpec.getTest();
+ for (Test test : tests) {
+ System.out.println("Starting test " + test.getName());
+ Expression testExpression = test.getExpr();
+ SyntaxElement rootElement = parseAndTransform(testExpression);
+ performTest(rootElement, test.getExpected());
+ }
+
+ }
+
+ private SyntaxElement parseAndTransform(Expression testExpression)
+ throws RecognitionException {
+ String body = testExpression.getBody();
+ body = body.substring(2, body.length() - 2);
+ System.out.println("Expression:" + body);
+ ANTLRStringStream stream = new ANTLRStringStream(body);
+ Xpand3NodeLexer lexer = new Xpand3NodeLexer(stream);
+ CommonTokenStream tokenStream = new CommonTokenStream(lexer);
+ Xpand3NodeParser xpand3NodeParser = new Xpand3NodeParser(tokenStream);
+
+ Node rootNode = xpand3NodeParser.r_file();
+ if (rootNode == null) {
+ System.out.println("Nothing parsed.");
+ }
+ Node2AstTransformer node2AstTransformer = new Node2AstTransformer();
+ return node2AstTransformer.doSwitch(rootNode);
+ }
+
+ @SuppressWarnings("unchecked")
+ protected void performTest(EObject object,
+ org.eclipse.tmf.common.treetest.Node testRootNode) {
+ EClass eClass = object.eClass();
+ assertEquals(eClass.getName(), testRootNode.getClassName());
+ PropertyList propertyList = testRootNode.getPropertyList();
+ if (propertyList != null) {
+ for (PropertyCheck propertyCheck : propertyList.getChecks()) {
+ String name = propertyCheck.getName();
+ EStructuralFeature attribute = eClass
+ .getEStructuralFeature(name);
+ String value = object.eGet(attribute).toString();
+ assertEquals(propertyCheck.getValue(), value);
+ }
+ }
+ Body body = testRootNode.getBody();
+ if (body != null) {
+ EList<NodeRef> nodeRefs = body.getChildren();
+ for (int i = 0; i < nodeRefs.size(); ++i) {
+ NodeRef nodeRef = nodeRefs.get(i);
+ EStructuralFeature reference = eClass
+ .getEStructuralFeature(nodeRefs.get(i).getRefName());
+ Object value = object.eGet(reference);
+ EList<org.eclipse.tmf.common.treetest.Node> refNodes = nodeRef
+ .getNodes();
+ if (reference.isMany()) {
+ List listValue = (List) value;
+ if (refNodes.isEmpty()) {
+ assertTrue(listValue == null || listValue.isEmpty());
+ return;
+ }
+ assertTrue(refNodes.size() <= listValue.size());
+ for (int j = 0; j < refNodes.size(); ++j) {
+ assertTrue(listValue.get(j) instanceof EObject);
+ performTest((EObject) listValue.get(j), refNodes.get(j));
+ }
+ } else {
+ if (refNodes.isEmpty()) {
+ assertTrue(value == null);
+ return;
+ }
+ assertTrue(refNodes.size() <= 1);
+ org.eclipse.tmf.common.treetest.Node refNode = refNodes
+ .get(0);
+ assertTrue(value instanceof EObject);
+ performTest((EObject) value, refNode);
+ }
+ }
+ }
+ }
+}
diff --git a/tests/org.eclipse.xpand3.parser.tests/src/org/eclipse/xpand3/parser/node2ast/test.ttst b/tests/org.eclipse.xpand3.parser.tests/src/org/eclipse/xpand3/parser/node2ast/test.ttst
new file mode 100644
index 0000000..b3c4bb4
--- /dev/null
+++ b/tests/org.eclipse.xpand3.parser.tests/src/org/eclipse/xpand3/parser/node2ast/test.ttst
@@ -0,0 +1,36 @@
+testCase AstTests
+test firstTest
+expr <#
+foo(Object this, Object that):
+ this.toString()==that.toString(); #>
+expected
+ Extension {
+ name=Identifier(value='foo')
+ params=[
+ DeclaredParameter {
+ name=Identifier(value='this')
+ type=Identifier(value='Object')
+ },
+ DeclaredParameter {
+ name=Identifier(value='that')
+ type=Identifier(value='Object')
+ }]
+ body=BooleanOperation{
+ left=OperationCall {
+ name=Identifier(value="toString")
+ target=FeatureCall {
+ name=Identifier(value="this")
+ target=null
+ }
+ }
+ operator=Identifier(value="==")
+ right=OperationCall {
+ name=Identifier(value="toString")
+ target=FeatureCall {
+ name=Identifier(value="that")
+ target=null
+ }
+ }
+ }
+ }
+
diff --git a/tests/org.eclipse.xpand3.parser.tests/src/org/eclipse/xpand3/parser/node2ast/test.txt b/tests/org.eclipse.xpand3.parser.tests/src/org/eclipse/xpand3/parser/node2ast/test.txt
deleted file mode 100644
index 0b93021..0000000
--- a/tests/org.eclipse.xpand3.parser.tests/src/org/eclipse/xpand3/parser/node2ast/test.txt
+++ /dev/null
@@ -1,2 +0,0 @@
-foo(Object this, Object that):
- this.toString()==that.toString();