Added support for collapseWhitespace function

This commit is contained in:
Shekhar Gulati
2016-05-04 19:46:48 +05:30
parent 59da6a956f
commit aee5d512ad
3 changed files with 64 additions and 19 deletions
+26 -8
View File
@@ -2,6 +2,7 @@ package strman;
import org.junit.Test;
import java.util.Arrays;
import java.util.Optional;
import static org.hamcrest.CoreMatchers.equalTo;
@@ -12,31 +13,31 @@ import static strman.Strman.*;
public class StrmanTest {
@Test
public void shouldAppendStringsToEndOfValue() throws Exception {
public void append_shouldAppendStringsToEndOfValue() throws Exception {
assertThat(append("f", "o", "o", "b", "a", "r"), equalTo("foobar"));
assertThat(append("foobar"), equalTo("foobar"));
assertThat(append("", "foobar"), equalTo("foobar"));
}
@Test(expected = IllegalArgumentException.class)
public void appendShouldThrowIllegalArgumentExceptionWhenValueIsNull() throws Exception {
public void append_shouldThrowIllegalArgumentExceptionWhenValueIsNull() throws Exception {
append(null);
}
@Test
public void shouldAppendStringArrayToEndOfValue() throws Exception {
public void appendArray_shouldAppendStringArrayToEndOfValue() throws Exception {
assertThat(appendArray("f", new String[]{"o", "o", "b", "a", "r"}), equalTo("foobar"));
assertThat(appendArray("foobar", new String[]{}), equalTo("foobar"));
assertThat(appendArray("", new String[]{"foobar"}), equalTo("foobar"));
}
@Test(expected = IllegalArgumentException.class)
public void appendArrayShouldThrowIllegalArgumentExceptionWhenValueIsNull() throws Exception {
public void appendArray_ShouldThrowIllegalArgumentExceptionWhenValueIsNull() throws Exception {
appendArray(null, new String[]{});
}
@Test
public void shouldFindCharacterAtIndex() throws Exception {
public void at_shouldFindCharacterAtIndex() throws Exception {
assertThat(at("foobar", 0), equalTo(Optional.of("f")));
assertThat(at("foobar", 1), equalTo(Optional.of("o")));
assertThat(at("foobar", -1), equalTo(Optional.of("r")));
@@ -46,21 +47,38 @@ public class StrmanTest {
}
@Test
public void shouldReturnArrayWithStringsBetweenStartAndEnd() throws Exception {
public void between_shouldReturnArrayWithStringsBetweenStartAndEnd() throws Exception {
assertThat(between("[abc][def]", "[", "]"), arrayContaining("abc", "def"));
assertThat(between("<span>foo</span>", "<span>", "</span>"), arrayContaining("foo"));
assertThat(between("<span>foo</span><span>bar</span>", "<span>", "</span>"), arrayContaining("foo", "bar"));
}
@Test
public void betweenShouldReturnFullStringWhenStartAndEndDoesNotExist() throws Exception {
public void between_shouldReturnFullStringWhenStartAndEndDoesNotExist() throws Exception {
assertThat(between("[abc][def]", "{", "}"), arrayContaining("[abc][def]"));
assertThat(between("", "{", "}"), arrayContaining(""));
}
@Test
public void charsShouldReturnAllCharactersInString() throws Exception {
public void chars_shouldReturnAllCharactersInString() throws Exception {
final String title = "title";
assertThat(chars(title), equalTo(new String[]{"t", "i", "t", "l", "e"}));
}
@Test
public void collapseWhitespace_shouldReplaceConsecutiveWhitespaceWithSingleSpace() throws Exception {
String[] fixture = {
"foo bar",
" foo bar ",
" foo bar ",
" foo bar "
};
Arrays.stream(fixture).forEach(el -> assertThat(collapseWhitespace(el), equalTo("foo bar")));
}
@Test
public void collapseWhitespace_shouldReplaceConsecutiveWhitespaceBetweenMultipleStrings() throws Exception {
String input = " foo bar bazz hello world ";
assertThat(collapseWhitespace(input), equalTo("foo bar bazz hello world"));
}
}