Skip to content

Commit aba2b24

Browse files
test: improve coverage for Strings utility class
Added missing tests for empty edge cases in countOccurrences and added coverage for the overloaded join methods utilizing StringBuilder. Co-authored-by: RoiSoleil <3462260+RoiSoleil@users.noreply.github.com>
1 parent 2768417 commit aba2b24

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

org.moreunit.core.test/test/org/moreunit/core/util/StringsTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ public void should_count_occurrences() throws Exception
8383
assertThat(Strings.countOccurrences("bla<>h<>", "<>")).isEqualTo(2);
8484
}
8585

86+
@Test
87+
public void should_return_zero_when_count_occurrences_is_called_with_empty_strings() throws Exception
88+
{
89+
assertThat(Strings.countOccurrences("", "test")).isEqualTo(0);
90+
assertThat(Strings.countOccurrences(null, "test")).isEqualTo(0);
91+
assertThat(Strings.countOccurrences("test", "")).isEqualTo(0);
92+
assertThat(Strings.countOccurrences("test", null)).isEqualTo(0);
93+
assertThat(Strings.countOccurrences(null, null)).isEqualTo(0);
94+
}
95+
8696
@Test
8797
public void split_should_ignore_blank_parts() throws Exception
8898
{
@@ -128,6 +138,22 @@ public void join_should_join_given_strings_whith_given_separator() throws Except
128138
assertThat(Strings.join(" -> ", "aBc", "2", "DEf")).isEqualTo("aBc -> 2 -> DEf");
129139
}
130140

141+
@Test
142+
public void join_should_append_to_string_builder_with_given_separator_for_collection() throws Exception
143+
{
144+
StringBuilder sb = new StringBuilder("prefix: ");
145+
Strings.join(sb, ",", java.util.Arrays.asList("1", "2", "3"));
146+
assertThat(sb.toString()).isEqualTo("prefix: 1,2,3");
147+
}
148+
149+
@Test
150+
public void join_should_append_to_string_builder_with_given_separator_for_array() throws Exception
151+
{
152+
StringBuilder sb = new StringBuilder("prefix: ");
153+
Strings.join(sb, " -> ", new String[]{"aBc", "2", "DEf"});
154+
assertThat(sb.toString()).isEqualTo("prefix: aBc -> 2 -> DEf");
155+
}
156+
131157
@Test
132158
public void emptyArray_should_return_empty_array()
133159
{

0 commit comments

Comments
 (0)