-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Ignore white-space when comparing text
In the case used, we compare to GraphQL schemas.
- Loading branch information
Showing
3 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
application/src/test/java/org/opentripplanner/_support/text/TextAssertions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.opentripplanner._support.text; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
|
||
/** | ||
* This class contains test assert methods not supported by the standard JUnit | ||
* framework. | ||
*/ | ||
public final class TextAssertions { | ||
|
||
private static final String LINE_DELIMITERS = "(\n|\r|\r\n)"; | ||
private static final int END_OF_TEXT = -111; | ||
|
||
/** | ||
* Assert to texts are equals line by line. Empty lines and white-space in the start and end of | ||
* a line is ignored. | ||
*/ | ||
public static void assertLinesEquals(String expected, String actual) { | ||
var expLines = expected.split(LINE_DELIMITERS); | ||
var actLines = actual.split(LINE_DELIMITERS); | ||
|
||
int i = -1; | ||
int j = -1; | ||
|
||
while (true) { | ||
i = next(expLines, i); | ||
j = next(actLines, j); | ||
|
||
if (i == END_OF_TEXT && j == END_OF_TEXT) { | ||
return; | ||
} | ||
|
||
var exp = getLine(expLines, i); | ||
var act = getLine(actLines, j); | ||
|
||
if (i == END_OF_TEXT || j == END_OF_TEXT || !exp.equals(act)) { | ||
Assertions.fail( | ||
"Expected%s: <%s>%n".formatted(lineText(i), exp) + | ||
"Actual %s: <%s>%n".formatted(lineText(j), act) | ||
); | ||
} | ||
} | ||
} | ||
|
||
private static String lineText(int index) { | ||
return index < 0 ? "(@end-of-text)" : "(@line %d)".formatted(index); | ||
} | ||
|
||
private static String getLine(String[] lines, int i) { | ||
return i == END_OF_TEXT ? "" : lines[i].trim(); | ||
} | ||
|
||
private static int next(String[] lines, int index) { | ||
++index; | ||
while (index < lines.length) { | ||
if (!lines[index].isBlank()) { | ||
return index; | ||
} | ||
++index; | ||
} | ||
return END_OF_TEXT; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
application/src/test/java/org/opentripplanner/_support/text/TextAssertionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.opentripplanner._support.text; | ||
|
||
import static org.opentripplanner._support.text.TextAssertions.assertLinesEquals; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class TextAssertionsTest { | ||
|
||
@Test | ||
void testIgnoreWhiteSpace() { | ||
// Empty text | ||
assertLinesEquals("", "\n\n"); | ||
|
||
// Text with white-space inserted | ||
assertLinesEquals( | ||
""" | ||
A Test | ||
Line 2 | ||
DOS\r\n | ||
line-shift | ||
""", | ||
""" | ||
A Test \t | ||
\t | ||
\tLine 2 | ||
DOS\rline-shift | ||
""" | ||
); | ||
} | ||
|
||
@Test | ||
void testEndOfText() { | ||
var ex = Assertions.assertThrows( | ||
org.opentest4j.AssertionFailedError.class, | ||
() -> assertLinesEquals("A\n", "A\nExtra Line") | ||
); | ||
Assertions.assertEquals( | ||
""" | ||
Expected(@end-of-text): <> | ||
Actual (@line 1): <Extra Line> | ||
""", | ||
ex.getMessage() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters