11package liquidjava .utils ;
22
3+ import java .io .BufferedReader ;
34import java .io .IOException ;
45import java .nio .file .Files ;
56import 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
910public 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