Skip to content

Commit

Permalink
Test chunk on chars and bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
mrginglymus committed Feb 24, 2021
1 parent 01099d7 commit 6e55c5e
Showing 1 changed file with 31 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,19 @@ public class TruncatedStringTest {
@Parameterized.Parameter(1)
public boolean chunkOnNewlines;

/**
* Parameter for chunking on chars (or not!).
*/
@Parameterized.Parameter(2)
public boolean chunkOnChars;

@Parameterized.Parameters(name = "{0}")
public static Object[][] parameters() {
return new Object[][]{
{"Chunks", false},
{"Newlines", true}
{"Chunks+Bytes", false, false},
{"Newlines+Bytes", true, false},
{"Chunks+Chars", false, true},
{"Newlines+Chars", true, true}
};
}

Expand All @@ -51,32 +59,32 @@ public void shouldBuildStrings() {
TruncatedString.Builder builder = getBuilder();
builder.addText("Hello\n");
assertThat(builder.build()).asString().isEqualTo("Hello\n");
assertThat(builder.build().build(1000)).isEqualTo("Hello\n");
assertThat(builder.build().build(1000, chunkOnChars)).isEqualTo("Hello\n");
builder.addText(", world!");
assertThat(builder.build()).asString().isEqualTo("Hello\n, world!");
assertThat(builder.build().build(1000)).isEqualTo("Hello\n, world!");
assertThat(builder.build().build(1000, chunkOnChars)).isEqualTo("Hello\n, world!");
}

@Test
public void shouldTruncateStrings() {
TruncatedString.Builder builder = getBuilder();
builder.addText("xxxxxxxxx\n"); // 10
assertThat(builder.build().build(20)).isEqualTo("xxxxxxxxx\n");
assertThat(builder.build().build(20, chunkOnChars)).isEqualTo("xxxxxxxxx\n");
builder.addText("yyyy\n"); // 5, doesn't cause overflow
assertThat(builder.build().build(20)).isEqualTo("xxxxxxxxx\nyyyy\n");
assertThat(builder.build().build(20, chunkOnChars)).isEqualTo("xxxxxxxxx\nyyyy\n");
builder.addText("zzzzzz\n"); // 7, does cause overflow
assertThat(builder.build().build(20)).isEqualTo("xxxxxxxxx\nTruncated");
assertThat(builder.build().build(20, chunkOnChars)).isEqualTo("xxxxxxxxx\nTruncated");
}

@Test
public void shouldHandleEdgeCases() {
TruncatedString.Builder builder = getBuilder();
assertThat(builder.build().build(10)).isEqualTo("");
assertThat(builder.build().build(10, chunkOnChars)).isEqualTo("");
assertThat(builder.build()).asString().isEqualTo("");
builder.addText("xxxxxxxxxxxxxx\n"); // 15
assertThat(builder.build().build(10)).isEqualTo("Truncated");
assertThat(builder.build().build(10, chunkOnChars)).isEqualTo("Truncated");
assertThatThrownBy(() -> {
builder.build().build(5);
builder.build().build(5, chunkOnChars);
}).isInstanceOf(IllegalArgumentException.class)
.hasMessage("Maximum length is less than truncation text.");
}
Expand All @@ -86,25 +94,25 @@ public void shouldHandleReversedChunking() {
TruncatedString.Builder builder = getBuilder()
.setTruncateStart();
builder.addText("zzzz\n"); // 5
assertThat(builder.build().build(20)).isEqualTo("zzzz\n");
assertThat(builder.build().build(20, chunkOnChars)).isEqualTo("zzzz\n");
builder.addText("xxxx\n"); // 5, doesn't cause overflow
assertThat(builder.build().build(20)).isEqualTo("zzzz\nxxxx\n");
assertThat(builder.build().build(20, chunkOnChars)).isEqualTo("zzzz\nxxxx\n");
builder.addText("cccc\n"); // 5, doesn't cause overflow
assertThat(builder.build().build(20)).isEqualTo("zzzz\nxxxx\ncccc\n");
assertThat(builder.build().build(20, chunkOnChars)).isEqualTo("zzzz\nxxxx\ncccc\n");
builder.addText("aaaaaa\n"); // 7, does cause overflow
assertThat(builder.build().build(20)).isEqualTo("Truncatedcccc\naaaaaa\n");
assertThat(builder.build().build(20, chunkOnChars)).isEqualTo("Truncatedcccc\naaaaaa\n");
}

@Test
public void shouldHandleEdgeCasesReversed() {
TruncatedString.Builder builder = getBuilder()
.setTruncateStart();
assertThat(builder.build().build(10)).isEqualTo("");
assertThat(builder.build().build(10, chunkOnChars)).isEqualTo("");
assertThat(builder.build()).asString().isEqualTo("");
builder.addText("xxxxxxxxxxxxxx\n"); // 15
assertThat(builder.build().build(10)).isEqualTo("Truncated");
assertThat(builder.build().build(10, chunkOnChars)).isEqualTo("Truncated");
assertThatThrownBy(() -> {
builder.build().build(5);
builder.build().build(5, chunkOnChars);
}).isInstanceOf(IllegalArgumentException.class)
.hasMessage("Maximum length is less than truncation text.");
}
Expand All @@ -114,12 +122,12 @@ public void shouldChunkNewlinesDifferently() {
TruncatedString.Builder builder = getBuilder();
builder.addText("xxxxxxxxxx"); // 10
builder.addText("yyyyyyyyyyy"); // 11
assertThat(builder.build().build(20)).isEqualTo(chunkOnNewlines ? "Truncated" : "xxxxxxxxxxTruncated");
assertThat(builder.build().build(20, chunkOnChars)).isEqualTo(chunkOnNewlines ? "Truncated" : "xxxxxxxxxxTruncated");

builder = getBuilder();
builder.addText("wwww\n"); // 5
builder.addText("xxxx\nyyyy\nzzzzz\n"); // 16
assertThat(builder.build().build(20)).isEqualTo(chunkOnNewlines ? "wwww\nxxxx\nTruncated" : "wwww\nTruncated");
assertThat(builder.build().build(20, chunkOnChars)).isEqualTo(chunkOnNewlines ? "wwww\nxxxx\nTruncated" : "wwww\nTruncated");
}

@Test
Expand All @@ -128,12 +136,12 @@ public void shouldTruncateToBytesNotChars() {
builder.addText("☃☃☃\n"); // 3 + 1
assertThat(builder.build().toString().length()).isEqualTo(4);
assertThat(builder.build().toString().getBytes(StandardCharsets.UTF_8).length).isEqualTo(10);
assertThat(builder.build().build(20)).isEqualTo("☃☃☃\n");
assertThat(builder.build().build(20, chunkOnChars)).isEqualTo("☃☃☃\n");

builder.addText("🕴️🕴️\n"); // 2 + 1
assertThat(builder.build().toString().length()).isEqualTo(11);
assertThat(builder.build().toString().getBytes(StandardCharsets.UTF_8).length).isEqualTo(25);
assertThat(builder.build().build(20)).isEqualTo("☃☃☃\nTruncated");
assertThat(builder.build().build(20, chunkOnChars)).isEqualTo(chunkOnChars ? "☃☃☃\n🕴️🕴️\n" : "☃☃☃\nTruncated");
}

@Test
Expand All @@ -146,8 +154,8 @@ public void shouldHandleLongCharsInTruncationTest() {
builder.withTruncationText(truncationText);
builder.addText("xxxx\n"); // 5
builder.addText("x\n"); // 2
assertThat(builder.build().build(20)).isEqualTo("xxxx\nx\n");
assertThat(builder.build().build(20, chunkOnChars)).isEqualTo("xxxx\nx\n");
builder.addText("xxxxxxxxxxxxxxx"); // 15
assertThat(builder.build().build(20)).isEqualTo("xxxx\nE_TOO_MUCH_☃");
assertThat(builder.build().build(20, chunkOnChars)).isEqualTo(chunkOnChars ? "xxxx\nx\nE_TOO_MUCH_☃" : "xxxx\nE_TOO_MUCH_☃");
}
}

0 comments on commit 6e55c5e

Please sign in to comment.