garydgregory commented on code in PR #1813:
URL: https://github.com/apache/commons-lang/pull/1813#discussion_r4272270688
##########
src/test/java/org/apache/commons/lang3/StringUtilsTest.java:
##########
@@ -340,6 +340,41 @@ void testCenter_StringIntString() {
assertEquals(" abc ", StringUtils.center("abc", 7, ""));
}
+ private void assertNoLoneSurrogates(final String result) {
+ for (int i = 0; i < result.length(); i++) {
+ final char ch = result.charAt(i);
+ if (Character.isHighSurrogate(ch)) {
+ assertTrue(i + 1 < result.length() &&
Character.isLowSurrogate(result.charAt(i + 1)),
+ "lone high surrogate in: " + result);
+ i++; // skip the paired low surrogate
+ } else {
+ assertFalse(Character.isLowSurrogate(ch), "lone low surrogate
in: " + result);
+ }
+ }
+ }
+
+ @Test
+ void testCenterSurrogatePair() {
+ final String grin = "😀";
+ assertEquals("a", StringUtils.center("a", 2, grin));
+ assertEquals("a😀", StringUtils.center("a", 3, grin));
+ assertEquals("a😀", StringUtils.center("a", 4, grin));
+ assertEquals("😀a😀", StringUtils.center("a", 5, grin));
+ assertEquals("😀a😀", StringUtils.center("a", 6, grin));
+ assertEquals("😀a😀😀", StringUtils.center("a", 7, grin));
+
+ final String[] testStrings = {"", "a", "hello", grin, "a" + grin +
"b"};
+ final String[] padStrings = {grin, "a" + grin, grin + "b", "a" + grin
+ "b", grin + "🎉"};
+ for (final String str : testStrings) {
+ for (final String padStr : padStrings) {
+ for (int size = 0; size <= 15; size++) {
+ final String result = StringUtils.center(str, size,
padStr);
+ assertNoLoneSurrogates(result);
+ }
+ }
+ }
Review Comment:
@Alwaysgaurav1
In fact, you can use a JUnit parameterized test for this.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]