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 java.io.*;
|
||||||
|
|
||||||
import environment.RuntimeEnvironment;
|
|
||||||
import interpreter.*;
|
import interpreter.*;
|
||||||
|
|
||||||
public class LispInterpreterFixture {
|
public class LispInterpreterFixture {
|
||||||
|
|
||||||
private ByteArrayOutputStream outputStream;
|
private ByteArrayOutputStream outputStream;
|
||||||
private RuntimeEnvironment environment;
|
|
||||||
private LispInterpreter interpreter;
|
private LispInterpreter interpreter;
|
||||||
|
|
||||||
public LispInterpreterFixture() throws IOException {
|
public LispInterpreterFixture() throws IOException {
|
||||||
this.outputStream = new ByteArrayOutputStream();
|
this.outputStream = new ByteArrayOutputStream();
|
||||||
this.environment = RuntimeEnvironment.getInstance();
|
|
||||||
this.interpreter = buildInterpreter();
|
this.interpreter = buildInterpreter();
|
||||||
}
|
}
|
||||||
|
|
||||||
private LispInterpreter buildInterpreter() {
|
private LispInterpreter buildInterpreter() {
|
||||||
LispInterpreterBuilder builder = LispInterpreterBuilderImpl.getInstance();
|
LispInterpreterBuilder builder = LispInterpreterBuilderImpl.getInstance();
|
||||||
builder.setInputName("fitnesse");
|
|
||||||
builder.setOutput(new PrintStream(outputStream));
|
builder.setOutput(new PrintStream(outputStream));
|
||||||
builder.setErrorOutput(new PrintStream(outputStream));
|
builder.setErrorOutput(new PrintStream(outputStream));
|
||||||
builder.setNotInteractive();
|
builder.setNotInteractive();
|
||||||
|
@ -30,7 +26,7 @@ public class LispInterpreterFixture {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String evaluate(String input) throws IOException {
|
public String evaluate(String input) throws IOException {
|
||||||
environment.setInput(new ByteArrayInputStream(input.getBytes()));
|
interpreter.setInput(new ByteArrayInputStream(input.getBytes()), "fitnesse");
|
||||||
interpreter.interpret();
|
interpreter.interpret();
|
||||||
String output = outputStream.toString();
|
String output = outputStream.toString();
|
||||||
outputStream.reset();
|
outputStream.reset();
|
||||||
|
|
|
@ -2,7 +2,7 @@ package interpreter;
|
||||||
|
|
||||||
import static function.builtin.EVAL.eval;
|
import static function.builtin.EVAL.eval;
|
||||||
|
|
||||||
import java.io.PrintStream;
|
import java.io.*;
|
||||||
|
|
||||||
import environment.RuntimeEnvironment;
|
import environment.RuntimeEnvironment;
|
||||||
import error.*;
|
import error.*;
|
||||||
|
@ -22,6 +22,11 @@ public class LispInterpreter {
|
||||||
this.output = environment.getOutput();
|
this.output = environment.getOutput();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setInput(InputStream input, String inputName) {
|
||||||
|
environment.setInput(input);
|
||||||
|
environment.setInputName(inputName);
|
||||||
|
}
|
||||||
|
|
||||||
public void interpret() {
|
public void interpret() {
|
||||||
createParser();
|
createParser();
|
||||||
printGreeting();
|
printGreeting();
|
||||||
|
|
|
@ -5,9 +5,7 @@ import java.util.function.Function;
|
||||||
|
|
||||||
public interface LispInterpreterBuilder {
|
public interface LispInterpreterBuilder {
|
||||||
|
|
||||||
void setInputName(String inputName);
|
void setInput(InputStream inputStream, String inputName);
|
||||||
|
|
||||||
void setInput(InputStream inputStream);
|
|
||||||
|
|
||||||
void setOutput(PrintStream outputStream);
|
void setOutput(PrintStream outputStream);
|
||||||
|
|
||||||
|
@ -31,6 +29,8 @@ public interface LispInterpreterBuilder {
|
||||||
|
|
||||||
void setCriticalOutputDecorator(Function<String, String> criticalOutputDecorator);
|
void setCriticalOutputDecorator(Function<String, String> criticalOutputDecorator);
|
||||||
|
|
||||||
|
default void reset() {}
|
||||||
|
|
||||||
LispInterpreter build();
|
LispInterpreter build();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,14 +27,14 @@ public class LispInterpreterBuilderImpl implements LispInterpreterBuilder {
|
||||||
private Function<String, String> criticalOutputDecorator;
|
private Function<String, String> criticalOutputDecorator;
|
||||||
private RuntimeEnvironment environment;
|
private RuntimeEnvironment environment;
|
||||||
private boolean isInteractive;
|
private boolean isInteractive;
|
||||||
private boolean isUseFile;
|
private boolean isFileBased;
|
||||||
private boolean isBuilt;
|
protected boolean isBuilt;
|
||||||
|
|
||||||
private LispInterpreterBuilderImpl() {
|
protected LispInterpreterBuilderImpl() {
|
||||||
this.environment = RuntimeEnvironment.getInstance();
|
this.environment = RuntimeEnvironment.getInstance();
|
||||||
this.inputName = "";
|
this.inputName = "";
|
||||||
this.isInteractive = true;
|
this.isInteractive = true;
|
||||||
this.isUseFile = false;
|
this.isFileBased = false;
|
||||||
this.isBuilt = false;
|
this.isBuilt = false;
|
||||||
this.outputDecorator = s -> s;
|
this.outputDecorator = s -> s;
|
||||||
this.valueOutputDecorator = s -> s;
|
this.valueOutputDecorator = s -> s;
|
||||||
|
@ -44,13 +44,9 @@ public class LispInterpreterBuilderImpl implements LispInterpreterBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setInputName(String inputName) {
|
public void setInput(InputStream inputStream, String inputName) {
|
||||||
this.inputName = inputName;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setInput(InputStream inputStream) {
|
|
||||||
this.inputStream = inputStream;
|
this.inputStream = inputStream;
|
||||||
|
this.inputName = inputName;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -81,8 +77,8 @@ public class LispInterpreterBuilderImpl implements LispInterpreterBuilder {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void useFile(String fileName) {
|
public void useFile(String fileName) {
|
||||||
this.isUseFile = true;
|
this.isFileBased = true;
|
||||||
this.setInputName(fileName);
|
this.inputName = fileName;
|
||||||
this.setNotInteractive();
|
this.setNotInteractive();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,14 +152,14 @@ public class LispInterpreterBuilderImpl implements LispInterpreterBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
private InputStream getInputStream() throws FileNotFoundException {
|
private InputStream getInputStream() throws FileNotFoundException {
|
||||||
return isUseFile ? new FileInputStream(inputName) : inputStream;
|
return isFileBased ? new FileInputStream(inputName) : inputStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
private LispInterpreter createInterpreter() {
|
private LispInterpreter createInterpreter() {
|
||||||
return isInteractive ? new InteractiveLispInterpreter() : new LispInterpreter();
|
return isInteractive ? new InteractiveLispInterpreter() : new LispInterpreter();
|
||||||
}
|
}
|
||||||
|
|
||||||
public class InterpreterAlreadyBuiltException extends CriticalLispException {
|
public static class InterpreterAlreadyBuiltException extends CriticalLispException {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
|
|
@ -37,10 +37,8 @@ public class LispMain {
|
||||||
private static void configureInput(String[] args, LispInterpreterBuilder builder) {
|
private static void configureInput(String[] args, LispInterpreterBuilder builder) {
|
||||||
if (args.length > 0)
|
if (args.length > 0)
|
||||||
builder.useFile(args[0]);
|
builder.useFile(args[0]);
|
||||||
else {
|
else
|
||||||
builder.setInputName("stdin");
|
builder.setInput(System.in, "stdin");
|
||||||
builder.setInput(System.in);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Function<String, String> makeColorDecorator(String color) {
|
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