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

80 lines
2.5 KiB
Java
Raw Normal View History

2017-03-02 15:26:21 -05:00
package acceptance.fixture;
2018-01-21 10:32:55 -05:00
import static main.LispMain.LANGUAGE_FILE_NAMES;
2017-03-03 15:06:49 -05:00
import static table.FunctionTable.resetFunctionTable;
import static util.Path.getPathPrefix;
2017-03-03 15:06:49 -05:00
2017-11-12 09:42:25 -05:00
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import environment.RuntimeEnvironment;
2017-11-12 09:42:25 -05:00
import interpreter.LispInterpreter;
import interpreter.LispInterpreterBuilder;
import interpreter.LispInterpreterBuilderImpl;
2017-03-03 15:06:49 -05:00
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;
2018-01-21 10:32:55 -05:00
public static void refreshInterpreter() {
cleanUp();
buildInterpreter();
}
public static void cleanUp() {
resetFunctionTable();
executionContext.clearContext();
environment.reset();
}
public static void buildInterpreter() {
LispInterpreterBuilder builder = new LispInterpreterBuilderImpl() {};
builder.setOutput(new PrintStream(outputStream));
builder.setErrorOutput(new PrintStream(outputStream));
builder.setNotInteractive();
2018-01-21 10:32:55 -05:00
builder.setLanguageFileNames(LANGUAGE_FILE_NAMES);
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 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();
}
}