Skip to content

Instantly share code, notes, and snippets.

@thieux
Created July 12, 2016 23:26
Show Gist options
  • Save thieux/8cb12d911ac28d211b53042c4d6dddf3 to your computer and use it in GitHub Desktop.
Save thieux/8cb12d911ac28d211b53042c4d6dddf3 to your computer and use it in GitHub Desktop.
Java String split behavior
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class StringTest {
@Test
public void empty_regex_on_empty_char() {
assertThat("".split("")).isEqualTo(new String[]{""});
}
@Test
public void empty_regex_cuts_each_char() {
assertThat("abc".split("")).isEqualTo(new String[]{"a", "b", "c"});
}
@Test
public void cuts_empty_string_to_empty_component() {
assertThat("".split(":")).isEqualTo(new String[]{""});
}
@Test
public void do_not_cut_when_input_equals_regex() {
assertThat(":".split(":")).isEqualTo(new String[]{});
}
@Test
public void cut_into_one_component() {
assertThat("A".split(":")).isEqualTo(new String[]{"A"});
}
@Test
public void suffix_input_by_regex_cut_into_one_component() {
assertThat("A:".split(":")).isEqualTo(new String[]{"A"});
}
@Test
public void prefix_input_by_regex_cuts_into_two_components() {
assertThat(":A".split(":")).isEqualTo(new String[]{"", "A"});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment