Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit b31409f

Browse files
author
Aleksi Salmela
committed
Simplify the result printer little bit.
1 parent 54a7fc7 commit b31409f

1 file changed

Lines changed: 30 additions & 20 deletions

File tree

src/main/java/fi/helsinki/cs/tmc/cli/io/ResultPrinter.java

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
import fi.helsinki.cs.tmc.langs.domain.SpecialLogs;
66
import fi.helsinki.cs.tmc.langs.domain.TestResult;
77

8+
import java.util.Arrays;
89
import java.util.List;
910

1011
public 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() {
@@ -154,9 +156,9 @@ public static int passedTests(List<TestResult> testResults) {
154156
private void printTestResults(List<TestResult> testResults) {
155157
for (TestResult testResult : testResults) {
156158
if (!testResult.isSuccessful()) {
157-
io.println(createFailMessage(testResult));
159+
printFailMessage(testResult);
158160
} else if (showPassed) {
159-
io.println(createPassMessage(testResult));
161+
printPassMessage(testResult);
160162
}
161163
}
162164
io.println("Test results: "
@@ -165,37 +167,45 @@ private void printTestResults(List<TestResult> testResults) {
165167

166168
}
167169

168-
private String createFailMessage(TestResult testResult) {
169-
StringBuilder sb = new StringBuilder();
170-
sb.append(FAIL).append(testResult.getName()).append(LF);
171-
sb.append(TAB).append(testResult.getMessage()).append(LF);
170+
private void printFailMessage(TestResult testResult) {
171+
io.print(Color.colorString(FAIL_MESSAGE, Color.AnsiColor.ANSI_RED));
172+
io.println(testResult.getName());
173+
io.println(this.tab + testResult.getMessage());
172174

173175
if (showDetails) {
174-
String details = listToString(testResult.getDetailedMessage(), LF);
176+
String details = listToString(testResult.getDetailedMessage());
175177
if (details != null) {
176-
sb.append(LF).append("Detailed message:").append(LF).append(details);
178+
io.println("\nDetailed message:");
179+
io.println(details);
177180
}
178-
String exception = listToString(testResult.getException(), LF);
181+
182+
String exception = listToString(testResult.getException());
179183
if (exception != null) {
180-
sb.append(LF).append("Exception:").append(LF).append(exception);
184+
io.println("\nException:");
185+
io.println(exception);
181186
}
182187
}
183-
return sb.toString();
184188
}
185189

186-
private String createPassMessage(TestResult testResult) {
187-
return PASS + testResult.getName() + LF;
190+
private void printPassMessage(TestResult testResult) {
191+
io.print(Color.colorString(PASS_MESSAGE, Color.AnsiColor.ANSI_GREEN));
192+
io.println(testResult.getName());
188193
}
189194

190-
private String listToString(List<String> strings, char separator) {
195+
private String listToString(List<String> strings) {
191196
if (strings == null || strings.isEmpty()) {
192197
return null;
193198
}
194199
StringBuilder sb = new StringBuilder();
195200
for (String string : strings) {
196-
sb.append(string).append(separator);
201+
sb.append(string).append("\n");
197202
}
198203
return sb.toString();
199204
}
200205

201-
}
206+
private String createPaddingString(int size) {
207+
char[] charArray = new char[size];
208+
Arrays.fill(charArray, ' ');
209+
return new String(charArray);
210+
}
211+
}

0 commit comments

Comments
 (0)