40: toVarName handles EReference differently

Task-Url: https://github.com/eclipse/xtext-eclipse/issues/40
Signed-off-by: Lorenzo Bettini <lorenzo.bettini@gmail.com>
This commit is contained in:
Lorenzo Bettini 2016-07-18 14:34:12 +02:00
parent 316b42e334
commit c0bc6616b5
4 changed files with 113 additions and 0 deletions

View file

@ -0,0 +1,47 @@
/*******************************************************************************
* Copyright (c) 2016 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.xtext.generator
import org.eclipse.emf.ecore.ENamedElement
import org.eclipse.emf.ecore.EcorePackage
import org.eclipse.xtext.xtext.generator.formatting.Formatter2Fragment2
import org.junit.Test
import static extension org.junit.Assert.*
/**
* @author Lorenzo Bettini - Initial contribution and API
*/
class Formatter2Fragment2Test {
var TestableFormatter2Fragment2 fragment = new TestableFormatter2Fragment2
static class TestableFormatter2Fragment2 extends Formatter2Fragment2 {
override public toVarName(ENamedElement element) {
super.toVarName(element)
}
}
@Test def void testVarNameWithEClass() {
"eClass".assertEquals(fragment.toVarName(EcorePackage.eINSTANCE.EClass))
}
@Test def void testVarNameWithMultiReference() {
"eOperation".assertEquals(fragment.toVarName(EcorePackage.eINSTANCE.EClass_EAllOperations))
}
@Test def void testVarNameWithSingleReference() {
"name".assertEquals(fragment.toVarName(EcorePackage.eINSTANCE.ENamedElement_Name))
}
@Test def void testVarNameConflictingWithXtendKeyword() {
"_abstract".assertEquals(fragment.toVarName(EcorePackage.eINSTANCE.EClass_Abstract))
}
}

View file

@ -0,0 +1,60 @@
/**
* Copyright (c) 2016 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.xtext.generator;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.xtext.xtext.generator.formatting.Formatter2Fragment2;
import org.junit.Assert;
import org.junit.Test;
/**
* @author Lorenzo Bettini - Initial contribution and API
*/
@SuppressWarnings("all")
public class Formatter2Fragment2Test {
public static class TestableFormatter2Fragment2 extends Formatter2Fragment2 {
@Override
public String toVarName(final ENamedElement element) {
return super.toVarName(element);
}
}
private Formatter2Fragment2Test.TestableFormatter2Fragment2 fragment = new Formatter2Fragment2Test.TestableFormatter2Fragment2();
@Test
public void testVarNameWithEClass() {
EClass _eClass = EcorePackage.eINSTANCE.getEClass();
String _varName = this.fragment.toVarName(_eClass);
Assert.assertEquals("eClass", _varName);
}
@Test
public void testVarNameWithMultiReference() {
EReference _eClass_EAllOperations = EcorePackage.eINSTANCE.getEClass_EAllOperations();
String _varName = this.fragment.toVarName(_eClass_EAllOperations);
Assert.assertEquals("eOperation", _varName);
}
@Test
public void testVarNameWithSingleReference() {
EAttribute _eNamedElement_Name = EcorePackage.eINSTANCE.getENamedElement_Name();
String _varName = this.fragment.toVarName(_eNamedElement_Name);
Assert.assertEquals("name", _varName);
}
@Test
public void testVarNameConflictingWithXtendKeyword() {
EAttribute _eClass_Abstract = EcorePackage.eINSTANCE.getEClass_Abstract();
String _varName = this.fragment.toVarName(_eClass_Abstract);
Assert.assertEquals("_abstract", _varName);
}
}

View file

@ -159,6 +159,8 @@ import org.eclipse.xtext.util.internal.Log
}
protected def String toVarName(ENamedElement element) {
if (element instanceof EReference)
return element.EReferenceType.toVarName
val name = element.name.toFirstLower
if (XtendFileAccess.XTEND_KEYWORDS.contains(name))
'_' + name

View file

@ -356,6 +356,10 @@ public class Formatter2Fragment2 extends AbstractStubGeneratingFragment {
protected String toVarName(final ENamedElement element) {
String _xblockexpression = null;
{
if ((element instanceof EReference)) {
EClass _eReferenceType = ((EReference)element).getEReferenceType();
return this.toVarName(_eReferenceType);
}
String _name = element.getName();
final String name = StringExtensions.toFirstLower(_name);
String _xifexpression = null;