88import java .nio .file .Files ;
99import java .nio .file .Path ;
1010import java .nio .file .Paths ;
11+ import java .util .ArrayList ;
1112import java .util .Collection ;
1213import java .util .List ;
1314import java .util .stream .Stream ;
@@ -40,32 +41,11 @@ public void testPath(final Path path) {
4041
4142 List <Pair <String , Integer >> expectedWarnings = isDirectory ? getExpectedWarningsFromDirectory (path )
4243 : getExpectedWarningsFromFile (path );
44+ List <Pair <String , Integer >> expectedErrors = isDirectory ? getExpectedErrorsFromDirectory (path )
45+ : getExpectedErrorsFromFile (path );
4346
44- if (shouldWarn (pathName )) {
45- checkExpectedDiagnostics (pathName , diagnostics .getWarnings (), expectedWarnings ,
46- diagnostics .getWarningOutput ());
47- }
48-
49- // verification should pass, check if any errors were found
50- if (shouldPass (pathName ) && diagnostics .foundError ()) {
51- System .out .println ("Error in: " + pathName + " --- should pass but an error was found. \n "
52- + diagnostics .getErrorOutput ());
53- fail ();
54- }
55- // verification should fail, check if it failed as expected (multiple errors can be found)
56- else if (shouldFail (pathName )) {
57- if (!diagnostics .foundError ()) {
58- System .out .println ("Error in: " + pathName + " --- should fail but no errors were found. \n "
59- + diagnostics .getErrorOutput ());
60- fail ();
61- } else {
62- // check if expected error was found
63- List <Pair <String , Integer >> expectedErrors = isDirectory ? getExpectedErrorsFromDirectory (path )
64- : getExpectedErrorsFromFile (path );
65- checkExpectedDiagnostics (pathName , diagnostics .getErrors (), expectedErrors ,
66- diagnostics .getErrorOutput ());
67- }
68- }
47+ checkExpectedDiagnostics (pathName , diagnostics .getErrors (), expectedErrors , diagnostics .getErrorOutput ());
48+ checkExpectedDiagnostics (pathName , diagnostics .getWarnings (), expectedWarnings , diagnostics .getWarningOutput ());
6949 }
7050
7151 /**
@@ -78,19 +58,21 @@ private static void checkExpectedDiagnostics(String pathName, Collection<? exten
7858 + expected .size () + ". \n " + output );
7959 fail ();
8060 }
81- if (expected .isEmpty ()) {
82- System .out .println ("No expected diagnostic messages found for: " + pathName );
83- System .out .println (
84- "Please specify each expected diagnostic in the test file as a comment on the line where it should be reported." );
85- fail ();
86- }
61+ List <Pair <String , Integer >> unmatched = new ArrayList <>(expected );
8762 for (LJDiagnostic diagnostic : found ) {
88- boolean match = expected .stream ().anyMatch (expectedDiagnostic -> matches (diagnostic , expectedDiagnostic ));
89- if (!match ) {
63+ int match = -1 ;
64+ for (int i = 0 ; i < unmatched .size (); i ++) {
65+ if (matches (diagnostic , unmatched .get (i ))) {
66+ match = i ;
67+ break ;
68+ }
69+ }
70+ if (match < 0 ) {
9071 System .out .println (
9172 "Unexpected diagnostic in: " + pathName + " --- expected: " + expected + ". \n " + output );
9273 fail ();
9374 }
75+ unmatched .remove (match );
9476 }
9577 }
9678
@@ -107,19 +89,16 @@ private static Stream<Path> sourcePaths() throws IOException {
10789 return Files .find (Paths .get ("../liquidjava-example/src/main/java/testSuite/" ), Integer .MAX_VALUE ,
10890 (filePath , fileAttr ) -> {
10991 String name = filePath .getFileName ().toString ();
110- // Files that start with "Correct", "Error" or "Warning"
111- boolean isFileStartingWithCorrectOrError = fileAttr .isRegularFile ()
112- && (shouldPass (name ) || shouldFail (name ) || shouldWarn (name ));
113-
114- // Directories that contain "correct", "error" or "warning"
115- boolean isDirectoryWithCorrectOrError = fileAttr .isDirectory ()
116- && (shouldPass (name ) || shouldFail (name ) || shouldWarn (name ));
117-
118- // Return true if either condition matches
119- return isFileStartingWithCorrectOrError || isDirectoryWithCorrectOrError ;
92+ return (fileAttr .isRegularFile () || fileAttr .isDirectory ()) && isTestPath (name );
12093 });
12194 }
12295
96+ private static boolean isTestPath (String path ) {
97+ String lowerCasePath = path .toLowerCase ();
98+ return lowerCasePath .contains ("correct" ) || lowerCasePath .contains ("error" )
99+ || lowerCasePath .contains ("warning" );
100+ }
101+
123102 /**
124103 * Verifies that multiple correct inputs can be processed together
125104 */
@@ -128,9 +107,10 @@ public void testMultiplePaths() {
128107 String [] paths = { "../liquidjava-example/src/main/java/testSuite/CorrectSimple.java" ,
129108 "../liquidjava-example/src/main/java/testSuite/classes/arraylist_correct" , };
130109 CommandLineLauncher .launch (paths );
131- // Check if any of the paths that should be correct found an error
132- if (diagnostics .foundError ()) {
133- System .out .println ("Error found in files that should be correct. \n " + diagnostics .getErrorOutput ());
110+ // The inputs have no expected diagnostics.
111+ if (diagnostics .foundError () || !diagnostics .getWarnings ().isEmpty ()) {
112+ System .out .println (
113+ "Unexpected diagnostic found. \n " + diagnostics .getErrorOutput () + diagnostics .getWarningOutput ());
134114 fail ();
135115 }
136116 }
0 commit comments