Skip to content

Commit

Permalink
testing JUnit 5 equivalent of XpectRunner
Browse files Browse the repository at this point in the history
  • Loading branch information
Simeon Andreev authored and Simeon Andreev committed May 23, 2024
1 parent e27f2f2 commit 9891333
Show file tree
Hide file tree
Showing 15 changed files with 594 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.eclipse.xpect.XpectFile;
import org.eclipse.xpect.registry.ILanguageInfo;
import org.eclipse.xpect.runner.XpectRunner;
import org.eclipse.xpect.runner.XpectTestGlobalState;

import com.google.inject.Injector;

Expand Down Expand Up @@ -67,8 +68,8 @@ protected static ResourceSet cloneResourceSet(ResourceSet rs) {
// need delegation or nothing because of "java" protocol
// result.setResourceFactoryRegistry(rs.getResourceFactoryRegistry());
result.setURIConverter(rs.getURIConverter());
if (XpectRunner.testClassloader != null) {
result.setClasspathURIContext(XpectRunner.testClassloader);
if (XpectTestGlobalState.INSTANCE.testClass() != null) {
result.setClasspathURIContext(XpectTestGlobalState.INSTANCE.testClass().getClassLoader());
result.setClasspathUriResolver(new ClassloaderClasspathUriResolver());
} else if (rs instanceof XtextResourceSet) {
XtextResourceSet xrs = (XtextResourceSet) rs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.eclipse.xpect.XpectFile;
import org.eclipse.xpect.XpectJavaModel;
import org.eclipse.xpect.runner.XpectRunner;
import org.eclipse.xpect.runner.XpectTestGlobalState;
import org.eclipse.xpect.ui.internal.XpectActivator;

import com.google.inject.Injector;
Expand All @@ -40,8 +41,8 @@ public static XpectFile loadFile(IFile file) {
Injector injector = XpectActivator.getInstance().getInjector(XpectActivator.ORG_ECLIPSE_XPECT_XPECT);
XtextResourceSet rs = new XtextResourceSet();
IJavaProject javaProject = JavaCore.create(file.getProject());
if (XpectRunner.testClassloader != null) {
rs.setClasspathURIContext(XpectRunner.testClassloader);
if (XpectTestGlobalState.INSTANCE.testClass() != null) {
rs.setClasspathURIContext(XpectTestGlobalState.INSTANCE.testClass().getClassLoader());
rs.setClasspathUriResolver(new ClassloaderClasspathUriResolver());
} else if (javaProject != null && javaProject.exists()) {
rs.setClasspathURIContext(javaProject);
Expand Down
11 changes: 5 additions & 6 deletions org.eclipse.xpect/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@ Require-Bundle: org.eclipse.xtext,
org.junit;bundle-version="4.11.0";visibility:=reexport,
org.eclipse.xtext.common.types;visibility:=reexport,
org.apache.log4j;bundle-version="1.2.0";visibility:=reexport,
org.objectweb.asm;bundle-version="[9.5.0,10.0.0)";resolution:=optional
org.objectweb.asm;bundle-version="[9.5.0,10.0.0)";resolution:=optional,
junit-jupiter-api;bundle-version="5.10.2"
Bundle-RequiredExecutionEnvironment: JavaSE-11
Export-Package: org.eclipse.xpect,
org.eclipse.xpect.dynamic,
org.eclipse.xpect.expectation,
org.eclipse.xpect.expectation.impl;
x-friends:="org.eclipse.xpect.xtext.lib,
org.eclipse.xpect.tests",
org.eclipse.xpect.expectation.impl;x-friends:="org.eclipse.xpect.xtext.lib,org.eclipse.xpect.tests",
org.eclipse.xpect.formatting,
org.eclipse.xpect.lexer;
x-friends:="org.eclipse.xpect.tests",
org.eclipse.xpect.lexer;x-friends:="org.eclipse.xpect.tests",
org.eclipse.xpect.lib,
org.eclipse.xpect.model,
org.eclipse.xpect.parameter,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.eclipse.xpect.dynamic;

import java.util.stream.Stream;

import org.eclipse.xpect.XpectImport;
import org.eclipse.xpect.runner.TestTitleProvider;
import org.junit.jupiter.api.DynamicContainer;
import org.junit.jupiter.api.TestFactory;

@XpectImport(TestTitleProvider.class)
public interface IXpectDynamicTestFactory {

@TestFactory
default Stream<DynamicContainer> generateTests() {
return XpectDynamicTestFactory.xpectTests(getClass());
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.eclipse.xpect.dynamic;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.xpect.XjmTestMethod;
import org.eclipse.xpect.runner.ValidatingSetup;
import org.eclipse.xpect.runner.XpectTestGlobalState;
import org.eclipse.xpect.setup.ThisTestObject;
import org.eclipse.xpect.state.Creates;
import org.eclipse.xpect.state.StateContainer;
import org.junit.jupiter.api.DynamicTest;

import com.google.common.base.Preconditions;

public class XpectDynamicTest {

private final String className;
private XjmTestMethod method;
private final StateContainer state;

public XpectDynamicTest(StateContainer state, XjmTestMethod method) {
Preconditions.checkNotNull(method);
this.className = XpectTestGlobalState.INSTANCE.testClass().getName();
this.method = method;
this.state = state;
}

@Creates
public XpectDynamicTest create() {
return this;
}

public XjmTestMethod getMethod() {
return method;
}

public StateContainer getState() {
return state;
}

public DynamicTest test() {
String testName = getName();
return DynamicTest.dynamicTest(testName, () -> {
state.get(ValidatingSetup.class).get().validate();
try {
runInternal();
} finally {
state.invalidate();
}
});
}

public String getName() {
String testName = formatDisplayName(method.getName(), className);
return testName;
}

protected void runInternal() throws Throwable {
Object test = state.get(Object.class, ThisTestObject.class).get();
// Object test = method.getTest().getJavaClass().newInstance();
// ctx.setMethod(method);
// ctx.setTestInstance(test);
try {
// if (setup != null)
// ctx.setUserTestCtx(setup.beforeTest(ctx, ctx.getUserFileCtx()));
method.getJavaMethod().invoke(test);
} catch (InvocationTargetException e) {
throw e.getCause();
}
// finally {
// if (setup != null)
// setup.afterTest(ctx, ctx.getUserTestCtx());
// }
}

private static String formatDisplayName(String name, String className) {
return String.format("%s(%s)", name, className);
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package org.eclipse.xpect.dynamic;

import static org.eclipse.xpect.runner.DescriptionFactory.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

import org.eclipse.emf.common.util.URI;
import org.eclipse.xpect.XjmMethod;
import org.eclipse.xpect.XjmTestMethod;
import org.eclipse.xpect.XpectFile;
import org.eclipse.xpect.XpectInvocation;
import org.eclipse.xpect.XpectJavaModel;
import org.eclipse.xpect.runner.IXpectURIProvider;
import org.eclipse.xpect.runner.TestExecutor;
import org.eclipse.xpect.runner.TestRunner;
import org.eclipse.xpect.runner.ValidatingSetup;
import org.eclipse.xpect.runner.XpectTestRunner;
import org.eclipse.xpect.setup.ThisRootTestClass;
import org.eclipse.xpect.state.Creates;
import org.eclipse.xpect.state.StateContainer;
import org.junit.jupiter.api.DynamicContainer;
import org.junit.jupiter.api.DynamicTest;
import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunNotifier;

import com.google.common.collect.Lists;

public class XpectDynamicTestCase {
private List<DynamicContainer> children;
private Description description;
private final StateContainer state;
private final XpectFile xpectFile;

public XpectDynamicTestCase(StateContainer state, XpectFile file) {
this.xpectFile = file;
this.state = state;
}

@Creates
public XpectDynamicTestCase create() {
return this;
}

public DynamicContainer dynamicContainer() {
return DynamicContainer.dynamicContainer(getName(), getChildren().stream());
}

protected List<DynamicContainer> createChildren() {
List<DynamicContainer> children = Lists.newArrayList();
if (xpectFile != null) {
XpectJavaModel xjm = xpectFile.getJavaModel();
for (XjmMethod method : xjm.getMethods().values())
if (method instanceof XjmTestMethod) {
XpectDynamicTest test = createDynamicTest((XjmTestMethod) method);
if (test != null)
children.add(DynamicContainer.dynamicContainer(test.getName(), Arrays.asList(test.test())));
}
for (XpectInvocation inv : xpectFile.getInvocations()) {
DynamicContainer test = createDynamicTest(inv);
if (test != null)
children.add(test);
}
}
return children;
}

protected XpectDynamicTest createDynamicTest(XjmTestMethod method) {
StateContainer childState = TestExecutor.createState(state, TestExecutor.createTestConfiguration(method));
return childState.get(XpectDynamicTest.class).get();
}

protected DynamicContainer createDynamicTest(XpectInvocation invocation) {
StateContainer childState = TestExecutor.createState(state, TestExecutor.createXpectConfiguration(invocation));
List<DynamicTest> tests = Arrays.asList(childState.get(XpectInvocationDynamicTest.class).get().test());
String testClassName = getJavaTestClass().getName();
IXpectURIProvider uriProvider = getURIProvider();
String testName = XpectInvocationDynamicTest.getTestNameForInvocation(uriProvider, testClassName, invocation);
return DynamicContainer.dynamicContainer(testName, tests);
}

protected List<DynamicContainer> getChildren() {
if (children == null)
children = createChildren();
return children;
}

public Class<?> getJavaTestClass() {
return state.get(Class.class, ThisRootTestClass.class).get();
}

public IXpectURIProvider getURIProvider() {
return state.get(IXpectURIProvider.class).get();
}

public StateContainer getState() {
return state;
}

public URI getUri() {
return xpectFile.eResource().getURI();
}

public XpectFile getXpectFile() {
return xpectFile;
}

public String getName() {
IXpectURIProvider uriProvider = getURIProvider();
URI uri = getUri();
URI deresolved = uriProvider.deresolveToProject(uri);
String pathInProject = deresolved.trimSegments(1).toString();
String fileName = deresolved.lastSegment();
return fileName + ": " + pathInProject;
}
}

Loading

0 comments on commit 9891333

Please sign in to comment.