From eb82a25f6478a8c186a57ac9366abc23eb7ed4d6 Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Fri, 24 Mar 2017 09:29:19 -0400 Subject: [PATCH] Increase test coverage --- src/main/LispMain.java | 51 +++++----- src/terminal/TerminalConfiguration.java | 4 +- test/main/MainTest.java | 100 +++++++++++-------- test/terminal/TerminalConfigurationTest.java | 38 +++++++ 4 files changed, 128 insertions(+), 65 deletions(-) create mode 100644 test/terminal/TerminalConfigurationTest.java diff --git a/src/main/LispMain.java b/src/main/LispMain.java index b79a7c4..4259b13 100644 --- a/src/main/LispMain.java +++ b/src/main/LispMain.java @@ -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(); diff --git a/src/terminal/TerminalConfiguration.java b/src/terminal/TerminalConfiguration.java index a6c2907..b282f3b 100644 --- a/src/terminal/TerminalConfiguration.java +++ b/src/terminal/TerminalConfiguration.java @@ -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) { diff --git a/test/main/MainTest.java b/test/main/MainTest.java index 59addac..462dd75 100644 --- a/test/main/MainTest.java +++ b/test/main/MainTest.java @@ -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(); + } } diff --git a/test/terminal/TerminalConfigurationTest.java b/test/terminal/TerminalConfigurationTest.java new file mode 100644 index 0000000..4ebc89c --- /dev/null +++ b/test/terminal/TerminalConfigurationTest.java @@ -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()); + } + +}