Skip to content

Commit 46c131a

Browse files
committed
Improved test error tracking
Errors are now gathered with their line number and compared based on the message and position. Added a Pair record to
1 parent a71772e commit 46c131a

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

liquidjava-verifier/src/test/java/liquidjava/api/tests/TestExamples.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package liquidjava.api.tests;
22

33
import static liquidjava.utils.TestUtils.*;
4+
45
import static org.junit.Assert.fail;
56

67
import java.io.IOException;
@@ -10,14 +11,19 @@
1011
import java.util.List;
1112
import java.util.Optional;
1213
import java.util.stream.Stream;
14+
1315
import liquidjava.api.CommandLineLauncher;
1416
import liquidjava.diagnostics.Diagnostics;
1517

1618
import liquidjava.diagnostics.errors.LJError;
19+
import liquidjava.utils.TestUtils.Pair;
20+
1721
import org.junit.Test;
1822
import org.junit.jupiter.params.ParameterizedTest;
1923
import org.junit.jupiter.params.provider.MethodSource;
2024

25+
import liquidjava.diagnostics.ErrorPosition;
26+
2127
public class TestExamples {
2228

2329
Diagnostics diagnostics = Diagnostics.getInstance();
@@ -52,25 +58,26 @@ else if (shouldFail(pathName)) {
5258
fail();
5359
} else {
5460
// check if expected error was found
55-
List<String> expected = isDirectory ? getExpectedErrorsFromDirectory(path)
61+
List<Pair> expected = isDirectory ? getExpectedErrorsFromDirectory(path)
5662
: getExpectedErrorsFromFile(path);
5763
if (diagnostics.getErrors().size() > expected.size()) {
5864
System.out.println("Multiple errors found in: " + pathName + " --- expected exactly "
5965
+ expected.size() + " errors. \n" + diagnostics.getErrorOutput());
6066
fail();
6167
}
62-
LJError error = diagnostics.getErrors().iterator().next();
63-
if (error.getPosition() == null) {
64-
System.out.println("Error in: " + pathName + " --- error has no position information. \n"
65-
+ diagnostics.getErrorOutput());
66-
fail();
67-
}
6868
if (!expected.isEmpty()) {
69-
String foundError = error.getTitle();
70-
if (!expected.contains(foundError)) {
71-
System.out.println("Error in: " + pathName + " --- expected errors: " + expected
72-
+ ", but found: " + foundError + ". \n" + diagnostics.getErrorOutput());
73-
fail();
69+
for (LJError e : diagnostics.getErrors()) {
70+
String foundError = e.getTitle();
71+
int errorPosition = e.getPosition().lineStart();
72+
boolean match = expected.stream().anyMatch(
73+
pair -> pair.errorMessage().equals(foundError) && pair.lineNumber() == errorPosition);
74+
75+
if (!match) {
76+
System.out.println("Error in: " + pathName + " --- expected errors: " + expected
77+
+ ", but found: " + foundError + " at " + errorPosition + ". \n"
78+
+ diagnostics.getErrorOutput());
79+
fail();
80+
}
7481
}
7582
} else {
7683
System.out.println("No expected error messages found for: " + pathName);

liquidjava-verifier/src/test/java/liquidjava/utils/TestUtils.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@
88
import java.util.List;
99

1010
public class TestUtils {
11+
/*
12+
* A simple record to store an error message and its line number, used for expected errors read from test files
13+
*/
14+
public record Pair(String errorMessage, int lineNumber) {
15+
@Override
16+
public String toString() {
17+
return errorMessage + " at " + lineNumber;
18+
};
19+
}
1120

1221
/**
1322
* Determines if the given path indicates that the test should pass
@@ -36,11 +45,13 @@ public static boolean shouldFail(String path) {
3645
* @return list of expected error messages found in the file, or empty list if there was an error reading the file
3746
* or if there are no expected error messages in the file
3847
*/
39-
public static List<String> getExpectedErrorsFromFile(Path filePath) {
40-
List<String> expectedErrors = new ArrayList<>();
48+
public static List<Pair> getExpectedErrorsFromFile(Path filePath) {
49+
List<Pair> expectedErrors = new ArrayList<>();
4150
try (BufferedReader reader = Files.newBufferedReader(filePath)) {
4251
String line;
52+
int lineNumber = 0;
4353
while ((line = reader.readLine()) != null) {
54+
lineNumber++;
4455
int idx = line.indexOf("//");
4556
if (idx != -1 && line.substring(idx).contains("Error")) {
4657
// only expects the error type, NOT the actual refinement error message, depends on deterministic
@@ -50,7 +61,7 @@ public static List<String> getExpectedErrorsFromFile(Path filePath) {
5061
if (dotIdx != -1) {
5162
comment = comment.substring(0, dotIdx).trim();
5263
}
53-
expectedErrors.add(comment);
64+
expectedErrors.add(new Pair(comment, lineNumber));
5465
}
5566
}
5667
} catch (IOException e) {
@@ -67,8 +78,8 @@ public static List<String> getExpectedErrorsFromFile(Path filePath) {
6778
* @return list of expected error messages from all files in the directory, or empty list if there was an error
6879
* reading the directory or if there are no files in the directory
6980
*/
70-
public static List<String> getExpectedErrorsFromDirectory(Path dirPath) {
71-
List<String> expectedErrors = new ArrayList<>();
81+
public static List<Pair> getExpectedErrorsFromDirectory(Path dirPath) {
82+
List<Pair> expectedErrors = new ArrayList<>();
7283
try {
7384
List<Path> files = Files.list(dirPath).filter(Files::isRegularFile).toList();
7485
for (Path file : files) {

0 commit comments

Comments
 (0)