Skip to content

Commit

Permalink
test: Ignore white-space when comparing text
Browse files Browse the repository at this point in the history
In the case used, we compare to GraphQL schemas.
  • Loading branch information
t2gran committed Jan 2, 2025
1 parent 90ac6d2 commit 4de6666
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package org.opentripplanner._support.text;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;


/**
* This class contains test assert methods not supported by the standard JUnit
* framework. F
*/
public final class TextAssertions {

public static final String LINE_DELIMITERS = "(\n|\r|\r\n)";
public 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;
}

@Test
void testIgnoreWhiteSpace() {
// Empty text
assertLinesEquals("", "\n\n");

// Text with white-space inserted
assertLinesEquals(
"""
A Test
Line 2
DOS
line-shift
""",
"""
A Test \t
\t
\tLine 2
DOS\r\nline-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()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.opentripplanner._support.text.TextAssertions;

/**
* This test read in a schema file, inject documentation and convert the
Expand Down Expand Up @@ -120,7 +121,8 @@ void test() {
var expected = List.of("BType.a.deprecated", "CType.b.deprecated.append");

assertEquals(expected, missingValues);
assertEquals(sdlExpected, result);

TextAssertions.assertLinesEquals(sdlExpected, result);
}

String loadSchemaResource(String suffix) throws IOException {
Expand Down

0 comments on commit 4de6666

Please sign in to comment.