2017-02-26 12:31:27 -05:00
|
|
|
package main;
|
|
|
|
|
2018-01-20 08:34:31 -05:00
|
|
|
import static com.googlecode.lanterna.input.KeyType.Enter;
|
2017-03-23 16:14:26 -04:00
|
|
|
import static java.text.MessageFormat.format;
|
2017-11-12 09:42:25 -05:00
|
|
|
import static main.LispMain.ANSI_GREEN;
|
|
|
|
import static main.LispMain.ANSI_PURPLE;
|
|
|
|
import static main.LispMain.ANSI_RESET;
|
|
|
|
import static main.LispMain.GREETING;
|
2017-03-23 16:14:26 -04:00
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
2017-11-12 09:42:25 -05:00
|
|
|
import java.io.PipedInputStream;
|
|
|
|
import java.io.PipedOutputStream;
|
2017-03-23 18:48:37 -04:00
|
|
|
import java.util.concurrent.CountDownLatch;
|
2017-03-23 16:14:26 -04:00
|
|
|
|
2017-11-12 09:42:25 -05:00
|
|
|
import org.junit.Rule;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.junit.contrib.java.lang.system.ExpectedSystemExit;
|
|
|
|
import org.junit.contrib.java.lang.system.SystemErrRule;
|
|
|
|
import org.junit.contrib.java.lang.system.SystemOutRule;
|
2017-03-23 16:14:26 -04:00
|
|
|
|
2017-03-23 18:48:37 -04:00
|
|
|
import com.googlecode.lanterna.terminal.virtual.DefaultVirtualTerminal;
|
2017-03-23 16:14:26 -04:00
|
|
|
|
|
|
|
import environment.RuntimeEnvironment;
|
|
|
|
import interpreter.LispInterpreterBuilderImpl;
|
2017-11-12 09:42:25 -05:00
|
|
|
import terminal.TerminalConfiguration;
|
|
|
|
import terminal.VirtualTerminalInteractor;
|
2018-01-21 10:32:55 -05:00
|
|
|
import testutil.SymbolAndFunctionCleaner;
|
2017-02-26 12:31:27 -05:00
|
|
|
|
2018-01-21 10:32:55 -05:00
|
|
|
public class MainTest extends SymbolAndFunctionCleaner {
|
2017-02-26 12:31:27 -05:00
|
|
|
|
2017-03-24 09:29:19 -04:00
|
|
|
private RuntimeEnvironment environment;
|
|
|
|
private CountDownLatch latch;
|
2017-03-23 16:14:26 -04:00
|
|
|
|
|
|
|
public MainTest() {
|
|
|
|
this.environment = RuntimeEnvironment.getInstance();
|
|
|
|
}
|
|
|
|
|
2017-03-24 09:29:19 -04:00
|
|
|
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(() -> {
|
2017-03-24 11:53:33 -04:00
|
|
|
try {
|
|
|
|
LispMain main = new LispMain(new LispInterpreterBuilderImpl() {}, terminal.getConfiguration());
|
|
|
|
main.runInteractive();
|
|
|
|
} finally {
|
|
|
|
latch.countDown();
|
|
|
|
}
|
2017-03-24 09:29:19 -04:00
|
|
|
}).start();
|
|
|
|
|
|
|
|
return terminal;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void waitForInterpreterToShutdown() {
|
|
|
|
try {
|
|
|
|
latch.await();
|
|
|
|
} catch (InterruptedException ignored) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getSystemOutLog() {
|
|
|
|
return systemOutRule.getLogWithNormalizedLineSeparator();
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getSystemErrLog() {
|
|
|
|
return systemErrRule.getLogWithNormalizedLineSeparator();
|
|
|
|
}
|
|
|
|
|
2018-01-20 08:34:31 -05:00
|
|
|
private String getExpectedGreeting() {
|
|
|
|
return format(GREETING, getClass().getPackage().getImplementationVersion());
|
|
|
|
}
|
|
|
|
|
2017-03-23 16:14:26 -04:00
|
|
|
@Rule
|
|
|
|
public ExpectedSystemExit exit = ExpectedSystemExit.none();
|
|
|
|
|
|
|
|
@Rule
|
|
|
|
public SystemErrRule systemErrRule = new SystemErrRule().enableLog().mute();
|
|
|
|
|
|
|
|
@Rule
|
|
|
|
public SystemOutRule systemOutRule = new SystemOutRule().enableLog().mute();
|
|
|
|
|
2018-01-21 10:32:55 -05:00
|
|
|
@Override
|
|
|
|
public void additionalSetUp() {
|
2017-03-23 18:48:37 -04:00
|
|
|
environment.reset();
|
2017-03-23 16:14:26 -04:00
|
|
|
}
|
2017-02-26 12:31:27 -05:00
|
|
|
|
2018-01-21 10:32:55 -05:00
|
|
|
@Override
|
|
|
|
public void additionalTearDown() {
|
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(() -> {
|
2017-03-24 09:29:19 -04:00
|
|
|
assertEquals(format("{0}{1}{2}\n", ANSI_PURPLE, expectedMessage, ANSI_RESET), getSystemErrLog());
|
|
|
|
assertEquals("", getSystemOutLog());
|
2017-03-23 16:14:26 -04:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2017-03-24 09:29:19 -04:00
|
|
|
runInterpreterWithFile("test/main/test-files/bad.lisp");
|
2017-03-23 16:14:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void runWithFile_PrintsDecoratedLastValueOnly() {
|
2017-03-24 09:29:19 -04:00
|
|
|
runInterpreterWithFile("test/main/test-files/file.lisp");
|
|
|
|
|
|
|
|
assertEquals("", getSystemErrLog());
|
|
|
|
assertEquals(format("{0}{1}{2}\n\n", ANSI_GREEN, "RADISH", ANSI_RESET), getSystemOutLog());
|
2017-03-23 16:14:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void runInteractive() {
|
2017-03-24 09:29:19 -04:00
|
|
|
VirtualTerminalInteractor terminal = runInterpreterAndGetInteractor();
|
|
|
|
|
|
|
|
terminal.waitForPrompt();
|
|
|
|
terminal.enterCharacters("'hi");
|
2018-01-20 08:34:31 -05:00
|
|
|
terminal.pressKey(Enter);
|
2017-03-24 09:29:19 -04:00
|
|
|
terminal.waitForPrompt();
|
|
|
|
terminal.assertCursorPosition(2, 5);
|
2018-01-20 08:34:31 -05:00
|
|
|
terminal.assertScreenText(getExpectedGreeting(), " ", "~ 'hi ", " ", "HI ", "~ ");
|
2017-03-24 09:29:19 -04:00
|
|
|
terminal.enterControlCharacter('d');
|
|
|
|
terminal.assertInputStreamClosed();
|
|
|
|
|
|
|
|
waitForInterpreterToShutdown();
|
2017-03-23 16:14:26 -04:00
|
|
|
}
|
2017-02-26 12:31:27 -05:00
|
|
|
|
2017-03-24 09:29:19 -04:00
|
|
|
@Test
|
2017-03-24 11:53:33 -04:00
|
|
|
public void runMain() {
|
|
|
|
LispMain.main(new String[] { "test/main/test-files/file.lisp" });
|
|
|
|
|
|
|
|
assertEquals("", getSystemErrLog());
|
|
|
|
assertEquals(format("{0}{1}{2}\n\n", ANSI_GREEN, "RADISH", ANSI_RESET), getSystemOutLog());
|
2017-03-24 09:29:19 -04:00
|
|
|
}
|
2017-03-23 18:48:37 -04:00
|
|
|
|
2017-02-26 12:31:27 -05:00
|
|
|
}
|