Skip to content

Commit 3d4bd2f

Browse files
committed
feat(tests): Check for specific error messages in test suite
Implements #105. The `TestExamples.java` test suite will now read a `// @expectederror: "Error Title"` comment from any "Error" file and fail the test if the error title does not match.
1 parent 1ca6aa2 commit 3d4bd2f

File tree

4 files changed

+68
-13
lines changed

4 files changed

+68
-13
lines changed

liquidjava-example/src/main/java/testSuite/ErrorAfterIf.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ExpectedError: "Type expected:(#r_26 == a || #r_26 == b)"
12
package testSuite;
23

34
import liquidjava.specification.Refinement;

liquidjava-example/src/main/java/testSuite/ErrorArithmeticBinaryOperations.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ExpectedError: "Type expected:(#z_2 > 0)"
12
package testSuite;
23

34
import liquidjava.specification.Refinement;

liquidjava-example/src/main/java/testSuite/ErrorSimpleAssignment.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ExpectedError: "Type expected:(#c_0 > 2)"
12
package testSuite;
23

34
import liquidjava.specification.Refinement;

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

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,51 @@ public class TestExamples {
2222
*
2323
* @param filePath
2424
* path to the file to test
25+
*
26+
* @throws IOException
27+
* if an I/O error occurs reading the test file
2528
*/
2629
@ParameterizedTest
2730
@MethodSource("fileNameSource")
28-
public void testFile(final Path filePath) {
31+
public void testFile(final Path filePath) throws IOException {
2932
String fileName = filePath.getFileName().toString();
33+
boolean isErrorFile = fileName.startsWith("Error") || fileName.contains("error");
34+
boolean isCorrectFile = fileName.startsWith("Correct") || fileName.contains("correct");
3035

31-
// 1. Run the verifier on the file or package
36+
// Run the verifier on the file or package
3237
ErrorEmitter errorEmitter = CommandLineLauncher.launch(filePath.toAbsolutePath().toString());
3338

34-
// 2. Check if the file is correct or contains an error
35-
if ((fileName.startsWith("Correct") && errorEmitter.foundError())
36-
|| (fileName.contains("correct") && errorEmitter.foundError())) {
37-
System.out.println("Error in directory: " + fileName + " --- should be correct but an error was found");
38-
fail();
39-
}
40-
// 3. Check if the file has an error but passed verification
41-
if ((fileName.startsWith("Error") && !errorEmitter.foundError())
42-
|| (fileName.contains("error") && !errorEmitter.foundError())) {
43-
System.out.println("Error in directory: " + fileName + " --- should be an error but passed verification");
44-
fail();
39+
if (isCorrectFile) {
40+
// A "Correct" file should NOT have an error
41+
if (errorEmitter.foundError()) {
42+
System.out.println("Error in directory: " + fileName + " --- should be correct but an error was found");
43+
System.out.println("Error: " + errorEmitter.getTitleMessage() + " - " + errorEmitter.getFullMessage());
44+
fail();
45+
}
46+
} else if (isErrorFile) {
47+
// An "Error" file SHOULD have an error
48+
if (!errorEmitter.foundError()) {
49+
System.out
50+
.println("Error in directory: " + fileName + " --- should be an error but passed verification");
51+
fail();
52+
} else {
53+
// NEW: Check if it's the *correct* error
54+
String expectedError = getExpectedError(filePath);
55+
56+
// If an expected error is specified in the file, check it.
57+
// We check the 'title' for a match.
58+
if (expectedError != null) {
59+
String actualErrorTitle = errorEmitter.getTitleMessage(); //
60+
if (actualErrorTitle == null || !actualErrorTitle.equals(expectedError)) {
61+
System.out.println("Error in directory: " + fileName + " --- wrong error message found.");
62+
System.out.println(" Expected: " + expectedError);
63+
System.out.println(" Actual: " + (actualErrorTitle != null ? actualErrorTitle : "NULL"));
64+
fail();
65+
}
66+
}
67+
}
4568
}
69+
// If it's neither "Correct" nor "Error", no assertions are made.
4670
}
4771

4872
/**
@@ -87,4 +111,32 @@ public void testMultiplePaths() {
87111
fail();
88112
}
89113
}
114+
115+
/**
116+
* Reads the given file to find an expected error message specified in a comment. The comment format is: //
117+
*
118+
* @ExpectedError: "Error Title"
119+
*
120+
* @param filePath
121+
* path to the test file
122+
*
123+
* @return The expected error title, or null if not specified.
124+
*
125+
* @throws IOException
126+
* if an I/O error occurs
127+
*/
128+
private String getExpectedError(Path filePath) throws IOException {
129+
if (Files.isDirectory(filePath)) {
130+
// Currently, we don't support expected errors for entire directories.
131+
return null;
132+
}
133+
134+
// Try to find the expected error comment in the first 10 lines
135+
try (Stream<String> lines = Files.lines(filePath).limit(10)) {
136+
return lines.map(String::trim).filter(line -> line.startsWith("// @ExpectedError:")).findFirst()
137+
.map(line -> line.substring(line.indexOf(":") + 1).trim()) // Get text after the colon
138+
.map(line -> line.replace("\"", "")) // Remove quotes
139+
.orElse(null); // No expected error specified
140+
}
141+
}
90142
}

0 commit comments

Comments
 (0)