11package liquidjava .api .tests ;
22
33import static liquidjava .utils .TestUtils .*;
4- import static org .junit .Assert .fail ;
54
65import java .io .IOException ;
76import java .nio .file .Files ;
87import java .nio .file .Path ;
98import java .nio .file .Paths ;
109import java .util .Optional ;
10+ import java .util .Set ;
11+ import java .util .stream .Collectors ;
1112import java .util .stream .Stream ;
13+
14+ import static org .junit .Assert .fail ;
15+
1216import liquidjava .api .CommandLineLauncher ;
17+ import liquidjava .api .Template ;
1318import liquidjava .diagnostics .Diagnostics ;
1419
1520import liquidjava .diagnostics .errors .LJError ;
21+
1622import org .junit .Test ;
1723import org .junit .jupiter .params .ParameterizedTest ;
1824import org .junit .jupiter .params .provider .MethodSource ;
@@ -39,40 +45,43 @@ public void testPath(final Path path) {
3945
4046 // verification should pass, check if any errors were found
4147 if (shouldPass (pathName ) && diagnostics .foundError ()) {
42- System .out .println ("Error in: " + pathName + " --- should pass but an error was found" );
43- fail ();
48+ LJError error = diagnostics .getErrors ().iterator ().next ();
49+ printFailureAtRight (pathName , "should pass but an error was found" );
50+ failCompact (formatShouldPassErrorMessage (pathName , error ));
4451 }
4552 // verification should fail, check if it failed as expected (we assume each error test has exactly one error)
4653 else if (shouldFail (pathName )) {
4754 if (!diagnostics .foundError ()) {
48- System . out . println ( "Error in: " + pathName + " --- should fail but no errors were found" );
49- fail ( );
55+ printFailureAtRight ( pathName , " should fail but no errors were found" );
56+ failCompact ( "Expected an error but none were found" );
5057 } else {
5158 // check if expected error was found
5259 Optional <String > expected = isDirectory ? getExpectedErrorFromDirectory (path )
5360 : getExpectedErrorFromFile (path );
5461 if (diagnostics .getErrors ().size () > 1 ) {
55- System .out .println ("Multiple errors found in: " + pathName + " --- expected exactly one error" );
56- fail ();
62+ Set <String > errorTitles = diagnostics .getErrors ().stream ().map (LJError ::getTitle )
63+ .collect (Collectors .toSet ());
64+ printFailureAtRight (pathName , "expected exactly one error, but found multiple: " + errorTitles );
65+ failCompact (errorTitles .toString ());
5766 }
5867 LJError error = diagnostics .getErrors ().iterator ().next ();
5968 if (error .getPosition () == null ) {
60- System . out . println ( "Error in: " + pathName + " --- error has no position information" );
61- fail ( );
69+ printFailureAtRight ( pathName , " error has no position information" );
70+ failCompact ( "Error has no position information" );
6271 }
6372 if (expected .isPresent ()) {
6473 String expectedError = expected .get ();
6574 String foundError = error .getTitle ();
6675 if (!foundError .equalsIgnoreCase (expectedError )) {
67- System . out . println ( "Error in: " + pathName + " --- expected error: " + expectedError
68- + ", but found: " + foundError );
69- fail ( );
76+ printFailureAtRight ( pathName ,
77+ "expected error: " + expectedError + ", but found: " + foundError );
78+ failCompact ( "Expected error: " + expectedError + ", but found: " + foundError );
7079 }
7180 } else {
72- System . out . println ( "No expected error message found for: " + pathName );
81+ printFailureAtRight ( pathName , "no expected error message found" );
7382 System .out .println ("Please provide an expected error in " + (isDirectory
7483 ? "a .expected file in the directory" : "the first line of the test file as a comment" ));
75- fail ( );
84+ failCompact ( "No expected error message provided for: " + pathName );
7685 }
7786 }
7887 }
@@ -115,8 +124,60 @@ public void testMultiplePaths() {
115124 CommandLineLauncher .launch (paths );
116125 // Check if any of the paths that should be correct found an error
117126 if (diagnostics .foundError ()) {
118- System .out .println ("Error found in files that should be correct" );
119- fail ();
127+ printFailureAtRight ("testMultiplePaths" , "error found in files that should be correct" );
128+ fail ("Error found in files that should be correct" );
129+ }
130+ }
131+
132+ private static void printFailureAtRight (String testName , String message ) {
133+ System .out .println (Template .formatFailureAtRight (testName , message ));
134+ }
135+
136+ private static void failCompact (String message ) {
137+ throw new CompactAssertionError (message );
138+ }
139+
140+ private static String formatShouldPassErrorMessage (String pathName , LJError error ) {
141+ String location ;
142+ if (error .getPosition () != null ) {
143+ location = pathName + ":" + error .getPosition ().lineStart () + ":" + error .getPosition ().colStart ();
144+ } else {
145+ location = pathName ;
146+ }
147+ StringBuilder message = new StringBuilder ();
148+ message .append ("Error found in file that should be correct: " ).append (location ).append ("\n " )
149+ .append (error .getTitle ()).append (error .getMessage ());
150+
151+ String snippet = error .getSnippet ();
152+ if (snippet != null && !snippet .isBlank ()) {
153+ message .append (System .lineSeparator ()).append (System .lineSeparator ()).append (snippet );
154+ }
155+
156+ return message .toString ();
157+ }
158+
159+ // Keep assertion failures concise by suppressing stack trace population.
160+ private static final class CompactAssertionError extends AssertionError {
161+ private final String compactMessage ;
162+
163+ CompactAssertionError (String message ) {
164+ super (message );
165+ this .compactMessage = message ;
166+ }
167+
168+ @ Override
169+ public String getMessage () {
170+ return compactMessage ;
171+ }
172+
173+ @ Override
174+ public String toString () {
175+ return compactMessage ;
176+ }
177+
178+ @ Override
179+ public Throwable fillInStackTrace () {
180+ return this ;
120181 }
121182 }
122183}
0 commit comments