Skip to content

Commit a35aad6

Browse files
core: add repeatStep to streamline reporting (#865)
1 parent 2887753 commit a35aad6

14 files changed

Lines changed: 429 additions & 52 deletions

File tree

webtau-cli/src/main/java/org/testingisdocumenting/webtau/cli/CliBackgroundCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ private Thread waitForProcessToFinishInBackground() {
159159
tokenizedMessage(),
160160
(exitCode) -> tokenizedMessage(action("background cli command"), COLON, stringValue(command),
161161
action("finished with exit code"), numberValue(exitCode)),
162-
() -> {
162+
(context) -> {
163163
synchronized (this) {
164164
backgroundProcess.setAsInactive();
165165
CliBackgroundCommandManager.remove(this);

webtau-core/src/main/java/org/testingisdocumenting/webtau/WebTauCore.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,18 @@
2727
import org.testingisdocumenting.webtau.persona.Persona;
2828
import org.testingisdocumenting.webtau.reporter.StepReportOptions;
2929
import org.testingisdocumenting.webtau.reporter.WebTauStep;
30+
import org.testingisdocumenting.webtau.reporter.WebTauStepContext;
3031
import org.testingisdocumenting.webtau.utils.CollectionUtils;
3132

3233
import java.util.Arrays;
3334
import java.util.Map;
35+
import java.util.function.Function;
3436
import java.util.function.Supplier;
3537

36-
import static org.testingisdocumenting.webtau.data.table.TableDataUnderscore.UNDERSCORE;
38+
import static org.testingisdocumenting.webtau.data.table.TableDataUnderscore.*;
3739
import static org.testingisdocumenting.webtau.reporter.IntegrationTestsMessageBuilder.*;
38-
import static org.testingisdocumenting.webtau.reporter.TokenizedMessage.tokenizedMessage;
40+
import static org.testingisdocumenting.webtau.reporter.TokenizedMessage.*;
41+
import static org.testingisdocumenting.webtau.utils.FunctionUtils.*;
3942

4043
/**
4144
* Convenient class for a single static * imports to have matchers and helper functions available for your test
@@ -122,9 +125,7 @@ public static Persona getCurrentPersona() {
122125
}
123126

124127
public static void step(String label, Runnable action) {
125-
WebTauStep.createAndExecuteStep(tokenizedMessage(action(label)),
126-
() -> tokenizedMessage(none("completed"), action(label)),
127-
action);
128+
step(label, toSupplier(action));
128129
}
129130

130131
public static <R> R step(String label, Supplier<Object> action) {
@@ -137,6 +138,15 @@ public static <R> R step(String label, Supplier<Object> action) {
137138
return step.execute(StepReportOptions.REPORT_ALL);
138139
}
139140

141+
public static void repeatStep(String label, int numberOfAttempts, Runnable action) {
142+
repeatStep(label, numberOfAttempts, toFunction(action));
143+
}
144+
145+
public static void repeatStep(String label, int numberOfAttempts, Function<WebTauStepContext, Object> action) {
146+
WebTauStep step = WebTauStep.createRepeatStep(label, numberOfAttempts, action);
147+
step.execute(StepReportOptions.REPORT_ALL);
148+
}
149+
140150
public static void fail(String message) {
141151
throw new AssertionError(message);
142152
}

webtau-core/src/main/java/org/testingisdocumenting/webtau/reporter/ConsoleStepReporter.java

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,26 @@ public void onStepFailure(WebTauStep step) {
4949
executeIfWithinVerboseLevel(step, () -> printStepFailure(step));
5050
}
5151

52+
@Override
53+
public void onStepRepeatStart(WebTauStep step, int current, int total) {
54+
executeIfWithinVerboseLevel(step, () -> printStepRepeatStart(step, current, total));
55+
}
56+
57+
@Override
58+
public void onStepRepeatSuccess(WebTauStep step, int current, int total) {
59+
executeIfWithinVerboseLevel(step, () -> printStepRepeatSuccess(step, current, total));
60+
}
61+
62+
@Override
63+
public void onStepRepeatFailure(WebTauStep step, int current, int total) {
64+
executeIfWithinVerboseLevel(step, () -> printStepRepeatFailure(step, current, total));
65+
}
66+
5267
private void printStepStart(WebTauStep step) {
5368
ConsoleOutputs.out(
5469
Stream.concat(
5570
Stream.concat(
56-
Stream.of(createIndentation(step.getNumberOfParents()), Color.YELLOW, "> "),
71+
stepStartBeginningStream(step),
5772
personaStream(step)),
5873
toAnsiConverter.convert(step.getInProgressMessage()).stream()
5974
).toArray());
@@ -64,38 +79,58 @@ private void printStepStart(WebTauStep step) {
6479
private void printStepSuccess(WebTauStep step) {
6580
TokenizedMessage completionMessage = step.getCompletionMessage();
6681

67-
int numberOfParents = step.getNumberOfParents();
68-
6982
TokenizedMessage completionMessageToUse = isLastTokenMatcher(completionMessage) ?
7083
completionMessage.subMessage(0, completionMessage.getNumberOfTokens() - 1)
71-
.add(reAlignText(numberOfParents + 2, completionMessage.getLastToken())) :
84+
.add(reAlignText(step.getNumberOfParents() + 2, completionMessage.getLastToken())) :
7285
completionMessage;
7386

7487
printStepOutput(step);
7588

76-
ConsoleOutputs.out(
77-
Stream.concat(
78-
Stream.concat(
79-
Stream.concat(
80-
Stream.of(createIndentation(numberOfParents), Color.GREEN, ". "),
81-
personaStream(step)),
82-
toAnsiConverter.convert(completionMessageToUse).stream()),
83-
timeTakenTokenStream(step)).toArray());
89+
ConsoleOutputs.out(Stream.concat(Stream.concat(Stream.concat(stepSuccessBeginningStream(step), personaStream(step)),
90+
toAnsiConverter.convert(completionMessageToUse).stream()),
91+
timeTakenTokenStream(step)).toArray());
8492
}
8593

8694
private void printStepFailure(WebTauStep step) {
8795
TokenizedMessage completionMessageToUse = messageTokensForFailedStep(step);
8896

8997
printStepOutput(step);
9098

91-
ConsoleOutputs.out(
99+
ConsoleOutputs.out(Stream.concat(Stream.concat(Stream.concat(stepFailureBeginningStream(step), personaStream(step)),
100+
toAnsiConverter.convert(completionMessageToUse).stream()),
101+
timeTakenTokenStream(step)).toArray());
102+
}
103+
104+
private void printStepRepeatStart(WebTauStep step, int currentIdx, int total) {
105+
ConsoleOutputs.out(Stream.concat(stepStartBeginningStream(step),
106+
stepCurrentIdxOfTotalStream(currentIdx, total)).toArray());
107+
}
108+
109+
private void printStepRepeatSuccess(WebTauStep step, int currentIdx, int total) {
110+
ConsoleOutputs.out(Stream.concat(stepSuccessBeginningStream(step),
92111
Stream.concat(
93-
Stream.concat(
94-
Stream.concat(
95-
Stream.of(createIndentation(step.getNumberOfParents()), Color.RED, "X "),
96-
personaStream(step)),
97-
toAnsiConverter.convert(completionMessageToUse).stream()),
98-
timeTakenTokenStream(step)).toArray());
112+
stepCurrentIdxOfTotalStream(currentIdx, total),
113+
timeTakenTokenStream(step))).toArray());
114+
}
115+
116+
private void printStepRepeatFailure(WebTauStep step, int currentIdx, int total) {
117+
printStepFailure(step);
118+
}
119+
120+
private Stream<Object> stepStartBeginningStream(WebTauStep step) {
121+
return Stream.of(createIndentation(step.getNumberOfParents()), Color.YELLOW, "> ");
122+
}
123+
124+
private Stream<Object> stepSuccessBeginningStream(WebTauStep step) {
125+
return Stream.of(createIndentation(step.getNumberOfParents()), Color.GREEN, ". ");
126+
}
127+
128+
private Stream<Object> stepFailureBeginningStream(WebTauStep step) {
129+
return Stream.of(createIndentation(step.getNumberOfParents()), Color.RED, "X ");
130+
}
131+
132+
private Stream<Object> stepCurrentIdxOfTotalStream(int currentIdx, int total) {
133+
return Stream.of(Color.BLUE, currentIdx + 1, Color.YELLOW, "/", Color.BLUE, total);
99134
}
100135

101136
private Stream<Object> timeTakenTokenStream(WebTauStep step) {

webtau-core/src/main/java/org/testingisdocumenting/webtau/reporter/StepReportOptions.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright 2021 webtau maintainers
23
* Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -22,5 +23,6 @@
2223
*/
2324
public enum StepReportOptions {
2425
SKIP_START,
25-
REPORT_ALL
26+
REPORT_ALL,
27+
SKIP_ALL,
2628
}

webtau-core/src/main/java/org/testingisdocumenting/webtau/reporter/StepReporter.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,13 @@ public interface StepReporter {
2121
void onStepStart(WebTauStep step);
2222
void onStepSuccess(WebTauStep step);
2323
void onStepFailure(WebTauStep step);
24+
25+
default void onStepRepeatStart(WebTauStep step, int currentIdx, int total) {
26+
}
27+
28+
default void onStepRepeatSuccess(WebTauStep step, int currentIdx, int total) {
29+
}
30+
31+
default void onStepRepeatFailure(WebTauStep step, int currentIdx, int total) {
32+
}
2433
}

webtau-core/src/main/java/org/testingisdocumenting/webtau/reporter/StepReporters.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public class StepReporters {
3535

3636
private static final ThreadLocal<List<StepReporter>> localReporters = ThreadLocal.withInitial(ArrayList::new);
3737

38+
private static final ThreadLocal<Boolean> disabled = ThreadLocal.withInitial(() -> false);
39+
3840
// for ad-hoc groovy script runs from IDE we want to use console reporter as long
3941
// as there were no explicit reporters added
4042
private static final AtomicBoolean explicitlyAdded = new AtomicBoolean(false);
@@ -57,6 +59,15 @@ public static <R> R withAdditionalReporter(StepReporter reporter, Supplier<R> co
5759
}
5860
}
5961

62+
public static <R> R withoutReporters(Supplier<R> code) {
63+
try {
64+
disabled.set(true);
65+
return code.get();
66+
} finally {
67+
disabled.set(false);
68+
}
69+
}
70+
6071
public static void onStart(WebTauStep step) {
6172
getReportersStream().forEach(r -> r.onStepStart(step));
6273
}
@@ -65,11 +76,27 @@ public static void onSuccess(WebTauStep step) {
6576
getReportersStream().forEach(r -> r.onStepSuccess(step));
6677
}
6778

79+
public static void onStepRepeatStart(WebTauStep step, int currentIdx, int total) {
80+
getReportersStream().forEach(r -> r.onStepRepeatStart(step, currentIdx, total));
81+
}
82+
83+
public static void onStepRepeatSuccess(WebTauStep step, int currentIdx, int total) {
84+
getReportersStream().forEach(r -> r.onStepRepeatSuccess(step, currentIdx, total));
85+
}
86+
87+
public static void onStepRepeatFailure(WebTauStep step, int currentIdx, int total) {
88+
getReportersStream().forEach(r -> r.onStepRepeatFailure(step, currentIdx, total));
89+
}
90+
6891
public static void onFailure(WebTauStep step) {
6992
getReportersStream().forEach(r -> r.onStepFailure(step));
7093
}
7194

7295
private static Stream<StepReporter> getReportersStream() {
96+
if (disabled.get()) {
97+
return Stream.empty();
98+
}
99+
73100
List<StepReporter> localReporters = StepReporters.localReporters.get();
74101

75102
if (!explicitlyAdded.get() && localReporters.isEmpty()) {

0 commit comments

Comments
 (0)