11
11
package org .junit .platform .console .tasks ;
12
12
13
13
import java .io .PrintWriter ;
14
+ import java .util .Arrays ;
15
+ import java .util .List ;
16
+ import java .util .stream .Collectors ;
17
+
18
+ import com .github .difflib .text .DiffRow ;
19
+ import com .github .difflib .text .DiffRowGenerator ;
14
20
15
21
import org .junit .platform .commons .util .ExceptionUtils ;
22
+ import org .junit .platform .commons .util .StringUtils ;
16
23
import org .junit .platform .engine .TestExecutionResult ;
17
24
import org .junit .platform .engine .reporting .ReportEntry ;
18
25
import org .junit .platform .launcher .TestIdentifier ;
19
26
import org .junit .platform .launcher .TestPlan ;
27
+ import org .opentest4j .AssertionFailedError ;
20
28
21
29
/**
22
30
* @since 1.0
@@ -27,10 +35,19 @@ class FlatPrintingListener implements DetailsPrintingListener {
27
35
28
36
private final PrintWriter out ;
29
37
private final ColorPalette colorPalette ;
38
+ private final DiffRowGenerator diffRowGenerator ;
30
39
31
40
FlatPrintingListener (PrintWriter out , ColorPalette colorPalette ) {
32
41
this .out = out ;
33
42
this .colorPalette = colorPalette ;
43
+ this .diffRowGenerator = DiffRowGenerator .create () //
44
+ .showInlineDiffs (true ) //
45
+ .mergeOriginalRevised (true ) //
46
+ .inlineDiffByWord (true ) //
47
+ .oldTag (f -> "~" ) //
48
+ .newTag (f -> "**" ) //
49
+ .build ();
50
+ ;
34
51
}
35
52
36
53
@ Override
@@ -78,9 +95,26 @@ private void printlnTestDescriptor(Style style, String message, TestIdentifier t
78
95
}
79
96
80
97
private void printlnException (Style style , Throwable throwable ) {
98
+ if (throwable instanceof AssertionFailedError ) {
99
+ AssertionFailedError assertionFailedError = (AssertionFailedError ) throwable ;
100
+ String expected = assertionFailedError .getExpected ().getStringRepresentation ();
101
+ String actual = assertionFailedError .getActual ().getStringRepresentation ();
102
+ String diff = calculateDiff (expected , actual );
103
+
104
+ printlnMessage (style , "Expected " , expected );
105
+ printlnMessage (style , "Actual " , actual );
106
+ printlnMessage (style , "Diff " , diff );
107
+ }
81
108
printlnMessage (style , "Exception" , ExceptionUtils .readStackTrace (throwable ));
82
109
}
83
110
111
+ private String calculateDiff (String expected , String actual ) {
112
+ List <String > expectedLines = Arrays .asList (StringUtils .nullSafeToString (expected ));
113
+ List <String > actualLines = Arrays .asList (StringUtils .nullSafeToString (actual ));
114
+ List <DiffRow > diffRows = diffRowGenerator .generateDiffRows (expectedLines , actualLines );
115
+ return diffRows .stream ().map (DiffRow ::getOldLine ).collect (Collectors .joining ("\n " ));
116
+ }
117
+
84
118
private void printlnMessage (Style style , String message , String detail ) {
85
119
println (style , INDENTATION + "=> " + message + ": %s" , indented (detail ));
86
120
}
0 commit comments