[parameterizedXtextRunner] improved robustness and added OffsetHelper

This commit is contained in:
Moritz Eysholdt 2011-10-29 17:36:32 +02:00
parent d36328926a
commit 3a2cf78f5c
6 changed files with 127 additions and 3 deletions

View file

@ -0,0 +1,100 @@
/*******************************************************************************
* 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.parameterized;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.xtext.Assignment;
import org.eclipse.xtext.GrammarUtil;
import org.eclipse.xtext.nodemodel.ILeafNode;
import org.eclipse.xtext.nodemodel.INode;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.eclipse.xtext.parsetree.reconstr.impl.NodeIterator;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.util.Pair;
import org.eclipse.xtext.util.Tuples;
import org.junit.Assert;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
/**
* @author Moritz Eysholdt - Initial contribution and API
*/
public class OffsetHelper {
public Offset at(XtextResource resource, int offset) {
return new Offset(resource, offset);
}
public Offset at(XtextResource resource, String offset) {
return at(resource, Integer.valueOf(offset));
}
public static class Offset {
protected int offset;
protected XtextResource resource;
public Offset(XtextResource resource, int offset) {
super();
this.resource = resource;
this.offset = offset;
}
public EObject getEObject() {
EObject object = NodeModelUtils.findActualSemanticObjectFor(getLeafNodeAtOffset());
Assert.assertNotNull("No EObject found at offset " + offset, object);
return object;
}
public Pair<EObject, EStructuralFeature> getEStructuralFeatureByParent() {
INode leaf = getLeafNodeAtOffset();
EObject object = NodeModelUtils.findActualSemanticObjectFor(leaf);
Assert.assertNotNull("No EObject found at offset " + offset, object);
Assignment ass = GrammarUtil.containingAssignment(leaf.getGrammarElement());
while (ass == null && leaf.getParent() != null) {
leaf = leaf.getParent();
ass = GrammarUtil.containingAssignment(leaf.getGrammarElement());
}
Assert.assertNotNull("No Assignment found at offset " + offset, ass);
@SuppressWarnings("null")
EStructuralFeature feature = object.eClass().getEStructuralFeature(ass.getFeature());
return Tuples.create(object, feature);
}
public Pair<EObject, EStructuralFeature> getEStructuralFeatureByOffset() {
return getEStructuralFeatureByOffset(Predicates.<EStructuralFeature> alwaysTrue());
}
public Pair<EObject, EStructuralFeature> getEStructuralFeatureByOffset(Predicate<EStructuralFeature> matches) {
INode leaf = getLeafNodeAtOffset();
NodeIterator ni = null;
while (ni == null || ni.hasNext()) {
INode next = ni == null ? leaf : ni.next();
if (ni == null)
ni = new NodeIterator(leaf);
Assignment ass = GrammarUtil.containingAssignment(next.getGrammarElement());
if (ass != null) {
EObject object = NodeModelUtils.findActualSemanticObjectFor(next);
EStructuralFeature feat = object.eClass().getEStructuralFeature(ass.getFeature());
if (feat != null && matches.apply(feat))
return Tuples.create(object, feat);
}
}
Assert.fail("No EStructuralFeature found at offset " + offset);
return null;
}
public ILeafNode getLeafNodeAtOffset() {
ILeafNode node = NodeModelUtils.findLeafNodeAtOffset(resource.getParseResult().getRootNode(), offset);
Assert.assertNotNull("No Leaf Node found at offset " + offset, node);
return node;
}
}
}

View file

@ -22,6 +22,7 @@ import org.eclipse.xtext.junit4.parameterized.TestExpectationValidator.TestResul
import org.eclipse.xtext.junit4.parameterized.XpectCommaSeparatedValues.CSVResultValidator;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.util.Pair;
import org.junit.Assert;
import org.junit.ComparisonFailure;
import com.google.common.base.Function;
@ -49,6 +50,11 @@ public @interface XpectCommaSeparatedValues {
}
public void validate(XtextResource res, IExpectation expectation, @TestResult Iterable<Object> actual) {
Assert.assertNotNull(res);
Assert.assertNotNull(expectation);
Assert.assertNotNull(expectation.getExpectation());
Assert.assertNotNull(actual);
ExpectationCollection exp = new ExpectationCollection();
exp.setCaseSensitive(cfg.caseSensitive());
exp.setOrdered(cfg.ordered());

View file

@ -23,6 +23,7 @@ import org.eclipse.xtext.junit4.parameterized.TestExpectationValidator.TestResul
import org.eclipse.xtext.junit4.parameterized.XpectLines.LinesResultValidator;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.util.Pair;
import org.junit.Assert;
import org.junit.ComparisonFailure;
import com.google.common.base.Function;
@ -46,6 +47,11 @@ public @interface XpectLines {
}
public void validate(XtextResource res, IExpectation expectation, @TestResult Iterable<Object> actual) {
Assert.assertNotNull(res);
Assert.assertNotNull(expectation);
Assert.assertNotNull(expectation.getExpectation());
Assert.assertNotNull(actual);
ExpectationCollection exp = new ExpectationCollection();
exp.setCaseSensitive(cfg.caseSensitive());
exp.setOrdered(cfg.ordered());

View file

@ -379,8 +379,10 @@ public class XpectParameterProvider implements IParameterProvider {
protected int parseXpectParams(Class<?> testClass, INode node, String methodName, final String text, int offset,
Map<String, String> params) {
int semanticOffset = getOffsetOfNextSemanticNode(node);
params.put(OFFSET, String.valueOf(semanticOffset));
String paramSyntax = getParameterSyntax(testClass, methodName);
if (org.eclipse.xtext.util.Strings.isEmpty(paramSyntax))
if (Strings.isEmpty(paramSyntax))
return -1;
Nfa<ProdElement> nfa = getParameterNfa(paramSyntax);
List<BacktrackItem> trace = new NfaUtil().backtrack(nfa, new BacktrackItem(offset),
@ -417,8 +419,6 @@ public class XpectParameterProvider implements IParameterProvider {
return followers;
}
});
int semanticOffset = getOffsetOfNextSemanticNode(node);
params.put(OFFSET, String.valueOf(semanticOffset));
if (trace != null && !trace.isEmpty()) {
for (BacktrackItem item : trace)
if (item.token != null && item.token.getName() != null)

View file

@ -18,6 +18,7 @@ import org.eclipse.xtext.junit4.parameterized.TestExpectationValidator.TestResul
import org.eclipse.xtext.junit4.parameterized.XpectString.StringResultValidator;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.util.internal.FormattingMigrator;
import org.junit.Assert;
import org.junit.ComparisonFailure;
/**
@ -37,6 +38,10 @@ public @interface XpectString {
}
public void validate(XtextResource resource, IExpectation expectation, @TestResult String actual) {
Assert.assertNotNull(resource);
Assert.assertNotNull(expectation);
Assert.assertNotNull(expectation.getExpectation());
Assert.assertNotNull(actual);
String exp;
if (!config.whitespaceSensitive()) {
FormattingMigrator migrator = new FormattingMigrator();

View file

@ -449,6 +449,13 @@ public class XpectParameterProviderTest
params(model));
}
@Test
public void xpectSLParameterDefault()
{
String model = "// XPECT meth";
Assert.assertEquals("meth(offset at 'h\\n !foo')", params(model));
}
@Test
public void xpectSLParameterWithExp()
{