2017-03-22 11:26:53 -04:00
|
|
|
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;
|
2017-03-22 11:26:53 -04:00
|
|
|
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;
|
2017-03-22 11:26:53 -04:00
|
|
|
|
2017-03-22 14:08:22 -04:00
|
|
|
import stream.SafeInputStream;
|
2017-03-22 11:26:53 -04:00
|
|
|
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));
|
2017-03-22 11:26:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void isEscapeDetectsEscapeCharacter() {
|
2017-11-23 11:35:28 -05:00
|
|
|
assertThat(isEscape('\u001b'), is(true));
|
2017-03-22 11:26:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@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"));
|
2017-03-22 11:26:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@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"));
|
2017-03-22 11:26:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@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"));
|
2017-03-22 11:26:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void parsedControlSequenceIsCorrectType_EOF() {
|
|
|
|
SafeInputStream input = createSafeInputStream("");
|
2017-11-23 11:35:28 -05:00
|
|
|
assertThat(handler.parse(input), instanceOf(NullControlSequence.class));
|
2017-03-22 11:26:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void parsedControlSequenceIsCorrectType_EOF_AfterFirstCharacter() {
|
|
|
|
SafeInputStream input = createSafeInputStream("[");
|
2017-11-23 11:35:28 -05:00
|
|
|
assertThat(handler.parse(input), instanceOf(NullControlSequence.class));
|
2017-03-22 11:26:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void parsedControlSequenceIsCorrectType_UnterminatedControlSequence() {
|
|
|
|
SafeInputStream input = createSafeInputStream("[data");
|
2017-11-23 11:35:28 -05:00
|
|
|
assertThat(handler.parse(input), instanceOf(NullControlSequence.class));
|
2017-03-22 11:26:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void parsedControlSequenceIsCorrectType_MalformedControlSequence() {
|
|
|
|
SafeInputStream input = createSafeInputStream("32mdata");
|
2017-11-23 11:35:28 -05:00
|
|
|
assertThat(handler.parse(input), instanceOf(NullControlSequence.class));
|
2017-03-22 11:26:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void parsedControlSequenceIsCorrectType_SGR_Reset() {
|
|
|
|
SafeInputStream input = createSafeInputStream("[0m");
|
2017-11-23 11:35:28 -05:00
|
|
|
assertThat(handler.parse(input), is(RESET));
|
2017-03-22 11:26:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void parsedControlSequenceIsCorrectType_SGR_Red() {
|
|
|
|
SafeInputStream input = createSafeInputStream("[31m");
|
2017-11-23 11:35:28 -05:00
|
|
|
assertThat(handler.parse(input), is(RED));
|
2017-03-22 11:26:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void parsedControlSequenceIsCorrectType_SGR_Green() {
|
|
|
|
SafeInputStream input = createSafeInputStream("[32m");
|
2017-11-23 11:35:28 -05:00
|
|
|
assertThat(handler.parse(input), is(GREEN));
|
2017-03-22 11:26:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void parsedControlSequenceIsCorrectType_SGR_Yellow() {
|
|
|
|
SafeInputStream input = createSafeInputStream("[33m");
|
2017-11-23 11:35:28 -05:00
|
|
|
assertThat(handler.parse(input), is(YELLOW));
|
2017-03-22 11:26:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void parsedControlSequenceIsCorrectType_SGR_Purple() {
|
|
|
|
SafeInputStream input = createSafeInputStream("[35m");
|
2017-11-23 11:35:28 -05:00
|
|
|
assertThat(handler.parse(input), is(PURPLE));
|
2017-03-22 11:26:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@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));
|
2017-03-22 11:26:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|