org.eclipse.xtext.junit4.serializer.SerializerTester Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2011 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.junit4.serializer;
import java.util.Iterator;
import java.util.List;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.junit4.util.ParseHelper;
import org.eclipse.xtext.junit4.validation.ValidationTestHelper;
import org.eclipse.xtext.nodemodel.ICompositeNode;
import org.eclipse.xtext.nodemodel.INode;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.serializer.acceptor.DelegatingSequenceAcceptor;
import org.eclipse.xtext.serializer.acceptor.ISemanticSequenceAcceptor;
import org.eclipse.xtext.serializer.acceptor.ISequenceAcceptor;
import org.eclipse.xtext.serializer.acceptor.ISyntacticSequenceAcceptor;
import org.eclipse.xtext.serializer.acceptor.StringBufferSequenceAcceptor;
import org.eclipse.xtext.serializer.acceptor.WhitespaceAddingSequenceAcceptor;
import org.eclipse.xtext.serializer.analysis.Context2NameFunction;
import org.eclipse.xtext.serializer.diagnostic.ISerializationDiagnostic;
import org.eclipse.xtext.serializer.sequencer.ContextFinder;
import org.eclipse.xtext.serializer.sequencer.IHiddenTokenSequencer;
import org.eclipse.xtext.serializer.sequencer.ISemanticSequencer;
import org.eclipse.xtext.serializer.sequencer.ISyntacticSequencer;
import org.eclipse.xtext.util.EmfFormatter;
import org.eclipse.xtext.util.Pair;
import org.eclipse.xtext.util.Tuples;
import org.junit.Assert;
import com.google.common.base.Joiner;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.inject.Inject;
import com.google.inject.Provider;
/**
* @author Moritz Eysholdt - Initial contribution and API
*/
public class SerializerTester {
@Inject
protected ContextFinder contextFinder;
@Inject
protected Provider hiddenTokenSequencerProvider;
@Inject
protected ParseHelper parseHelper;
@Inject
protected Provider semanticSequencerProvider;
@Inject
protected Provider syntacticSequencerProvider;
@Inject
protected ValidationTestHelper validationHelper;
/**
* @since 2.3
*/
protected void assertEqualWithEmfFormatter(EObject semanticObject, EObject parsed) {
String expected = EmfFormatter.objToStr(semanticObject);
String actual = EmfFormatter.objToStr(parsed);
Assert.assertEquals(expected, actual);
}
/**
* @since 2.3
*/
public void assertSerializable(EObject semanticObject) {
assertSerializeWithNodeModel(semanticObject);
assertSerializeWithoutNodeModel(semanticObject);
}
/**
* @since 2.3
*/
public void assertSerializable(String semanticObject) {
assertSerializeWithNodeModel(semanticObject);
assertSerializeWithoutNodeModel(semanticObject);
}
public void assertSerializeWithNodeModel(EObject semanticObject) {
String expected = getTextFromNodeModel(semanticObject);
String actual = serializeWithNodeModel(semanticObject);
Assert.assertEquals(expected, actual);
}
public void assertSerializeWithNodeModel(String semanticModel) {
try {
EObject semanticObject = parseHelper.parse(semanticModel);
validationHelper.assertNoErrors(semanticObject);
assertSerializeWithNodeModel(semanticObject);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void assertSerializeWithoutNodeModel(EObject semanticObject) {
try {
EObject parsed;
if (semanticObject.eResource().getContents().contains(semanticObject)) {
List> nodes = detachNodeModel(semanticObject);
String serialized = serializeWithoutNodeModel(semanticObject);
parsed = parseHelper.parse(serialized, semanticObject.eResource().getResourceSet());
reattachNodes(nodes);
} else {
INode oldNode = NodeModelUtils.getNode(semanticObject);
String oldtext = oldNode.getRootNode().getText();
String oldURI = semanticObject.eResource().getURIFragment(semanticObject);
List> nodes = detachNodeModel(semanticObject);
String serialized = serializeWithoutNodeModel(semanticObject);
String newtext = oldtext.substring(0, oldNode.getOffset()) + serialized
+ oldtext.substring(oldNode.getOffset() + oldNode.getLength());
EObject newmodel = parseHelper.parse(newtext, semanticObject.eResource().getResourceSet());
parsed = newmodel.eResource().getEObject(oldURI);
reattachNodes(nodes);
}
EcoreUtil.resolveAll(parsed);
Assert.assertTrue(parsed.eResource().getErrors().toString(), parsed.eResource().getErrors().isEmpty());
parsed.eResource().getResourceSet().getResources().remove(parsed.eResource());
assertEqualWithEmfFormatter(semanticObject, parsed);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void assertSerializeWithoutNodeModel(String semanticModel) {
EObject semanticObject;
try {
semanticObject = parseHelper.parse(semanticModel);
validationHelper.assertNoErrors(semanticObject);
assertSerializeWithoutNodeModel(semanticObject);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
protected List> detachNodeModel(EObject eObject) {
EcoreUtil.resolveAll(eObject);
List> result = Lists.newArrayList();
Iterator
© 2015 - 2025 Weber Informatics LLC | Privacy Policy