summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMoritz Eysholdt2011-08-05 09:37:19 (EDT)
committer Ed Merks2011-08-05 09:37:34 (EDT)
commitc6cf72698b2d0eaafdba21dcdac66648dec46fda (patch)
tree59afdd803284aab58be8580c5dffeda4f420029d
parent4170dd836c05811675844da7f3d978620048ed19 (diff)
downloadorg.eclipse.emf-c6cf72698b2d0eaafdba21dcdac66648dec46fda.zip
org.eclipse.emf-c6cf72698b2d0eaafdba21dcdac66648dec46fda.tar.gz
org.eclipse.emf-c6cf72698b2d0eaafdba21dcdac66648dec46fda.tar.bz2
improved generic formatting of test data
-rw-r--r--org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/EObjectFormatter.java177
-rw-r--r--org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/genmodel/GenFeatures1.xcore20
-rw-r--r--org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/genmodel/GenPackage1.xcore26
-rw-r--r--org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/genmodel/XcoreGenModelTest.java6
4 files changed, 203 insertions, 26 deletions
diff --git a/org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/EObjectFormatter.java b/org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/EObjectFormatter.java
new file mode 100644
index 0000000..f98a674
--- /dev/null
+++ b/org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/EObjectFormatter.java
@@ -0,0 +1,177 @@
+/*******************************************************************************
+ * Copyright (c) 2011 itemis AG (http://www.itemis.eu) 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
+ *******************************************************************************/
+package org.eclipse.emf.ecore.xcore.tests;
+
+import java.util.List;
+
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.EAttribute;
+import org.eclipse.emf.ecore.EFactory;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EReference;
+import org.eclipse.emf.ecore.EStructuralFeature;
+import org.eclipse.emf.ecore.InternalEObject;
+import org.eclipse.emf.ecore.util.EcoreUtil;
+
+import com.google.common.base.Function;
+import com.google.common.base.Joiner;
+import com.google.common.collect.Iterables;
+
+/**
+ * @author Moritz Eysholdt - Initial contribution and API
+ *
+ * TODO: move this to xtext.util
+ */
+public class EObjectFormatter implements Function<EObject, String>
+{
+
+ protected boolean resolveCrossReferences = false;
+
+ protected boolean showIndex = false;
+
+ public String apply(EObject from)
+ {
+ return format(from);
+ }
+
+ protected String assignmentOperator(EObject object, EStructuralFeature feature)
+ {
+ if (feature instanceof EReference && !((EReference) feature).isContainment())
+ return "->";
+ else
+ return "=";
+ }
+
+ public String format(EObject object)
+ {
+ StringBuilder result = new StringBuilder();
+ result.append(object.eClass().getName());
+ result.append(" {");
+ for (EStructuralFeature feature : object.eClass().getEAllStructuralFeatures())
+ if (shouldFormat(object, feature))
+ result.append(indent("\n" + format(object, feature)));
+ result.append("\n}");
+ return result.toString();
+ }
+
+ protected String format(EObject object, EStructuralFeature feature)
+ {
+ StringBuilder result = new StringBuilder();
+ result.append(feature.getName());
+ result.append(" ");
+ result.append(assignmentOperator(object, feature));
+ result.append(" ");
+ Object val = object.eGet(feature, resolveCrossReferences);
+ if (feature.isMany())
+ {
+ result.append("[");
+ List<?> vals = (List<?>) val;
+ List<?> sortedVals = sort(object, feature, vals);
+ if (vals == sortedVals)
+ {
+ for (int i = 0; i < vals.size(); i++)
+ if (shouldFormat(object, feature, i, vals.get(i)))
+ result.append(indent("\n" + format(object, feature, i, vals.get(i))));
+ } else
+ {
+ for (int i = 0; i < sortedVals.size(); i++)
+ if (shouldFormat(object, feature, sortedVals.get(i)))
+ result.append(indent("\n" + format(object, feature, sortedVals.get(i))));
+ }
+ result.append("\n]");
+ } else
+ result.append(indent(format(object, feature, val)));
+ return result.toString();
+ }
+
+ protected String format(EObject object, EStructuralFeature feature, int index, Object value)
+ {
+ if (showIndex)
+ return index + ": " + format(object, feature, value);
+ return format(object, feature, value);
+ }
+
+ protected String format(EObject object, EStructuralFeature feature, Object value)
+ {
+ if (feature instanceof EAttribute)
+ return formatAttributeValue(object, (EAttribute) feature, value);
+ else if (feature instanceof EReference)
+ {
+ EReference ref = (EReference) feature;
+ if (ref.isContainment())
+ return format((EObject) value);
+ return formatCrossRefValue(object, ref, (EObject) value);
+ }
+ return "";
+ }
+
+ public String format(Iterable<? extends EObject> object)
+ {
+ return Joiner.on('\n').join(Iterables.transform(object, this));
+ }
+
+ protected String formatAttributeValue(EObject object, EAttribute feature, Object value)
+ {
+ if (value == null)
+ return "null";
+ EFactory factory = feature.getEAttributeType().getEPackage().getEFactoryInstance();
+ String stringVal = factory.convertToString(feature.getEAttributeType(), value);
+ return "'" + stringVal + "'";
+ }
+
+ protected String formatCrossRefValue(EObject object, EReference feature, EObject value)
+ {
+ if (value == null)
+ return "null";
+ if (value.eIsProxy())
+ return "proxy (URI: " + ((InternalEObject) value).eProxyURI() + ")";
+ if (value.eResource() == object.eResource())
+ return value.eClass().getName() + " " + object.eResource().getURIFragment(value);
+ URI uri = EcoreUtil.getURI(value);
+ uri = uri.deresolve(object.eResource().getURI());
+ return value.eClass().getName() + " " + uri.toString();
+ }
+
+ protected String indent(String string)
+ {
+ return string.replaceAll("\\n", "\n\t");
+ }
+
+ public EObjectFormatter resolveCrossReferences()
+ {
+ this.resolveCrossReferences = true;
+ return this;
+ }
+
+ protected boolean shouldFormat(EObject object, EStructuralFeature feature)
+ {
+ return !feature.isDerived() && object.eIsSet(feature);
+ }
+
+ protected boolean shouldFormat(EObject object, EStructuralFeature feature, int index, Object value)
+ {
+ return true;
+ }
+
+ protected boolean shouldFormat(EObject object, EStructuralFeature feature, Object value)
+ {
+ return true;
+ }
+
+ public EObjectFormatter showIndex()
+ {
+ this.showIndex = true;
+ return this;
+ }
+
+ protected List<?> sort(EObject obj, EStructuralFeature feature, List<?> values)
+ {
+ return values;
+ }
+
+}
diff --git a/org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/genmodel/GenFeatures1.xcore b/org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/genmodel/GenFeatures1.xcore
index 195244c..3d0aac9 100644
--- a/org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/genmodel/GenFeatures1.xcore
+++ b/org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/genmodel/GenFeatures1.xcore
@@ -4,22 +4,22 @@ class X
{
/* XPECT gen ---
GenFeature {
- attr EBoolean notify 'false'
- attr EBoolean createChild 'false'
- attr EBoolean propertySortChoices 'true'
- ref GenClass genClass ref: GenClass@/1/foo/X
- ref EStructuralFeature ecoreFeature 'x' ref: EReference@/2/X/x
+ notify = 'false'
+ createChild = 'false'
+ propertySortChoices = 'true'
+ genClass -> GenClass /1/foo/X
+ ecoreFeature -> EReference /2/X/x
}
--- */
refers X x opposite y
/* XPECT gen ---
GenFeature {
- attr EBoolean notify 'false'
- attr EBoolean createChild 'false'
- attr EBoolean propertySortChoices 'true'
- ref GenClass genClass ref: GenClass@/1/foo/X
- ref EStructuralFeature ecoreFeature 'y' ref: EReference@/2/X/y
+ notify = 'false'
+ createChild = 'false'
+ propertySortChoices = 'true'
+ genClass -> GenClass /1/foo/X
+ ecoreFeature -> EReference /2/X/y
}
--- */
refers X y opposite x
diff --git a/org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/genmodel/GenPackage1.xcore b/org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/genmodel/GenPackage1.xcore
index 65fded0..06e8eda 100644
--- a/org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/genmodel/GenPackage1.xcore
+++ b/org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/genmodel/GenPackage1.xcore
@@ -1,18 +1,18 @@
/* XPECT gen ---
GenPackage {
- attr EString prefix 'Foo'
- attr EBoolean disposableProviderFactory 'true'
- ref EPackage ecorePackage 'foo' ref: EPackage@/2
- ref GenModel genModel ref: GenModel@/1
- cref GenClass genClasses [
- 0: GenClass {
- ref GenPackage genPackage ref: GenPackage@/1/foo
- ref EClass ecoreClass 'Bar' ref: EClass@/2/Bar
- }
- ]
- ref GenClassifier genClassifiers [
- 0: GenClass@/1/foo/Bar
- ]
+ prefix = 'Foo'
+ disposableProviderFactory = 'true'
+ ecorePackage -> EPackage /2
+ genModel -> GenModel /1
+ genClasses = [
+ GenClass {
+ genPackage -> GenPackage /1/foo
+ ecoreClass -> EClass /2/Bar
+ }
+ ]
+ genClassifiers -> [
+ GenClass /1/foo/Bar
+ ]
}
---
*/
diff --git a/org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/genmodel/XcoreGenModelTest.java b/org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/genmodel/XcoreGenModelTest.java
index 717f95f..2f4ae1c 100644
--- a/org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/genmodel/XcoreGenModelTest.java
+++ b/org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/genmodel/XcoreGenModelTest.java
@@ -5,6 +5,7 @@ import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.xcore.XNamedElement;
import org.eclipse.emf.ecore.xcore.XcoreInjectorProvider;
import org.eclipse.emf.ecore.xcore.mappings.XcoreMapper;
+import org.eclipse.emf.ecore.xcore.tests.EObjectFormatter;
import org.eclipse.xtext.junit4.InjectWith;
import org.eclipse.xtext.junit4.parameterized.ParameterizedXtextRunner;
import org.eclipse.xtext.junit4.parameterized.ResourceURIs;
@@ -12,7 +13,6 @@ import org.eclipse.xtext.nodemodel.ILeafNode;
import org.eclipse.xtext.nodemodel.INode;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.eclipse.xtext.parsetree.reconstr.impl.NodeIterator;
-import org.eclipse.xtext.util.EmfFormatter;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -43,7 +43,7 @@ public class XcoreGenModelTest
}
this.expected = expected;
}
-
+
@Inject
private XcoreMapper mapper = new XcoreMapper();
@@ -53,7 +53,7 @@ public class XcoreGenModelTest
EObject obj = NodeModelUtils.findActualSemanticObjectFor(leaf);
EcoreUtil.resolveAll(obj.eResource());
EObject gen = mapper.getGen((XNamedElement) obj);
- return EmfFormatter.objToStr(gen);
+ return new EObjectFormatter().resolveCrossReferences().format(gen);
}
}