package main; import static java.text.MessageFormat.format; import static main.LispMain.*; import static org.junit.Assert.assertEquals; import java.io.*; import java.util.concurrent.CountDownLatch; import org.junit.*; import org.junit.contrib.java.lang.system.*; import com.googlecode.lanterna.input.KeyType; import com.googlecode.lanterna.terminal.virtual.DefaultVirtualTerminal; import environment.RuntimeEnvironment; import interpreter.LispInterpreterBuilderImpl; import terminal.*; public class MainTest { LispMain main; PipedInputStream outputReader; RuntimeEnvironment environment; public MainTest() { this.environment = RuntimeEnvironment.getInstance(); } @Rule public ExpectedSystemExit exit = ExpectedSystemExit.none(); @Rule public SystemErrRule systemErrRule = new SystemErrRule().enableLog().mute(); @Rule public SystemOutRule systemOutRule = new SystemOutRule().enableLog().mute(); @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(); } @After public void tearDown() { environment.reset(); } @Test public void runWithBadFile() { String expectedMessage = "[critical] test/main/test-files/bad.lisp (No such file or directory)"; exit.expectSystemExitWithStatus(1); exit.checkAssertionAfterwards(() -> { assertEquals(format("{0}{1}{2}\n", ANSI_PURPLE, expectedMessage, ANSI_RESET), systemErrRule.getLogWithNormalizedLineSeparator()); assertEquals("", systemOutRule.getLogWithNormalizedLineSeparator()); }); main.runWithFile("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()); } @Test public void runInteractive() { CountDownLatch latch = new CountDownLatch(1); VirtualTerminalInteractor t = new VirtualTerminalInteractor(); new Thread(() -> { main = new LispMain(new LispInterpreterBuilderImpl() {}, t.getConfiguration()); main.runInteractive(); latch.countDown(); }).start(); 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) {} } // @Test // public void defaultConstructorCoverage() { // new LispMain(); // } }