-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
AssertLinesMatch.java
161 lines (139 loc) · 4.98 KB
/
AssertLinesMatch.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright 2015-2017 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.junit.jupiter.api;
import static java.lang.String.format;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.platform.commons.util.Preconditions.condition;
import static org.junit.platform.commons.util.Preconditions.notNull;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.List;
import java.util.regex.PatternSyntaxException;
/**
* {@code AssertLinesMatch} is a collection of utility methods that support asserting
* lines of {@link String} equality or {@link java.util.regex.Pattern}-match in tests.
*
* @since 5.0
*/
class AssertLinesMatch {
static void assertLinesMatch(List<String> expectedLines, List<String> actualLines) {
notNull(expectedLines, "expectedLines must not be null");
notNull(actualLines, "actualLines must not be null");
// trivial case: same list instance
if (expectedLines == actualLines) {
return;
}
int expectedSize = expectedLines.size();
int actualSize = actualLines.size();
// trivial case: when expecting more than actual lines available, something is wrong
if (expectedSize > actualSize) {
// use standard assertEquals(Object, Object, message) to let IDEs present the textual difference
String expected = String.join(System.lineSeparator(), expectedLines);
String actual = String.join(System.lineSeparator(), actualLines);
assertEquals(expected, actual, "expected " + expectedSize + " lines, but only got " + actualSize);
fail("should not happen as expected != actual was asserted");
}
// simple case: both list are equally sized, compare them line-by-line
if (expectedSize == actualSize) {
boolean allOk = true;
for (int i = 0; i < expectedSize; i++) {
if (matches(expectedLines.get(i), actualLines.get(i))) {
continue;
}
allOk = false;
break;
}
if (allOk) {
return;
}
}
assertLinesMatchWithFastForward(expectedLines, actualLines);
}
private static void assertLinesMatchWithFastForward(List<String> expectedLines, List<String> actualLines) {
Deque<String> expectedDeque = new ArrayDeque<>(expectedLines);
Deque<String> actualDeque = new ArrayDeque<>(actualLines);
while (!expectedDeque.isEmpty()) {
String expectedLine = expectedDeque.pop();
String actualLine = actualDeque.peek();
// trivial case: take the fast path when they simply match
if (matches(expectedLine, actualLine)) {
actualDeque.pop();
continue;
}
// fast-forward marker found in expected line: fast-forward actual line...
if (isFastForwardLine(expectedLine)) {
int fastForwardLimit = parseFastForwardLimit(expectedLine);
// trivial case: fast-forward marker was in last expected line
if (expectedDeque.isEmpty()) {
int actualRemaining = actualDeque.size();
// no limit given or perfect match? we're done.
if (fastForwardLimit == Integer.MAX_VALUE || fastForwardLimit == actualRemaining) {
return;
}
fail(format("terminal fast-forward(%d) error: fast-forward(%d) expected", fastForwardLimit,
actualRemaining));
}
// peek next expected line
expectedLine = expectedDeque.peek();
// fast-forward limit was given: use it
if (fastForwardLimit != Integer.MAX_VALUE) {
// fast-forward now: actualDeque.pop(fastForwardLimit)
for (int i = 0; i < fastForwardLimit; i++) {
actualDeque.pop();
}
if (actualDeque.isEmpty()) {
fail(format("%d more lines expected, actual lines is empty", expectedDeque.size()));
}
continue;
}
// fast-forward "unlimited": until next match
while (true) {
if (actualDeque.isEmpty()) {
fail(format("no match for `%s` line fast-forwarding: %n%s", expectedLine, actualLines));
}
if (matches(expectedLine, actualDeque.pop())) {
break;
}
}
}
}
// after math
if (!actualDeque.isEmpty()) {
fail("more actual lines than expected: " + actualDeque.size());
}
}
static boolean isFastForwardLine(String line) {
line = line.trim();
return line.length() >= 4 && line.startsWith(">>") && line.endsWith(">>");
}
static int parseFastForwardLimit(String fastForwardLine) {
String text = fastForwardLine.trim().substring(2, fastForwardLine.length() - 2).trim();
try {
int limit = Integer.parseInt(text);
condition(limit > 0, "fast-forward limit must be greater than zero, it is: " + limit);
return limit;
}
catch (NumberFormatException e) {
return Integer.MAX_VALUE;
}
}
static boolean matches(String expectedLine, String actualLine) {
if (expectedLine.equals(actualLine)) {
return true;
}
try {
return actualLine.matches(expectedLine);
}
catch (PatternSyntaxException ignore) {
return false;
}
}
}