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 ;
16
22
import org .junit .platform .engine .TestExecutionResult ;
17
23
import org .junit .platform .engine .reporting .ReportEntry ;
18
24
import org .junit .platform .launcher .TestIdentifier ;
19
25
import org .junit .platform .launcher .TestPlan ;
26
+ import org .opentest4j .AssertionFailedError ;
27
+ import org .opentest4j .ValueWrapper ;
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,31 @@ 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
+ ValueWrapper expected = assertionFailedError .getExpected ();
101
+ ValueWrapper actual = assertionFailedError .getActual ();
102
+
103
+ if (isCharSequence (expected ) && isCharSequence (actual )) {
104
+ printlnMessage (style , "Expected " , expected .getStringRepresentation ());
105
+ printlnMessage (style , "Actual " , actual .getStringRepresentation ());
106
+ printlnMessage (style , "Diff " , calculateDiff (expected , actual ));
107
+ }
108
+ }
81
109
printlnMessage (style , "Exception" , ExceptionUtils .readStackTrace (throwable ));
82
110
}
83
111
112
+ private boolean isCharSequence (ValueWrapper value ) {
113
+ return value != null && CharSequence .class .isAssignableFrom (value .getType ());
114
+ }
115
+
116
+ private String calculateDiff (ValueWrapper expected , ValueWrapper actual ) {
117
+ List <String > expectedLines = Arrays .asList (expected .getStringRepresentation ());
118
+ List <String > actualLines = Arrays .asList (actual .getStringRepresentation ());
119
+ List <DiffRow > diffRows = diffRowGenerator .generateDiffRows (expectedLines , actualLines );
120
+ return diffRows .stream ().map (DiffRow ::getOldLine ).collect (Collectors .joining ("\n " ));
121
+ }
122
+
84
123
private void printlnMessage (Style style , String message , String detail ) {
85
124
println (style , INDENTATION + "=> " + message + ": %s" , indented (detail ));
86
125
}
0 commit comments