1616
1717public class TestUtils {
1818
19+ private static final Pattern EXPECTED_DIAGNOSTIC = Pattern .compile ("//\\ s*(.*?\\ b(Error|Warning)\\ b)" ,
20+ Pattern .CASE_INSENSITIVE );
1921 private final static Factory factory = new Launcher ().getFactory ();
2022 private final static Context context = Context .getInstance ();
2123
@@ -56,22 +58,25 @@ public static boolean shouldWarn(String path) {
5658 * or if there are no expected error messages in the file
5759 */
5860 public static List <Pair <String , Integer >> getExpectedErrorsFromFile (Path filePath ) {
59- List <Pair <String , Integer >> expectedErrors = new ArrayList <>();
61+ return getExpectedDiagnosticsFromFile (filePath , "error" );
62+ }
63+
64+ private static List <Pair <String , Integer >> getExpectedDiagnosticsFromFile (Path filePath , String type ) {
65+ List <Pair <String , Integer >> expectedDiagnostics = new ArrayList <>();
6066 try (BufferedReader reader = Files .newBufferedReader (filePath )) {
6167 String line ;
6268 int lineNumber = 0 ;
6369 while ((line = reader .readLine ()) != null ) {
6470 lineNumber ++;
65- Pattern p = Pattern .compile ("//\\ s*(.*?\\ bError\\ b)" , Pattern .CASE_INSENSITIVE );
66- Matcher m = p .matcher (line );
67- if (m .find ()) {
68- expectedErrors .add (new Pair <>(m .group (1 ).trim (), lineNumber ));
71+ Matcher matcher = EXPECTED_DIAGNOSTIC .matcher (line );
72+ if (matcher .find () && matcher .group (2 ).equalsIgnoreCase (type )) {
73+ expectedDiagnostics .add (new Pair <>(matcher .group (1 ).trim (), lineNumber ));
6974 }
7075 }
7176 } catch (IOException e ) {
7277 return List .of ();
7378 }
74- return expectedErrors ;
79+ return expectedDiagnostics ;
7580 }
7681
7782 /**
@@ -84,22 +89,7 @@ public static List<Pair<String, Integer>> getExpectedErrorsFromFile(Path filePat
8489 * or if there are no expected warning messages in the file
8590 */
8691 public static List <Pair <String , Integer >> getExpectedWarningsFromFile (Path filePath ) {
87- List <Pair <String , Integer >> expectedWarnings = new ArrayList <>();
88- try (BufferedReader reader = Files .newBufferedReader (filePath )) {
89- String line ;
90- int lineNumber = 0 ;
91- while ((line = reader .readLine ()) != null ) {
92- lineNumber ++;
93- Pattern p = Pattern .compile ("//\\ s*(.*?\\ bWarning\\ b)" , Pattern .CASE_INSENSITIVE );
94- Matcher m = p .matcher (line );
95- if (m .find ()) {
96- expectedWarnings .add (new Pair <>(m .group (1 ).trim (), lineNumber ));
97- }
98- }
99- } catch (IOException e ) {
100- return List .of ();
101- }
102- return expectedWarnings ;
92+ return getExpectedDiagnosticsFromFile (filePath , "warning" );
10393 }
10494
10595 /**
@@ -111,16 +101,7 @@ public static List<Pair<String, Integer>> getExpectedWarningsFromFile(Path fileP
111101 * reading the directory or if there are no files in the directory
112102 */
113103 public static List <Pair <String , Integer >> getExpectedErrorsFromDirectory (Path dirPath ) {
114- List <Pair <String , Integer >> expectedErrors = new ArrayList <>();
115- try {
116- List <Path > files = Files .list (dirPath ).filter (Files ::isRegularFile ).toList ();
117- for (Path file : files ) {
118- expectedErrors .addAll (getExpectedErrorsFromFile (file ));
119- }
120- } catch (IOException e ) {
121- return List .of ();
122- }
123- return expectedErrors ;
104+ return getExpectedDiagnosticsFromDirectory (dirPath , "error" );
124105 }
125106
126107 /**
@@ -132,16 +113,20 @@ public static List<Pair<String, Integer>> getExpectedErrorsFromDirectory(Path di
132113 * reading the directory or if there are no files in the directory
133114 */
134115 public static List <Pair <String , Integer >> getExpectedWarningsFromDirectory (Path dirPath ) {
135- List <Pair <String , Integer >> expectedWarnings = new ArrayList <>();
116+ return getExpectedDiagnosticsFromDirectory (dirPath , "warning" );
117+ }
118+
119+ private static List <Pair <String , Integer >> getExpectedDiagnosticsFromDirectory (Path dirPath , String type ) {
120+ List <Pair <String , Integer >> expectedDiagnostics = new ArrayList <>();
136121 try {
137122 List <Path > files = Files .list (dirPath ).filter (Files ::isRegularFile ).toList ();
138123 for (Path file : files ) {
139- expectedWarnings .addAll (getExpectedWarningsFromFile (file ));
124+ expectedDiagnostics .addAll (getExpectedDiagnosticsFromFile (file , type ));
140125 }
141126 } catch (IOException e ) {
142127 return List .of ();
143128 }
144- return expectedWarnings ;
129+ return expectedDiagnostics ;
145130 }
146131
147132 /**
0 commit comments