Add ParsableByteArray tests for \r line endings

PiperOrigin-RevId: 733140077
This commit is contained in:
ibaker 2025-03-03 19:00:06 -08:00 committed by Copybara-Service
parent a92cf5311f
commit 2da814c8ef

View File

@ -622,6 +622,16 @@ public final class ParsableByteArrayTest {
assertThat(parser.readLine(US_ASCII)).isNull(); assertThat(parser.readLine(US_ASCII)).isNull();
} }
@Test
public void readSingleLineWithEndingCr_ascii() {
byte[] bytes = "foo\r".getBytes(US_ASCII);
ParsableByteArray parser = new ParsableByteArray(bytes);
assertThat(parser.readLine(US_ASCII)).isEqualTo("foo");
assertThat(parser.getPosition()).isEqualTo(4);
assertThat(parser.readLine(US_ASCII)).isNull();
}
@Test @Test
public void readTwoLinesWithCrFollowedByLf_ascii() { public void readTwoLinesWithCrFollowedByLf_ascii() {
byte[] bytes = "foo\r\nbar".getBytes(US_ASCII); byte[] bytes = "foo\r\nbar".getBytes(US_ASCII);
@ -684,6 +694,16 @@ public final class ParsableByteArrayTest {
assertThat(parser.readLine()).isNull(); assertThat(parser.readLine()).isNull();
} }
@Test
public void readSingleLineWithEndingCr_utf8() {
byte[] bytes = "foo\r".getBytes(UTF_8);
ParsableByteArray parser = new ParsableByteArray(bytes);
assertThat(parser.readLine()).isEqualTo("foo");
assertThat(parser.getPosition()).isEqualTo(4);
assertThat(parser.readLine()).isNull();
}
@Test @Test
public void readTwoLinesWithCr_utf8() { public void readTwoLinesWithCr_utf8() {
byte[] bytes = "foo\rbar".getBytes(UTF_8); byte[] bytes = "foo\rbar".getBytes(UTF_8);
@ -775,6 +795,18 @@ public final class ParsableByteArrayTest {
assertThat(parser.readLine(UTF_16)).isNull(); assertThat(parser.readLine(UTF_16)).isNull();
} }
@Test
public void readSingleLineWithEndingCr_utf16() {
// Use UTF_16BE because we don't want the leading BOM that's added by getBytes(UTF_16). We
// explicitly test with a BOM elsewhere.
byte[] bytes = "foo\r".getBytes(UTF_16BE);
ParsableByteArray parser = new ParsableByteArray(bytes);
assertThat(parser.readLine(UTF_16)).isEqualTo("foo");
assertThat(parser.getPosition()).isEqualTo(8);
assertThat(parser.readLine(UTF_16)).isNull();
}
@Test @Test
public void readTwoLinesWithCrFollowedByLf_utf16() { public void readTwoLinesWithCrFollowedByLf_utf16() {
// Use UTF_16BE because we don't want the leading BOM that's added by getBytes(UTF_16). We // Use UTF_16BE because we don't want the leading BOM that's added by getBytes(UTF_16). We
@ -842,6 +874,16 @@ public final class ParsableByteArrayTest {
assertThat(parser.readLine(UTF_16BE)).isNull(); assertThat(parser.readLine(UTF_16BE)).isNull();
} }
@Test
public void readSingleLineWithEndingCr_utf16be() {
byte[] bytes = "foo\r".getBytes(UTF_16BE);
ParsableByteArray parser = new ParsableByteArray(bytes);
assertThat(parser.readLine(UTF_16BE)).isEqualTo("foo");
assertThat(parser.getPosition()).isEqualTo(8);
assertThat(parser.readLine(UTF_16BE)).isNull();
}
@Test @Test
public void readTwoLinesWithCrFollowedByLf_utf16be() { public void readTwoLinesWithCrFollowedByLf_utf16be() {
byte[] bytes = "foo\r\nbar".getBytes(UTF_16BE); byte[] bytes = "foo\r\nbar".getBytes(UTF_16BE);
@ -904,6 +946,16 @@ public final class ParsableByteArrayTest {
assertThat(parser.readLine(UTF_16LE)).isNull(); assertThat(parser.readLine(UTF_16LE)).isNull();
} }
@Test
public void readSingleLineWithEndingCr_utf16le() {
byte[] bytes = "foo\r".getBytes(UTF_16LE);
ParsableByteArray parser = new ParsableByteArray(bytes);
assertThat(parser.readLine(UTF_16LE)).isEqualTo("foo");
assertThat(parser.getPosition()).isEqualTo(8);
assertThat(parser.readLine(UTF_16LE)).isNull();
}
@Test @Test
public void readTwoLinesWithCrFollowedByLf_utf16le() { public void readTwoLinesWithCrFollowedByLf_utf16le() {
byte[] bytes = "foo\r\nbar".getBytes(UTF_16LE); byte[] bytes = "foo\r\nbar".getBytes(UTF_16LE);