2017-03-02 15:26:21 -05:00
|
|
|
package acceptance.fixture;
|
2017-02-22 14:11:40 -05:00
|
|
|
|
2017-03-03 15:06:49 -05:00
|
|
|
import static table.FunctionTable.resetFunctionTable;
|
2017-03-06 12:44:06 -05:00
|
|
|
import static util.Path.getPathPrefix;
|
2017-03-03 15:06:49 -05:00
|
|
|
|
2017-02-22 14:11:40 -05:00
|
|
|
import java.io.*;
|
|
|
|
|
2017-03-06 12:44:06 -05:00
|
|
|
import environment.RuntimeEnvironment;
|
2017-02-22 14:11:40 -05:00
|
|
|
import interpreter.*;
|
2017-03-03 15:06:49 -05:00
|
|
|
import table.ExecutionContext;
|
2017-02-22 14:11:40 -05:00
|
|
|
|
|
|
|
public class LispInterpreterFixture {
|
|
|
|
|
2017-02-27 12:00:24 -05:00
|
|
|
private static ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
2017-03-06 12:44:06 -05:00
|
|
|
private static ExecutionContext executionContext = ExecutionContext.getInstance();
|
|
|
|
private static RuntimeEnvironment environment = RuntimeEnvironment.getInstance();
|
2017-02-27 12:00:24 -05:00
|
|
|
private static LispInterpreter interpreter = null;
|
2017-02-22 14:11:40 -05:00
|
|
|
|
2017-03-06 12:44:06 -05:00
|
|
|
public static void buildInterpreter() {
|
2017-02-22 14:11:40 -05:00
|
|
|
LispInterpreterBuilder builder = LispInterpreterBuilderImpl.getInstance();
|
|
|
|
builder.setOutput(new PrintStream(outputStream));
|
|
|
|
builder.setErrorOutput(new PrintStream(outputStream));
|
|
|
|
builder.setNotInteractive();
|
2017-03-06 12:44:06 -05:00
|
|
|
builder.setTerminationFunction(LispInterpreterFixture::terminate);
|
|
|
|
builder.setErrorTerminationFunction(LispInterpreterFixture::terminateFromError);
|
2017-02-22 14:11:40 -05:00
|
|
|
|
2017-02-27 12:00:24 -05:00
|
|
|
interpreter = builder.build();
|
2017-02-22 14:11:40 -05:00
|
|
|
}
|
|
|
|
|
2017-03-06 12:44:06 -05:00
|
|
|
public static void terminate() {}
|
|
|
|
|
|
|
|
public static void terminateFromError() {
|
|
|
|
throw new RuntimeException("Error Termination");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void cleanUp() {
|
|
|
|
clearFunctionsAndSymbols();
|
|
|
|
environment.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void clearFunctionsAndSymbols() {
|
2017-03-03 15:06:49 -05:00
|
|
|
resetFunctionTable();
|
2017-03-06 12:44:06 -05:00
|
|
|
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();
|
2017-03-01 11:11:59 -05:00
|
|
|
}
|
|
|
|
|
2017-03-06 12:44:06 -05:00
|
|
|
private String evaluate() {
|
2017-02-22 14:11:40 -05:00
|
|
|
interpreter.interpret();
|
|
|
|
String output = outputStream.toString();
|
|
|
|
outputStream.reset();
|
2017-02-24 11:07:06 -05:00
|
|
|
|
|
|
|
return output.trim();
|
2017-02-22 14:11:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|