Skip to content

Commit a71772e

Browse files
committed
Modified test utils to allow inline errors
Instead of using separate files or stating at the top of a class file the error that it should report, now it is read and all correctly defined errors are added to a list. It is then checked against the first error that occurred and reports back. Still only works with a single error reported, however.
1 parent 232cb9a commit a71772e

File tree

2 files changed

+51
-38
lines changed

2 files changed

+51
-38
lines changed

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.nio.file.Files;
88
import java.nio.file.Path;
99
import java.nio.file.Paths;
10+
import java.util.List;
1011
import java.util.Optional;
1112
import java.util.stream.Stream;
1213
import liquidjava.api.CommandLineLauncher;
@@ -43,41 +44,38 @@ public void testPath(final Path path) {
4344
+ diagnostics.getErrorOutput());
4445
fail();
4546
}
46-
// verification should fail, check if it failed as expected (we assume each error test has exactly one error)
47+
// verification should fail, check if it failed as expected (multiple errors can be found)
4748
else if (shouldFail(pathName)) {
4849
if (!diagnostics.foundError()) {
4950
System.out.println("Error in: " + pathName + " --- should fail but no errors were found. \n"
5051
+ diagnostics.getErrorOutput());
5152
fail();
5253
} else {
5354
// check if expected error was found
54-
Optional<String> expected = isDirectory ? getExpectedErrorFromDirectory(path)
55-
: getExpectedErrorFromFile(path);
56-
if (diagnostics.getErrors().size() > 1) {
57-
System.out.println("Multiple errors found in: " + pathName + " --- expected exactly one error. \n"
58-
+ diagnostics.getErrorOutput());
59-
System.out.println(diagnostics.getErrorOutput());
55+
List<String> expected = isDirectory ? getExpectedErrorsFromDirectory(path)
56+
: getExpectedErrorsFromFile(path);
57+
if (diagnostics.getErrors().size() > expected.size()) {
58+
System.out.println("Multiple errors found in: " + pathName + " --- expected exactly "
59+
+ expected.size() + " errors. \n" + diagnostics.getErrorOutput());
6060
fail();
6161
}
6262
LJError error = diagnostics.getErrors().iterator().next();
6363
if (error.getPosition() == null) {
6464
System.out.println("Error in: " + pathName + " --- error has no position information. \n"
6565
+ diagnostics.getErrorOutput());
66-
System.out.println(diagnostics.getErrorOutput());
6766
fail();
6867
}
69-
if (expected.isPresent()) {
70-
String expectedError = expected.get();
68+
if (!expected.isEmpty()) {
7169
String foundError = error.getTitle();
72-
if (!foundError.equalsIgnoreCase(expectedError)) {
73-
System.out.println("Error in: " + pathName + " --- expected error: " + expectedError
70+
if (!expected.contains(foundError)) {
71+
System.out.println("Error in: " + pathName + " --- expected errors: " + expected
7472
+ ", but found: " + foundError + ". \n" + diagnostics.getErrorOutput());
7573
fail();
7674
}
7775
} else {
78-
System.out.println("No expected error message found for: " + pathName);
79-
System.out.println("Please provide an expected error in " + (isDirectory
80-
? "a .expected file in the directory" : "the first line of the test file as a comment"));
76+
System.out.println("No expected error messages found for: " + pathName);
77+
System.out.println(
78+
"Please provide the expected errors in the test file as comments starting with // at the end of the supposed error line.");
8179
fail();
8280
}
8381
}
Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package liquidjava.utils;
22

3+
import java.io.BufferedReader;
34
import java.io.IOException;
45
import java.nio.file.Files;
56
import java.nio.file.Path;
6-
import java.util.Optional;
7-
import java.util.stream.Stream;
7+
import java.util.ArrayList;
8+
import java.util.List;
89

910
public class TestUtils {
1011

@@ -27,41 +28,55 @@ public static boolean shouldFail(String path) {
2728
}
2829

2930
/**
30-
* Reads the expected error message from the first line of the given test file
31+
* Reads the expected error messages from the given file by looking for a comment containing the expected error
32+
* message.
3133
*
3234
* @param filePath
3335
*
34-
* @return optional containing the expected error message if present, otherwise empty
36+
* @return list of expected error messages found in the file, or empty list if there was an error reading the file
37+
* or if there are no expected error messages in the file
3538
*/
36-
public static Optional<String> getExpectedErrorFromFile(Path filePath) {
37-
try (Stream<String> lines = Files.lines(filePath)) {
38-
Optional<String> first = lines.findFirst();
39-
if (first.isPresent() && first.get().startsWith("//")) {
40-
return Optional.of(first.get().substring(2).trim());
41-
} else {
42-
return Optional.empty();
39+
public static List<String> getExpectedErrorsFromFile(Path filePath) {
40+
List<String> expectedErrors = new ArrayList<>();
41+
try (BufferedReader reader = Files.newBufferedReader(filePath)) {
42+
String line;
43+
while ((line = reader.readLine()) != null) {
44+
int idx = line.indexOf("//");
45+
if (idx != -1 && line.substring(idx).contains("Error")) {
46+
// only expects the error type, NOT the actual refinement error message, depends on deterministic
47+
// variable names
48+
String comment = line.substring(idx + 2).trim();
49+
int dotIdx = comment.indexOf(":");
50+
if (dotIdx != -1) {
51+
comment = comment.substring(0, dotIdx).trim();
52+
}
53+
expectedErrors.add(comment);
54+
}
4355
}
44-
} catch (Exception e) {
45-
return Optional.empty();
56+
} catch (IOException e) {
57+
return List.of();
4658
}
59+
return expectedErrors;
4760
}
4861

4962
/**
50-
* Reads the expected error message from a .expected file in the given directory
63+
* Reads the expected error messages from all files in the given directory and combines them into a single list
5164
*
5265
* @param dirPath
5366
*
54-
* @return optional containing the expected error message if present, otherwise empty
67+
* @return list of expected error messages from all files in the directory, or empty list if there was an error
68+
* reading the directory or if there are no files in the directory
5569
*/
56-
public static Optional<String> getExpectedErrorFromDirectory(Path dirPath) {
57-
Path expectedFilePath = dirPath.resolve(".expected");
58-
if (Files.exists(expectedFilePath)) {
59-
try (Stream<String> lines = Files.lines(expectedFilePath)) {
60-
return lines.findFirst().map(String::trim);
61-
} catch (IOException e) {
62-
return Optional.empty();
70+
public static List<String> getExpectedErrorsFromDirectory(Path dirPath) {
71+
List<String> expectedErrors = new ArrayList<>();
72+
try {
73+
List<Path> files = Files.list(dirPath).filter(Files::isRegularFile).toList();
74+
for (Path file : files) {
75+
getExpectedErrorsFromFile(file).forEach(expectedErrors::add);
6376
}
77+
} catch (IOException e) {
78+
return List.of();
6479
}
65-
return Optional.empty();
80+
return expectedErrors;
6681
}
6782
}

0 commit comments

Comments
 (0)