55import fi .helsinki .cs .tmc .langs .domain .SpecialLogs ;
66import fi .helsinki .cs .tmc .langs .domain .TestResult ;
77
8+ import java .util .Arrays ;
89import java .util .List ;
910
1011public class ResultPrinter {
1112
1213 private static final String COMPILE_ERROR_MESSAGE
1314 = Color .colorString ("Failed to compile project" , Color .AnsiColor .ANSI_PURPLE );
14- private static final String FAIL = Color .colorString ("Failed: " , Color .AnsiColor .ANSI_RED );
15- private static final String PASS = Color .colorString ("Passed: " , Color .AnsiColor .ANSI_GREEN );
16- private static final String TAB = " " ;
17- private static final char LF = '\n' ;
15+ private static final String FAIL_MESSAGE = "Failed: " ;
16+ private static final String PASS_MESSAGE = "Passed: " ;
17+ private final String tab ;
1818
1919 private final Io io ;
2020
@@ -27,6 +27,8 @@ public ResultPrinter(Io io, boolean showDetails, boolean showPassed) {
2727 this .io = io ;
2828 this .showDetails = showDetails ;
2929 this .showPassed = showPassed ;
30+
31+ this .tab = createPaddingString (PASS_MESSAGE .length ());
3032 }
3133
3234 public boolean isShowDetails () {
@@ -155,9 +157,9 @@ public static int passedTests(List<TestResult> testResults) {
155157 private void printTestResults (List <TestResult > testResults ) {
156158 for (TestResult testResult : testResults ) {
157159 if (!testResult .isSuccessful ()) {
158- io . println ( createFailMessage ( testResult ) );
160+ printFailMessage ( testResult );
159161 } else if (showPassed ) {
160- io . println ( createPassMessage ( testResult ) );
162+ printPassMessage ( testResult );
161163 }
162164 }
163165 io .println ("Test results: "
@@ -166,37 +168,45 @@ private void printTestResults(List<TestResult> testResults) {
166168
167169 }
168170
169- private String createFailMessage (TestResult testResult ) {
170- StringBuilder sb = new StringBuilder ( );
171- sb . append ( FAIL ). append ( testResult .getName ()). append ( LF );
172- sb . append ( TAB ). append ( testResult .getMessage ()). append ( LF );
171+ private void printFailMessage (TestResult testResult ) {
172+ io . print ( Color . colorString ( FAIL_MESSAGE , Color . AnsiColor . ANSI_RED ) );
173+ io . println ( testResult .getName ());
174+ io . println ( this . tab + testResult .getMessage ());
173175
174176 if (showDetails ) {
175- String details = listToString (testResult .getDetailedMessage (), LF );
177+ String details = listToString (testResult .getDetailedMessage ());
176178 if (details != null ) {
177- sb .append (LF ).append ("Detailed message:" ).append (LF ).append (details );
179+ io .println ("\n Detailed message:" );
180+ io .println (details );
178181 }
179- String exception = listToString (testResult .getException (), LF );
182+
183+ String exception = listToString (testResult .getException ());
180184 if (exception != null ) {
181- sb .append (LF ).append ("Exception:" ).append (LF ).append (exception );
185+ io .println ("\n Exception:" );
186+ io .println (exception );
182187 }
183188 }
184- return sb .toString ();
185189 }
186190
187- private String createPassMessage (TestResult testResult ) {
188- return PASS + testResult .getName () + LF ;
191+ private void printPassMessage (TestResult testResult ) {
192+ io .print (Color .colorString (PASS_MESSAGE , Color .AnsiColor .ANSI_GREEN ));
193+ io .println (testResult .getName ());
189194 }
190195
191- private String listToString (List <String > strings , char separator ) {
196+ private String listToString (List <String > strings ) {
192197 if (strings == null || strings .isEmpty ()) {
193198 return null ;
194199 }
195200 StringBuilder sb = new StringBuilder ();
196201 for (String string : strings ) {
197- sb .append (string ).append (separator );
202+ sb .append (string ).append (" \n " );
198203 }
199204 return sb .toString ();
200205 }
201206
202- }
207+ private String createPaddingString (int size ) {
208+ char [] charArray = new char [size ];
209+ Arrays .fill (charArray , ' ' );
210+ return new String (charArray );
211+ }
212+ }
0 commit comments