View Javadoc
1   /*
2     Licensed to the Apache Software Foundation (ASF) under one or more
3     contributor license agreements.  See the NOTICE file distributed with
4     this work for additional information regarding copyright ownership.
5     The ASF licenses this file to You under the Apache License, Version 2.0
6     (the "License"); you may not use this file except in compliance with
7     the License.  You may obtain a copy of the License at
8   
9         https://www.apache.org/licenses/LICENSE-2.0
10  
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
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          // @formatter:off
39          final char[] whitespace = {' ', '\t', '\n', '\f', '\r',
40                  Character.SPACE_SEPARATOR,
41                  Character.LINE_SEPARATOR,
42                  Character.PARAGRAPH_SEPARATOR,
43                  '\u000B', // VERTICAL TABULATION.
44                  '\u001C', // FILE SEPARATOR.
45                  '\u001D', // GROUP SEPARATOR.
46                  '\u001E', // RECORD SEPARATOR.
47                  '\u001F', // UNIT SEPARATOR.
48          };
49          // @formatter:on
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  }