2017-02-26 12:31:27 -05:00
|
|
|
package interpreter;
|
|
|
|
|
2017-03-03 12:07:23 -05:00
|
|
|
import static error.ErrorManager.Severity.CRITICAL;
|
2017-02-26 12:31:27 -05:00
|
|
|
import static org.junit.Assert.*;
|
|
|
|
|
|
|
|
import java.io.*;
|
2017-03-22 14:08:22 -04:00
|
|
|
import java.util.*;
|
2017-02-26 12:31:27 -05:00
|
|
|
|
|
|
|
import org.junit.*;
|
|
|
|
|
2017-03-22 14:08:22 -04:00
|
|
|
import environment.RuntimeEnvironment;
|
2017-02-26 12:31:27 -05:00
|
|
|
import interpreter.LispInterpreterBuilderImpl.InterpreterAlreadyBuiltException;
|
|
|
|
|
2017-03-15 13:37:39 -04:00
|
|
|
public class LispInterpreterBuilderTest {
|
2017-02-26 12:31:27 -05:00
|
|
|
|
2017-03-22 14:08:22 -04:00
|
|
|
private static final String TERMINATED = "terminated";
|
2017-02-26 12:31:27 -05:00
|
|
|
|
2017-03-22 14:08:22 -04:00
|
|
|
private Set<String> indicatorSet;
|
|
|
|
private ByteArrayOutputStream outputStream;
|
|
|
|
private ByteArrayOutputStream errorOutputStream;
|
|
|
|
private RuntimeEnvironment environment;
|
|
|
|
private LispInterpreterBuilder builder;
|
|
|
|
|
|
|
|
public LispInterpreterBuilderTest() {
|
|
|
|
this.environment = RuntimeEnvironment.getInstance();
|
|
|
|
|
|
|
|
this.builder = new LispInterpreterBuilderImpl() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void reset() {
|
|
|
|
this.isBuilt = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2017-02-26 12:31:27 -05:00
|
|
|
|
|
|
|
private void setCommonFeatures() {
|
2017-03-22 14:08:22 -04:00
|
|
|
builder.setOutput(new PrintStream(outputStream));
|
|
|
|
builder.setErrorOutput(new PrintStream(errorOutputStream));
|
2017-02-26 12:31:27 -05:00
|
|
|
builder.setTerminationFunction(() -> {});
|
2017-03-22 14:08:22 -04:00
|
|
|
builder.setErrorTerminationFunction(() -> indicatorSet.add(TERMINATED));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void assertTerminated() {
|
|
|
|
assertTrue(indicatorSet.contains(TERMINATED));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void assertErrorMessageWritten() {
|
|
|
|
assertTrue(errorOutputStream.toByteArray().length > 0);
|
2017-02-26 12:31:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Before
|
|
|
|
public void setUp() throws Exception {
|
2017-03-22 14:08:22 -04:00
|
|
|
indicatorSet = new HashSet<>();
|
|
|
|
outputStream = new ByteArrayOutputStream();
|
|
|
|
errorOutputStream = new ByteArrayOutputStream();
|
2017-02-26 12:31:27 -05:00
|
|
|
builder.reset();
|
2017-03-22 14:08:22 -04:00
|
|
|
environment.reset();
|
2017-02-26 12:31:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@After
|
|
|
|
public void tearDown() throws Exception {
|
|
|
|
builder.reset();
|
2017-03-22 14:08:22 -04:00
|
|
|
environment.reset();
|
2017-02-26 12:31:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
2017-03-22 14:08:22 -04:00
|
|
|
assertFalse(interpreter instanceof FileLispInterpreter);
|
2017-02-26 12:31:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void buildFileBasedInterpreter() {
|
|
|
|
setCommonFeatures();
|
|
|
|
builder.useFile("test/interpreter/test-files/file.lisp");
|
|
|
|
LispInterpreter interpreter = builder.build();
|
2017-03-22 14:08:22 -04:00
|
|
|
assertTrue(interpreter instanceof FileLispInterpreter);
|
2017-02-26 12:31:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = InterpreterAlreadyBuiltException.class)
|
|
|
|
public void cannotBuildMoreThanOneInterpreter() {
|
|
|
|
builder.build();
|
|
|
|
builder.build();
|
|
|
|
}
|
2017-02-26 12:39:08 -05:00
|
|
|
|
|
|
|
@Test
|
2017-03-03 12:07:23 -05:00
|
|
|
public void interpreterAlreadyBuiltException_HasCorrectAttributes() {
|
2017-02-26 12:39:08 -05:00
|
|
|
InterpreterAlreadyBuiltException e = new InterpreterAlreadyBuiltException();
|
2017-03-03 12:07:23 -05:00
|
|
|
assertEquals(CRITICAL, e.getSeverity());
|
2017-02-26 12:39:08 -05:00
|
|
|
assertNotNull(e.getMessage());
|
|
|
|
assertTrue(e.getMessage().length() > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = InterpreterAlreadyBuiltException.class)
|
|
|
|
public void resetNormallyDoesNothing() {
|
|
|
|
builder = new LispInterpreterBuilderImpl();
|
|
|
|
|
|
|
|
builder.build();
|
|
|
|
builder.reset();
|
|
|
|
builder.build();
|
2017-02-26 12:31:27 -05:00
|
|
|
}
|
|
|
|
|
2017-03-22 14:08:22 -04:00
|
|
|
@Test
|
|
|
|
public void attemptToBuildInterpreterOnBadFile() {
|
|
|
|
setCommonFeatures();
|
|
|
|
builder.useFile("test/interpreter/test-files/does-not-exist.lisp");
|
|
|
|
builder.build();
|
|
|
|
assertErrorMessageWritten();
|
|
|
|
assertTerminated();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void makeSureDecoratorsAreInitializedWithDefaults() {
|
|
|
|
builder.build();
|
|
|
|
assertEquals("", environment.decoratePrompt(""));
|
|
|
|
assertEquals("", environment.decorateValueOutput(""));
|
|
|
|
assertEquals("", environment.decorateWarningOutput(""));
|
|
|
|
assertEquals("", environment.decorateErrorOutput(""));
|
|
|
|
assertEquals("", environment.decorateCriticalOutput(""));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void makeSureDecoratorsAreSetCorrectly() {
|
|
|
|
builder.setPromptDecorator(s -> "#" + s + "#");
|
|
|
|
builder.setValueOutputDecorator(s -> "@" + s + "@");
|
|
|
|
builder.setWarningOutputDecorator(s -> "%" + s + "%");
|
|
|
|
builder.setErrorOutputDecorator(s -> "*" + s + "*");
|
|
|
|
builder.setCriticalOutputDecorator(s -> "$" + s + "$");
|
|
|
|
builder.build();
|
|
|
|
assertEquals("#x#", environment.decoratePrompt("x"));
|
|
|
|
assertEquals("@x@", environment.decorateValueOutput("x"));
|
|
|
|
assertEquals("%x%", environment.decorateWarningOutput("x"));
|
|
|
|
assertEquals("*x*", environment.decorateErrorOutput("x"));
|
|
|
|
assertEquals("$x$", environment.decorateCriticalOutput("x"));
|
|
|
|
}
|
|
|
|
|
2017-02-26 12:31:27 -05:00
|
|
|
}
|