Path: blob/master/test/jdk/java/lang/String/Indent.java
41152 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary Unit tests for String#indent26* @run main Indent27*/2829import java.util.Arrays;30import java.util.List;31import java.util.stream.Collectors;32import java.util.stream.Stream;3334public class Indent {35static final List<String> ENDS = List.of("", "\n", " \n", "\n\n", "\n\n\n");36static final List<String> MIDDLES = List.of(37"",38"xyz",39" xyz",40" xyz",41"xyz ",42" xyz ",43" xyz ",44"xyz\u2022",45" xyz\u2022",46"xyz\u2022 ",47" xyz\u2022 ",48" // comment"49);5051public static void main(String[] args) {52test1();53}5455/*56* Test String#indent(int n) functionality.57*/58static void test1() {59for (int adjust : new int[] {-8, -7, -4, -3, -2, -1, 0, 1, 2, 3, 4, 7, 8}) {60for (String prefix : ENDS) {61for (String suffix : ENDS) {62for (String middle : MIDDLES) {63String input = prefix + " abc \n" + middle + "\n def \n" + suffix;64String output = input.indent(adjust);6566Stream<String> stream = input.lines();67if (adjust > 0) {68final String spaces = " ".repeat(adjust);69stream = stream.map(s -> spaces + s);70} else if (adjust < 0) {71stream = stream.map(s -> s.substring(Math.min(-adjust, indexOfNonWhitespace(s))));72}73String expected = stream.collect(Collectors.joining("\n", "", "\n"));7475if (!output.equals(expected)) {76report("String::indent(int n)",77"Result indentation not as expected", expected, output);78}79}80}81}82}83}8485public static int indexOfNonWhitespace(String s) {86int left = 0;87while (left < s.length()) {88char ch = s.charAt(left);89if (ch != ' ' && ch != '\t' && !Character.isWhitespace(ch)) {90break;91}92left++;93}94return left;95}969798private static String[] getBody(String[] inLines) {99int from = -1, to = -1;100for (int i = 0; i < inLines.length; i++) {101String line = inLines[i];102if (!line.isBlank()) {103if (from == -1) {104from = i;105}106to = i + 1;107}108}109return Arrays.copyOfRange(inLines, from, to);110}111112/*113* Report difference in result.114*/115static void report(String test, String message, String input, String output) {116System.err.println("Testing " + test + ": " + message);117System.err.println();118System.err.println("Input: length = " + input.length());119System.err.println("_".repeat(40));120System.err.print(input.replaceAll(" ", "."));121System.err.println("_".repeat(40));122System.err.println();123System.err.println("Output: length = " + output.length());124System.err.println("_".repeat(40));125System.err.print(output.replaceAll(" ", "."));126System.err.println("_".repeat(40));127throw new RuntimeException();128}129}130131132