Java Code Examples for org.eclipse.emf.ecore.EEnumLiteral

The following examples show how to use org.eclipse.emf.ecore.EEnumLiteral. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
private Object parsePrimitive(EClassifier eType, String text) throws DeserializeException {
	if (eType == EcorePackage.eINSTANCE.getEString()) {
		return text;
	} else if (eType == EcorePackage.eINSTANCE.getEInt()) {
		return Integer.parseInt(text);
	} else if (eType == EcorePackage.eINSTANCE.getELong()) {
		return Long.parseLong(text);
	} else if (eType == EcorePackage.eINSTANCE.getEDouble()) {
		return Double.parseDouble(text);
	} else if (eType == EcorePackage.eINSTANCE.getEBoolean()) {
		return Boolean.parseBoolean(text);
	} else if (eType instanceof EEnum) {
		EEnumLiteral eEnumLiteral = ((EEnum) eType).getEEnumLiteral(text.toUpperCase());
		if (eEnumLiteral == null) {
			if (text.equals("unknown")) {
				return null;
			} else {
				throw new DeserializeException(DeserializerErrorCode.UNSPECIFIED_IFCXML_ERROR, "Unknown enum literal " + text + " in enum " + ((EEnum) eType).getName());
			}
		}
		return eEnumLiteral.getInstance();
	} else {
		throw new DeserializeException(DeserializerErrorCode.UNSPECIFIED_IFCXML_ERROR, "Unimplemented primitive type: " + eType.getName());
	}
}
 
Example 2
@Override
public List<String> resolveValues(TemplateVariable variable,
		XtextTemplateContext castedContext) {
	String enumerationName = variable.getVariableType()
			.getParams().iterator().next();
	Grammar grammar = getGrammar(castedContext);
	if (grammar == null)
		return Collections.emptyList();
	EEnum enumeration = (EEnum) getEClassifierForGrammar(enumerationName, grammar);
	if (enumeration == null) {
		return Collections.emptyList();
	}
	return Lists.transform(enumeration.getELiterals(), new Function<EEnumLiteral, String>() {
		@Override
		public String apply(EEnumLiteral enumLiteral) {
			return enumLiteral.getLiteral();
		}
	});
}
 
Example 3
Source Project: xtext-core   Source File: GrammarParserTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testEnum_01() throws Exception {
	String modelAsString =
		"grammar TestLanguage with org.eclipse.xtext.common.Terminals\n" +
		"generate testLanguage 'http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/1'\n" +
		"Model: enumValue=MyEnum;\n" +
		"enum MyEnum: Value1;";
	Grammar grammar = (Grammar) getModel(modelAsString);
	assertTrue(grammar.eResource().getErrors().toString(), grammar.eResource().getErrors().isEmpty());
	checkEnums(grammar);
	EPackage pack = grammar.getMetamodelDeclarations().get(0).getEPackage();
	assertEquals("http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/1", pack.getNsURI());
	EEnum eEnum = (EEnum) pack.getEClassifier("MyEnum");
	assertNotNull(eEnum);
	assertEquals(1, eEnum.getELiterals().size());
	EEnumLiteral value = eEnum.getELiterals().get(0);
	assertEquals("Value1", value.getName());
	assertEquals(0, value.getValue());
	assertEquals("Value1", value.getLiteral());
}
 
Example 4
Source Project: xtext-core   Source File: GrammarParserTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testEnum_02() throws Exception {
	String modelAsString =
		"grammar TestLanguage with org.eclipse.xtext.common.Terminals\n" +
		"generate testLanguage 'http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/2'\n" +
		"Model: enumValue=MyEnumRule;\n" +
		"enum MyEnumRule returns MyEnum: Value1;";
	Grammar grammar = (Grammar) getModel(modelAsString);
	assertTrue(grammar.eResource().getErrors().toString(), grammar.eResource().getErrors().isEmpty());
	checkEnums(grammar);
	EPackage pack = grammar.getMetamodelDeclarations().get(0).getEPackage();
	assertEquals("http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/2", pack.getNsURI());
	EEnum eEnum = (EEnum) pack.getEClassifier("MyEnum");
	assertNotNull(eEnum);
	assertEquals(1, eEnum.getELiterals().size());
	EEnumLiteral value = eEnum.getELiterals().get(0);
	assertEquals("Value1", value.getName());
	assertEquals(0, value.getValue());
	assertEquals("Value1", value.getLiteral());
}
 
Example 5
Source Project: xtext-core   Source File: GrammarParserTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testEnum_03() throws Exception {
	String modelAsString =
		"grammar TestLanguage with org.eclipse.xtext.common.Terminals\n" +
		"generate testLanguage 'http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/3'\n" +
		"Model: enumValue=MyEnumRule;\n" +
		"enum MyEnumRule returns MyEnum: Value1 = 'value';";
	Grammar grammar = (Grammar) getModel(modelAsString);
	assertTrue(grammar.eResource().getErrors().toString(), grammar.eResource().getErrors().isEmpty());
	checkEnums(grammar);
	EPackage pack = grammar.getMetamodelDeclarations().get(0).getEPackage();
	assertEquals("http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/3", pack.getNsURI());
	EEnum eEnum = (EEnum) pack.getEClassifier("MyEnum");
	assertNotNull(eEnum);
	assertEquals(1, eEnum.getELiterals().size());
	EEnumLiteral value = eEnum.getELiterals().get(0);
	assertEquals("Value1", value.getName());
	assertEquals(0, value.getValue());
	assertEquals("value", value.getLiteral());
}
 
Example 6
Source Project: xtext-core   Source File: GrammarParserTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test 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/AbstractEnumRulesTest/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());
	checkEnums(grammar);
	EPackage pack = grammar.getMetamodelDeclarations().get(0).getEPackage();
	assertEquals("http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/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());
}
 
Example 7
Source Project: xtext-core   Source File: GrammarParserTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test 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/AbstractEnumRulesTest/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());
	checkEnums(grammar);
	EPackage pack = grammar.getMetamodelDeclarations().get(0).getEPackage();
	assertEquals("http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/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());
}
 
Example 8
private void writeEnum(EStructuralFeature feature, Object value) {
	if (value == null) {
		ensureCapacity(buffer.position(), 4);
		buffer.putInt(-1);
	} else {
		EEnum eEnum = (EEnum) feature.getEType();
		EEnumLiteral eEnumLiteral = eEnum.getEEnumLiteralByLiteral(((Enum<?>) value).toString());
		ensureCapacity(buffer.position(), 4);
		if (eEnumLiteral != null) {
			buffer.putInt(eEnumLiteral.getValue());
		} else {
			LOGGER.error(((Enum<?>) value).toString() + " not found");
			buffer.putInt(-1);
		}
	}
}
 
Example 9
Source Project: BIMserver   Source File: Schema.java    License: GNU Affero General Public License v3.0 6 votes vote down vote up
public EEnumLiteral createEEnumLiteral(EEnum eEnum, String name) {
	EEnumLiteral eEnumLiteral = EcoreFactory.eINSTANCE.createEEnumLiteral();
	if (eEnum.getELiterals().size() == 0) {
		eEnumLiteral.setValue(0);
	} else {
		int largestValue = Integer.MIN_VALUE;
		for (EEnumLiteral existingLiteral : eEnum.getELiterals()) {
			if (existingLiteral.getValue() > largestValue) {
				largestValue = existingLiteral.getValue();
			}
		}
		eEnumLiteral.setValue(largestValue + 1);
	}
	eEnum.getELiterals().add(eEnumLiteral);
	eEnumLiteral.setName(name);
	return eEnumLiteral;
}
 
Example 10
Source Project: BIMserver   Source File: Express2EMF.java    License: GNU Affero General Public License v3.0 6 votes vote down vote up
private void createTristate() {
	tristate = eFactory.createEEnum();
	tristate.setName("Tristate");
	EEnumLiteral trueLiteral = eFactory.createEEnumLiteral();
	trueLiteral.setName("TRUE");
	trueLiteral.setValue(0);
	EEnumLiteral falseLiteral = eFactory.createEEnumLiteral();
	falseLiteral.setName("FALSE");
	falseLiteral.setValue(1);
	EEnumLiteral undefinedLiteral = eFactory.createEEnumLiteral();
	undefinedLiteral.setName("UNDEFINED");
	undefinedLiteral.setValue(2);
	tristate.getELiterals().add(trueLiteral);
	tristate.getELiterals().add(falseLiteral);
	tristate.getELiterals().add(undefinedLiteral);
	schemaPack.getEClassifiers().add(tristate);
}
 
Example 11
public EEnumLiteral getEEnumLiteral(String enumName, String literalName) {
	EClassifier eClassifier = ePackage.getEClassifier(enumName);
	if (eClassifier == null) {
		throw new RuntimeException("Classifier " + enumName + " not found in package " + ePackage.getName());
	}
	if (eClassifier instanceof EEnum) {
		EEnum eEnum = (EEnum)eClassifier;
		EEnumLiteral literal = eEnum.getEEnumLiteral(literalName);
		if (literal == null) {
			throw new RuntimeException("No enum literal " + literalName + " found on " + ePackage.getName() + "." + enumName);
		}
		return literal;
	} else {
		throw new RuntimeException("Classifier " + enumName + " is not of type enum");
	}
}
 
Example 12
Source Project: bonita-studio   Source File: MetamodelImpl.java    License: GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void refreshCaches() {
	for (final EPackage ePackage : getEPackages()) {
		for (final Iterator<EObject> i = ePackage.eAllContents(); i.hasNext();) {
			final EObject element = i.next();
			if (element instanceof EStructuralFeatureImpl) {
				final EStructuralFeatureImpl feature = (EStructuralFeatureImpl) element;
				feature.setSettingDelegate(null);
			}
			if (element instanceof EEnumLiteral) {
				final EEnumLiteral literal = (EEnumLiteral) element;
				literal.setInstance(literal);
			}
		}
	}
}
 
Example 13
/** {@inheritDoc} */
@Override
protected Enumerator resolveLiteral(Object literal) {
	if (EEnumLiteral.class.isInstance(literal)) {
		final EEnum sourceEnum = EEnumLiteral.class.cast(literal).getEEnum();
		final String value = sourceEnum.getEPackage().getEFactoryInstance()
			.convertToString(sourceEnum, literal);
		final EEnum targetEnum = (EEnum) resolveEClassifier(sourceEnum);
		return (Enumerator) targetEnum.getEPackage().getEFactoryInstance()
			.createFromString(targetEnum, value);
	} else if (Enumerator.class.isInstance(literal)) {
		return Enumerator.class.cast(literal);
	} else if (literal == null) {
		return null;
	}
	throw new IllegalArgumentException(MessageFormat.format(
		"Unexpected literal {0} of type {1} cannot be converted to an Enumerator", literal, literal.getClass())); //$NON-NLS-1$
}
 
Example 14
Source Project: n4js   Source File: PackageJsonUtils.java    License: Eclipse Public License 1.0 5 votes vote down vote up
private static <T extends Enumerator> T parseEnumLiteral(EEnum emfEnumType, Class<T> javaEnumType,
		String enumLiteralStr) {
	EEnumLiteral emfLit = enumLiteralStr != null ? emfEnumType.getELiterals().stream()
			.filter(lit -> lit.getName().equalsIgnoreCase(enumLiteralStr))
			.findFirst().orElse(null) : null;
	if (emfLit == null) {
		return null;
	}
	final Enumerator javaLit = emfLit.getInstance();
	@SuppressWarnings("unchecked")
	T javaLitCasted = javaEnumType.isInstance(javaLit) ? (T) javaLit : null;
	return javaLitCasted;
}
 
Example 15
Source Project: xtext-core   Source File: XtextScopeProvider.java    License: Eclipse Public License 2.0 5 votes vote down vote up
protected IScope createEnumLiteralsScope(EEnum eEnum) {
	return new SimpleScope(IScope.NULLSCOPE,Iterables.transform(eEnum.getELiterals(), new Function<EEnumLiteral, IEObjectDescription>() {
						@Override
						public IEObjectDescription apply(EEnumLiteral param) {
							return EObjectDescription.create(QualifiedName.create(param.getName()), param);
						}
					}));
}
 
Example 16
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public void setEnumLiteral(EEnumLiteral newEnumLiteral) {
	EEnumLiteral oldEnumLiteral = enumLiteral;
	enumLiteral = newEnumLiteral;
	if (eNotificationRequired())
		eNotify(new ENotificationImpl(this, Notification.SET, XtextPackage.ENUM_LITERAL_DECLARATION__ENUM_LITERAL, oldEnumLiteral, enumLiteral));
}
 
Example 17
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public void eSet(int featureID, Object newValue) {
	switch (featureID) {
		case XtextPackage.ENUM_LITERAL_DECLARATION__ENUM_LITERAL:
			setEnumLiteral((EEnumLiteral)newValue);
			return;
		case XtextPackage.ENUM_LITERAL_DECLARATION__LITERAL:
			setLiteral((Keyword)newValue);
			return;
	}
	super.eSet(featureID, newValue);
}
 
Example 18
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public void eUnset(int featureID) {
	switch (featureID) {
		case XtextPackage.ENUM_LITERAL_DECLARATION__ENUM_LITERAL:
			setEnumLiteral((EEnumLiteral)null);
			return;
		case XtextPackage.ENUM_LITERAL_DECLARATION__LITERAL:
			setLiteral((Keyword)null);
			return;
	}
	super.eUnset(featureID);
}
 
Example 19
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public void setEnumLiteral(EEnumLiteral newEnumLiteral)
{
  EEnumLiteral oldEnumLiteral = enumLiteral;
  enumLiteral = newEnumLiteral;
  if (eNotificationRequired())
    eNotify(new ENotificationImpl(this, Notification.SET, XtextGrammarTestPackage.ENUM_LITERAL_DECLARATION__ENUM_LITERAL, oldEnumLiteral, enumLiteral));
}
 
Example 20
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public void eSet(int featureID, Object newValue)
{
  switch (featureID)
  {
    case XtextGrammarTestPackage.ENUM_LITERAL_DECLARATION__ENUM_LITERAL:
      setEnumLiteral((EEnumLiteral)newValue);
      return;
    case XtextGrammarTestPackage.ENUM_LITERAL_DECLARATION__LITERAL:
      setLiteral((Keyword)newValue);
      return;
  }
  super.eSet(featureID, newValue);
}
 
Example 21
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public void eUnset(int featureID)
{
  switch (featureID)
  {
    case XtextGrammarTestPackage.ENUM_LITERAL_DECLARATION__ENUM_LITERAL:
      setEnumLiteral((EEnumLiteral)null);
      return;
    case XtextGrammarTestPackage.ENUM_LITERAL_DECLARATION__LITERAL:
      setLiteral((Keyword)null);
      return;
  }
  super.eUnset(featureID);
}
 
Example 22
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public void setEnumLiteral(EEnumLiteral newEnumLiteral)
{
  EEnumLiteral oldEnumLiteral = enumLiteral;
  enumLiteral = newEnumLiteral;
  if (eNotificationRequired())
    eNotify(new ENotificationImpl(this, Notification.SET, XtextTerminalsTestLanguagePackage.ENUM_LITERAL_DECLARATION__ENUM_LITERAL, oldEnumLiteral, enumLiteral));
}
 
Example 23
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public void eSet(int featureID, Object newValue)
{
  switch (featureID)
  {
    case XtextTerminalsTestLanguagePackage.ENUM_LITERAL_DECLARATION__ENUM_LITERAL:
      setEnumLiteral((EEnumLiteral)newValue);
      return;
    case XtextTerminalsTestLanguagePackage.ENUM_LITERAL_DECLARATION__LITERAL:
      setLiteral((Keyword)newValue);
      return;
  }
  super.eSet(featureID, newValue);
}
 
Example 24
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public void eUnset(int featureID)
{
  switch (featureID)
  {
    case XtextTerminalsTestLanguagePackage.ENUM_LITERAL_DECLARATION__ENUM_LITERAL:
      setEnumLiteral((EEnumLiteral)null);
      return;
    case XtextTerminalsTestLanguagePackage.ENUM_LITERAL_DECLARATION__LITERAL:
      setLiteral((Keyword)null);
      return;
  }
  super.eUnset(featureID);
}
 
Example 25
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public void setEnumLiteral(EEnumLiteral newEnumLiteral)
{
  EEnumLiteral oldEnumLiteral = enumLiteral;
  enumLiteral = newEnumLiteral;
  if (eNotificationRequired())
    eNotify(new ENotificationImpl(this, Notification.SET, XtextTestPackage.ENUM_LITERAL_DECLARATION__ENUM_LITERAL, oldEnumLiteral, enumLiteral));
}
 
Example 26
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public void eSet(int featureID, Object newValue)
{
  switch (featureID)
  {
    case XtextTestPackage.ENUM_LITERAL_DECLARATION__ENUM_LITERAL:
      setEnumLiteral((EEnumLiteral)newValue);
      return;
    case XtextTestPackage.ENUM_LITERAL_DECLARATION__LITERAL:
      setLiteral((Keyword)newValue);
      return;
  }
  super.eSet(featureID, newValue);
}
 
Example 27
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public void eUnset(int featureID)
{
  switch (featureID)
  {
    case XtextTestPackage.ENUM_LITERAL_DECLARATION__ENUM_LITERAL:
      setEnumLiteral((EEnumLiteral)null);
      return;
    case XtextTestPackage.ENUM_LITERAL_DECLARATION__LITERAL:
      setLiteral((Keyword)null);
      return;
  }
  super.eUnset(featureID);
}
 
Example 28
Source Project: xtext-core   Source File: GrammarParserTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testEnum_04() 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/AbstractEnumRulesTest/TestEnum/4'\n" +
		"Model: enumValue=ExistingEnum;\n" +
		"enum ExistingEnum: SameName;";
	Grammar grammar = (Grammar) getModel(modelAsString);
	assertTrue(grammar.eResource().getErrors().toString(), grammar.eResource().getErrors().isEmpty());
	checkEnums(grammar);
	EPackage pack = grammar.getMetamodelDeclarations().get(0).getEPackage();
	assertEquals("http://www.eclipse.org/2009/tmf/xtext/EnumRulesTestLanguage/imported", pack.getNsURI());
	EEnum eEnum = (EEnum) pack.getEClassifier("ExistingEnum");
	assertNotNull(eEnum);
	assertEquals(3, eEnum.getELiterals().size());
	EEnumLiteral value = eEnum.getELiterals().get(0);
	assertEquals(ExistingEnum.SAME_NAME.getName(), value.getName());
	assertEquals(ExistingEnum.SAME_NAME.getValue(), value.getValue());
	assertEquals(ExistingEnum.SAME_NAME.getLiteral(), value.getLiteral());
	
	EnumRule rule = (EnumRule) grammar.getRules().get(1);
	assertEquals(eEnum, rule.getType().getClassifier());
	EnumLiteralDeclaration decl = (EnumLiteralDeclaration) rule.getAlternatives();
	assertEquals(value, decl.getEnumLiteral());
	assertNotNull(decl.getLiteral());
	assertEquals(value.getLiteral(), decl.getLiteral().getValue());
}
 
Example 29
Source Project: xtext-core   Source File: GrammarParserTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testEnum_05() 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/AbstractEnumRulesTest/TestEnum/5'\n" +
		"Model: enumValue=ExistingEnum;\n" +
		"enum ExistingEnum: SameName = 'value';";
	Grammar grammar = (Grammar) getModel(modelAsString);
	assertTrue(grammar.eResource().getErrors().toString(), grammar.eResource().getErrors().isEmpty());
	checkEnums(grammar);
	EPackage pack = grammar.getMetamodelDeclarations().get(0).getEPackage();
	assertEquals("http://www.eclipse.org/2009/tmf/xtext/EnumRulesTestLanguage/imported", pack.getNsURI());
	EEnum eEnum = (EEnum) pack.getEClassifier("ExistingEnum");
	assertNotNull(eEnum);
	assertEquals(3, eEnum.getELiterals().size());
	EEnumLiteral value = eEnum.getELiterals().get(0);
	assertEquals(ExistingEnum.SAME_NAME.getName(), value.getName());
	assertEquals(ExistingEnum.SAME_NAME.getValue(), value.getValue());
	assertEquals(ExistingEnum.SAME_NAME.getLiteral(), value.getLiteral());
	
	EnumRule rule = (EnumRule) grammar.getRules().get(1);
	assertEquals(eEnum, rule.getType().getClassifier());
	EnumLiteralDeclaration decl = (EnumLiteralDeclaration) rule.getAlternatives();
	assertEquals(value, decl.getEnumLiteral());
	assertNotNull(decl.getLiteral());
	assertEquals("value", decl.getLiteral().getValue());
}
 
Example 30
Source Project: xtext-core   Source File: GrammarParserTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test 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/AbstractEnumRulesTest/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());
	checkEnums(grammar);
	EPackage pack = grammar.getMetamodelDeclarations().get(0).getEPackage();
	assertEquals("http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/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());
}