TruffleRunner
public class TruffleRunner extends BlockJUnit4ClassRunner- Inheritance
ObjectRunnerParentRunnerBlockJUnit4ClassRunnerTruffleRunner
JUnit test runner for unit testing Truffle AST interpreters.
A test using TruffleRunner consists of 2 parts, a Truffle AST to be tested, and a test method that drives the test, provides input argument values and validates the result.
Writing a test AST
The Truffle AST to be tested is written as a RootNode subclass, for example:
public class TestExecuteNode extends RootNode {
@Child InteropLibrary interop;
public TestExecuteNode() {
super(runWithPolyglot.getTestLanguage());
interop = InteropLibrary.getFactory().createDispatched(5);
}
@Override
public Object execute(VirtualFrame frame) {
Object obj = frame.getArguments()[0];
try {
return interop.execute(obj);
} catch (InteropException ex) {
CompilerDirectives.transferToInterpreter();
Assert.fail(ex.getMessage());
return null;
}
}
}Writing a test method
The test method is a normal method annotated with Test. It may have one or more arguments of type CallTarget that are annotated with Inject. The Inject annotation specifies a RootNode subclass that is the root of a test AST, and the TruffleRunner will create one CallTarget for each of these test ASTs. The test method can then execute the AST by calling the CallTarget.call method.
Typically a test method will prepare some arguments, and then do a single call to CallTarget.call. Then it should verify the result by inspecting the return value and checking the expected side effects of the test code.
@RunWith(TruffleRunner.class)
public class ExampleTest {
@Test
public void executeTest(@Inject(TestExecuteNode.class) CallTarget target) {
TruffleObject receiver = prepareArgumentValue();
Object ret = target.call(receiver);
Assert.assertEquals(expectedRetValue(), ret);
}
}Running a test in the polyglot engine
If a test should be run in the context of a polyglot engine, RunWithPolyglotRule can be used.
@RunWith(TruffleRunner.class)
public static class PolyglotTest {
@ClassRule RunWithPolyglotRule runWithPolyglot = new RunWithPolyglotRule();
private static Object prepared;
@BeforeClass
public void prepare() {
prepared = runWithPolyglot.getTruffleTestEnv().importSymbol("...");
}
@Test
public void executeTest(@Inject(TestExecuteNode.class) CallTarget target) {
Object ret = target.call(prepared);
Assert.assertEquals(expectedRetValue(), ret);
}
}Constructors2
TruffleRunner(Class<?>)
TruffleRunner
since 0.25public TruffleRunner(Class< ?> klass) throws InitializationErrorShould not be called directly. To use this class, annotate your test class with @RunWith(TruffleRunner.class).
Parameters
klassClass<?> - No description.
Throws
InitializationError- No description.
See also
TruffleRunner(TestClass)
TruffleRunner
since 24.2public TruffleRunner(TestClass testClass) throws InitializationErrorShould not be called directly. To use this class, annotate your test class with @RunWith(TruffleRunner.class).
Parameters
testClassTestClass- No description.
Throws
InitializationError- No description.
See also
Methods2
Statement methodInvoker(FrameworkMethod, Object)
methodInvoker
since 0.25protected final Statement methodInvoker(FrameworkMethod method, Object test)Internal method used by the JUnit framework. Do not call directly.
Parameters
methodFrameworkMethod- No description.
testObject- No description.
Returns
Statement- No description.
Overrides methodInvoker(org.junit.runners.model.FrameworkMethod,java.lang.Object).
void validateTestMethods(List<Throwable>)
validateTestMethods
since 0.25Internal method used by the JUnit framework. Do not call directly.
Overrides validateTestMethods(java.util.List).
Nested types
public @interface .InjectA parameter annotated with
Injectspecifies theRootNodeof the test AST.public static final class .ParametersFactoryParametersRunnerFactoryfor testing Truffle AST interpreters usingParameterizedunit tests.public static final class .RunWithPolyglotRuleJUnit rule to run the tests in the context of a polyglot engine.
public @interface .WarmupA test method can be annotated with
Warmupto specify how many warmup iterations of a test should be done before the Truffle tree is compiled.
Inherited members
From class BlockJUnit4ClassRunner
collectInitializationErrors, computeTestMethods, createTest, createTest, describeChild, getChildren, getTestRules, isIgnored, methodBlock, possiblyExpectingExceptions, rules, runChild, testName, validateConstructor, validateFields, validateInstanceMethods, validateNoNonStaticInnerClass, validateOnlyOneConstructor, validateZeroArgConstructor, withAfters, withBefores, withPotentialTimeout
From class ParentRunner
childrenInvoker, classBlock, classRules, createTestClass, filter, getDescription, getName, getRunnerAnnotations, getTestClass, order, run, runLeaf, setScheduler, sort, validatePublicVoidNoArgMethods, withAfterClasses, withBeforeClasses, withInterruptIsolation
From class Runner
testCount