transcendental-lisp/test/main/MainTest.java

109 lines
3.2 KiB
Java
Raw Normal View History

package main;
2017-03-23 16:14:26 -04:00
import static java.text.MessageFormat.format;
import static main.LispMain.*;
import static org.junit.Assert.assertEquals;
import java.io.*;
2017-03-23 18:48:37 -04:00
import java.util.concurrent.CountDownLatch;
2017-03-23 16:14:26 -04:00
import org.junit.*;
2017-03-23 16:14:26 -04:00
import org.junit.contrib.java.lang.system.*;
2017-03-23 18:48:37 -04:00
import com.googlecode.lanterna.input.KeyType;
import com.googlecode.lanterna.terminal.virtual.DefaultVirtualTerminal;
2017-03-23 16:14:26 -04:00
import environment.RuntimeEnvironment;
import interpreter.LispInterpreterBuilderImpl;
import terminal.*;
2017-03-15 13:37:39 -04:00
public class MainTest {
2017-03-23 16:14:26 -04:00
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
2017-03-23 18:48:37 -04:00
public void setUp() {
2017-03-23 16:14:26 -04:00
TerminalConfiguration configuration = new TerminalConfiguration();
2017-03-23 18:48:37 -04:00
configuration.setInputPair(new PipedOutputStream(), new PipedInputStream());
configuration.setOutputPair(new PipedOutputStream(), new PipedInputStream());
configuration.setTerminal(new DefaultVirtualTerminal());
2017-03-23 16:14:26 -04:00
main = new LispMain(new LispInterpreterBuilderImpl() {}, configuration);
2017-03-23 18:48:37 -04:00
environment.reset();
2017-03-23 16:14:26 -04:00
}
@After
2017-03-23 18:48:37 -04:00
public void tearDown() {
2017-03-23 16:14:26 -04:00
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() {
2017-03-23 18:48:37 -04:00
CountDownLatch latch = new CountDownLatch(1);
2017-03-23 16:14:26 -04:00
VirtualTerminalInteractor t = new VirtualTerminalInteractor();
2017-03-23 18:48:37 -04:00
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();
2017-03-23 16:14:26 -04:00
try {
2017-03-23 18:48:37 -04:00
latch.await();
} catch (InterruptedException ignored) {}
2017-03-23 16:14:26 -04:00
}
2017-03-23 18:48:37 -04:00
// @Test
// public void defaultConstructorCoverage() {
// new LispMain();
// }
}