|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 Simeon Andreev and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * Simeon Andreev - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.debug.tests.console; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 17 | + |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.stream.Stream; |
| 20 | + |
| 21 | +import org.eclipse.ui.internal.console.ConsoleOutputLineWrap; |
| 22 | +import org.junit.jupiter.params.ParameterizedTest; |
| 23 | +import org.junit.jupiter.params.provider.Arguments; |
| 24 | +import org.junit.jupiter.params.provider.MethodSource; |
| 25 | + |
| 26 | +/** |
| 27 | + * Tests {@link ConsoleOutputLineWrap} handling chunks of input, breaking input |
| 28 | + * lines at a specific length limit. |
| 29 | + */ |
| 30 | +public class ConsoleOutputLineWrapTest { |
| 31 | + |
| 32 | + /** |
| 33 | + * Parameters of a test for {@link ConsoleOutputLineWrap}. |
| 34 | + * |
| 35 | + * @param limit the line length limit for the test |
| 36 | + * @param chunks how many times the {@code input} is repeated |
| 37 | + * @param repeat how many times {@code output} is repeated in the expected |
| 38 | + * output, line breaks are inserted between concatenated |
| 39 | + * {@code output} |
| 40 | + * @param input input string passed to the tested |
| 41 | + * {@link ConsoleOutputLineWrap} |
| 42 | + * @param output expected output, repeated {@code repeat} times |
| 43 | + * @param nl the newline character sequences |
| 44 | + */ |
| 45 | + record Parameters(int limit, int chunks, int repeat, String input, String output, String... nl) { |
| 46 | + } |
| 47 | + |
| 48 | + private static Parameters test(int limit, String input, String output, String... nl) { |
| 49 | + return test(limit, 1, 1, input, output, nl); |
| 50 | + } |
| 51 | + |
| 52 | + private static Parameters test(int limit, int chunks, String input, String output, String... nl) { |
| 53 | + return test(limit, chunks, chunks, input, output, nl); |
| 54 | + } |
| 55 | + |
| 56 | + private static Parameters test(int limit, int chunks, int repeat, String input, String output, String... nl) { |
| 57 | + return new Parameters(limit, chunks, repeat, input, output, nl); |
| 58 | + } |
| 59 | + |
| 60 | + private static final Parameters[] TESTS = { |
| 61 | + // Unix newlines |
| 62 | + test(4, "\n========", "\n====\n====", "\n"), |
| 63 | + |
| 64 | + test(10, "========" , "========" , "\n"), |
| 65 | + test(10, 10, 8, "========" , "==========", "\n"), |
| 66 | + test( 4, "========" , "====\n====", "\n"), |
| 67 | + test( 4, 10, "========" , "====\n====", "\n"), |
| 68 | + test( 4, "====\n====" , "====\n====", "\n"), |
| 69 | + test( 4, 10, "====\n====" , "====\n====", "\n"), |
| 70 | + |
| 71 | + test( 2, 10, "=======\n==" , "==\n==\n==\n=\n==", "\n"), |
| 72 | + test( 2, "====\n====" , "==\n==\n==\n==" , "\n"), |
| 73 | + test( 2, 10, "====\n====" , "==\n==\n==\n==" , "\n"), |
| 74 | + test( 2, "=========" , "==\n==\n==\n==\n=", "\n"), |
| 75 | + test( 2, "=======\n==" , "==\n==\n==\n=\n==", "\n"), |
| 76 | + test( 3, "=========" , "===\n===\n===" , "\n"), |
| 77 | + test( 3, 10, "=========" , "===\n===\n===" , "\n"), |
| 78 | + test( 3, "========\n=" , "===\n===\n==\n=" , "\n"), |
| 79 | + |
| 80 | + test( 2, "======\n======", "==\n==\n==\n==\n==\n==", "\n"), |
| 81 | + test( 2, 10, "======\n======", "==\n==\n==\n==\n==\n==", "\n"), |
| 82 | + |
| 83 | + test( 3, 3, 1, "========\n=" , "===\n===\n==\n===\n===\n===\n===\n===\n===\n=", "\n"), |
| 84 | + test( 4, 3, 1, "========\n=" , "====\n====\n====\n====\n=\n====\n====\n=\n=" , "\n"), |
| 85 | + |
| 86 | + // Windows newlines |
| 87 | + test(4, "\r\n========", "\r\n====\r\n====", "\r\n"), |
| 88 | + |
| 89 | + test(10, "========" , "========" , "\r\n"), |
| 90 | + test(10, 10, 8, "========" , "==========" , "\r\n"), |
| 91 | + test( 4, "========" , "====\r\n====", "\r\n"), |
| 92 | + test( 4, 10, "========" , "====\r\n====", "\r\n"), |
| 93 | + test( 4, "====\r\n====" , "====\r\n====", "\r\n"), |
| 94 | + test( 4, 10, "====\r\n====" , "====\r\n====", "\r\n"), |
| 95 | + |
| 96 | + test( 2, 10, "=======\r\n==" , "==\r\n==\r\n==\r\n=\r\n==", "\r\n"), |
| 97 | + test( 2, "====\r\n====" , "==\r\n==\r\n==\r\n==" , "\r\n"), |
| 98 | + test( 2, 10, "====\r\n====" , "==\r\n==\r\n==\r\n==" , "\r\n"), |
| 99 | + test( 2, "=========" , "==\r\n==\r\n==\r\n==\r\n=", "\r\n"), |
| 100 | + test( 2, "=======\r\n==" , "==\r\n==\r\n==\r\n=\r\n==", "\r\n"), |
| 101 | + test( 3, "=========" , "===\r\n===\r\n===" , "\r\n"), |
| 102 | + test( 3, 10, "=========" , "===\r\n===\r\n===" , "\r\n"), |
| 103 | + test( 3, "========\r\n=" , "===\r\n===\r\n==\r\n=" , "\r\n"), |
| 104 | + |
| 105 | + test( 2, "======\r\n======", "==\r\n==\r\n==\r\n==\r\n==\r\n==", "\r\n"), |
| 106 | + test( 2, 10, "======\r\n======", "==\r\n==\r\n==\r\n==\r\n==\r\n==", "\r\n"), |
| 107 | + |
| 108 | + test( 3, 3, 1, "========\r\n=" , "===\r\n===\r\n==\r\n===\r\n===\r\n===\r\n===\r\n===\r\n===\r\n=", "\r\n"), |
| 109 | + test( 4, 3, 1, "========\r\n=" , "====\r\n====\r\n====\r\n====\r\n=\r\n====\r\n====\r\n=\r\n=" , "\r\n"), |
| 110 | + |
| 111 | + // multiple newlines |
| 112 | + test( 3, 3, 1, "========\r\n=" , "===\r\n===\r\n==\r\n===\r\n===\r\n===\r\n===\r\n===\r\n===\r\n=", "\r\n", "\r", "\n"), |
| 113 | + test( 4, 3, 1, "========\r\n=" , "====\r\n====\r\n====\r\n====\r\n=\r\n====\r\n====\r\n=\r\n=" , "\r\n", "\r", "\n"), |
| 114 | + |
| 115 | + test( 3, 3, 1, "========\r\n=" , "===\r===\r==\r\n===\r===\r===\r\n===\r===\r===\r\n=", "\r", "\n", "\r\n"), |
| 116 | + test( 4, 3, 1, "========\r\n=" , "====\r====\r\n====\r====\r=\r\n====\r====\r=\r\n=" , "\r", "\n", "\r\n"), |
| 117 | + }; |
| 118 | + |
| 119 | + private static Stream<Arguments> tests() { |
| 120 | + return Arrays.stream(TESTS).map(Arguments::of); |
| 121 | + } |
| 122 | + |
| 123 | + @ParameterizedTest |
| 124 | + @MethodSource("tests") |
| 125 | + public void test(Parameters p) { |
| 126 | + ConsoleOutputLineWrap lineBreak = new ConsoleOutputLineWrap(p.limit, p.nl); |
| 127 | + StringBuilder c = new StringBuilder(); |
| 128 | + for (int i = 0; i < p.chunks; ++i) { |
| 129 | + StringBuilder s = new StringBuilder(p.input); |
| 130 | + CharSequence text = lineBreak.modify(s); |
| 131 | + c.append(text); |
| 132 | + } |
| 133 | + String expected = repeat(p.output, p.repeat, p.nl[0]); |
| 134 | + assertEquals(expected, c.toString()); |
| 135 | + } |
| 136 | + |
| 137 | + private static String repeat(String s, int r, String nl) { |
| 138 | + return (nl + s).repeat(r).substring(nl.length()); |
| 139 | + } |
| 140 | +} |
0 commit comments