Elide GraalVM25.6.0-elide.4
All types
Versions and modules

TruffleRunner

Javaclassin com.oracle.truffle.tcksince 0.25

public class TruffleRunner extends BlockJUnit4ClassRunner
Inheritance
  1. Object
  2. Runner
  3. ParentRunner
  4. BlockJUnit4ClassRunner
  5. TruffleRunner

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.25
public TruffleRunner(Class<?> klass) throws InitializationError

Should not be called directly. To use this class, annotate your test class with @RunWith(TruffleRunner.class).

Parameters

klassClass<?>
No description.

Throws

InitializationError
No description.
TruffleRunner(TestClass)

TruffleRunner

since 24.2
public TruffleRunner(TestClass testClass) throws InitializationError

Should not be called directly. To use this class, annotate your test class with @RunWith(TruffleRunner.class).

Parameters

testClassTestClass
No description.

Throws

InitializationError
No description.

Methods2

Statement methodInvoker(FrameworkMethod, Object)

methodInvoker

since 0.25
protected 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.25
protected final void validateTestMethods(List<Throwable> errors)

Internal method used by the JUnit framework. Do not call directly.

Parameters

errorsList<Throwable>
No description.

Overrides validateTestMethods(java.util.List).

Nested types

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