transcendental-lisp/test/terminal/LispTerminalTest.java

506 lines
15 KiB
Java
Raw Normal View History

package terminal;
2017-11-12 09:42:25 -05:00
import static com.googlecode.lanterna.input.KeyType.ArrowDown;
import static com.googlecode.lanterna.input.KeyType.ArrowLeft;
import static com.googlecode.lanterna.input.KeyType.ArrowRight;
import static com.googlecode.lanterna.input.KeyType.ArrowUp;
import static com.googlecode.lanterna.input.KeyType.Backspace;
import static com.googlecode.lanterna.input.KeyType.Delete;
import static com.googlecode.lanterna.input.KeyType.Enter;
import static com.googlecode.lanterna.input.KeyType.Escape;
import static terminal.LispTerminal.END_OF_SEGMENT;
2017-11-12 09:42:25 -05:00
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class LispTerminalTest {
2017-03-23 16:14:26 -04:00
private VirtualTerminalInteractor terminal;
2017-03-20 11:18:36 -04:00
@Before
2017-03-23 16:14:26 -04:00
public void setUp() {
terminal = new VirtualTerminalInteractor();
terminal.start();
}
@After
2017-03-23 16:14:26 -04:00
public void tearDown() {
terminal.stop();
}
@Test
public void leftArrowDoesNotMovePastOrigin() {
2017-03-23 16:14:26 -04:00
terminal.pressKey(ArrowLeft);
terminal.assertCursorPosition(0, 0);
}
@Test
public void leftArrowWorksAfterEnteringCharacters() {
2017-03-23 16:14:26 -04:00
terminal.enterCharacters("abc");
terminal.assertCursorPosition(3, 0);
terminal.pressKey(ArrowLeft);
terminal.assertCursorPosition(2, 0);
terminal.pressKey(ArrowLeft);
terminal.assertCursorPosition(1, 0);
terminal.pressKey(ArrowLeft);
terminal.assertCursorPosition(0, 0);
terminal.pressKey(ArrowLeft);
terminal.assertCursorPosition(0, 0);
}
@Test
public void leftArrowWorksAcrossRows() {
2017-03-23 16:14:26 -04:00
terminal.setColumns(5);
terminal.enterCharacters("123451");
terminal.assertCursorPosition(1, 1);
terminal.pressKeyTimes(ArrowLeft, 2);
terminal.assertCursorPosition(4, 0);
}
@Test
public void rightArrowDoesNotMovePastEndOfInput() {
2017-03-23 16:14:26 -04:00
terminal.pressKey(ArrowRight);
terminal.assertCursorPosition(0, 0);
}
@Test
public void rightArrowWorksAfterMovingLeft() {
2017-03-23 16:14:26 -04:00
terminal.enterCharacters("12");
terminal.assertCursorPosition(2, 0);
terminal.pressKey(ArrowLeft);
terminal.assertCursorPosition(1, 0);
terminal.pressKey(ArrowRight);
terminal.assertCursorPosition(2, 0);
terminal.pressKey(ArrowRight);
terminal.assertCursorPosition(2, 0);
}
@Test
public void rightArrowWorksAcrossRow() {
2017-03-23 16:14:26 -04:00
terminal.setColumns(5);
terminal.enterCharacters("123451");
terminal.assertCursorPosition(1, 1);
terminal.pressKeyTimes(ArrowLeft, 3);
terminal.assertCursorPosition(3, 0);
terminal.pressKeyTimes(ArrowRight, 3);
terminal.assertCursorPosition(1, 1);
}
@Test
public void characterKeyIsEchoed() {
2017-03-23 16:14:26 -04:00
terminal.enterCharacter('a');
terminal.assertCursorPosition(1, 0);
2017-04-12 09:16:09 -04:00
terminal.assertScreenText("a");
}
@Test
public void characterIsInserted() {
2017-03-23 16:14:26 -04:00
terminal.enterCharacters("abcd");
terminal.pressKeyTimes(ArrowLeft, 2);
terminal.enterCharacter('x');
2017-04-12 09:16:09 -04:00
terminal.assertScreenText("abxcd");
}
@Test
public void characterIsInserted_PushesInputToNextRow() {
2017-03-23 16:14:26 -04:00
terminal.setColumns(4);
terminal.enterCharacters("abcd");
terminal.pressKeyTimes(ArrowLeft, 2);
terminal.enterCharacter('x');
2017-04-12 09:16:09 -04:00
terminal.assertScreenText("abxc", "d ");
}
@Test
public void backspaceDoesNothingAtOrigin() {
2017-03-23 16:14:26 -04:00
terminal.pressKey(Backspace);
terminal.assertCursorPosition(0, 0);
}
@Test
public void backspaceWorksAfterInput() {
2017-03-23 16:14:26 -04:00
terminal.enterCharacters("12345");
terminal.pressKeyTimes(Backspace, 2);
terminal.assertCursorPosition(3, 0);
2017-04-12 09:16:09 -04:00
terminal.assertScreenText("123 ");
}
@Test
public void backspaceWorksAcrossRow() {
2017-03-23 16:14:26 -04:00
terminal.setColumns(4);
terminal.enterCharacters("1234567");
terminal.pressKeyTimes(Backspace, 5);
terminal.assertCursorPosition(2, 0);
2017-04-12 09:16:09 -04:00
terminal.assertScreenText("12 ", " ");
}
@Test
public void backspaceWorksInMiddleOfInput() {
2017-03-23 16:14:26 -04:00
terminal.enterCharacters("12345");
terminal.pressKeyTimes(ArrowLeft, 2);
terminal.pressKey(Backspace);
terminal.assertCursorPosition(2, 0);
2017-04-12 09:16:09 -04:00
terminal.assertScreenText("1245");
}
@Test
public void deleteDoesNothingAtOrigin() {
2017-03-23 16:14:26 -04:00
terminal.pressKey(Delete);
terminal.assertCursorPosition(0, 0);
}
@Test
public void deleteDoesNothingAtEndOfInput() {
2017-03-23 16:14:26 -04:00
terminal.enterCharacters("del");
terminal.pressKey(Delete);
terminal.assertCursorPosition(3, 0);
2017-04-12 09:16:09 -04:00
terminal.assertScreenText("del");
}
@Test
public void deleteWorksAtStartOfInput() {
2017-03-23 16:14:26 -04:00
terminal.enterCharacters("del");
terminal.pressKeyTimes(ArrowLeft, 3);
terminal.pressKeyTimes(Delete, 3);
terminal.assertCursorPosition(0, 0);
2017-04-12 09:16:09 -04:00
terminal.assertScreenText(" ");
}
@Test
public void deleteWorksAcrossRow() {
2017-03-23 16:14:26 -04:00
terminal.setColumns(4);
terminal.enterCharacters("delete");
terminal.pressKeyTimes(ArrowLeft, 5);
terminal.pressKey(Delete);
terminal.assertCursorPosition(1, 0);
2017-04-12 09:16:09 -04:00
terminal.assertScreenText("dlet", "e ");
}
@Test
public void enterMovesToNextLine() {
2017-03-23 16:14:26 -04:00
terminal.pressKey(Enter);
terminal.assertCursorPosition(0, 1);
}
@Test
public void enterWritesLineToPipedStream() {
2017-03-23 16:14:26 -04:00
terminal.enterCharacters("enter");
terminal.pressKey(Enter);
terminal.assertInputWritten("enter\n");
}
@Test
public void enterPressedInMiddleOfInput_WritesEntireLineToPipedStream() {
2017-03-23 16:14:26 -04:00
terminal.enterCharacters("enter");
terminal.pressKeyTimes(ArrowLeft, 2);
terminal.pressKey(Enter);
terminal.assertInputWritten("enter\n");
}
@Test
public void enterAfterInsertedText_WritesLineToPipedStream() {
2017-03-23 16:14:26 -04:00
terminal.enterCharacters("enter");
terminal.pressKeyTimes(ArrowLeft, 2);
terminal.enterCharacters("||");
terminal.pressKey(Enter);
terminal.assertInputWritten("ent||er\n");
}
@Test
public void enterAfterBackspace_WritesLineToPipedStream() {
2017-03-23 16:14:26 -04:00
terminal.enterCharacters("enter");
terminal.pressKeyTimes(Backspace, 2);
terminal.pressKey(Enter);
terminal.assertInputWritten("ent\n");
}
@Test
public void enterAfterDelete_WritesLineToPipedStream() {
2017-03-23 16:14:26 -04:00
terminal.enterCharacters("enter");
terminal.pressKeyTimes(ArrowLeft, 2);
terminal.pressKeyTimes(Delete, 2);
terminal.pressKey(Enter);
terminal.assertInputWritten("ent\n");
}
@Test
public void controlDWorks() {
2017-03-23 16:14:26 -04:00
terminal.enterCharacters("control-d");
terminal.enterControlCharacter('d');
terminal.assertInputStreamClosed();
terminal.assertInputWritten("control-d\n");
}
@Test
public void controlCWorks() {
2017-03-23 16:14:26 -04:00
terminal.enterCharacters("ctrl-c");
terminal.enterControlCharacter('c');
terminal.produceOutput("");
terminal.assertInputStreamClosed();
terminal.assertInputWritten("");
2017-04-12 09:16:09 -04:00
terminal.assertScreenText("ctrl-c ", " ");
}
@Test
public void controlDWorksInMiddleOfInput() {
2017-03-23 16:14:26 -04:00
terminal.enterCharacters("control-d");
terminal.pressKeyTimes(ArrowLeft, 2);
terminal.enterControlCharacter('d');
terminal.assertInputStreamClosed();
terminal.assertInputWritten("control-d\n");
}
@Test
public void escapeDoesNothing() {
2017-03-23 16:14:26 -04:00
terminal.pressKey(Escape);
terminal.assertCursorPosition(0, 0);
terminal.assertInputWritten("");
}
@Test
public void controlQDoesNothing() {
2017-03-23 16:14:26 -04:00
terminal.enterControlCharacter('q');
terminal.assertCursorPosition(0, 0);
terminal.assertInputWritten("");
}
@Test
public void controlEnterDoesNothing() {
2017-03-23 16:14:26 -04:00
terminal.pressControlKey(Enter);
terminal.assertCursorPosition(0, 0);
terminal.assertInputWritten("");
}
@Test
public void outputIsWritten() {
2017-03-23 16:14:26 -04:00
terminal.produceOutput("output");
terminal.assertCursorPosition(6, 0);
2017-04-12 09:16:09 -04:00
terminal.assertScreenText("output");
}
@Test
public void endOfSegmentCharacterIsNotPrinted() {
2017-03-23 16:14:26 -04:00
terminal.produceOutput("> " + END_OF_SEGMENT);
terminal.assertCursorPosition(2, 0);
2017-04-12 09:16:09 -04:00
terminal.assertScreenText("> ");
}
@Test
public void enterTextPastLastLineOfBuffer() {
2017-03-23 16:14:26 -04:00
terminal.setColumns(3);
terminal.setRows(2);
terminal.enterCharacters("01201201");
terminal.assertCursorPosition(2, 1);
2017-04-12 09:16:09 -04:00
terminal.assertScreenText("012", "01 ");
}
@Test
public void insertingTextPushesInputPastEndOfBuffer() {
2017-03-23 16:14:26 -04:00
terminal.setColumns(3);
terminal.setRows(4);
terminal.pressKey(Enter);
terminal.enterCharacters("00011122");
terminal.pressKeyTimes(ArrowLeft, 4);
terminal.assertCursorPosition(1, 2);
terminal.enterCharacters("zz");
terminal.assertCursorPosition(0, 2);
2017-04-12 09:16:09 -04:00
terminal.assertScreenText("000", "1zz", "112", "2 ");
}
@Test
public void insertingTextDoesNothingWhenBufferFilled() {
2017-03-23 16:14:26 -04:00
terminal.setColumns(3);
terminal.setRows(3);
terminal.enterCharacters("00011122");
terminal.pressKeyTimes(ArrowLeft, 4);
terminal.assertCursorPosition(1, 1);
terminal.enterCharacters("zz");
terminal.assertCursorPosition(1, 1);
2017-04-12 09:16:09 -04:00
terminal.assertScreenText("000", "111", "22 ");
}
2017-03-21 09:25:40 -04:00
@Test
public void appendingTextDoesNothingWhenBufferFilled() {
2017-03-23 16:14:26 -04:00
terminal.setColumns(3);
terminal.setRows(3);
terminal.enterCharacters("000111222333444");
terminal.assertCursorPosition(2, 2);
2017-04-12 09:16:09 -04:00
terminal.assertScreenText("000", "111", "22 ");
2017-03-21 09:25:40 -04:00
}
@Test
public void printedOutputToEndOfRow_MovesCursorToNextRow() {
2017-03-23 16:14:26 -04:00
terminal.setColumns(3);
terminal.produceOutput("out");
terminal.assertCursorPosition(0, 1);
}
@Test
public void printedOutputToEndOfBuffer_MovesCursorToNewRow() {
2017-03-23 16:14:26 -04:00
terminal.setColumns(3);
terminal.setRows(2);
terminal.produceOutput("output");
terminal.assertCursorPosition(0, 1);
2017-04-12 09:16:09 -04:00
terminal.assertScreenText("put", " ");
}
@Test
public void outputDoesNotOverwriteInput_AndRedisplaysInput() {
2017-03-23 16:14:26 -04:00
terminal.setColumns(3);
terminal.enterCharacters("0123");
terminal.pressKeyTimes(ArrowLeft, 3);
terminal.produceOutput("out");
terminal.assertCursorPosition(2, 3);
2017-04-12 09:16:09 -04:00
terminal.assertScreenText("012", "3ou", "t01", "23 ", " ");
}
@Test
public void outputEndsOnSecondToLastColumn_MovesToNewRow() {
2017-03-23 16:14:26 -04:00
terminal.setColumns(3);
terminal.enterCharacters("01234");
terminal.pressKeyTimes(ArrowLeft, 3);
terminal.produceOutput("out");
terminal.assertCursorPosition(2, 4);
2017-04-12 09:16:09 -04:00
terminal.assertScreenText("012", "34o", "ut ", "012", "34 ");
}
@Test
public void outputEndsOnLastColumn_MovesToNewRow() {
2017-03-23 16:14:26 -04:00
terminal.setColumns(3);
terminal.enterCharacters("012345");
terminal.pressKeyTimes(ArrowLeft, 3);
terminal.produceOutput("out");
terminal.assertCursorPosition(0, 5);
2017-04-12 09:16:09 -04:00
terminal.assertScreenText("012", "345", "out", "012", "345", " ");
}
@Test
public void outputRedisplaysInputAtEndOfBuffer() {
2017-03-23 16:14:26 -04:00
terminal.setColumns(3);
terminal.setRows(4);
terminal.enterCharacters("01234");
terminal.pressKeyTimes(ArrowLeft, 3);
terminal.produceOutput("out");
terminal.assertCursorPosition(2, 3);
2017-04-12 09:16:09 -04:00
terminal.assertScreenText("34o", "ut ", "012");
}
2017-03-20 11:18:36 -04:00
@Test
public void outputDoesNotOverwriteInput_AfterEnter() {
2017-03-23 16:14:26 -04:00
terminal.setColumns(3);
terminal.enterCharacters("01201201");
terminal.pressKeyTimes(ArrowLeft, 5);
terminal.pressKey(Enter);
terminal.produceOutput("out");
terminal.assertCursorPosition(0, 4);
2017-04-12 09:16:09 -04:00
terminal.assertScreenText("012", "012", "01 ", "out", " ");
}
2017-03-21 12:07:22 -04:00
@Test
public void resizeIsHandledGracefully() {
2017-03-23 16:14:26 -04:00
terminal.enterCharacters("resize");
terminal.pressKey(Enter);
terminal.enterCharacters("test");
terminal.setColumns(3);
terminal.assertCursorPosition(1, 1);
2017-04-12 09:16:09 -04:00
terminal.assertScreenText("tes", "t ");
}
@Test
public void backspaceWorksAfterResize() {
2017-03-23 16:14:26 -04:00
terminal.enterCharacters("resize");
terminal.pressKey(Enter);
terminal.enterCharacters("test");
terminal.setColumns(3);
terminal.pressKeyTimes(Backspace, 20);
terminal.assertCursorPosition(0, 0);
2017-04-12 09:16:09 -04:00
terminal.assertScreenText(" ", " ");
2017-03-21 12:07:22 -04:00
}
@Test
public void deleteWorksAfterResize() {
2017-03-23 16:14:26 -04:00
terminal.enterCharacters("resize");
terminal.pressKey(Enter);
terminal.enterCharacters("test");
terminal.setColumns(3);
terminal.pressKeyTimes(ArrowLeft, 20);
terminal.pressKeyTimes(Delete, 20);
terminal.pressKeyTimes(ArrowRight, 20);
terminal.assertCursorPosition(0, 0);
2017-04-12 09:16:09 -04:00
terminal.assertScreenText(" ", " ");
}
@Test
public void controlSequencesAreNotPrinted() {
2017-03-23 16:14:26 -04:00
terminal.produceOutput("\u001B[32mcontrol\u001B[0mseq");
2017-04-12 09:16:09 -04:00
terminal.assertScreenText("controlseq");
}
@Test
public void upArrowWorks() {
terminal.enterCharacters("(one)");
terminal.pressKey(Enter);
terminal.enterCharacters("(two)");
terminal.pressKey(Enter);
terminal.pressKey(ArrowUp);
terminal.assertScreenText("(one)", "(two)", "(two)");
terminal.assertCursorPosition(5, 2);
}
@Test
public void upArrowErasesCurrentLine() {
terminal.enterCharacters("(one)");
terminal.pressKey(Enter);
terminal.enterCharacters("(two)");
terminal.pressKey(Enter);
terminal.enterCharacters("(three)");
terminal.pressKey(ArrowUp);
terminal.assertScreenText("(one)", "(two)", "(two) ");
terminal.assertCursorPosition(5, 2);
}
@Test
public void upArrowStopsAtFirstLine() {
terminal.enterCharacters("(one)");
terminal.pressKey(Enter);
terminal.enterCharacters("(two)");
terminal.pressKey(Enter);
terminal.pressKeyTimes(ArrowUp, 5);
terminal.assertScreenText("(one)", "(two)", "(one)");
terminal.assertCursorPosition(5, 2);
}
@Test
public void originIsUpdatedWhenPreviousLineMovesPastEndOfBuffer() {
terminal.setRows(3);
terminal.setColumns(3);
terminal.enterCharacters("12345");
terminal.pressKey(Enter);
terminal.pressKey(ArrowUp);
terminal.assertScreenText("45 ", "123", "45 ");
terminal.pressKeyTimes(ArrowLeft, 10);
terminal.assertCursorPosition(0, 1);
}
@Test
public void downArrowWorks() {
terminal.enterCharacters("(one)");
terminal.pressKey(Enter);
terminal.enterCharacters("(two)");
terminal.pressKey(Enter);
terminal.enterCharacters("(three)");
terminal.pressKey(ArrowUp);
terminal.pressKey(ArrowDown);
terminal.assertScreenText("(one)", "(two)", "(three)");
terminal.assertCursorPosition(7, 2);
}
@Test
public void downArrowStopsAtLastLine() {
terminal.pressKeyTimes(ArrowDown, 5);
terminal.assertScreenText(" ");
terminal.assertCursorPosition(0, 0);
}
}