Skip to content

Commit 152bbf5

Browse files
committed
Minimal test coverage
1 parent 7016a5f commit 152bbf5

File tree

3 files changed

+49
-372
lines changed

3 files changed

+49
-372
lines changed

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import io.cucumber.plugin.ColorAware;
1313
import io.cucumber.plugin.ConcurrentEventListener;
1414
import io.cucumber.plugin.event.EventPublisher;
15-
import io.cucumber.plugin.event.TestRunFinished;
1615
import io.cucumber.query.Query;
1716
import io.cucumber.query.Repository;
1817

@@ -21,7 +20,6 @@
2120
import java.text.DecimalFormat;
2221
import java.text.DecimalFormatSymbols;
2322
import java.util.Collection;
24-
import java.util.Collections;
2523
import java.util.LinkedHashSet;
2624
import java.util.List;
2725
import java.util.Locale;
@@ -66,26 +64,26 @@ public DefaultSummaryPrinter() {
6664

6765
@Override
6866
public void setEventPublisher(EventPublisher publisher) {
69-
publisher.registerHandlerFor(Envelope.class, repository::update);
70-
publisher.registerHandlerFor(TestRunFinished.class, event -> print());
67+
publisher.registerHandlerFor(Envelope.class, envelope -> {
68+
repository.update(envelope);
69+
envelope.getTestRunFinished().ifPresent(testRunFinished -> print());
70+
});
7171
}
7272

7373
private void print() {
7474
out.println();
7575
printStats();
7676
printErrors();
7777
printSnippets();
78-
out.println();
7978
}
8079

8180
private void printStats() {
8281
printNonPassingScenarios();
8382
printScenarioCounts();
8483
printStepCounts();
8584
printDuration();
86-
out.println();
8785
}
88-
86+
8987
private void printNonPassingScenarios() {
9088
Map<TestStepResultStatus, List<TestCaseFinished>> testCaseFinishedByStatus = query
9189
.findAllTestCaseFinished()
@@ -109,15 +107,16 @@ private void printScenarios(
109107
}
110108
for (TestCaseFinished testCaseFinished : scenarios) {
111109
query.findPickleBy(testCaseFinished).ifPresent(pickle -> {
112-
String location = pickle.getUri() + query.findLocationOf(pickle).map(Location::getLine).map(line -> ":" + line).orElse("");
110+
String location = pickle.getUri()
111+
+ query.findLocationOf(pickle).map(Location::getLine).map(line -> ":" + line).orElse("");
113112
out.println(location + " # " + pickle.getName());
114113
});
115114
}
116115
if (!scenarios.isEmpty()) {
117116
out.println();
118117
}
119118
}
120-
119+
121120
private void printScenarioCounts() {
122121
List<TestCaseFinished> allTestCaseFinished = query.findAllTestCaseFinished();
123122
if (allTestCaseFinished.isEmpty()) {
@@ -134,14 +133,13 @@ private void printScenarioCounts() {
134133
out.println(")");
135134
}
136135

137-
138136
private void printStepCounts() {
139137
List<TestStepFinished> testStepsFinished = query.findAllTestStepFinished();
140138
if (testStepsFinished.isEmpty()) {
141139
out.println("0 Steps");
142140
return;
143141
}
144-
142+
145143
Map<TestStepResultStatus, Long> testStepResultStatus = testStepsFinished.stream()
146144
.collect(countTestStepResultStatusByTestStepFinished());
147145

@@ -229,14 +227,14 @@ private void printSnippets() {
229227
private Collector<TestCaseFinished, ?, Map<TestStepResultStatus, Long>> countTestStepResultStatusByTestCaseFinished() {
230228
return groupingBy(this::getTestStepResultStatusBy, counting());
231229
}
232-
230+
233231
private TestStepResultStatus getTestStepResultStatusBy(TestCaseFinished testCaseFinished) {
234232
return query.findMostSevereTestStepResultBy(testCaseFinished)
235233
.map(TestStepResult::getStatus)
236234
// By definition
237235
.orElse(TestStepResultStatus.PASSED);
238236
}
239-
237+
240238
private static Collector<TestStepFinished, ?, Map<TestStepResultStatus, Long>> countTestStepResultStatusByTestStepFinished() {
241239
return groupingBy(DefaultSummaryPrinter::getTestStepResultStatusBy, counting());
242240
}
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)