Applied patch from heiko

This commit is contained in:
jkohnlein 2008-09-22 09:21:51 +00:00 committed by sefftinge
parent 902691d3e5
commit f7f1591998
2 changed files with 118 additions and 8 deletions

View file

@ -40,6 +40,7 @@ public class AutoTestSuite {
suite.addTestSuite(org.eclipse.xtext.parsetree.reconstr.WhitespacePreservingCallbackTest.class);
suite.addTestSuite(org.eclipse.xtext.reference.CommentOnEofBug_234135_Test.class);
suite.addTestSuite(org.eclipse.xtext.reference.LeafNodeBug_234132_Test.class);
suite.addTestSuite(org.eclipse.xtext.resource.metamodel.Xtext2EcoreTransformerTests.class);
suite.addTestSuite(org.eclipse.xtext.typeresolution.XtextUtilConstructionTest.class);
suite.addTestSuite(org.eclipse.xtext.xtext2ecore.ActionTest.class);
suite.addTestSuite(org.eclipse.xtext.xtext2ecore.BootstrapModelTest.class);

View file

@ -7,11 +7,16 @@
*******************************************************************************/
package org.eclipse.xtext.resource.metamodel;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.impl.AESCipherImpl;
import org.eclipse.xtext.AbstractRule;
import org.eclipse.xtext.Grammar;
import org.eclipse.xtext.XtextStandaloneSetup;
@ -28,18 +33,122 @@ public class Xtext2EcoreTransformerTests extends AbstractGeneratorTest {
with(XtextStandaloneSetup.class);
}
public void testRuleWithoutExplicitReturnType() throws Exception {
Grammar grammar = (Grammar) getModel("language test generate test 'http://test' MyRule: myFeature=INT;");
EList<AbstractRule> rules = grammar.getRules();
AbstractRule firstRule = rules.get(0);
assertNull(firstRule.getType());
private EPackage getEPackageFromGrammar(String xtextGrammar)
throws Exception {
Grammar grammar = (Grammar) getModel(xtextGrammar);
Xtext2EcoreTransformer xtext2EcoreTransformer = new Xtext2EcoreTransformer();
List<EPackage> metamodels = xtext2EcoreTransformer.transform(grammar);
assertTrue(metamodels != null && !metamodels.isEmpty());
EPackage firstEPackage = metamodels.get(0);
EList<EClassifier> classifiers = firstEPackage.getEClassifiers();
assertNotNull(metamodels);
assertEquals(1, metamodels.size());
EPackage result = metamodels.get(0);
assertNotNull(result);
return result;
}
private void assertFeatureConfiguration(EClass eClass, int attributeIndex,
String featureName, String featureTypeName) {
EAttribute feature = eClass.getEAttributes().get(attributeIndex);
assertEquals(featureName, feature.getName());
assertNotNull(feature.getEType());
assertEquals(featureTypeName, feature.getEType().getName());
}
public void testTypesOfImplicitSuperGrammar() throws Exception {
final String xtextGrammar = "language test generate test 'http://test' MyRule: myFeature=INT;";
Grammar grammar = (Grammar) getModel(xtextGrammar);
Xtext2EcoreTransformer transformer = new Xtext2EcoreTransformer();
transformer.transform(grammar);
// directly from grammar
assertNotNull(transformer.getEClassifierInfos().getInfo("MyRule"));
// ecore data types
assertNotNull(transformer.getEClassifierInfos().getInfo("ecore::EString"));
assertNotNull(transformer.getEClassifierInfos().getInfo("ecore::EInt"));
}
public void testRuleWithoutExplicitReturnType() throws Exception {
final String grammar = "language test generate test 'http://test' MyRule: myFeature=INT;";
EPackage ePackage = getEPackageFromGrammar(grammar);
EList<EClassifier> classifiers = ePackage.getEClassifiers();
assertEquals(1, classifiers.size());
EClassifier implicitlyDefinedMetatype = classifiers.get(0);
assertEquals("MyRule", implicitlyDefinedMetatype.getName());
}
public void testRulesWithExplicitReturnType() throws Exception {
final String grammar = "language test generate test 'http://test' RuleA returns TypeA: featureA=INT; RuleB returns TypeB: featureB= INT;";
EPackage ePackage = getEPackageFromGrammar(grammar);
assertEquals(2, ePackage.getEClassifiers().size());
assertNotNull(ePackage.getEClassifier("TypeA"));
assertNotNull(ePackage.getEClassifier("TypeB"));
}
public void testSimpleHierarchy() throws Exception {
final String grammar = "language test generate test 'http://test' RuleA: RuleB; RuleB: featureB= INT;";
EPackage ePackage = getEPackageFromGrammar(grammar);
assertEquals(2, ePackage.getEClassifiers().size());
EClass ruleA = (EClass) ePackage.getEClassifier("RuleA");
assertNotNull(ruleA);
EClass ruleB = (EClass) ePackage.getEClassifier("RuleB");
assertNotNull(ruleB);
assertTrue(ruleA.getESuperTypes().isEmpty());
assertEquals(1, ruleB.getESuperTypes().size());
EClass superClass = ruleB.getESuperTypes().get(0);
assertEquals(ruleA, superClass);
}
public void testSingleFeatures() throws Exception {
final String grammar = "language test generate test 'http://test' RuleA: featureA=INT;";
EPackage ePackage = getEPackageFromGrammar(grammar);
EClass ruleA = (EClass) ePackage.getEClassifier("RuleA");
assertNotNull(ruleA);
assertEquals(1, ruleA.getEAttributes().size());
assertFeatureConfiguration(ruleA, 0, "featureA", "EInt");
}
public void testBuiltInFeatureTypes() throws Exception {
final String grammar = "language test generate test 'http://test' RuleA: featureA=ID featureB=INT featureC=STRING;";
EPackage ePackage = getEPackageFromGrammar(grammar);
EClass ruleA = (EClass) ePackage.getEClassifier("RuleA");
assertNotNull(ruleA);
assertEquals(3, ruleA.getEAttributes().size());
assertFeatureConfiguration(ruleA, 0, "featureA", "EString");
assertFeatureConfiguration(ruleA, 1, "featureB", "EInt");
assertFeatureConfiguration(ruleA, 2, "featureC", "EString");
}
public void testFeatureAndInheritanceOptionalRuleCall() throws Exception {
final String grammar = "language test generate test 'http://test' RuleA: RuleB? featureA=INT; RuleB: featureB=STRING;";
EPackage ePackage = getEPackageFromGrammar(grammar);
assertEquals(2, ePackage.getEClassifiers().size());
EClass ruleA = (EClass) ePackage.getEClassifier("RuleA");
assertNotNull(ruleA);
EClass ruleB = (EClass) ePackage.getEClassifier("RuleB");
assertNotNull(ruleB);
assertEquals(1, ruleA.getEAttributes().size());
assertFeatureConfiguration(ruleA, 0, "featureA", "EInt");
assertEquals(1, ruleB.getEAttributes().size());
assertFeatureConfiguration(ruleB, 0, "featureB", "EString");
}
public void _testFeatureAndInheritanceMandatoryRuleCall() throws Exception {
final String grammar = "language test generate test 'http://test' RuleA: RuleB featureA=INT; RuleB: featureB=STRING;";
EPackage ePackage = getEPackageFromGrammar(grammar);
assertEquals(2, ePackage.getEClassifiers().size());
EClass ruleA = (EClass) ePackage.getEClassifier("RuleA");
assertNotNull(ruleA);
EClass ruleB = (EClass) ePackage.getEClassifier("RuleB");
assertNotNull(ruleB);
assertEquals(0, ruleA.getEAttributes().size());
assertEquals(2, ruleB.getEAttributes().size());
assertFeatureConfiguration(ruleB, 0, "featureA", "EInt");
assertFeatureConfiguration(ruleB, 1, "featureB", "EString");
}
}