Skip to content

Commit f9490c5

Browse files
committed
Minimal test coverage
1 parent 7016a5f commit f9490c5

File tree

3 files changed

+49
-370
lines changed

3 files changed

+49
-370
lines changed

cucumber-core/src/main/java/io/cucumber/core/plugin/DefaultSummaryPrinter.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,26 @@ public DefaultSummaryPrinter() {
6666

6767
@Override
6868
public void setEventPublisher(EventPublisher publisher) {
69-
publisher.registerHandlerFor(Envelope.class, repository::update);
70-
publisher.registerHandlerFor(TestRunFinished.class, event -> print());
69+
publisher.registerHandlerFor(Envelope.class, envelope -> {
70+
repository.update(envelope);
71+
envelope.getTestRunFinished().ifPresent(testRunFinished -> print());
72+
});
7173
}
7274

7375
private void print() {
7476
out.println();
7577
printStats();
7678
printErrors();
7779
printSnippets();
78-
out.println();
7980
}
8081

8182
private void printStats() {
8283
printNonPassingScenarios();
8384
printScenarioCounts();
8485
printStepCounts();
8586
printDuration();
86-
out.println();
8787
}
88-
88+
8989
private void printNonPassingScenarios() {
9090
Map<TestStepResultStatus, List<TestCaseFinished>> testCaseFinishedByStatus = query
9191
.findAllTestCaseFinished()
@@ -109,15 +109,16 @@ private void printScenarios(
109109
}
110110
for (TestCaseFinished testCaseFinished : scenarios) {
111111
query.findPickleBy(testCaseFinished).ifPresent(pickle -> {
112-
String location = pickle.getUri() + query.findLocationOf(pickle).map(Location::getLine).map(line -> ":" + line).orElse("");
112+
String location = pickle.getUri()
113+
+ query.findLocationOf(pickle).map(Location::getLine).map(line -> ":" + line).orElse("");
113114
out.println(location + " # " + pickle.getName());
114115
});
115116
}
116117
if (!scenarios.isEmpty()) {
117118
out.println();
118119
}
119120
}
120-
121+
121122
private void printScenarioCounts() {
122123
List<TestCaseFinished> allTestCaseFinished = query.findAllTestCaseFinished();
123124
if (allTestCaseFinished.isEmpty()) {
@@ -134,14 +135,13 @@ private void printScenarioCounts() {
134135
out.println(")");
135136
}
136137

137-
138138
private void printStepCounts() {
139139
List<TestStepFinished> testStepsFinished = query.findAllTestStepFinished();
140140
if (testStepsFinished.isEmpty()) {
141141
out.println("0 Steps");
142142
return;
143143
}
144-
144+
145145
Map<TestStepResultStatus, Long> testStepResultStatus = testStepsFinished.stream()
146146
.collect(countTestStepResultStatusByTestStepFinished());
147147

@@ -229,14 +229,14 @@ private void printSnippets() {
229229
private Collector<TestCaseFinished, ?, Map<TestStepResultStatus, Long>> countTestStepResultStatusByTestCaseFinished() {
230230
return groupingBy(this::getTestStepResultStatusBy, counting());
231231
}
232-
232+
233233
private TestStepResultStatus getTestStepResultStatusBy(TestCaseFinished testCaseFinished) {
234234
return query.findMostSevereTestStepResultBy(testCaseFinished)
235235
.map(TestStepResult::getStatus)
236236
// By definition
237237
.orElse(TestStepResultStatus.PASSED);
238238
}
239-
239+
240240
private static Collector<TestStepFinished, ?, Map<TestStepResultStatus, Long>> countTestStepResultStatusByTestStepFinished() {
241241
return groupingBy(DefaultSummaryPrinter::getTestStepResultStatusBy, counting());
242242
}
Lines changed: 38 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,58 @@
11
package io.cucumber.core.plugin;
22

3-
import io.cucumber.core.eventbus.EventBus;
3+
import io.cucumber.core.backend.StubStepDefinition;
4+
import io.cucumber.core.feature.TestFeatureParser;
5+
import io.cucumber.core.gherkin.Feature;
6+
import io.cucumber.core.options.RuntimeOptionsBuilder;
7+
import io.cucumber.core.runner.StepDurationTimeService;
8+
import io.cucumber.core.runtime.Runtime;
9+
import io.cucumber.core.runtime.StubBackendSupplier;
10+
import io.cucumber.core.runtime.StubFeatureSupplier;
411
import io.cucumber.core.runtime.TimeServiceEventBus;
5-
import io.cucumber.plugin.event.Location;
6-
import io.cucumber.plugin.event.Result;
7-
import io.cucumber.plugin.event.SnippetsSuggestedEvent;
8-
import io.cucumber.plugin.event.SnippetsSuggestedEvent.Suggestion;
9-
import io.cucumber.plugin.event.Status;
10-
import io.cucumber.plugin.event.TestRunFinished;
11-
import org.junit.jupiter.api.BeforeEach;
1212
import org.junit.jupiter.api.Test;
1313

1414
import java.io.ByteArrayOutputStream;
15-
import java.net.URI;
16-
import java.time.Clock;
1715
import java.time.Duration;
18-
import java.time.ZoneId;
1916
import java.util.Locale;
2017
import java.util.UUID;
2118

19+
import static io.cucumber.core.plugin.Bytes.bytes;
2220
import static io.cucumber.core.plugin.IsEqualCompressingLineSeparators.equalCompressingLineSeparators;
23-
import static java.nio.charset.StandardCharsets.UTF_8;
24-
import static java.time.Instant.ofEpochSecond;
25-
import static java.util.Collections.singletonList;
21+
import static io.cucumber.core.plugin.PrettyFormatterStepDefinition.oneReference;
22+
import static io.cucumber.core.plugin.PrettyFormatterStepDefinition.threeReference;
23+
import static io.cucumber.core.plugin.PrettyFormatterStepDefinition.twoReference;
2624
import static org.hamcrest.MatcherAssert.assertThat;
2725

2826
class DefaultSummaryPrinterTest {
2927

30-
private final ByteArrayOutputStream out = new ByteArrayOutputStream();
31-
private final DefaultSummaryPrinter summaryPrinter = new DefaultSummaryPrinter(out, Locale.US);
32-
private final EventBus bus = new TimeServiceEventBus(
33-
Clock.fixed(ofEpochSecond(0), ZoneId.of("UTC")),
34-
UUID::randomUUID);
35-
36-
@BeforeEach
37-
void setup() {
38-
summaryPrinter.setEventPublisher(bus);
39-
}
40-
4128
@Test
42-
void does_not_print_duplicate_snippets() {
43-
bus.send(new SnippetsSuggestedEvent(
44-
bus.getInstant(),
45-
URI.create("classpath:com/example.feature"),
46-
new Location(12, -1),
47-
new Location(13, -1),
48-
new Suggestion("", singletonList("snippet"))));
49-
50-
bus.send(new SnippetsSuggestedEvent(
51-
bus.getInstant(),
52-
URI.create("classpath:com/example.feature"),
53-
new Location(12, -1),
54-
new Location(14, -1),
55-
new Suggestion("", singletonList("snippet"))));
56-
57-
bus.send(new TestRunFinished(bus.getInstant(), new Result(Status.PASSED, Duration.ZERO, null)));
58-
59-
assertThat(new String(out.toByteArray(), UTF_8), equalCompressingLineSeparators("" +
29+
void writesSummary() {
30+
Feature feature = TestFeatureParser.parse("path/test.feature", "" +
31+
"Feature: feature name\n" +
32+
" Scenario: scenario name\n" +
33+
" Given first step\n" +
34+
" When second step\n" +
35+
" Then third step\n");
36+
37+
StepDurationTimeService timeService = new StepDurationTimeService(Duration.ofMillis(1128));
38+
ByteArrayOutputStream out = new ByteArrayOutputStream();
39+
Runtime.builder()
40+
.withEventBus(new TimeServiceEventBus(timeService, UUID::randomUUID))
41+
.withFeatureSupplier(new StubFeatureSupplier(feature))
42+
.withAdditionalPlugins(timeService, new DefaultSummaryPrinter(out, Locale.US))
43+
.withRuntimeOptions(new RuntimeOptionsBuilder().setMonochrome().build())
44+
.withBackendSupplier(new StubBackendSupplier(
45+
new StubStepDefinition("first step", oneReference()),
46+
new StubStepDefinition("second step", twoReference()),
47+
new StubStepDefinition("third step", threeReference())))
48+
.build()
49+
.run();
50+
51+
assertThat(out, bytes(equalCompressingLineSeparators("" +
6052
"\n" +
61-
"0 Scenarios\n" +
62-
"0 Steps\n" +
63-
"0m0.000s\n" +
64-
"\n" +
65-
"\n" +
66-
"You can implement missing steps with the snippets below:\n" +
67-
"\n" +
68-
"snippet\n" +
69-
"\n" +
70-
"\n"));
71-
53+
"1 Scenarios (1 passed)\n" +
54+
"3 Steps (3 passed)\n" +
55+
"0m3.384s\n")));
7256
}
7357

7458
}

0 commit comments

Comments
 (0)