<commit_before>/*******************************************************************************
 * Copyright (c) 2008 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.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;
import org.eclipse.xtext.tests.AbstractGeneratorTest;

/**
 * @author <NAME> - Initial contribution and API
 */
public class Xtext2EcoreTransformerTests extends AbstractGeneratorTest {

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		with(XtextStandaloneSetup.class);
	}

	private EPackage getEPackageFromGrammar(String xtextGrammar)
			throws Exception {
		Grammar grammar = (Grammar) getModel(xtextGrammar);
		Xtext2EcoreTransformer xtext2EcoreTransformer = new Xtext2EcoreTransformer();
		List<EPackage> metamodels = xtext2EcoreTransformer.transform(grammar);
		assertNotNull(metamodels);
		assertEquals(1, metamodels.size());

		EPackage result = metamodels.get(0);
		assertNotNull(result);
		return result;
	}

	private EAttribute 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());
		
		return feature;
	}
	
	private EAttribute assertFeatureConfiguration(EClass eClass, int attributeIndex,
			String featureName, String featureTypeName, int lowerBound, int upperBound) {
		EAttribute feature = assertFeatureConfiguration(eClass, attributeIndex, featureName, featureTypeName);
		assertEquals(lowerBound, feature.getLowerBound());
		assertEquals(upperBound, feature.getUpperBound());
		
		return feature;
	}

	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 testCardinalityOfFeatures() 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", "EBoolean", 0, 1);
		assertFeatureConfiguration(ruleA, 1, "featureB", "EInt", 0, 1);
		assertFeatureConfiguration(ruleA, 2, "featureC", "EString", 0, -1);
	}

	public void testOptionalAssignmentsInGroup() throws Exception {
		final String grammar = "language test generate test 'http://test' RuleA: (featureA?='abstract' featureB+=INT)?;";
		EPackage ePackage = getEPackageFromGrammar(grammar);
		assertEquals(1, ePackage.getEClassifiers().size());
		EClass ruleA = (EClass) ePackage.getEClassifier("RuleA");
		assertNotNull(ruleA);
		assertEquals(2, ruleA.getEAttributes().size());
		assertFeatureConfiguration(ruleA, 0, "featureA", "EBoolean", 0, 1);
		assertFeatureConfiguration(ruleA, 1, "featureB", "EInt", 0, -1);
	}

	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");
	}

}
<commit_msg>Next patch from Heiko See https://bugs.eclipse.org/bugs/show_bug.cgi?id=247406<commit_after>/*******************************************************************************
 * Copyright (c) 2008 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.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;
import org.eclipse.xtext.tests.AbstractGeneratorTest;

/**
 * @author <NAME> - Initial contribution and API
 */
public class Xtext2EcoreTransformerTests extends AbstractGeneratorTest {

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		with(XtextStandaloneSetup.class);
	}

	private EPackage getEPackageFromGrammar(String xtextGrammar) throws Exception {
		Grammar grammar = (Grammar) getModel(xtextGrammar);
		Xtext2EcoreTransformer xtext2EcoreTransformer = new Xtext2EcoreTransformer();
		List<EPackage> metamodels = xtext2EcoreTransformer.transform(grammar);
		assertNotNull(metamodels);
		assertEquals(1, metamodels.size());

		EPackage result = metamodels.get(0);
		assertNotNull(result);
		return result;
	}

	private EAttribute 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());

		return feature;
	}

	private EAttribute assertFeatureConfiguration(EClass eClass, int attributeIndex, String featureName,
			String featureTypeName, int lowerBound, int upperBound) {
		EAttribute feature = assertFeatureConfiguration(eClass, attributeIndex, featureName, featureTypeName);
		assertEquals(lowerBound, feature.getLowerBound());
		assertEquals(upperBound, feature.getUpperBound());

		return feature;
	}

	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 testCardinalityOfFeatures() 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", "EBoolean", 0, 1);
		assertFeatureConfiguration(ruleA, 1, "featureB", "EInt", 0, 1);
		assertFeatureConfiguration(ruleA, 2, "featureC", "EString", 0, -1);
	}

	public void testOptionalAssignmentsInGroup() throws Exception {
		final String grammar = "language test generate test 'http://test' RuleA: (featureA?='abstract' featureB+=INT)?;";
		EPackage ePackage = getEPackageFromGrammar(grammar);
		assertEquals(1, ePackage.getEClassifiers().size());
		EClass ruleA = (EClass) ePackage.getEClassifier("RuleA");
		assertNotNull(ruleA);
		assertEquals(2, ruleA.getEAttributes().size());
		assertFeatureConfiguration(ruleA, 0, "featureA", "EBoolean", 0, 1);
		assertFeatureConfiguration(ruleA, 1, "featureB", "EInt", 0, -1);
	}

	public void testFeaturesAndInheritanceOptionalRuleCall() 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 testFeaturesAndInheritanceMandatoryRuleCall() 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");
	}

	public void testFeaturesAndInheritanceOfMandatoryAlternativeRuleCalls() throws Exception {
		final String grammar = "language test generate test 'http://test' RuleA: (RuleB|RuleC featureC1=ID) featureA=ID; RuleB: featureB=ID; RuleC: featureC2=ID;";
		EPackage ePackage = getEPackageFromGrammar(grammar);
		assertEquals(3, ePackage.getEClassifiers().size());
		EClass ruleA = (EClass) ePackage.getEClassifier("RuleA");
		assertNotNull(ruleA);
		EClass ruleB = (EClass) ePackage.getEClassifier("RuleB");
		assertNotNull(ruleB);
		EClass ruleC = (EClass) ePackage.getEClassifier("RuleC");
		assertNotNull(ruleC);

		// test inheritance
		assertTrue(ruleA.getESuperTypes().isEmpty());
		assertEquals(1, ruleB.getESuperTypes().size());
		assertEquals(ruleA, ruleB.getESuperTypes().get(0));
		assertEquals(1, ruleC.getESuperTypes().size());
		assertEquals(ruleA, ruleC.getESuperTypes().get(0));

		// test all features are separated
		assertEquals(0, ruleA.getEAttributes().size());
		assertEquals(2, ruleB.getEAttributes().size());
		assertFeatureConfiguration(ruleB, 0, "featureA", "EString");
		assertFeatureConfiguration(ruleB, 1, "featureB", "EString");
		assertEquals(3, ruleC.getEAttributes().size());
		assertFeatureConfiguration(ruleC, 0, "featureC1", "EString");
		assertFeatureConfiguration(ruleC, 1, "featureA", "EString");
		assertFeatureConfiguration(ruleC, 2, "featureC2", "EString");
	}

	public void testFeaturesAndInheritanceOfOptionalOptionalRuleCalls() throws Exception {
		final String grammar = "language test generate test 'http://test' RuleA: (RuleB|RuleC featureC1=ID)? featureA=ID; RuleB: featureB=ID; RuleC: featureC2=ID;";
		EPackage ePackage = getEPackageFromGrammar(grammar);
		assertEquals(3, ePackage.getEClassifiers().size());
		EClass ruleA = (EClass) ePackage.getEClassifier("RuleA");
		assertNotNull(ruleA);
		EClass ruleB = (EClass) ePackage.getEClassifier("RuleB");
		assertNotNull(ruleB);
		EClass ruleC = (EClass) ePackage.getEClassifier("RuleC");
		assertNotNull(ruleC);

		// test inheritance
		assertTrue(ruleA.getESuperTypes().isEmpty());
		assertEquals(1, ruleB.getESuperTypes().size());
		assertEquals(ruleA, ruleB.getESuperTypes().get(0));
		assertEquals(1, ruleC.getESuperTypes().size());
		assertEquals(ruleA, ruleC.getESuperTypes().get(0));

		// test all features are separated
		assertEquals(1, ruleA.getEAttributes().size());
		assertFeatureConfiguration(ruleA, 0, "featureA", "EString");
		assertEquals(1, ruleB.getEAttributes().size());
		assertFeatureConfiguration(ruleB, 0, "featureB", "EString");
		assertEquals(2, ruleC.getEAttributes().size());
		assertFeatureConfiguration(ruleC, 0, "featureC1", "EString");
		assertFeatureConfiguration(ruleC, 1, "featureC2", "EString");
	}

	public void testFeaturesAndInheritanceOfNestedRuleCalls() throws Exception {
		final String grammar = "language test generate test 'http://test' RuleA: ((RuleB|RuleC featureC1=ID)? featureBC=ID | (RuleC|RuleD featureD1=ID) featureCD=ID) featureA=ID; RuleB: featureB2=ID; RuleC: featureC2=ID; RuleD: featureD2=ID;";
		EPackage ePackage = getEPackageFromGrammar(grammar);
		assertEquals(4, ePackage.getEClassifiers().size());
		EClass ruleA = (EClass) ePackage.getEClassifier("RuleA");
		assertNotNull(ruleA);
		EClass ruleB = (EClass) ePackage.getEClassifier("RuleB");
		assertNotNull(ruleB);
		EClass ruleC = (EClass) ePackage.getEClassifier("RuleC");
		assertNotNull(ruleC);
		EClass ruleD = (EClass) ePackage.getEClassifier("RuleD");
		assertNotNull(ruleD);

		// test inheritance
		assertTrue(ruleA.getESuperTypes().isEmpty());
		assertEquals(1, ruleB.getESuperTypes().size());
		assertEquals(ruleA, ruleB.getESuperTypes().get(0));
		assertEquals(1, ruleC.getESuperTypes().size());
		assertEquals(ruleA, ruleC.getESuperTypes().get(0));
		assertEquals(1, ruleD.getESuperTypes().size());
		assertEquals(ruleA, ruleD.getESuperTypes().get(0));

		// test all features are separated
		assertEquals(2, ruleA.getEAttributes().size());
		assertFeatureConfiguration(ruleA, 0, "featureBC", "EString");
		assertFeatureConfiguration(ruleA, 1, "featureA", "EString");
		assertEquals(1, ruleB.getEAttributes().size());
		assertFeatureConfiguration(ruleB, 0, "featureB2", "EString");
		assertEquals(4, ruleC.getEAttributes().size());
		assertFeatureConfiguration(ruleC, 0, "featureC1", "EString");
		assertFeatureConfiguration(ruleC, 1, "featureCD", "EString");
		assertFeatureConfiguration(ruleC, 2, "featureA", "EString");
		assertFeatureConfiguration(ruleC, 3, "featureC2", "EString");
		assertEquals(4, ruleD.getEAttributes().size());
		assertFeatureConfiguration(ruleD, 0, "featureD1", "EString");
		assertFeatureConfiguration(ruleD, 1, "featureCD", "EString");
		assertFeatureConfiguration(ruleD, 2, "featureA", "EString");
		assertFeatureConfiguration(ruleD, 3, "featureD2", "EString");
	}

}

{'fraction_non_alphanumeric': 0.1424581005586592, 'fraction_numerical': 0.00875864028027649, 'mean_word_length': 6.774383511225617, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2660002', 'n_tokens_mistral': 6908, 'n_tokens_neox': 6395, 'n_words': 1355}