1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.cli.help;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertFalse;
22 import static org.junit.jupiter.api.Assertions.assertNotEquals;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.stream.Stream;
28
29 import org.junit.jupiter.api.Test;
30 import org.junit.jupiter.params.ParameterizedTest;
31 import org.junit.jupiter.params.provider.Arguments;
32 import org.junit.jupiter.params.provider.MethodSource;
33
34 class UtilTest {
35
36 public static Stream<Arguments> charArgs() {
37 final List<Arguments> lst = new ArrayList<>();
38
39 final char[] whitespace = {' ', '\t', '\n', '\f', '\r',
40 Character.SPACE_SEPARATOR,
41 Character.LINE_SEPARATOR,
42 Character.PARAGRAPH_SEPARATOR,
43 '\u000B',
44 '\u001C',
45 '\u001D',
46 '\u001E',
47 '\u001F',
48 };
49
50 final char[] nonBreakingSpace = { '\u00A0', '\u2007', '\u202F' };
51 for (final char c : whitespace) {
52 lst.add(Arguments.of(Character.valueOf(c), Boolean.TRUE));
53 }
54 for (final char c : nonBreakingSpace) {
55 lst.add(Arguments.of(Character.valueOf(c), Boolean.FALSE));
56 }
57 return lst.stream();
58 }
59
60 @Test
61 void testFindNonWhitespacePos() {
62 assertEquals(-1, Util.indexOfNonWhitespace(null, 0));
63 assertEquals(-1, Util.indexOfNonWhitespace("", 0));
64 }
65
66 @ParameterizedTest
67 @MethodSource("charArgs")
68 void testFindNonWhitespacePos(final Character c, final boolean isWhitespace) {
69 String text = String.format("%cWorld", c);
70 assertEquals(isWhitespace ? 1 : 0, Util.indexOfNonWhitespace(text, 0));
71 text = String.format("%c%c%c", c, c, c);
72 assertEquals(isWhitespace ? -1 : 0, Util.indexOfNonWhitespace(text, 0));
73 }
74
75 @Test
76 void testIsEmpty() {
77 String str = null;
78 assertTrue(Util.isEmpty(str), "null string should be empty");
79 str = "";
80 assertTrue(Util.isEmpty(str), "empty string should be empty");
81 str = " ";
82 assertFalse(Util.isEmpty(str), "string with whitespace should not be empty");
83 }
84
85 @ParameterizedTest
86 @MethodSource("charArgs")
87 void testRtrim(final Character c, final boolean isWhitespace) {
88 if (isWhitespace) {
89 assertEquals("worx", Util.rtrim(String.format("worx%s", c)), () -> String.format("Did not process character 0x%x", (int) c));
90 } else {
91 assertNotEquals("worx", Util.rtrim(String.format("worx%s", c)), () -> String.format("Did not process character 0x%x", (int) c));
92 }
93 final String text = String.format("%c%c%c", c, c, c);
94 assertEquals(isWhitespace ? "" : text, Util.ltrim(text));
95 }
96 }