Skip to content

Commit 734d496

Browse files
committed
Applied suggested fixes
Used existing Pairs, removing old code. Changed the way strings are matched via Regex.
1 parent 93dfe8f commit 734d496

File tree

3 files changed

+20
-30
lines changed

3 files changed

+20
-30
lines changed

liquidjava-example/src/main/java/testSuite/classes/overload_constructors_error/.expected

Lines changed: 0 additions & 1 deletion
This file was deleted.

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import liquidjava.diagnostics.Diagnostics;
1616

1717
import liquidjava.diagnostics.errors.LJError;
18-
import liquidjava.utils.TestUtils.Pair;
18+
import liquidjava.utils.Pair;
1919

2020
import org.junit.Test;
2121
import org.junit.jupiter.params.ParameterizedTest;
@@ -55,22 +55,22 @@ else if (shouldFail(pathName)) {
5555
fail();
5656
} else {
5757
// check if expected error was found
58-
List<Pair> expected = isDirectory ? getExpectedErrorsFromDirectory(path)
58+
List<Pair<String, Integer>> expectedErrors = isDirectory ? getExpectedErrorsFromDirectory(path)
5959
: getExpectedErrorsFromFile(path);
60-
if (diagnostics.getErrors().size() > expected.size()) {
60+
if (diagnostics.getErrors().size() != expectedErrors.size()) {
6161
System.out.println("Multiple errors found in: " + pathName + " --- expected exactly "
62-
+ expected.size() + " errors. \n" + diagnostics.getErrorOutput());
62+
+ expectedErrors.size() + " errors. \n" + diagnostics.getErrorOutput());
6363
fail();
6464
}
65-
if (!expected.isEmpty()) {
65+
if (!expectedErrors.isEmpty()) {
6666
for (LJError e : diagnostics.getErrors()) {
6767
String foundError = e.getTitle();
6868
int errorPosition = e.getPosition().getLine();
69-
boolean match = expected.stream().anyMatch(
70-
pair -> pair.errorMessage().equals(foundError) && pair.lineNumber() == errorPosition);
69+
boolean match = expectedErrors.stream().anyMatch(
70+
expected -> expected.first().equals(foundError) && expected.second() == errorPosition);
7171

7272
if (!match) {
73-
System.out.println("Error in: " + pathName + " --- expected errors: " + expected
73+
System.out.println("Error in: " + pathName + " --- expected errors: " + expectedErrors
7474
+ ", but found: " + foundError + " at " + errorPosition + ". \n"
7575
+ diagnostics.getErrorOutput());
7676
fail();
@@ -79,7 +79,7 @@ else if (shouldFail(pathName)) {
7979
} else {
8080
System.out.println("No expected error messages found for: " + pathName);
8181
System.out.println(
82-
"Please provide the expected errors in the test file as comments starting with // at the end of the supposed error line.");
82+
"Please specify each expected error in the test file as a comment on the line where the error should be reported.");
8383
fail();
8484
}
8585
}

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

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,10 @@
66
import java.nio.file.Path;
77
import java.util.ArrayList;
88
import java.util.List;
9+
import java.util.regex.Matcher;
10+
import java.util.regex.Pattern;
911

1012
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-
}
20-
2113
/**
2214
* Determines if the given path indicates that the test should pass
2315
*
@@ -45,23 +37,22 @@ public static boolean shouldFail(String path) {
4537
* @return list of expected error messages found in the file, or empty list if there was an error reading the file
4638
* or if there are no expected error messages in the file
4739
*/
48-
public static List<Pair> getExpectedErrorsFromFile(Path filePath) {
49-
List<Pair> expectedErrors = new ArrayList<>();
40+
public static List<Pair<String, Integer>> getExpectedErrorsFromFile(Path filePath) {
41+
List<Pair<String, Integer>> expectedErrors = new ArrayList<>();
5042
try (BufferedReader reader = Files.newBufferedReader(filePath)) {
5143
String line;
5244
int lineNumber = 0;
5345
while ((line = reader.readLine()) != null) {
5446
lineNumber++;
55-
int idx = line.indexOf("//");
56-
if (idx != -1 && line.substring(idx).contains("Error")) {
57-
// only expects the error type, NOT the actual refinement error message, depends on deterministic
58-
// variable names
59-
String comment = line.substring(idx + 2).trim();
47+
Pattern p = Pattern.compile("//\\s*(.*?\\bError\\b)", Pattern.CASE_INSENSITIVE);
48+
Matcher m = p.matcher(line);
49+
if (m.find()) {
50+
String comment = m.group(1).trim();
6051
int dotIdx = comment.indexOf(":");
6152
if (dotIdx != -1) {
6253
comment = comment.substring(0, dotIdx).trim();
6354
}
64-
expectedErrors.add(new Pair(comment, lineNumber));
55+
expectedErrors.add(new Pair<>(comment, lineNumber));
6556
}
6657
}
6758
} catch (IOException e) {
@@ -78,8 +69,8 @@ public static List<Pair> getExpectedErrorsFromFile(Path filePath) {
7869
* @return list of expected error messages from all files in the directory, or empty list if there was an error
7970
* reading the directory or if there are no files in the directory
8071
*/
81-
public static List<Pair> getExpectedErrorsFromDirectory(Path dirPath) {
82-
List<Pair> expectedErrors = new ArrayList<>();
72+
public static List<Pair<String, Integer>> getExpectedErrorsFromDirectory(Path dirPath) {
73+
List<Pair<String, Integer>> expectedErrors = new ArrayList<>();
8374
try {
8475
List<Path> files = Files.list(dirPath).filter(Files::isRegularFile).toList();
8576
for (Path file : files) {

0 commit comments

Comments
 (0)