Skip to content

Commit

Permalink
Emit report lines for expected facts in the same order in which they …
Browse files Browse the repository at this point in the history
…appear in the input file. (#105)

Fixes #103.
  • Loading branch information
netdpb authored Aug 23, 2023
1 parent 9e6590f commit c183d5b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
25 changes: 17 additions & 8 deletions src/test/java/tests/conformance/AbstractConformanceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -122,19 +123,20 @@ private Stream<ConformanceTestReport> analyzeFiles(List<Path> files) {
private ImmutableList<ExpectedFact> readExpectedFacts(Path file) {
try {
ImmutableList.Builder<ExpectedFact> expectedFacts = ImmutableList.builder();
List<String> facts = new ArrayList<>();
Map<Long, String> facts = new HashMap<>();
for (ListIterator<String> 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();
}
}
Expand Down Expand Up @@ -302,17 +304,24 @@ 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
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) {
Expand Down
10 changes: 6 additions & 4 deletions src/test/java/tests/conformance/ConformanceTestReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ static ConformanceTestReport forFile(
index(reportedFacts, ReportedFact::getLineNumber);
ImmutableListMultimap<ExpectedFact, ReportedFact> matchingFacts =
expectedFacts.stream()
.sorted(comparingLong(ExpectedFact::getLineNumber))
.collect(
flatteningToImmutableListMultimap(
f -> f,
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit c183d5b

Please sign in to comment.