Increase test coverage

This commit is contained in:
Mike Cifelli 2017-03-24 09:29:19 -04:00
parent 111dd06d6f
commit eb82a25f64
4 changed files with 128 additions and 65 deletions

View File

@ -39,12 +39,36 @@ public class LispMain {
public LispMain() { public LispMain() {
this.builder = LispInterpreterBuilderImpl.getInstance(); this.builder = LispInterpreterBuilderImpl.getInstance();
this.configuration = new TerminalConfiguration(); }
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.setInputPair(new PipedOutputStream(), new PipedInputStream());
configuration.setOutputPair(new PipedOutputStream(), new PipedInputStream()); configuration.setOutputPair(new PipedOutputStream(), new PipedInputStream());
configuration.setTerminal(createIOSafeTerminal()); configuration.setTerminal(createIOSafeTerminal());
} }
}
private IOSafeTerminal createIOSafeTerminal() { private IOSafeTerminal createIOSafeTerminal() {
return createRuntimeExceptionConvertingAdapter(createDefaultTerminal()); return createRuntimeExceptionConvertingAdapter(createDefaultTerminal());
@ -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() { private void printGreeting() {
output.println(GREETING); output.println(GREETING);
output.println(); output.println();

View File

@ -17,10 +17,10 @@ public class TerminalConfiguration {
public void setInputPair(PipedOutputStream inputWriter, PipedInputStream inputReader) { public void setInputPair(PipedOutputStream inputWriter, PipedInputStream inputReader) {
this.inputWriter = inputWriter; this.inputWriter = inputWriter;
this.inputReader = inputReader; this.inputReader = inputReader;
connectInputPairj(); connectInputPair();
} }
private void connectInputPairj() { private void connectInputPair() {
try { try {
inputWriter.connect(inputReader); inputWriter.connect(inputReader);
} catch (IOException e) { } catch (IOException e) {

View File

@ -19,14 +19,50 @@ import terminal.*;
public class MainTest { public class MainTest {
LispMain main; private RuntimeEnvironment environment;
PipedInputStream outputReader; private CountDownLatch latch;
RuntimeEnvironment environment;
public MainTest() { public MainTest() {
this.environment = RuntimeEnvironment.getInstance(); 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 @Rule
public ExpectedSystemExit exit = ExpectedSystemExit.none(); public ExpectedSystemExit exit = ExpectedSystemExit.none();
@ -38,11 +74,6 @@ public class MainTest {
@Before @Before
public void setUp() { 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(); environment.reset();
} }
@ -57,52 +88,41 @@ public class MainTest {
exit.expectSystemExitWithStatus(1); exit.expectSystemExitWithStatus(1);
exit.checkAssertionAfterwards(() -> { exit.checkAssertionAfterwards(() -> {
assertEquals(format("{0}{1}{2}\n", ANSI_PURPLE, expectedMessage, ANSI_RESET), assertEquals(format("{0}{1}{2}\n", ANSI_PURPLE, expectedMessage, ANSI_RESET), getSystemErrLog());
systemErrRule.getLogWithNormalizedLineSeparator()); assertEquals("", getSystemOutLog());
assertEquals("", systemOutRule.getLogWithNormalizedLineSeparator());
}); });
main.runWithFile("test/main/test-files/bad.lisp"); runInterpreterWithFile("test/main/test-files/bad.lisp");
} }
@Test @Test
public void runWithFile_PrintsDecoratedLastValueOnly() { public void runWithFile_PrintsDecoratedLastValueOnly() {
main.runWithFile("test/main/test-files/file.lisp"); runInterpreterWithFile("test/main/test-files/file.lisp");
assertEquals("", systemErrRule.getLogWithNormalizedLineSeparator());
assertEquals(format("{0}{1}{2}\n\n", ANSI_GREEN, "RADISH", ANSI_RESET), assertEquals("", getSystemErrLog());
systemOutRule.getLogWithNormalizedLineSeparator()); assertEquals(format("{0}{1}{2}\n\n", ANSI_GREEN, "RADISH", ANSI_RESET), getSystemOutLog());
} }
@Test @Test
public void runInteractive() { public void runInteractive() {
CountDownLatch latch = new CountDownLatch(1); VirtualTerminalInteractor terminal = runInterpreterAndGetInteractor();
VirtualTerminalInteractor t = new VirtualTerminalInteractor();
new Thread(() -> { terminal.waitForPrompt();
main = new LispMain(new LispInterpreterBuilderImpl() {}, t.getConfiguration()); terminal.enterCharacters("'hi");
main.runInteractive(); terminal.pressKey(KeyType.Enter);
latch.countDown(); terminal.waitForPrompt();
}).start(); terminal.assertCursorPosition(2, 5);
terminal.assertScreenText(GREETING, " ", "~ 'hi ", " ", "HI ", "~ ");
terminal.enterControlCharacter('d');
terminal.assertInputStreamClosed();
t.waitForPrompt(); waitForInterpreterToShutdown();
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) {}
} }
// @Test @Test
// public void defaultConstructorCoverage() { public void defaultConstructorCoverage() {
// new LispMain(); new LispMain();
// } }
} }

View File

@ -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());
}
}