| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
package org.junit.jupiter.api; |
| 12 |
|
| 13 |
import static java.lang.String.format; |
| 14 |
import static java.lang.String.join; |
| 15 |
import static org.junit.jupiter.api.AssertionFailureBuilder.assertionFailure; |
| 16 |
import static org.junit.platform.commons.util.Preconditions.condition; |
| 17 |
import static org.junit.platform.commons.util.Preconditions.notNull; |
| 18 |
|
| 19 |
import java.util.ArrayDeque; |
| 20 |
import java.util.Deque; |
| 21 |
import java.util.List; |
| 22 |
import java.util.regex.PatternSyntaxException; |
| 23 |
import java.util.stream.Collectors; |
| 24 |
import java.util.stream.IntStream; |
| 25 |
import java.util.stream.Stream; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class AssertLinesMatch { |
| 34 |
|
| 35 |
private AssertLinesMatch() { |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
private static final int MAX_SNIPPET_LENGTH = 21; |
| 40 |
|
| 41 |
static void assertLinesMatch(List<String> expectedLines, List<String> actualLines) { |
| 42 |
assertLinesMatch(expectedLines, actualLines, (Object) null); |
| 43 |
} |
| 44 |
|
| 45 |
static void assertLinesMatch(List<String> expectedLines, List<String> actualLines, String message) { |
| 46 |
assertLinesMatch(expectedLines, actualLines, (Object) message); |
| 47 |
} |
| 48 |
|
| 49 |
static void assertLinesMatch(Stream<String> expectedLines, Stream<String> actualLines) { |
| 50 |
assertLinesMatch(expectedLines, actualLines, (Object) null); |
| 51 |
} |
| 52 |
|
| 53 |
static void assertLinesMatch(Stream<String> expectedLines, Stream<String> actualLines, String message) { |
| 54 |
assertLinesMatch(expectedLines, actualLines, (Object) message); |
| 55 |
} |
| 56 |
|
| 57 |
static void assertLinesMatch(Stream<String> expectedLines, Stream<String> actualLines, Object messageOrSupplier) { |
| 58 |
notNull(expectedLines, "expectedLines must not be null"); |
| 59 |
notNull(actualLines, "actualLines must not be null"); |
| 60 |
|
| 61 |
|
| 62 |
if (expectedLines == actualLines) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
List<String> expectedListOfStrings = expectedLines.collect(Collectors.toList()); |
| 67 |
List<String> actualListOfStrings = actualLines.collect(Collectors.toList()); |
| 68 |
assertLinesMatch(expectedListOfStrings, actualListOfStrings, messageOrSupplier); |
| 69 |
} |
| 70 |
|
| 71 |
static void assertLinesMatch(List<String> expectedLines, List<String> actualLines, Object messageOrSupplier) { |
| 72 |
notNull(expectedLines, "expectedLines must not be null"); |
| 73 |
notNull(actualLines, "actualLines must not be null"); |
| 74 |
|
| 75 |
|
| 76 |
if (expectedLines == actualLines) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
new LinesMatcher(expectedLines, actualLines, messageOrSupplier).assertLinesMatch(); |
| 81 |
} |
| 82 |
|
| 83 |
private static class LinesMatcher { |
| 84 |
|
| 85 |
private final List<String> expectedLines; |
| 86 |
private final List<String> actualLines; |
| 87 |
private final Object messageOrSupplier; |
| 88 |
|
| 89 |
LinesMatcher(List<String> expectedLines, List<String> actualLines, Object messageOrSupplier) { |
| 90 |
this.expectedLines = expectedLines; |
| 91 |
this.actualLines = actualLines; |
| 92 |
this.messageOrSupplier = messageOrSupplier; |
| 93 |
} |
| 94 |
|
| 95 |
void assertLinesMatch() { |
| 96 |
int expectedSize = expectedLines.size(); |
| 97 |
int actualSize = actualLines.size(); |
| 98 |
|
| 99 |
|
| 100 |
if (expectedSize > actualSize) { |
| 101 |
fail("expected %d lines, but only got %d", expectedSize, actualSize); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
if (expectedSize == actualSize) { |
| 106 |
if (IntStream.range(0, expectedSize).allMatch(i -> matches(expectedLines.get(i), actualLines.get(i)))) { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
assertLinesMatchWithFastForward(); |
| 113 |
} |
| 114 |
|
| 115 |
void assertLinesMatchWithFastForward() { |
| 116 |
Deque<String> expectedDeque = new ArrayDeque<>(expectedLines); |
| 117 |
Deque<String> actualDeque = new ArrayDeque<>(actualLines); |
| 118 |
|
| 119 |
main: while (!expectedDeque.isEmpty()) { |
| 120 |
String expectedLine = expectedDeque.pop(); |
| 121 |
int expectedLineNumber = expectedLines.size() - expectedDeque.size(); |
| 122 |
|
| 123 |
if (actualDeque.isEmpty()) { |
| 124 |
fail("expected line #%d:`%s` not found - actual lines depleted", expectedLineNumber, |
| 125 |
snippet(expectedLine)); |
| 126 |
} |
| 127 |
|
| 128 |
String actualLine = actualDeque.peek(); |
| 129 |
|
| 130 |
if (matches(expectedLine, actualLine)) { |
| 131 |
actualDeque.pop(); |
| 132 |
continue; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
if (isFastForwardLine(expectedLine)) { |
| 137 |
int fastForwardLimit = parseFastForwardLimit(expectedLine); |
| 138 |
int actualRemaining = actualDeque.size(); |
| 139 |
|
| 140 |
|
| 141 |
if (expectedDeque.isEmpty()) { |
| 142 |
|
| 143 |
if (fastForwardLimit == Integer.MAX_VALUE || fastForwardLimit == actualRemaining) { |
| 144 |
return; |
| 145 |
} |
| 146 |
fail("terminal fast-forward(%d) error: fast-forward(%d) expected", fastForwardLimit, |
| 147 |
actualRemaining); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
if (fastForwardLimit != Integer.MAX_VALUE) { |
| 152 |
if (actualRemaining < fastForwardLimit) { |
| 153 |
fail("fast-forward(%d) error: not enough actual lines remaining (%s)", fastForwardLimit, |
| 154 |
actualRemaining); |
| 155 |
} |
| 156 |
|
| 157 |
for (int i = 0; i < fastForwardLimit; i++) { |
| 158 |
actualDeque.pop(); |
| 159 |
} |
| 160 |
continue; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
expectedLine = expectedDeque.peek(); |
| 165 |
|
| 166 |
while (true) { |
| 167 |
if (actualDeque.isEmpty()) { |
| 168 |
fail("fast-forward(∞) didn't find: `%s`", snippet(expectedLine)); |
| 169 |
} |
| 170 |
if (matches(expectedLine, actualDeque.peek())) { |
| 171 |
continue main; |
| 172 |
} |
| 173 |
actualDeque.pop(); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
int actualLineNumber = actualLines.size() - actualDeque.size() + 1; |
| 178 |
fail("expected line #%d doesn't match actual line #%d%n" + "\texpected: `%s`%n" + "\t actual: `%s`", |
| 179 |
expectedLineNumber, actualLineNumber, expectedLine, actualLine); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
if (!actualDeque.isEmpty()) { |
| 184 |
fail("more actual lines than expected: %d", actualDeque.size()); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
String snippet(String line) { |
| 189 |
if (line.length() <= MAX_SNIPPET_LENGTH) { |
| 190 |
return line; |
| 191 |
} |
| 192 |
return line.substring(0, MAX_SNIPPET_LENGTH - 5) + "[...]"; |
| 193 |
} |
| 194 |
|
| 195 |
void fail(String format, Object... args) { |
| 196 |
String newLine = System.lineSeparator(); |
| 197 |
assertionFailure() |
| 198 |
.message(messageOrSupplier) |
| 199 |
.reason(format(format, args)) |
| 200 |
.expected(join(newLine, expectedLines)) |
| 201 |
.actual(join(newLine, actualLines)) |
| 202 |
.includeValuesInMessage(false) |
| 203 |
.buildAndThrow(); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
static boolean isFastForwardLine(String line) { |
| 208 |
line = line.trim(); |
| 209 |
return line.length() >= 4 && line.startsWith(">>") && line.endsWith(">>"); |
| 210 |
} |
| 211 |
|
| 212 |
static int parseFastForwardLimit(String fastForwardLine) { |
| 213 |
fastForwardLine = fastForwardLine.trim(); |
| 214 |
String text = fastForwardLine.substring(2, fastForwardLine.length() - 2).trim(); |
| 215 |
try { |
| 216 |
int limit = Integer.parseInt(text); |
| 217 |
condition(limit > 0, () -> format("fast-forward(%d) limit must be greater than zero", limit)); |
| 218 |
return limit; |
| 219 |
} |
| 220 |
catch (NumberFormatException e) { |
| 221 |
return Integer.MAX_VALUE; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
static boolean matches(String expectedLine, String actualLine) { |
| 226 |
notNull(expectedLine, "expected line must not be null"); |
| 227 |
notNull(actualLine, "actual line must not be null"); |
| 228 |
if (expectedLine.equals(actualLine)) { |
| 229 |
return true; |
| 230 |
} |
| 231 |
try { |
| 232 |
return actualLine.matches(expectedLine); |
| 233 |
} |
| 234 |
catch (PatternSyntaxException ignore) { |
| 235 |
return false; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
} |