From c183d5b97cc31724dbea8e2c44c9646c800f61a2 Mon Sep 17 00:00:00 2001 From: "David P. Baker" Date: Wed, 23 Aug 2023 14:13:39 -0400 Subject: [PATCH] Emit report lines for expected facts in the same order in which they appear in the input file. (#105) Fixes #103. --- .../conformance/AbstractConformanceTest.java | 25 +++++++++++++------ .../conformance/ConformanceTestReport.java | 10 +++++--- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/test/java/tests/conformance/AbstractConformanceTest.java b/src/test/java/tests/conformance/AbstractConformanceTest.java index f980e69..914d405 100644 --- a/src/test/java/tests/conformance/AbstractConformanceTest.java +++ b/src/test/java/tests/conformance/AbstractConformanceTest.java @@ -26,9 +26,10 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.ListIterator; +import java.util.Map; import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -122,19 +123,20 @@ private Stream analyzeFiles(List files) { private ImmutableList readExpectedFacts(Path file) { try { ImmutableList.Builder expectedFacts = ImmutableList.builder(); - List facts = new ArrayList<>(); + Map facts = new HashMap<>(); for (ListIterator i = readAllLines(testDirectory.resolve(file), UTF_8).listIterator(); i.hasNext(); ) { String line = i.next(); + long lineNumber = i.nextIndex(); Matcher matcher = EXPECTATION_COMMENT.matcher(line); String fact = matcher.matches() ? readExpectedFact(matcher.group("expectation")) : null; if (fact != null) { - facts.add(fact); + facts.put(lineNumber, fact); } else { - long lineNumber = i.nextIndex(); - facts.stream() - .map(expectedFact -> new ExpectedFact(file, lineNumber, expectedFact)) - .forEach(expectedFacts::add); + facts.forEach( + (factLineNumber, expectedFact) -> + expectedFacts.add( + new ExpectedFact(file, lineNumber, expectedFact, factLineNumber))); facts.clear(); } } @@ -302,10 +304,12 @@ public static boolean isNullnessMismatch(String fact) { } private final String fact; + private final long factLineNumber; - ExpectedFact(Path file, long lineNumber, String fact) { + ExpectedFact(Path file, long lineNumber, String fact, long factLineNumber) { super(file, lineNumber); this.fact = fact; + this.factLineNumber = factLineNumber; } @Override @@ -313,6 +317,11 @@ public String getFactText() { return fact; } + /** Returns the line number in the input file where the expected fact is. */ + public long getFactLineNumber() { + return factLineNumber; + } + @Override public boolean equals(Object o) { if (this == o) { diff --git a/src/test/java/tests/conformance/ConformanceTestReport.java b/src/test/java/tests/conformance/ConformanceTestReport.java index 2210c8d..98d749e 100644 --- a/src/test/java/tests/conformance/ConformanceTestReport.java +++ b/src/test/java/tests/conformance/ConformanceTestReport.java @@ -49,7 +49,6 @@ static ConformanceTestReport forFile( index(reportedFacts, ReportedFact::getLineNumber); ImmutableListMultimap matchingFacts = expectedFacts.stream() - .sorted(comparingLong(ExpectedFact::getLineNumber)) .collect( flatteningToImmutableListMultimap( f -> f, @@ -112,9 +111,12 @@ public String report(boolean details) { ImmutableSortedSet.copyOf( union(expectedFactsInFile.keySet(), reportedFactsInFile.keySet()))) { // Report all expected facts on this line and whether they're reported or not. - for (ExpectedFact expectedFact : expectedFactsInFile.get(lineNumber)) { - writeFact(report, expectedFact, matchesReportedFact(expectedFact) ? "PASS" : "FAIL"); - } + expectedFactsInFile.get(lineNumber).stream() + .sorted(comparingLong(ExpectedFact::getFactLineNumber)) + .forEach( + expectedFact -> + writeFact( + report, expectedFact, matchesReportedFact(expectedFact) ? "PASS" : "FAIL")); if (details) { // Report all unexpected facts on this line and whether they must be expected or not. for (ReportedFact reportedFact : reportedFactsInFile.get(lineNumber)) {