A Lambda Calculus intepreter in Java
July 26, 2015 • Alex Leighton
In the course of making a formatter for our lambda calculus terms, I stumbled on a situation where the ability to ignore a test was needed. Let’s see how to add “ignored” tests to our testing framework.
Similar to JUnit, I’ve added an @Ignore annotation for marking tests as ignored. Next, we need to a new kind of TestResult:
/**
* Objects representing an ignored test result for given tests.
*/
public static class Ignored extends TestResult {
private Ignored(final String testName) {
super(testName);
}
}/tst/alonzo/unit/TestResult.java
RunnableTest needs to be changed in order to understand ignored tests. When making the change, I chose to not construct the test class instance or run the test method, opting for an early return with no work done. Further, as this addition made RunnableTest.get() too large for my taste, I refactored construction of the test class instance into a method.
if (isIgnoredTest()) {
return TestResult.ignore(testName);
}
final Object instance = constructTestInstance();/tst/alonzo/unit/RunnableTest.java
private boolean isIgnoredTest() {
return method.getAnnotation(Ignore.class) != null;
}/tst/alonzo/unit/RunnableTest.java
Finally, at the top level of TestRunner, all that’s needed is a simple collection of ignored test results and the addition of an ignored section in the overall result output.
final List<TestResult> ignored = results.stream()
.filter(r -> r.isIgnored()).collect(Collectors.toList());/tst/alonzo/unit/TestRunner.java
A good test of any software system is how easy it is to adjust the system incrementally. I’m pleased with how easy it was to adjust our testing framework in this way.