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

Pretty-Print DocStringArgument Step Arguments #2953

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed
- [JUnit Platform Engine] Use JUnit Platform 1.11.3 (JUnit Jupiter 5.11.3)
### Fixed
- [Core] Pretty-Print DocStringArgument Step Arguments([#2953](https://github.com/cucumber/cucumber-jvm/pull/2953) Daniel Miladinov)

## [7.20.1] - 2024-10-09
### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.cucumber.core.exception.CucumberException;
import io.cucumber.core.gherkin.DataTableArgument;
import io.cucumber.core.gherkin.DocStringArgument;
import io.cucumber.datatable.DataTable;
import io.cucumber.datatable.DataTableFormatter;
import io.cucumber.plugin.ColorAware;
Expand Down Expand Up @@ -141,7 +142,7 @@ private void printStep(TestStepFinished event) {
String locationComment = formatLocationComment(event, testStep, keyword, stepText);
out.println(STEP_INDENT + formattedStepText + locationComment);
StepArgument stepArgument = testStep.getStep().getArgument();
if (DataTableArgument.class.isInstance(stepArgument)) {
if (stepArgument instanceof DataTableArgument) {
DataTableFormatter tableFormatter = DataTableFormatter
.builder()
.prefixRow(STEP_SCENARIO_INDENT)
Expand All @@ -153,6 +154,16 @@ private void printStep(TestStepFinished event) {
} catch (IOException e) {
throw new CucumberException(e);
}
} else if (stepArgument instanceof DocStringArgument) {
DocStringArgument docStringArgument = (DocStringArgument) stepArgument;
String contentType = docStringArgument.getContentType();
String printableContentType = contentType == null ? "" : contentType;
out.println(STEP_INDENT + "\"\"\"" + printableContentType);
for (String l : docStringArgument.getContent().split("\\r?\\n|\\r")) {
String s = STEP_INDENT + l;
out.println(s);
}
out.println(STEP_INDENT + "\"\"\"");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.cucumber.core.stepexpression.StepExpressionFactory;
import io.cucumber.core.stepexpression.StepTypeRegistry;
import io.cucumber.datatable.DataTable;
import io.cucumber.docstring.DocString;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -611,4 +612,69 @@ void should_print_system_failure_for_failed_hooks() {
" " + AnsiEscapes.RED + "the stack trace" + AnsiEscapes.RESET)));
}

@Test
void should_print_docstring_without_content_type() {
Feature feature = TestFeatureParser.parse("path/test.feature", "" +
"Feature: Test feature\n" +
" Scenario: Test Scenario\n" +
" Given first step\n" +
" \"\"\"\n" +
" {\"key1\": \"value1\",\n" +
" \"key2\": \"value2\",\n" +
" \"another1\": \"another2\"}\n" +
" \"\"\"\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", DocString.class)))
.build()
.run();

assertThat(out, bytes(equalToCompressingWhiteSpace("" +
"\n" +
"Scenario: Test Scenario # path/test.feature:2\n" +
" Given first step # path/step_definitions.java:7\n" +
" \"\"\"\n" +
" {\"key1\": \"value1\",\n" +
" \"key2\": \"value2\",\n" +
" \"another1\": \"another2\"}\n" +
" \"\"\"\n")));
}

@Test
void should_print_docstring_including_content_type() {
Feature feature = TestFeatureParser.parse("path/test.feature", "" +
"Feature: Test feature\n" +
" Scenario: Test Scenario\n" +
" Given first step\n" +
" \"\"\"json\n" +
" {\"key1\": \"value1\",\n" +
" \"key2\": \"value2\",\n" +
" \"another1\": \"another2\"}\n" +
" \"\"\"\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", DocString.class)))
.build()
.run();

assertThat(out, bytes(equalToCompressingWhiteSpace("" +
"\n" +
"Scenario: Test Scenario # path/test.feature:2\n" +
" Given first step # path/step_definitions.java:7\n" +
" \"\"\"json\n" +
" {\"key1\": \"value1\",\n" +
" \"key2\": \"value2\",\n" +
" \"another1\": \"another2\"}\n" +
" \"\"\"\n")));
}
}
Loading