package main; import static java.text.MessageFormat.format; import static main.LispMain.*; import static org.junit.Assert.assertEquals; import java.io.*; import org.junit.*; import org.junit.contrib.java.lang.system.*; import com.googlecode.lanterna.terminal.virtual.*; import environment.RuntimeEnvironment; import interpreter.LispInterpreterBuilderImpl; import terminal.*; public class MainTest { LispMain main; PipedOutputStream inputWriter; PipedInputStream outputReader; VirtualTerminal terminal; 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() throws Exception { environment.reset(); TerminalConfiguration configuration = new TerminalConfiguration(); inputWriter = new PipedOutputStream(); outputReader = new PipedInputStream(); terminal = new DefaultVirtualTerminal(); configuration.setInputWriter(inputWriter); configuration.setOutputReader(outputReader); configuration.setTerminal(terminal); main = new LispMain(new LispInterpreterBuilderImpl() {}, configuration); } @After public void tearDown() throws Exception { terminal.close(); 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() { VirtualTerminalInteractor t = new VirtualTerminalInteractor(); try { t.start(); } finally { t.stop(); } } }