Increase test coverage
This commit is contained in:
parent
111dd06d6f
commit
eb82a25f64
|
@ -39,11 +39,35 @@ public class LispMain {
|
|||
|
||||
public LispMain() {
|
||||
this.builder = LispInterpreterBuilderImpl.getInstance();
|
||||
this.configuration = new TerminalConfiguration();
|
||||
}
|
||||
|
||||
configuration.setInputPair(new PipedOutputStream(), new PipedInputStream());
|
||||
configuration.setOutputPair(new PipedOutputStream(), new PipedInputStream());
|
||||
configuration.setTerminal(createIOSafeTerminal());
|
||||
public LispMain(LispInterpreterBuilder builder, TerminalConfiguration configuration) {
|
||||
this.builder = builder;
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
public void runInteractive() {
|
||||
initializeTerminal();
|
||||
printGreeting();
|
||||
lispTerminal.start();
|
||||
buildInteractiveInterpreter().interpret();
|
||||
shutdownTerminal();
|
||||
}
|
||||
|
||||
private void initializeTerminal() {
|
||||
createTerminalConfiguration();
|
||||
inputReader = configuration.getInputReader();
|
||||
output = new PrintStream(configuration.getOutputWriter());
|
||||
lispTerminal = new LispTerminal(configuration);
|
||||
}
|
||||
|
||||
private void createTerminalConfiguration() {
|
||||
if (configuration == null) {
|
||||
configuration = new TerminalConfiguration();
|
||||
configuration.setInputPair(new PipedOutputStream(), new PipedInputStream());
|
||||
configuration.setOutputPair(new PipedOutputStream(), new PipedInputStream());
|
||||
configuration.setTerminal(createIOSafeTerminal());
|
||||
}
|
||||
}
|
||||
|
||||
private IOSafeTerminal createIOSafeTerminal() {
|
||||
|
@ -58,25 +82,6 @@ public class LispMain {
|
|||
}
|
||||
}
|
||||
|
||||
public LispMain(LispInterpreterBuilder builder, TerminalConfiguration configuration) {
|
||||
this.builder = builder;
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
private void initializeTerminal() {
|
||||
inputReader = configuration.getInputReader();
|
||||
output = new PrintStream(configuration.getOutputWriter());
|
||||
lispTerminal = new LispTerminal(configuration);
|
||||
}
|
||||
|
||||
public void runInteractive() {
|
||||
initializeTerminal();
|
||||
printGreeting();
|
||||
lispTerminal.start();
|
||||
buildInteractiveInterpreter().interpret();
|
||||
shutdownTerminal();
|
||||
}
|
||||
|
||||
private void printGreeting() {
|
||||
output.println(GREETING);
|
||||
output.println();
|
||||
|
|
|
@ -17,10 +17,10 @@ public class TerminalConfiguration {
|
|||
public void setInputPair(PipedOutputStream inputWriter, PipedInputStream inputReader) {
|
||||
this.inputWriter = inputWriter;
|
||||
this.inputReader = inputReader;
|
||||
connectInputPairj();
|
||||
connectInputPair();
|
||||
}
|
||||
|
||||
private void connectInputPairj() {
|
||||
private void connectInputPair() {
|
||||
try {
|
||||
inputWriter.connect(inputReader);
|
||||
} catch (IOException e) {
|
||||
|
|
|
@ -19,14 +19,50 @@ import terminal.*;
|
|||
|
||||
public class MainTest {
|
||||
|
||||
LispMain main;
|
||||
PipedInputStream outputReader;
|
||||
RuntimeEnvironment environment;
|
||||
private RuntimeEnvironment environment;
|
||||
private CountDownLatch latch;
|
||||
|
||||
public MainTest() {
|
||||
this.environment = RuntimeEnvironment.getInstance();
|
||||
}
|
||||
|
||||
private void runInterpreterWithFile(String fileName) {
|
||||
TerminalConfiguration configuration = new TerminalConfiguration();
|
||||
configuration.setInputPair(new PipedOutputStream(), new PipedInputStream());
|
||||
configuration.setOutputPair(new PipedOutputStream(), new PipedInputStream());
|
||||
configuration.setTerminal(new DefaultVirtualTerminal());
|
||||
LispMain main = new LispMain(new LispInterpreterBuilderImpl() {}, configuration);
|
||||
|
||||
main.runWithFile(fileName);
|
||||
}
|
||||
|
||||
private VirtualTerminalInteractor runInterpreterAndGetInteractor() {
|
||||
VirtualTerminalInteractor terminal = new VirtualTerminalInteractor();
|
||||
latch = new CountDownLatch(1);
|
||||
|
||||
new Thread(() -> {
|
||||
LispMain main = new LispMain(new LispInterpreterBuilderImpl() {}, terminal.getConfiguration());
|
||||
main.runInteractive();
|
||||
latch.countDown();
|
||||
}).start();
|
||||
|
||||
return terminal;
|
||||
}
|
||||
|
||||
private void waitForInterpreterToShutdown() {
|
||||
try {
|
||||
latch.await();
|
||||
} catch (InterruptedException ignored) {}
|
||||
}
|
||||
|
||||
private String getSystemOutLog() {
|
||||
return systemOutRule.getLogWithNormalizedLineSeparator();
|
||||
}
|
||||
|
||||
private String getSystemErrLog() {
|
||||
return systemErrRule.getLogWithNormalizedLineSeparator();
|
||||
}
|
||||
|
||||
@Rule
|
||||
public ExpectedSystemExit exit = ExpectedSystemExit.none();
|
||||
|
||||
|
@ -38,11 +74,6 @@ public class MainTest {
|
|||
|
||||
@Before
|
||||
public void setUp() {
|
||||
TerminalConfiguration configuration = new TerminalConfiguration();
|
||||
configuration.setInputPair(new PipedOutputStream(), new PipedInputStream());
|
||||
configuration.setOutputPair(new PipedOutputStream(), new PipedInputStream());
|
||||
configuration.setTerminal(new DefaultVirtualTerminal());
|
||||
main = new LispMain(new LispInterpreterBuilderImpl() {}, configuration);
|
||||
environment.reset();
|
||||
}
|
||||
|
||||
|
@ -57,52 +88,41 @@ public class MainTest {
|
|||
|
||||
exit.expectSystemExitWithStatus(1);
|
||||
exit.checkAssertionAfterwards(() -> {
|
||||
assertEquals(format("{0}{1}{2}\n", ANSI_PURPLE, expectedMessage, ANSI_RESET),
|
||||
systemErrRule.getLogWithNormalizedLineSeparator());
|
||||
assertEquals("", systemOutRule.getLogWithNormalizedLineSeparator());
|
||||
assertEquals(format("{0}{1}{2}\n", ANSI_PURPLE, expectedMessage, ANSI_RESET), getSystemErrLog());
|
||||
assertEquals("", getSystemOutLog());
|
||||
|
||||
});
|
||||
|
||||
main.runWithFile("test/main/test-files/bad.lisp");
|
||||
|
||||
runInterpreterWithFile("test/main/test-files/bad.lisp");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runWithFile_PrintsDecoratedLastValueOnly() {
|
||||
main.runWithFile("test/main/test-files/file.lisp");
|
||||
assertEquals("", systemErrRule.getLogWithNormalizedLineSeparator());
|
||||
assertEquals(format("{0}{1}{2}\n\n", ANSI_GREEN, "RADISH", ANSI_RESET),
|
||||
systemOutRule.getLogWithNormalizedLineSeparator());
|
||||
runInterpreterWithFile("test/main/test-files/file.lisp");
|
||||
|
||||
assertEquals("", getSystemErrLog());
|
||||
assertEquals(format("{0}{1}{2}\n\n", ANSI_GREEN, "RADISH", ANSI_RESET), getSystemOutLog());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runInteractive() {
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
VirtualTerminalInteractor t = new VirtualTerminalInteractor();
|
||||
VirtualTerminalInteractor terminal = runInterpreterAndGetInteractor();
|
||||
|
||||
new Thread(() -> {
|
||||
main = new LispMain(new LispInterpreterBuilderImpl() {}, t.getConfiguration());
|
||||
main.runInteractive();
|
||||
latch.countDown();
|
||||
}).start();
|
||||
terminal.waitForPrompt();
|
||||
terminal.enterCharacters("'hi");
|
||||
terminal.pressKey(KeyType.Enter);
|
||||
terminal.waitForPrompt();
|
||||
terminal.assertCursorPosition(2, 5);
|
||||
terminal.assertScreenText(GREETING, " ", "~ 'hi ", " ", "HI ", "~ ");
|
||||
terminal.enterControlCharacter('d');
|
||||
terminal.assertInputStreamClosed();
|
||||
|
||||
t.waitForPrompt();
|
||||
t.enterCharacters("'hi");
|
||||
t.pressKey(KeyType.Enter);
|
||||
t.waitForPrompt();
|
||||
t.assertCursorPosition(2, 5);
|
||||
t.assertScreenText(GREETING, " ", "~ 'hi ", " ", "HI ", "~ ");
|
||||
t.enterControlCharacter('d');
|
||||
t.assertInputStreamClosed();
|
||||
|
||||
try {
|
||||
latch.await();
|
||||
} catch (InterruptedException ignored) {}
|
||||
waitForInterpreterToShutdown();
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public void defaultConstructorCoverage() {
|
||||
// new LispMain();
|
||||
// }
|
||||
@Test
|
||||
public void defaultConstructorCoverage() {
|
||||
new LispMain();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
package terminal;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import org.junit.*;
|
||||
|
||||
import stream.UncheckedIOException;
|
||||
|
||||
public class TerminalConfigurationTest {
|
||||
|
||||
TerminalConfiguration configuration;
|
||||
|
||||
private PipedOutputStream createIOExceptionThrowingPipedOutputStream() {
|
||||
return new PipedOutputStream() {
|
||||
|
||||
@Override
|
||||
public void connect(PipedInputStream inputStream) throws IOException {
|
||||
throw new IOException();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
configuration = new TerminalConfiguration();
|
||||
}
|
||||
|
||||
@Test(expected = UncheckedIOException.class)
|
||||
public void setInputPairThrowsUncheckedException() {
|
||||
configuration.setInputPair(createIOExceptionThrowingPipedOutputStream(), new PipedInputStream());
|
||||
}
|
||||
|
||||
@Test(expected = UncheckedIOException.class)
|
||||
public void setOutputPairThrowsUncheckedException() {
|
||||
configuration.setOutputPair(createIOExceptionThrowingPipedOutputStream(), new PipedInputStream());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue