transcendental-lisp/test/interpreter/LispInterpreterBuilderTest....

92 lines
2.5 KiB
Java
Raw Normal View History

package interpreter;
import static error.ErrorManager.Severity.CRITICAL;
import static org.junit.Assert.*;
import java.io.*;
import org.junit.*;
import interpreter.LispInterpreterBuilderImpl.InterpreterAlreadyBuiltException;
2017-03-15 13:37:39 -04:00
public class LispInterpreterBuilderTest {
private LispInterpreterBuilder builder = new LispInterpreterBuilderImpl() {
@Override
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();
}
2017-02-26 12:39:08 -05:00
@Test
public void interpreterAlreadyBuiltException_HasCorrectAttributes() {
2017-02-26 12:39:08 -05:00
InterpreterAlreadyBuiltException e = new InterpreterAlreadyBuiltException();
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();
}
}