Skip to content
This repository was archived by the owner on Sep 9, 2024. It is now read-only.

Commit 50ef39c

Browse files
committed
feat: Add ResultPrinter implementation
1 parent 3ba66cf commit 50ef39c

2 files changed

Lines changed: 99 additions & 8 deletions

File tree

contractcase/src/main/java/io/contracttesting/contractcase/LogPrinter.java

Lines changed: 84 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,26 @@
22

33
import com.diogonunes.jcolor.AnsiFormat;
44
import com.diogonunes.jcolor.Attribute;
5-
import io.contract_testing.contractcase.case_boundary.BoundaryFailure;
65
import io.contract_testing.contractcase.case_boundary.BoundaryResult;
76
import io.contract_testing.contractcase.case_boundary.BoundarySuccess;
87
import io.contract_testing.contractcase.case_boundary.ILogPrinter;
9-
import java.util.Arrays;
10-
import java.util.stream.Collectors;
8+
import io.contract_testing.contractcase.case_boundary.IResultPrinter;
9+
import io.contract_testing.contractcase.case_boundary.PrintableMatchError;
10+
import io.contract_testing.contractcase.case_boundary.PrintableMessageError;
11+
import io.contract_testing.contractcase.case_boundary.PrintableTestTitle;
12+
import io.contracttesting.contractcase.connectors.ExceptionMapper;
1113
import org.jetbrains.annotations.NotNull;
1214

13-
public class LogPrinter implements ILogPrinter {
15+
public class LogPrinter implements ILogPrinter, IResultPrinter {
1416

1517
private static final AnsiFormat brightRed = new AnsiFormat(Attribute.BRIGHT_RED_TEXT());
1618
private static final AnsiFormat brightYellow = new AnsiFormat(Attribute.BRIGHT_YELLOW_TEXT());
1719

20+
private static final AnsiFormat redBack = new AnsiFormat(Attribute.RED_BACK(),
21+
Attribute.WHITE_TEXT());
22+
23+
private static final AnsiFormat red = new AnsiFormat(Attribute.RED_TEXT());
24+
1825
private static final AnsiFormat cyan = new AnsiFormat(Attribute.CYAN_TEXT());
1926

2027
private static final AnsiFormat magentaBackground = new AnsiFormat(Attribute.MAGENTA_BACK(),
@@ -31,8 +38,9 @@ public class LogPrinter implements ILogPrinter {
3138
private static final AnsiFormat blueBack = new AnsiFormat(Attribute.BRIGHT_BLUE_BACK(),
3239
Attribute.BLACK_TEXT());
3340

34-
@Override
41+
private static final AnsiFormat green = new AnsiFormat(Attribute.GREEN_TEXT());
3542

43+
@Override
3644
public @NotNull BoundaryResult log(@NotNull String level, @NotNull String timestamp,
3745
@NotNull String version, @NotNull String typeString, @NotNull String location,
3846
@NotNull String message, @NotNull String additional) {
@@ -73,9 +81,77 @@ public class LogPrinter implements ILogPrinter {
7381
? "" : "\n" + messageColour.format(additional)));
7482
return new BoundarySuccess();
7583
} catch (Exception e) {
76-
return new BoundaryFailure(e.getClass().getName(), e.getMessage(),
77-
Arrays.stream(e.getStackTrace()).map(StackTraceElement::toString).collect(
78-
Collectors.joining()));
84+
return ExceptionMapper.map(e);
85+
}
86+
}
87+
88+
private String spaces(int num, String toPad) {
89+
return toPad.replace("\n", "\n" + " ".repeat(Math.max(0, num)));
90+
}
91+
92+
@Override
93+
public @NotNull BoundaryResult printMatchError(
94+
@NotNull PrintableMatchError description) {
95+
try {
96+
// TODO: Clean up this mess.
97+
// For now it's just a
98+
// copy/paste of the javascript, which
99+
// makes for very unpleasant Java
100+
System.out.println(spaces(6,
101+
redBack.format(" " + description.getKind() + " ") + " "
102+
+ whiteBright.format(description.getLocation()) + " "
103+
+ whiteBright.format(
104+
description.getMessage())) + "\n" + spaces(9,
105+
"Expected something like:\n" + spaces(3,
106+
green.format(description.getExpected()))) + "\n" + spaces(9,
107+
"Actual:\n" + spaces(3, red.format(description.getActual()))) + "\n\n" + spaces(
108+
12, white.format(
109+
" - " + description.getLocation() + " ["
110+
+ description.getErrorTypeTag() + "]")));
111+
112+
return new BoundarySuccess();
113+
} catch (Exception e) {
114+
return ExceptionMapper.map(e);
115+
}
116+
}
117+
118+
@Override
119+
public @NotNull BoundaryResult printMessageError(
120+
@NotNull PrintableMessageError description) {
121+
try {
122+
// TODO: Clean up this mess.
123+
// For now it's just a
124+
// copy/paste of the javascript, which
125+
// makes for very unpleasant Java
126+
System.out.println(spaces(6, redBack.format(" " + description.getKind() + " ")
127+
+ " " + whiteBright.format(description.getLocation()) + " "
128+
+ whiteBright.format(
129+
description.getMessage())) + "\n\n" + spaces(
130+
12, white.format(
131+
" - " + description.getLocation() + " ["
132+
+ description.getErrorTypeTag() + "]")));
133+
134+
return new BoundarySuccess();
135+
} catch (Exception e) {
136+
return ExceptionMapper.map(e);
137+
}
138+
}
139+
140+
@Override
141+
public @NotNull BoundaryResult printTestTitle(@NotNull PrintableTestTitle titleDetails) {
142+
try {
143+
// TODO: Clean up this mess.
144+
// For now it's just a
145+
// copy/paste of the javascript, which
146+
// makes for very unpleasant Java
147+
System.out.println(spaces(3,
148+
titleDetails.getKind().equals("SUCCESS") ? green.format(titleDetails.getIcon())
149+
: red.format(titleDetails.getIcon()) + " " + whiteBright.format(
150+
titleDetails.getTitle()) + "\n" + titleDetails.getAdditionalText()));
151+
152+
return new BoundarySuccess();
153+
} catch (Exception e) {
154+
return ExceptionMapper.map(e);
79155
}
80156
}
81157
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.contracttesting.contractcase.connectors;
2+
3+
import io.contract_testing.contractcase.case_boundary.BoundaryFailure;
4+
import java.util.Arrays;
5+
import java.util.stream.Collectors;
6+
7+
public class ExceptionMapper {
8+
9+
public static BoundaryFailure map(Exception e) {
10+
return new BoundaryFailure(e.getClass().getName(), e.getMessage(),
11+
Arrays.stream(e.getStackTrace()).map(StackTraceElement::toString).collect(
12+
Collectors.joining()));
13+
}
14+
15+
}

0 commit comments

Comments
 (0)