BAEL-838 - Added regex method and updated tests.

This commit is contained in:
iaforek 2017-05-10 13:47:45 +01:00
parent d9d35f8a4f
commit 0fb69164c0
2 changed files with 17 additions and 0 deletions

View File

@ -8,4 +8,12 @@ public class RemoveLastChar {
return (s.substring(0, s.length() - 1));
}
}
public static String chop(String s) {
if (s == null) {
return s;
} else {
return s.replaceAll(".$", "");
}
}
}

View File

@ -18,12 +18,15 @@ public class RemoveLastCharTest {
assertEquals("abcde", RemoveLastChar.substring(TEST_STRING));
assertEquals("abcde", StringUtils.substring(TEST_STRING, 0, TEST_STRING.length() - 1));
assertEquals("abcde", StringUtils.chop(TEST_STRING));
assertEquals("abcde", TEST_STRING.replaceAll(".$", ""));
assertEquals("abcde", RemoveLastChar.chop(TEST_STRING));
}
@Test
public void givenNullString_whenSubstring_thenGetNullString() {
assertEquals(NULL_STRING, RemoveLastChar.substring(NULL_STRING));
assertEquals(NULL_STRING, StringUtils.chop(NULL_STRING));
assertEquals(NULL_STRING, RemoveLastChar.chop(NULL_STRING));
}
@Test
@ -31,6 +34,8 @@ public class RemoveLastCharTest {
assertEquals(EMPTY_STRING, RemoveLastChar.substring(EMPTY_STRING));
assertEquals(EMPTY_STRING, StringUtils.substring(EMPTY_STRING, 0, EMPTY_STRING.length() - 1));
assertEquals(EMPTY_STRING, StringUtils.chop(EMPTY_STRING));
assertEquals(EMPTY_STRING, EMPTY_STRING.replaceAll(".$", ""));
assertEquals(EMPTY_STRING, RemoveLastChar.chop(EMPTY_STRING));
}
@Test
@ -38,6 +43,8 @@ public class RemoveLastCharTest {
assertEquals(EMPTY_STRING, RemoveLastChar.substring(ONE_CHAR_STRING));
assertEquals(EMPTY_STRING, StringUtils.substring(ONE_CHAR_STRING, 0, ONE_CHAR_STRING.length() - 1));
assertEquals(EMPTY_STRING, StringUtils.chop(ONE_CHAR_STRING));
assertEquals(EMPTY_STRING, ONE_CHAR_STRING.replaceAll(".$", ""));
assertEquals(EMPTY_STRING, RemoveLastChar.chop(ONE_CHAR_STRING));
}
@Test
@ -45,5 +52,7 @@ public class RemoveLastCharTest {
assertEquals("abc", RemoveLastChar.substring(WHITE_SPACE_AT_THE_END_STRING));
assertEquals("abc", StringUtils.substring(WHITE_SPACE_AT_THE_END_STRING, 0, WHITE_SPACE_AT_THE_END_STRING.length() - 1));
assertEquals("abc", StringUtils.chop(WHITE_SPACE_AT_THE_END_STRING));
assertEquals("abc", WHITE_SPACE_AT_THE_END_STRING.replaceAll(".$", ""));
assertEquals("abc", RemoveLastChar.chop(WHITE_SPACE_AT_THE_END_STRING));
}
}