-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
testing JUnit 5 equivalent of XpectRunner
- Loading branch information
1 parent
e27f2f2
commit ab28e2f
Showing
13 changed files
with
565 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
org.eclipse.xpect/src/org/eclipse/xpect/dynamic/IXpectDynamicTestFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
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()); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
org.eclipse.xpect/src/org/eclipse/xpect/dynamic/XpectDynamicTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
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, () -> runInternal()); | ||
} | ||
|
||
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(); | ||
try { | ||
try { | ||
state.get(ValidatingSetup.class).get().validate(); | ||
} catch (Throwable e) { | ||
throw new AssertionError("Setup validation failed", e); | ||
} | ||
method.getJavaMethod().invoke(test); | ||
} catch (InvocationTargetException e) { | ||
throw e.getCause(); | ||
} finally { | ||
state.invalidate(); | ||
} | ||
} | ||
|
||
private static String formatDisplayName(String name, String className) { | ||
return String.format("%s(%s)", name, className); | ||
} | ||
|
||
} |
148 changes: 148 additions & 0 deletions
148
org.eclipse.xpect/src/org/eclipse/xpect/dynamic/XpectDynamicTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package org.eclipse.xpect.dynamic; | ||
|
||
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.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 com.google.common.collect.Lists; | ||
|
||
public class XpectDynamicTestCase { | ||
private Stream<DynamicTest> children; | ||
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; | ||
} | ||
|
||
/** | ||
* <p> | ||
* To run code before an Xpect test begins, extend this class and annotate the extending class with: | ||
* {@code @org.eclipse.xpect.XpectReplace(org.eclipse.xpect.dynamic.XpectDynamicTestCase)} | ||
* </p> | ||
* <p> | ||
* The extended class must then be included in the values of the annotation of the test case: | ||
* {@code @org.eclipse.xpect.XpectImport({...class})} | ||
* </p> | ||
*/ | ||
public void setUp() throws Throwable { | ||
// nothing to set up | ||
} | ||
|
||
/** | ||
* <p> | ||
* To run code after an Xpect test finishes, extend this class and annotate the extending class with: | ||
* {@code @org.eclipse.xpect.XpectReplace(org.eclipse.xpect.dynamic.XpectDynamicTestCase)} | ||
* </p> | ||
* <p> | ||
* The extended class must then be included in the values of the annotation of the test case: | ||
* {@code @org.eclipse.xpect.XpectImport({...class})} | ||
* </p> | ||
*/ | ||
public void tearDown() throws Throwable { | ||
// nothing to tear down | ||
} | ||
|
||
public DynamicContainer dynamicContainer() { | ||
return DynamicContainer.dynamicContainer(getName(), getChildren()); | ||
} | ||
|
||
protected Stream<DynamicTest> createChildren() { | ||
List<DynamicTest> children = Lists.newArrayList(); | ||
if (xpectFile != null) { | ||
XpectJavaModel xjm = xpectFile.getJavaModel(); | ||
for (XjmMethod method : xjm.getMethods().values()) | ||
if (method instanceof XjmTestMethod) { | ||
DynamicTest test = createDynamicTest((XjmTestMethod) method); | ||
if (test != null) | ||
children.add(test); | ||
} | ||
for (XpectInvocation inv : xpectFile.getInvocations()) { | ||
DynamicTest test = createDynamicTest(inv); | ||
if (test != null) | ||
children.add(test); | ||
} | ||
} | ||
return children.stream().map(test -> wrapTest(test)); | ||
} | ||
|
||
protected DynamicTest createDynamicTest(XjmTestMethod method) { | ||
StateContainer childState = TestExecutor.createState(state, TestExecutor.createTestConfiguration(method)); | ||
return childState.get(XpectDynamicTest.class).get().test(); | ||
} | ||
|
||
protected DynamicTest createDynamicTest(XpectInvocation invocation) { | ||
StateContainer childState = TestExecutor.createState(state, TestExecutor.createXpectConfiguration(invocation)); | ||
DynamicTest test = childState.get(XpectInvocationDynamicTest.class).get().test(); | ||
return wrapTest(test); | ||
} | ||
|
||
protected DynamicTest wrapTest(DynamicTest test) { | ||
return DynamicTest.dynamicTest(test.getDisplayName(), () -> { | ||
setUp(); | ||
try { | ||
test.getExecutable().execute(); | ||
} finally { | ||
try { | ||
state.invalidate(); | ||
} finally { | ||
tearDown(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
protected Stream<DynamicTest> 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; | ||
} | ||
} |
Oops, something went wrong.