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

137 lines
4.3 KiB
Java
Raw Normal View History

package terminal;
2017-11-23 11:35:28 -05:00
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static terminal.ControlSequenceHandler.isEscape;
2017-11-12 09:42:25 -05:00
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() {
2017-11-23 11:35:28 -05:00
assertThat(isEscape('x'), is(false));
}
@Test
public void isEscapeDetectsEscapeCharacter() {
2017-11-23 11:35:28 -05:00
assertThat(isEscape('\u001b'), is(true));
}
@Test
public void correctlyParsesControlSequence_LeavesRestOfStreamIntact() {
SafeInputStream input = createSafeInputStream("[32mdata");
handler.parse(input);
2017-11-23 11:35:28 -05:00
assertThat(readRemaining(input), is("data"));
}
@Test
public void unterminatedControlSequence_OnlyConsumesFirstNonSequenceCharacter() {
SafeInputStream input = createSafeInputStream("[32data");
handler.parse(input);
2017-11-23 11:35:28 -05:00
assertThat(readRemaining(input), is("ata"));
}
@Test
public void malformedControlSequence_OnlyConsumesOneCharacter() {
SafeInputStream input = createSafeInputStream("32mdata");
handler.parse(input);
2017-11-23 11:35:28 -05:00
assertThat(readRemaining(input), is("2mdata"));
}
@Test
public void parsedControlSequenceIsCorrectType_EOF() {
SafeInputStream input = createSafeInputStream("");
2017-11-23 11:35:28 -05:00
assertThat(handler.parse(input), instanceOf(NullControlSequence.class));
}
@Test
public void parsedControlSequenceIsCorrectType_EOF_AfterFirstCharacter() {
SafeInputStream input = createSafeInputStream("[");
2017-11-23 11:35:28 -05:00
assertThat(handler.parse(input), instanceOf(NullControlSequence.class));
}
@Test
public void parsedControlSequenceIsCorrectType_UnterminatedControlSequence() {
SafeInputStream input = createSafeInputStream("[data");
2017-11-23 11:35:28 -05:00
assertThat(handler.parse(input), instanceOf(NullControlSequence.class));
}
@Test
public void parsedControlSequenceIsCorrectType_MalformedControlSequence() {
SafeInputStream input = createSafeInputStream("32mdata");
2017-11-23 11:35:28 -05:00
assertThat(handler.parse(input), instanceOf(NullControlSequence.class));
}
@Test
public void parsedControlSequenceIsCorrectType_SGR_Reset() {
SafeInputStream input = createSafeInputStream("[0m");
2017-11-23 11:35:28 -05:00
assertThat(handler.parse(input), is(RESET));
}
@Test
public void parsedControlSequenceIsCorrectType_SGR_Red() {
SafeInputStream input = createSafeInputStream("[31m");
2017-11-23 11:35:28 -05:00
assertThat(handler.parse(input), is(RED));
}
@Test
public void parsedControlSequenceIsCorrectType_SGR_Green() {
SafeInputStream input = createSafeInputStream("[32m");
2017-11-23 11:35:28 -05:00
assertThat(handler.parse(input), is(GREEN));
}
@Test
public void parsedControlSequenceIsCorrectType_SGR_Yellow() {
SafeInputStream input = createSafeInputStream("[33m");
2017-11-23 11:35:28 -05:00
assertThat(handler.parse(input), is(YELLOW));
}
@Test
public void parsedControlSequenceIsCorrectType_SGR_Purple() {
SafeInputStream input = createSafeInputStream("[35m");
2017-11-23 11:35:28 -05:00
assertThat(handler.parse(input), is(PURPLE));
}
@Test
public void parseMultipleControlSequences() {
SafeInputStream input = createSafeInputStream("[35m[32m[0m");
2017-11-23 11:35:28 -05:00
assertThat(handler.parse(input), is(PURPLE));
assertThat(handler.parse(input), is(GREEN));
assertThat(handler.parse(input), is(RESET));
}
}