summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorszarnekow2009-03-19 05:32:07 (EDT)
committer sefftinge2009-03-19 05:32:07 (EDT)
commitcb6243c3bf47faff7ce1cea60d2b8b5b9c3ccefb (patch)
tree29ad1940fd16673364d6b368ce11df55d37f5888
parent0cbfe6c7a8e757b0445e9328f457eb15517aa4b0 (diff)
downloadorg.eclipse.xtext-cb6243c3bf47faff7ce1cea60d2b8b5b9c3ccefb.zip
org.eclipse.xtext-cb6243c3bf47faff7ce1cea60d2b8b5b9c3ccefb.tar.gz
org.eclipse.xtext-cb6243c3bf47faff7ce1cea60d2b8b5b9c3ccefb.tar.bz2
enum rules continued, handle enum inference more gracefully, some more tests
-rw-r--r--tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/enumrules/AbstractEnumRulesTest.java26
-rw-r--r--tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/enumrules/GrammarParserTest.java85
-rw-r--r--tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/enumrules/ParserTest.java6
3 files changed, 110 insertions, 7 deletions
diff --git a/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/enumrules/AbstractEnumRulesTest.java b/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/enumrules/AbstractEnumRulesTest.java
new file mode 100644
index 0000000..edfcd2c
--- /dev/null
+++ b/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/enumrules/AbstractEnumRulesTest.java
@@ -0,0 +1,26 @@
+/*******************************************************************************
+ * Copyright (c) 2009 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.xtext.enumrules;
+
+import org.eclipse.emf.ecore.EPackage;
+import org.eclipse.xtext.enumrules.enums.EnumsPackage;
+import org.eclipse.xtext.junit.AbstractXtextTests;
+
+/**
+ * @author Sebastian Zarnekow - Initial contribution and API
+ */
+public abstract class AbstractEnumRulesTest extends AbstractXtextTests {
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ EnumsPackage pack = EnumsPackage.eINSTANCE;
+ EPackage.Registry.INSTANCE.put(pack.getNsURI(), pack);
+ }
+
+}
diff --git a/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/enumrules/GrammarParserTest.java b/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/enumrules/GrammarParserTest.java
index f7d7903..1cc844f 100644
--- a/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/enumrules/GrammarParserTest.java
+++ b/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/enumrules/GrammarParserTest.java
@@ -15,12 +15,12 @@ import org.eclipse.xtext.EnumRule;
import org.eclipse.xtext.Grammar;
import org.eclipse.xtext.XtextStandaloneSetup;
import org.eclipse.xtext.enumrules.enums.ExistingEnum;
-import org.eclipse.xtext.junit.AbstractXtextTests;
+import org.eclipse.xtext.resource.metamodel.TransformationDiagnostic;
/**
* @author Sebastian Zarnekow - Initial contribution and API
*/
-public class GrammarParserTest extends AbstractXtextTests {
+public class GrammarParserTest extends AbstractEnumRulesTest {
@Override
protected void setUp() throws Exception {
@@ -138,4 +138,85 @@ public class GrammarParserTest extends AbstractXtextTests {
assertNotNull(decl.getLiteral());
assertEquals("value", decl.getLiteral().getValue());
}
+
+ public void testEnum_06() throws Exception {
+ String modelAsString =
+ "grammar TestLanguage with org.eclipse.xtext.common.Terminals\n" +
+ "generate testLanguage 'http://www.eclipse.org/2009/tmf/xtext/EnumRulesTest/TestEnum/6'\n" +
+ "Model: enumValue=MyEnum;\n" +
+ "enum MyEnum: Value1 | Value2='value' | Value3;";
+ Grammar grammar = (Grammar) getModel(modelAsString);
+ assertTrue(grammar.eResource().getErrors().toString(), grammar.eResource().getErrors().isEmpty());
+ EPackage pack = grammar.getMetamodelDeclarations().get(0).getEPackage();
+ assertEquals("http://www.eclipse.org/2009/tmf/xtext/EnumRulesTest/TestEnum/6", pack.getNsURI());
+ EEnum eEnum = (EEnum) pack.getEClassifier("MyEnum");
+ assertNotNull(eEnum);
+ assertEquals(3, eEnum.getELiterals().size());
+ EEnumLiteral value = eEnum.getELiterals().get(0);
+ assertEquals("Value1", value.getName());
+ assertEquals(0, value.getValue());
+ assertEquals("Value1", value.getLiteral());
+
+ value = eEnum.getELiterals().get(1);
+ assertEquals("Value2", value.getName());
+ assertEquals(1, value.getValue());
+ assertEquals("value", value.getLiteral());
+
+ value = eEnum.getELiterals().get(2);
+ assertEquals("Value3", value.getName());
+ assertEquals(2, value.getValue());
+ assertEquals("Value3", value.getLiteral());
+ }
+
+ public void testEnum_07() throws Exception {
+ String modelAsString =
+ "grammar TestLanguage with org.eclipse.xtext.common.Terminals\n" +
+ "generate testLanguage 'http://www.eclipse.org/2009/tmf/xtext/EnumRulesTest/TestEnum/7'\n" +
+ "Model: enumValue=MyEnum;\n" +
+ "enum MyEnum: Value | Value;";
+ Grammar grammar = (Grammar) getModel(modelAsString);
+ assertTrue(grammar.eResource().getErrors().toString(), grammar.eResource().getErrors().isEmpty());
+ EPackage pack = grammar.getMetamodelDeclarations().get(0).getEPackage();
+ assertEquals("http://www.eclipse.org/2009/tmf/xtext/EnumRulesTest/TestEnum/7", pack.getNsURI());
+ EEnum eEnum = (EEnum) pack.getEClassifier("MyEnum");
+ assertNotNull(eEnum);
+ assertEquals(1, eEnum.getELiterals().size());
+ EEnumLiteral value = eEnum.getELiterals().get(0);
+ assertEquals("Value", value.getName());
+ assertEquals(0, value.getValue());
+ assertEquals("Value", value.getLiteral());
+ }
+
+ public void testEnum_08() throws Exception {
+ String modelAsString =
+ "grammar TestLanguage with org.eclipse.xtext.common.Terminals\n" +
+ "generate testLanguage 'http://www.eclipse.org/2009/tmf/xtext/EnumRulesTest/TestEnum/8'\n" +
+ "Model: enumValue=MyEnum;\n" +
+ "enum MyEnum: Value | Value = 'foo';";
+ Grammar grammar = (Grammar) getModel(modelAsString);
+ assertTrue(grammar.eResource().getErrors().toString(), grammar.eResource().getErrors().isEmpty());
+ EPackage pack = grammar.getMetamodelDeclarations().get(0).getEPackage();
+ assertEquals("http://www.eclipse.org/2009/tmf/xtext/EnumRulesTest/TestEnum/8", pack.getNsURI());
+ EEnum eEnum = (EEnum) pack.getEClassifier("MyEnum");
+ assertNotNull(eEnum);
+ assertEquals(1, eEnum.getELiterals().size());
+ EEnumLiteral value = eEnum.getELiterals().get(0);
+ assertEquals("Value", value.getName());
+ assertEquals(0, value.getValue());
+ assertEquals("Value", value.getLiteral());
+ }
+
+ public void testEnum_09() throws Exception {
+ String modelAsString =
+ "grammar TestLanguage with org.eclipse.xtext.common.Terminals\n" +
+ "import 'classpath:/org/eclipse/xtext/enumrules/enums.ecore'\n" +
+ "generate testLanguage 'http://www.eclipse.org/2009/tmf/xtext/EnumRulesTest/TestEnum/9'\n" +
+ "Model: enumValue=ExistingEnum;\n" +
+ "enum ExistingEnum: SameName | Absent | SameName;";
+ Grammar grammar = (Grammar) getModel(modelAsString);
+ assertEquals(grammar.eResource().getErrors().toString(), 1, grammar.eResource().getErrors().size());
+ TransformationDiagnostic diag = (TransformationDiagnostic) grammar.eResource().getErrors().get(0);
+ int offset = diag.getOffset();
+ assertEquals(modelAsString.lastIndexOf("Absent"), offset);
+ }
}
diff --git a/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/enumrules/ParserTest.java b/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/enumrules/ParserTest.java
index f7fe49b..6162856 100644
--- a/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/enumrules/ParserTest.java
+++ b/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/enumrules/ParserTest.java
@@ -7,28 +7,24 @@
*******************************************************************************/
package org.eclipse.xtext.enumrules;
-import org.eclipse.emf.ecore.EPackage;
import org.eclipse.xtext.enumrules.enumRulesTestLanguage.EnumRulesTestLanguagePackage;
import org.eclipse.xtext.enumrules.enumRulesTestLanguage.GeneratedEnum;
import org.eclipse.xtext.enumrules.enumRulesTestLanguage.Model;
import org.eclipse.xtext.enumrules.enums.EnumsPackage;
import org.eclipse.xtext.enumrules.enums.ExistingEnum;
-import org.eclipse.xtext.junit.AbstractXtextTests;
import org.eclipse.xtext.parser.IParser;
import org.eclipse.xtext.parser.ParserTestHelper;
/**
* @author Sebastian Zarnekow - Initial contribution and API
*/
-public abstract class ParserTest extends AbstractXtextTests {
+public abstract class ParserTest extends AbstractEnumRulesTest {
private ParserTestHelper helper;
@Override
protected void setUp() throws Exception {
super.setUp();
- EnumsPackage pack = EnumsPackage.eINSTANCE;
- EPackage.Registry.INSTANCE.put(pack.getNsURI(), pack);
with(EnumRulesTestLanguageStandaloneSetup.class);
helper = new ParserTestHelper(getResourceFactory(), getParser(), get(Keys.RESOURCE_SET_KEY));
}