transcendental-lisp/test/acceptance/fixture/LispInterpreterFixture.java

71 lines
2.1 KiB
Java

package acceptance.fixture;
import static table.FunctionTable.resetFunctionTable;
import static util.Path.getPathPrefix;
import java.io.*;
import environment.RuntimeEnvironment;
import interpreter.*;
import table.ExecutionContext;
public class LispInterpreterFixture {
private static ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
private static ExecutionContext executionContext = ExecutionContext.getInstance();
private static RuntimeEnvironment environment = RuntimeEnvironment.getInstance();
private static LispInterpreter interpreter = null;
public static void buildInterpreter() {
LispInterpreterBuilder builder = LispInterpreterBuilderImpl.getInstance();
builder.setOutput(new PrintStream(outputStream));
builder.setErrorOutput(new PrintStream(outputStream));
builder.setNotInteractive();
builder.setTerminationFunction(LispInterpreterFixture::terminate);
builder.setErrorTerminationFunction(LispInterpreterFixture::terminateFromError);
interpreter = builder.build();
}
public static void terminate() {}
public static void terminateFromError() {
throw new RuntimeException("Error Termination");
}
public static void cleanUp() {
clearFunctionsAndSymbols();
environment.reset();
}
public static void clearFunctionsAndSymbols() {
resetFunctionTable();
executionContext.clearContext();
}
public String evaluateText(String input) {
environment.setInputName("fitnesse");
environment.setInput(new ByteArrayInputStream(input.getBytes()));
environment.setPath("");
return evaluate();
}
public String evaluateFile(String inputFile) throws FileNotFoundException {
environment.setInputName(inputFile);
environment.setInput(new FileInputStream(inputFile));
environment.setPath(getPathPrefix(inputFile));
return evaluate();
}
private String evaluate() {
interpreter.interpret();
String output = outputStream.toString();
outputStream.reset();
return output.trim();
}
}