Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] Pretty formatter to print step DataTables #2330

Merged
merged 3 commits into from
Aug 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ private static <T> Map<String, List<T>> openEnvelopes(List<JsonNode> actual) {
}

private void sortStepDefinitions(Map<String, List<JsonNode>> envelopes) {
Comparator<JsonNode> stepDefinitionPatternComparator = Comparator.comparing(a -> a.get("pattern").asText());
Comparator<JsonNode> stepDefinitionPatternComparator = Comparator
.comparing(a -> a.get("pattern").get("source").asText());
mpkorstanje marked this conversation as resolved.
Show resolved Hide resolved
List<JsonNode> actualStepDefinitions = envelopes.get("stepDefinition");
if (actualStepDefinitions != null) {
actualStepDefinitions.sort(stepDefinitionPatternComparator);
Expand Down
19 changes: 19 additions & 0 deletions core/src/main/java/io/cucumber/core/plugin/PrettyFormatter.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package io.cucumber.core.plugin;

import io.cucumber.core.exception.CucumberException;
import io.cucumber.core.gherkin.DataTableArgument;
import io.cucumber.datatable.DataTable;
import io.cucumber.datatable.DataTableFormatter;
import io.cucumber.plugin.ColorAware;
import io.cucumber.plugin.ConcurrentEventListener;
import io.cucumber.plugin.event.Argument;
import io.cucumber.plugin.event.EmbedEvent;
import io.cucumber.plugin.event.EventPublisher;
import io.cucumber.plugin.event.PickleStepTestStep;
import io.cucumber.plugin.event.Result;
import io.cucumber.plugin.event.StepArgument;
import io.cucumber.plugin.event.TestCase;
import io.cucumber.plugin.event.TestCaseStarted;
import io.cucumber.plugin.event.TestRunFinished;
Expand Down Expand Up @@ -128,6 +132,21 @@ private void printStep(TestStepFinished event) {
formats.get(status + "_arg"), testStep.getDefinitionArgument());
String locationIndent = calculateLocationIndent(event.getTestCase(), formatPlainStep(keyword, stepText));
out.println(STEP_INDENT + formattedStepText + locationIndent + formatLocation(testStep.getCodeLocation()));

StepArgument stepArgument = testStep.getStep().getArgument();
if (DataTableArgument.class.isInstance(stepArgument)) {
DataTableFormatter tableFormatter = DataTableFormatter
.builder()
.prefixRow(STEP_SCENARIO_INDENT)
.escapeDelimiters(false)
.build();
DataTableArgument dataTableArgument = (DataTableArgument) stepArgument;
try {
tableFormatter.formatTo(DataTable.create(dataTableArgument.cells()), out);
mpkorstanje marked this conversation as resolved.
Show resolved Hide resolved
} catch (IOException e) {
throw new CucumberException(e);
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.cucumber.core.stepexpression.StepExpression;
import io.cucumber.core.stepexpression.StepExpressionFactory;
import io.cucumber.core.stepexpression.StepTypeRegistry;
import io.cucumber.datatable.DataTable;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -174,6 +175,75 @@ void should_print_tags() {
" Then second step # path/step_definitions.java:11\n"));
}

@Test
void should_print_table() {
Feature feature = TestFeatureParser.parse("path/test.feature", "" +
"Feature: Test feature\n" +
" Scenario: Test Scenario\n" +
" Given first step\n" +
" | key1 | key2 |\n" +
" | value1 | value2 |\n" +
" | another1 | another2 |\n");

ByteArrayOutputStream out = new ByteArrayOutputStream();
Runtime.builder()
.withFeatureSupplier(new StubFeatureSupplier(feature))
.withAdditionalPlugins(new PrettyFormatter(out))
.withRuntimeOptions(new RuntimeOptionsBuilder().setMonochrome().build())
.withBackendSupplier(new StubBackendSupplier(
new StubStepDefinition("first step", "path/step_definitions.java:7", DataTable.class)))
.build()
.run();

assertThat(out, isBytesEqualTo("" +

"\n" +
"Scenario: Test Scenario # path/test.feature:2\n" +
" Given first step # path/step_definitions.java:7\n" +
" | key1 | key2 |\n" +
" | value1 | value2 |\n" +
" | another1 | another2 |\n"));
}

@Test
void should_print_multiple_tables() {
Feature feature = TestFeatureParser.parse("path/test.feature", "" +
"Feature: Test feature\n" +
" Scenario: Test Scenario\n" +
" Given first step\n" +
" | key1 | key2 |\n" +
" | value1 | value2 |\n" +
" | another1 | another2 |\n" +
" Given second step\n" +
" | key3 | key4 |\n" +
" | value3 | value4 |\n" +
" | another3 | another4 |\n");

ByteArrayOutputStream out = new ByteArrayOutputStream();
Runtime.builder()
.withFeatureSupplier(new StubFeatureSupplier(feature))
.withAdditionalPlugins(new PrettyFormatter(out))
.withRuntimeOptions(new RuntimeOptionsBuilder().setMonochrome().build())
.withBackendSupplier(new StubBackendSupplier(
new StubStepDefinition("first step", "path/step_definitions.java:7", DataTable.class),
new StubStepDefinition("second step", "path/step_definitions.java:15", DataTable.class)))
.build()
.run();

assertThat(out, isBytesEqualTo("" +

"\n" +
"Scenario: Test Scenario # path/test.feature:2\n" +
" Given first step # path/step_definitions.java:7\n" +
" | key1 | key2 |\n" +
" | value1 | value2 |\n" +
" | another1 | another2 |\n" +
" Given second step # path/step_definitions.java:15\n" +
" | key3 | key4 |\n" +
" | value3 | value4 |\n" +
" | another3 | another4 |\n"));
}

@Test
void should_print_error_message_for_failed_steps() {
Feature feature = TestFeatureParser.parse("path/test.feature", "" +
Expand Down