51714
|
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 |
|
|
24 |
/*
|
|
25 |
* @test
|
53231
|
26 |
* @summary Unit tests for String#indent
|
|
27 |
* @run main Indent
|
51714
|
28 |
*/
|
|
29 |
|
|
30 |
import java.util.Arrays;
|
|
31 |
import java.util.List;
|
|
32 |
import java.util.stream.Collectors;
|
|
33 |
import java.util.stream.Stream;
|
|
34 |
|
53231
|
35 |
public class Indent {
|
51714
|
36 |
static final List<String> ENDS = List.of("", "\n", " \n", "\n\n", "\n\n\n");
|
|
37 |
static final List<String> MIDDLES = List.of(
|
|
38 |
"",
|
|
39 |
"xyz",
|
|
40 |
" xyz",
|
|
41 |
" xyz",
|
|
42 |
"xyz ",
|
|
43 |
" xyz ",
|
|
44 |
" xyz ",
|
|
45 |
"xyz\u2022",
|
|
46 |
" xyz\u2022",
|
|
47 |
"xyz\u2022 ",
|
|
48 |
" xyz\u2022 ",
|
|
49 |
" // comment"
|
|
50 |
);
|
|
51 |
|
|
52 |
public static void main(String[] args) {
|
|
53 |
test1();
|
|
54 |
}
|
|
55 |
|
|
56 |
/*
|
|
57 |
* Test String#indent(int n) functionality.
|
|
58 |
*/
|
53231
|
59 |
static void test1() {
|
51714
|
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 -> s.isBlank() ? 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 |
if (!output.equals(expected)) {
|
|
77 |
report("String::indent(int n)",
|
|
78 |
"Result indentation not as expected", expected, output);
|
|
79 |
}
|
|
80 |
}
|
|
81 |
}
|
|
82 |
}
|
|
83 |
}
|
|
84 |
}
|
|
85 |
|
|
86 |
public static int indexOfNonWhitespace(String s) {
|
|
87 |
int left = 0;
|
|
88 |
while (left < s.length()) {
|
|
89 |
char ch = s.charAt(left);
|
|
90 |
if (ch != ' ' && ch != '\t' && !Character.isWhitespace(ch)) {
|
|
91 |
break;
|
|
92 |
}
|
|
93 |
left++;
|
|
94 |
}
|
|
95 |
return left;
|
|
96 |
}
|
|
97 |
|
|
98 |
|
|
99 |
private static String[] getBody(String[] inLines) {
|
|
100 |
int from = -1, to = -1;
|
|
101 |
for (int i = 0; i < inLines.length; i++) {
|
|
102 |
String line = inLines[i];
|
|
103 |
if (!line.isBlank()) {
|
|
104 |
if (from == -1) {
|
|
105 |
from = i;
|
|
106 |
}
|
|
107 |
to = i + 1;
|
|
108 |
}
|
|
109 |
}
|
|
110 |
return Arrays.copyOfRange(inLines, from, to);
|
|
111 |
}
|
|
112 |
|
|
113 |
/*
|
|
114 |
* Report difference in result.
|
|
115 |
*/
|
|
116 |
static void report(String test, String message, String input, String output) {
|
|
117 |
System.err.println("Testing " + test + ": " + message);
|
|
118 |
System.err.println();
|
|
119 |
System.err.println("Input: length = " + input.length());
|
|
120 |
System.err.println("_".repeat(40));
|
|
121 |
System.err.print(input.replaceAll(" ", "."));
|
|
122 |
System.err.println("_".repeat(40));
|
|
123 |
System.err.println();
|
|
124 |
System.err.println("Output: length = " + output.length());
|
|
125 |
System.err.println("_".repeat(40));
|
|
126 |
System.err.print(output.replaceAll(" ", "."));
|
|
127 |
System.err.println("_".repeat(40));
|
|
128 |
throw new RuntimeException();
|
|
129 |
}
|
|
130 |
}
|