• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 package test.java.lang.String;
24 
25 /*
26  * @test
27  * @summary Unit tests for String#indent
28  * @run main Indent
29  */
30 
31 import java.util.List;
32 import java.util.stream.Collectors;
33 import java.util.stream.Stream;
34 
35 import org.testng.annotations.Test;
36 import static org.testng.Assert.assertEquals;
37 
38 public class Indent {
39     static final List<String> ENDS = List.of("", "\n", "   \n", "\n\n", "\n\n\n");
40     static final List<String> MIDDLES = List.of(
41             "",
42             "xyz",
43             "   xyz",
44             "      xyz",
45             "xyz   ",
46             "   xyz   ",
47             "      xyz   ",
48             "xyz\u2022",
49             "   xyz\u2022",
50             "xyz\u2022   ",
51             "   xyz\u2022   ",
52             "   // comment"
53     );
54 
55     /*
56      * Test String#indent(int n) functionality.
57      */
58     @Test
testIndent()59     public void testIndent() {
60         for (int adjust : new int[] {-8, -7, -4, -3, -2, -1, 0, 1, 2, 3, 4, 7, 8}) {
61             for (String prefix : ENDS) {
62                 for (String suffix : ENDS) {
63                     for (String middle : MIDDLES) {
64                         String input = prefix + "   abc   \n" + middle + "\n   def   \n" + suffix;
65                         String output = input.indent(adjust);
66 
67                         Stream<String> stream = input.lines();
68                         if (adjust > 0) {
69                             final String spaces = " ".repeat(adjust);
70                             stream = stream.map(s -> spaces + s);
71                         } else if (adjust < 0) {
72                             stream = stream.map(s -> s.substring(Math.min(-adjust, indexOfNonWhitespace(s))));
73                         }
74                         String expected = stream.collect(Collectors.joining("\n", "", "\n"));
75 
76                         assertEquals(output, expected);
77                     }
78                 }
79             }
80         }
81     }
82 
indexOfNonWhitespace(String s)83     public static int indexOfNonWhitespace(String s) {
84         int left = 0;
85         while (left < s.length()) {
86             char ch = s.charAt(left);
87             if (ch != ' ' && ch != '\t' && !Character.isWhitespace(ch)) {
88                 break;
89             }
90             left++;
91         }
92         return left;
93     }
94 }
95