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:
TruffleRunnerSnippets#TestExecuteNode
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.
TruffleRunnerSnippets#ExampleTest
Running a test in the polyglot engine
If a test should be run in the context of a polyglot engine,
RunWithPolyglotRule can be
used.
TruffleRunnerSnippets#RunWithPolyglotRule