Started added unit tests for the interpreter and main packages
This commit is contained in:
parent
1fed0433e5
commit
e51d275b76
|
@ -2,24 +2,20 @@ package fixture;
|
|||
|
||||
import java.io.*;
|
||||
|
||||
import environment.RuntimeEnvironment;
|
||||
import interpreter.*;
|
||||
|
||||
public class LispInterpreterFixture {
|
||||
|
||||
private ByteArrayOutputStream outputStream;
|
||||
private RuntimeEnvironment environment;
|
||||
private LispInterpreter interpreter;
|
||||
|
||||
public LispInterpreterFixture() throws IOException {
|
||||
this.outputStream = new ByteArrayOutputStream();
|
||||
this.environment = RuntimeEnvironment.getInstance();
|
||||
this.interpreter = buildInterpreter();
|
||||
}
|
||||
|
||||
private LispInterpreter buildInterpreter() {
|
||||
LispInterpreterBuilder builder = LispInterpreterBuilderImpl.getInstance();
|
||||
builder.setInputName("fitnesse");
|
||||
builder.setOutput(new PrintStream(outputStream));
|
||||
builder.setErrorOutput(new PrintStream(outputStream));
|
||||
builder.setNotInteractive();
|
||||
|
@ -30,7 +26,7 @@ public class LispInterpreterFixture {
|
|||
}
|
||||
|
||||
public String evaluate(String input) throws IOException {
|
||||
environment.setInput(new ByteArrayInputStream(input.getBytes()));
|
||||
interpreter.setInput(new ByteArrayInputStream(input.getBytes()), "fitnesse");
|
||||
interpreter.interpret();
|
||||
String output = outputStream.toString();
|
||||
outputStream.reset();
|
||||
|
|
|
@ -2,7 +2,7 @@ package interpreter;
|
|||
|
||||
import static function.builtin.EVAL.eval;
|
||||
|
||||
import java.io.PrintStream;
|
||||
import java.io.*;
|
||||
|
||||
import environment.RuntimeEnvironment;
|
||||
import error.*;
|
||||
|
@ -22,6 +22,11 @@ public class LispInterpreter {
|
|||
this.output = environment.getOutput();
|
||||
}
|
||||
|
||||
public void setInput(InputStream input, String inputName) {
|
||||
environment.setInput(input);
|
||||
environment.setInputName(inputName);
|
||||
}
|
||||
|
||||
public void interpret() {
|
||||
createParser();
|
||||
printGreeting();
|
||||
|
|
|
@ -5,9 +5,7 @@ import java.util.function.Function;
|
|||
|
||||
public interface LispInterpreterBuilder {
|
||||
|
||||
void setInputName(String inputName);
|
||||
|
||||
void setInput(InputStream inputStream);
|
||||
void setInput(InputStream inputStream, String inputName);
|
||||
|
||||
void setOutput(PrintStream outputStream);
|
||||
|
||||
|
@ -31,6 +29,8 @@ public interface LispInterpreterBuilder {
|
|||
|
||||
void setCriticalOutputDecorator(Function<String, String> criticalOutputDecorator);
|
||||
|
||||
default void reset() {}
|
||||
|
||||
LispInterpreter build();
|
||||
|
||||
}
|
||||
|
|
|
@ -27,14 +27,14 @@ public class LispInterpreterBuilderImpl implements LispInterpreterBuilder {
|
|||
private Function<String, String> criticalOutputDecorator;
|
||||
private RuntimeEnvironment environment;
|
||||
private boolean isInteractive;
|
||||
private boolean isUseFile;
|
||||
private boolean isBuilt;
|
||||
private boolean isFileBased;
|
||||
protected boolean isBuilt;
|
||||
|
||||
private LispInterpreterBuilderImpl() {
|
||||
protected LispInterpreterBuilderImpl() {
|
||||
this.environment = RuntimeEnvironment.getInstance();
|
||||
this.inputName = "";
|
||||
this.isInteractive = true;
|
||||
this.isUseFile = false;
|
||||
this.isFileBased = false;
|
||||
this.isBuilt = false;
|
||||
this.outputDecorator = s -> s;
|
||||
this.valueOutputDecorator = s -> s;
|
||||
|
@ -44,13 +44,9 @@ public class LispInterpreterBuilderImpl implements LispInterpreterBuilder {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void setInputName(String inputName) {
|
||||
this.inputName = inputName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInput(InputStream inputStream) {
|
||||
public void setInput(InputStream inputStream, String inputName) {
|
||||
this.inputStream = inputStream;
|
||||
this.inputName = inputName;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -81,8 +77,8 @@ public class LispInterpreterBuilderImpl implements LispInterpreterBuilder {
|
|||
|
||||
@Override
|
||||
public void useFile(String fileName) {
|
||||
this.isUseFile = true;
|
||||
this.setInputName(fileName);
|
||||
this.isFileBased = true;
|
||||
this.inputName = fileName;
|
||||
this.setNotInteractive();
|
||||
}
|
||||
|
||||
|
@ -156,14 +152,14 @@ public class LispInterpreterBuilderImpl implements LispInterpreterBuilder {
|
|||
}
|
||||
|
||||
private InputStream getInputStream() throws FileNotFoundException {
|
||||
return isUseFile ? new FileInputStream(inputName) : inputStream;
|
||||
return isFileBased ? new FileInputStream(inputName) : inputStream;
|
||||
}
|
||||
|
||||
private LispInterpreter createInterpreter() {
|
||||
return isInteractive ? new InteractiveLispInterpreter() : new LispInterpreter();
|
||||
}
|
||||
|
||||
public class InterpreterAlreadyBuiltException extends CriticalLispException {
|
||||
public static class InterpreterAlreadyBuiltException extends CriticalLispException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
|
|
@ -37,10 +37,8 @@ public class LispMain {
|
|||
private static void configureInput(String[] args, LispInterpreterBuilder builder) {
|
||||
if (args.length > 0)
|
||||
builder.useFile(args[0]);
|
||||
else {
|
||||
builder.setInputName("stdin");
|
||||
builder.setInput(System.in);
|
||||
}
|
||||
else
|
||||
builder.setInput(System.in, "stdin");
|
||||
}
|
||||
|
||||
private static Function<String, String> makeColorDecorator(String color) {
|
||||
|
|
|
@ -0,0 +1,78 @@
|
|||
package interpreter;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import org.junit.*;
|
||||
|
||||
import interpreter.LispInterpreterBuilderImpl.InterpreterAlreadyBuiltException;
|
||||
|
||||
public class LispInterpreterBuilderTester {
|
||||
|
||||
private LispInterpreterBuilder builder = new LispInterpreterBuilderImpl() {
|
||||
|
||||
public void reset() {
|
||||
this.isBuilt = false;
|
||||
}
|
||||
};
|
||||
|
||||
private void setCommonFeatures() {
|
||||
builder.setOutput(new PrintStream(new ByteArrayOutputStream()));
|
||||
builder.setErrorOutput(new PrintStream(new ByteArrayOutputStream()));
|
||||
builder.setTerminationFunction(() -> {});
|
||||
builder.setErrorTerminationFunction(() -> {});
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
builder.reset();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
builder.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildInteractiveInterpreter() {
|
||||
setCommonFeatures();
|
||||
builder.setInput(System.in, "stdin");
|
||||
LispInterpreter interpreter = builder.build();
|
||||
|
||||
assertTrue(interpreter instanceof InteractiveLispInterpreter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildNonInteractiveInterpreter() {
|
||||
setCommonFeatures();
|
||||
builder.setInput(System.in, "stdin");
|
||||
builder.setNotInteractive();
|
||||
LispInterpreter interpreter = builder.build();
|
||||
|
||||
assertFalse(interpreter instanceof InteractiveLispInterpreter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildFileBasedInterpreter() {
|
||||
setCommonFeatures();
|
||||
builder.useFile("test/interpreter/test-files/file.lisp");
|
||||
LispInterpreter interpreter = builder.build();
|
||||
|
||||
assertFalse(interpreter instanceof InteractiveLispInterpreter);
|
||||
}
|
||||
|
||||
@Test(expected = InterpreterAlreadyBuiltException.class)
|
||||
public void cannotBuildMoreThanOneInterpreter() {
|
||||
builder.build();
|
||||
builder.build();
|
||||
}
|
||||
|
||||
public void interpreterAlreadyBuiltException_HasMessage() {
|
||||
InterpreterAlreadyBuiltException e = new InterpreterAlreadyBuiltException();
|
||||
|
||||
assertNotNull(e.getMessage());
|
||||
assertTrue(e.getMessage().length() > 0);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package main;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.*;
|
||||
|
||||
|
||||
public class MainTester {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue