transcendental-lisp/test/terminal/ControlSequenceHandlerTest....

137 lines
4.3 KiB
Java

package terminal;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static terminal.ControlSequenceHandler.isEscape;
import static terminal.SelectGraphicRendition.GREEN;
import static terminal.SelectGraphicRendition.PURPLE;
import static terminal.SelectGraphicRendition.RED;
import static terminal.SelectGraphicRendition.RESET;
import static terminal.SelectGraphicRendition.YELLOW;
import org.junit.Before;
import org.junit.Test;
import stream.SafeInputStream;
import terminal.ControlSequence.NullControlSequence;
import testutil.TestUtilities;
import util.Characters;
public class ControlSequenceHandlerTest {
private ControlSequenceHandler handler;
private Object readRemaining(SafeInputStream input) {
String remaining = "";
for (int c = input.read(); c != Characters.EOF; c = input.read())
remaining += (char) c;
return remaining;
}
private SafeInputStream createSafeInputStream(String data) {
return new SafeInputStream(TestUtilities.createInputStreamFromString(data));
}
@Before
public void setUp() {
handler = new ControlSequenceHandler();
}
@Test
public void isEscapeDetectsNonEscapeCharacter() {
assertThat(isEscape('x'), is(false));
}
@Test
public void isEscapeDetectsEscapeCharacter() {
assertThat(isEscape('\u001b'), is(true));
}
@Test
public void correctlyParsesControlSequence_LeavesRestOfStreamIntact() {
SafeInputStream input = createSafeInputStream("[32mdata");
handler.parse(input);
assertThat(readRemaining(input), is("data"));
}
@Test
public void unterminatedControlSequence_OnlyConsumesFirstNonSequenceCharacter() {
SafeInputStream input = createSafeInputStream("[32data");
handler.parse(input);
assertThat(readRemaining(input), is("ata"));
}
@Test
public void malformedControlSequence_OnlyConsumesOneCharacter() {
SafeInputStream input = createSafeInputStream("32mdata");
handler.parse(input);
assertThat(readRemaining(input), is("2mdata"));
}
@Test
public void parsedControlSequenceIsCorrectType_EOF() {
SafeInputStream input = createSafeInputStream("");
assertThat(handler.parse(input), instanceOf(NullControlSequence.class));
}
@Test
public void parsedControlSequenceIsCorrectType_EOF_AfterFirstCharacter() {
SafeInputStream input = createSafeInputStream("[");
assertThat(handler.parse(input), instanceOf(NullControlSequence.class));
}
@Test
public void parsedControlSequenceIsCorrectType_UnterminatedControlSequence() {
SafeInputStream input = createSafeInputStream("[data");
assertThat(handler.parse(input), instanceOf(NullControlSequence.class));
}
@Test
public void parsedControlSequenceIsCorrectType_MalformedControlSequence() {
SafeInputStream input = createSafeInputStream("32mdata");
assertThat(handler.parse(input), instanceOf(NullControlSequence.class));
}
@Test
public void parsedControlSequenceIsCorrectType_SGR_Reset() {
SafeInputStream input = createSafeInputStream("[0m");
assertThat(handler.parse(input), is(RESET));
}
@Test
public void parsedControlSequenceIsCorrectType_SGR_Red() {
SafeInputStream input = createSafeInputStream("[31m");
assertThat(handler.parse(input), is(RED));
}
@Test
public void parsedControlSequenceIsCorrectType_SGR_Green() {
SafeInputStream input = createSafeInputStream("[32m");
assertThat(handler.parse(input), is(GREEN));
}
@Test
public void parsedControlSequenceIsCorrectType_SGR_Yellow() {
SafeInputStream input = createSafeInputStream("[33m");
assertThat(handler.parse(input), is(YELLOW));
}
@Test
public void parsedControlSequenceIsCorrectType_SGR_Purple() {
SafeInputStream input = createSafeInputStream("[35m");
assertThat(handler.parse(input), is(PURPLE));
}
@Test
public void parseMultipleControlSequences() {
SafeInputStream input = createSafeInputStream("[35m[32m[0m");
assertThat(handler.parse(input), is(PURPLE));
assertThat(handler.parse(input), is(GREEN));
assertThat(handler.parse(input), is(RESET));
}
}