226 lines
6.7 KiB
Java
226 lines
6.7 KiB
Java
package terminal;
|
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
import java.io.*;
|
|
|
|
import org.junit.*;
|
|
|
|
import com.googlecode.lanterna.*;
|
|
import com.googlecode.lanterna.input.*;
|
|
import com.googlecode.lanterna.terminal.virtual.*;
|
|
|
|
public class LispTerminalAttempt {
|
|
|
|
private PipedInputStream inputReader;
|
|
private PipedOutputStream inputWriter;
|
|
private PipedInputStream outputReader;
|
|
private PipedOutputStream outputWriter;
|
|
private VirtualTerminal virtualTerminal;
|
|
private LispTerminal lispTerminal;
|
|
|
|
private void pressKey(KeyType keyType) {
|
|
virtualTerminal.addInput(new KeyStroke(keyType));
|
|
sleep();
|
|
}
|
|
|
|
private void enterCharacter(char character) {
|
|
virtualTerminal.addInput(new KeyStroke(character, false, false));
|
|
sleep();
|
|
}
|
|
|
|
private void enterCharacters(String characters) {
|
|
for (char c : characters.toCharArray())
|
|
virtualTerminal.addInput(new KeyStroke(c, false, false));
|
|
|
|
sleep();
|
|
}
|
|
|
|
private void sleep() {
|
|
try {
|
|
Thread.sleep(15);
|
|
} catch (InterruptedException e) {}
|
|
}
|
|
|
|
private void assertCursorPosition(int column, int row) {
|
|
assertTrue(virtualTerminal.getCursorBufferPosition().getColumn() == column);
|
|
assertTrue(virtualTerminal.getCursorBufferPosition().getRow() == row);
|
|
}
|
|
|
|
private void assertCharacterAtPosition(char character, int column, int row) {
|
|
TerminalPosition position = new TerminalPosition(column, row);
|
|
assertTrue(virtualTerminal.getBufferCharacter(position).getCharacter() == character);
|
|
}
|
|
|
|
private void setColumns(int columns) {
|
|
virtualTerminal.setTerminalSize(new TerminalSize(columns, virtualTerminal.getTerminalSize().getRows()));
|
|
}
|
|
|
|
@Before
|
|
public void setUp() throws IOException {
|
|
inputReader = new PipedInputStream();
|
|
inputWriter = new PipedOutputStream(inputReader);
|
|
outputReader = new PipedInputStream();
|
|
outputWriter = new PipedOutputStream(outputReader);
|
|
virtualTerminal = new DefaultVirtualTerminal();
|
|
lispTerminal = new LispTerminal(virtualTerminal, inputWriter, outputReader);
|
|
lispTerminal.run();
|
|
}
|
|
|
|
@After
|
|
public void tearDown() throws IOException {
|
|
lispTerminal.finish();
|
|
outputWriter.close();
|
|
}
|
|
|
|
@Test
|
|
public void leftArrowDoesNotMovePastOrigin() {
|
|
pressKey(KeyType.ArrowLeft);
|
|
assertCursorPosition(0, 0);
|
|
}
|
|
|
|
@Test
|
|
public void leftArrowWorksAfterEnteringCharacters() {
|
|
enterCharacters("abc");
|
|
assertCursorPosition(3, 0);
|
|
pressKey(KeyType.ArrowLeft);
|
|
assertCursorPosition(2, 0);
|
|
pressKey(KeyType.ArrowLeft);
|
|
assertCursorPosition(1, 0);
|
|
pressKey(KeyType.ArrowLeft);
|
|
assertCursorPosition(0, 0);
|
|
pressKey(KeyType.ArrowLeft);
|
|
assertCursorPosition(0, 0);
|
|
}
|
|
|
|
@Test
|
|
public void leftArrowWorksAcrossRows() {
|
|
setColumns(5);
|
|
enterCharacters("123451");
|
|
assertCursorPosition(1, 1);
|
|
pressKey(KeyType.ArrowLeft);
|
|
pressKey(KeyType.ArrowLeft);
|
|
assertCursorPosition(4, 0);
|
|
}
|
|
|
|
@Test
|
|
public void rightArrowDoesNotMovePastEndOfInput() {
|
|
pressKey(KeyType.ArrowRight);
|
|
assertCursorPosition(0, 0);
|
|
}
|
|
|
|
@Test
|
|
public void rightArrowWorksAfterMovingLeft() {
|
|
enterCharacters("12");
|
|
assertCursorPosition(2, 0);
|
|
pressKey(KeyType.ArrowLeft);
|
|
assertCursorPosition(1, 0);
|
|
pressKey(KeyType.ArrowRight);
|
|
assertCursorPosition(2, 0);
|
|
pressKey(KeyType.ArrowRight);
|
|
assertCursorPosition(2, 0);
|
|
}
|
|
|
|
@Test
|
|
public void rightArrowWorksAcrossRow() {
|
|
setColumns(5);
|
|
enterCharacters("123451");
|
|
assertCursorPosition(1, 1);
|
|
pressKey(KeyType.ArrowLeft);
|
|
pressKey(KeyType.ArrowLeft);
|
|
pressKey(KeyType.ArrowLeft);
|
|
assertCursorPosition(3, 0);
|
|
pressKey(KeyType.ArrowRight);
|
|
pressKey(KeyType.ArrowRight);
|
|
pressKey(KeyType.ArrowRight);
|
|
assertCursorPosition(1, 1);
|
|
}
|
|
|
|
@Test
|
|
public void characterKeyIsEchoed() {
|
|
enterCharacter('a');
|
|
assertCursorPosition(1, 0);
|
|
assertCharacterAtPosition('a', 0, 0);
|
|
}
|
|
|
|
@Test
|
|
public void characterIsInserted() {
|
|
enterCharacters("abcd");
|
|
pressKey(KeyType.ArrowLeft);
|
|
pressKey(KeyType.ArrowLeft);
|
|
enterCharacter('x');
|
|
assertCharacterAtPosition('a', 0, 0);
|
|
assertCharacterAtPosition('b', 1, 0);
|
|
assertCharacterAtPosition('x', 2, 0);
|
|
assertCharacterAtPosition('c', 3, 0);
|
|
assertCharacterAtPosition('d', 4, 0);
|
|
}
|
|
|
|
@Test
|
|
public void characterIsInserted_PushesInputToNextRow() {
|
|
setColumns(4);
|
|
enterCharacters("abcd");
|
|
pressKey(KeyType.ArrowLeft);
|
|
pressKey(KeyType.ArrowLeft);
|
|
enterCharacter('x');
|
|
assertCharacterAtPosition('a', 0, 0);
|
|
assertCharacterAtPosition('b', 1, 0);
|
|
assertCharacterAtPosition('x', 2, 0);
|
|
assertCharacterAtPosition('c', 3, 0);
|
|
assertCharacterAtPosition('d', 0, 1);
|
|
}
|
|
|
|
@Test
|
|
public void backspaceDoesNothingAtOrigin() {
|
|
pressKey(KeyType.Backspace);
|
|
assertCursorPosition(0, 0);
|
|
}
|
|
|
|
@Test
|
|
public void backspaceWorksAfterInput() {
|
|
enterCharacters("12345");
|
|
pressKey(KeyType.Backspace);
|
|
pressKey(KeyType.Backspace);
|
|
assertCursorPosition(3, 0);
|
|
assertCharacterAtPosition('1', 0, 0);
|
|
assertCharacterAtPosition('2', 1, 0);
|
|
assertCharacterAtPosition('3', 2, 0);
|
|
assertCharacterAtPosition(' ', 3, 0);
|
|
assertCharacterAtPosition(' ', 4, 0);
|
|
assertCharacterAtPosition(' ', 5, 0);
|
|
}
|
|
|
|
@Test
|
|
public void backspaceWorksAcrossRow() {
|
|
setColumns(4);
|
|
enterCharacters("1234567");
|
|
pressKey(KeyType.Backspace);
|
|
pressKey(KeyType.Backspace);
|
|
pressKey(KeyType.Backspace);
|
|
pressKey(KeyType.Backspace);
|
|
pressKey(KeyType.Backspace);
|
|
assertCursorPosition(2, 0);
|
|
assertCharacterAtPosition('1', 0, 0);
|
|
assertCharacterAtPosition('2', 1, 0);
|
|
assertCharacterAtPosition(' ', 2, 0);
|
|
assertCharacterAtPosition(' ', 3, 0);
|
|
assertCharacterAtPosition(' ', 0, 1);
|
|
assertCharacterAtPosition(' ', 1, 1);
|
|
assertCharacterAtPosition(' ', 2, 1);
|
|
}
|
|
|
|
@Test
|
|
public void backspaceWorksInMiddleOfInput() {
|
|
enterCharacters("12345");
|
|
pressKey(KeyType.ArrowLeft);
|
|
pressKey(KeyType.ArrowLeft);
|
|
pressKey(KeyType.Backspace);
|
|
assertCursorPosition(2, 0);
|
|
assertCharacterAtPosition('1', 0, 0);
|
|
assertCharacterAtPosition('2', 1, 0);
|
|
assertCharacterAtPosition('4', 2, 0);
|
|
assertCharacterAtPosition('5', 3, 0);
|
|
}
|
|
|
|
}
|