|
7 | 7 | import java.nio.file.Path; |
8 | 8 | import java.nio.file.Paths; |
9 | 9 | import java.util.stream.Stream; |
| 10 | +import java.util.Optional; |
10 | 11 | import liquidjava.api.CommandLineLauncher; |
11 | 12 | import liquidjava.diagnostics.ErrorEmitter; |
12 | 13 |
|
@@ -51,16 +52,18 @@ public void testFile(final Path filePath) throws IOException { |
51 | 52 | fail(); |
52 | 53 | } else { |
53 | 54 | // NEW: Check if it's the *correct* error |
54 | | - String expectedError = getExpectedError(filePath); |
| 55 | + Optional<String> expectedError = getExpectedError(filePath); |
55 | 56 |
|
56 | 57 | // 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)) { |
| 58 | + if (expectedError.isPresent()) { |
| 59 | + String expected = expectedError.get(); |
| 60 | + String actualErrorTitle = errorEmitter.getTitleMessage(); |
| 61 | + |
| 62 | + if (actualErrorTitle == null || !actualErrorTitle.contains(expected)) { |
61 | 63 | 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 | + System.out.println(" Expected to contain: \"" + expected + "\""); |
| 65 | + System.out.println(" Actual: \"" |
| 66 | + + (actualErrorTitle != null ? actualErrorTitle : "NULL") + "\""); |
64 | 67 | fail(); |
65 | 68 | } |
66 | 69 | } |
@@ -113,30 +116,29 @@ public void testMultiplePaths() { |
113 | 116 | } |
114 | 117 |
|
115 | 118 | /** |
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 | + * Reads the given file to find an expected error message specified in a comment on the first line. The comment |
| 120 | + * format is: // @ExpectedError: "Error Title" |
119 | 121 | * |
120 | 122 | * @param filePath |
121 | 123 | * path to the test file |
122 | 124 | * |
123 | | - * @return The expected error title, or null if not specified. |
| 125 | + * @return An Optional containing the expected error string, or Optional.empty() if not specified or if it's a |
| 126 | + * directory. |
124 | 127 | * |
125 | 128 | * @throws IOException |
126 | 129 | * if an I/O error occurs |
127 | 130 | */ |
128 | | - private String getExpectedError(Path filePath) throws IOException { |
| 131 | + private Optional<String> getExpectedError(Path filePath) throws IOException { |
129 | 132 | if (Files.isDirectory(filePath)) { |
130 | | - // Currently, we don't support expected errors for entire directories. |
131 | | - return null; |
| 133 | + return Optional.empty(); |
132 | 134 | } |
133 | 135 |
|
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() |
| 136 | + // Try to find the expected error comment on the first line |
| 137 | + try (Stream<String> lines = Files.lines(filePath)) { |
| 138 | + return lines.findFirst() // Get only the first line |
| 139 | + .map(String::trim).filter(line -> line.startsWith("// @ExpectedError:")) |
137 | 140 | .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 |
| 141 | + .map(line -> line.replace("\"", "")); // Remove quotes |
140 | 142 | } |
141 | 143 | } |
142 | 144 | } |
0 commit comments