Skip to content

Pretty-Print DocStringArgument Step Arguments #2953

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

Merged
merged 6 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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,8 +2,11 @@

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.docstring.DocString;
import io.cucumber.docstring.DocStringFormatter;
import io.cucumber.plugin.ColorAware;
import io.cucumber.plugin.ConcurrentEventListener;
import io.cucumber.plugin.event.Argument;
Expand Down Expand Up @@ -141,15 +144,29 @@ 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)
.escapeDelimiters(false)
.build();
DataTableArgument dataTableArgument = (DataTableArgument) stepArgument;
DataTable table = DataTable.create(dataTableArgument.cells());
try {
tableFormatter.formatTo(DataTable.create(dataTableArgument.cells()), out);
tableFormatter.formatTo(table, out);
} catch (IOException e) {
throw new CucumberException(e);
}
} else if (stepArgument instanceof DocStringArgument) {
DocStringFormatter docStringFormatter = DocStringFormatter
.builder()
.indentation(STEP_SCENARIO_INDENT)
.build();
DocStringArgument docStringArgument = (DocStringArgument) stepArgument;
DocString docString = DocString.create(docStringArgument.getContent(),
docStringArgument.getContentType());
try {
docStringFormatter.formatTo(docString, out);
} catch (IOException e) {
throw new CucumberException(e);
}
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,36 @@ void should_print_system_failure_for_failed_hooks() {
" " + AnsiEscapes.RED + "the stack trace" + AnsiEscapes.RESET)));
}

@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")));
}
}
10 changes: 3 additions & 7 deletions docstring/src/main/java/io/cucumber/docstring/DocString.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import java.lang.reflect.Type;
import java.util.Objects;

import static java.util.Arrays.stream;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.joining;

/**
* A doc string. For example:
Expand Down Expand Up @@ -81,11 +79,9 @@ public boolean equals(Object o) {

@Override
public String toString() {
return stream(content.split("\n"))
.collect(joining(
"\n ",
" \"\"\"" + contentType + "\n ",
"\n \"\"\""));
return DocStringFormatter.builder()
.build()
.format(this);
}

public interface DocStringConverter {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package io.cucumber.docstring;

import org.apiguardian.api.API;

import java.io.IOException;

import static java.util.Objects.requireNonNull;

@API(status = API.Status.EXPERIMENTAL)
public final class DocStringFormatter {

private final String indentation;

private DocStringFormatter(String indentation) {
this.indentation = indentation;
}

public static DocStringFormatter.Builder builder() {
return new Builder();
}

public String format(DocString docString) {
StringBuilder result = new StringBuilder();
formatTo(docString, result);
return result.toString();
}

public void formatTo(DocString docString, StringBuilder appendable) {
requireNonNull(docString, "docString may not be null");
requireNonNull(appendable, "appendable may not be null");
try {
formatTo(docString, (Appendable) appendable);
} catch (IOException e) {
throw new CucumberDocStringException(e.getMessage(), e);
}
}

public void formatTo(DocString docString, Appendable out) throws IOException {
String printableContentType = docString.getContentType() == null ? "" : docString.getContentType();
out.append(indentation).append("\"\"\"").append(printableContentType).append("\n");
for (String l : docString.getContent().split("\\n")) {
out.append(indentation).append(l).append("\n");
}
out.append(indentation).append("\"\"\"").append("\n");
}

public static final class Builder {

private String indentation = "";

private Builder() {

}

public Builder indentation(String indentation) {
requireNonNull(indentation, "indentation may not be null");
this.indentation = indentation;
return this;
}

public DocStringFormatter build() {
return new DocStringFormatter(indentation);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package io.cucumber.docstring;

import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

class DocStringFormatterTest {

@Test
void should_print_docstring_with_content_type() {
DocString docString = DocString.create("{\n" +
" \"key1\": \"value1\",\n" +
" \"key2\": \"value2\",\n" +
" \"another1\": \"another2\"\n" +
"}\n",
"application/json");

DocStringFormatter formatter = DocStringFormatter.builder().build();
String format = formatter.format(docString);
assertThat(format, equalTo(
"\"\"\"application/json\n" +
"{\n" +
" \"key1\": \"value1\",\n" +
" \"key2\": \"value2\",\n" +
" \"another1\": \"another2\"\n" +
"}\n" +
"\"\"\"\n"));
}

@Test
void should_print_docstring_without_content_type() {
DocString docString = DocString.create("{\n" +
" \"key1\": \"value1\",\n" +
" \"key2\": \"value2\",\n" +
" \"another1\": \"another2\"\n" +
"}\n");

DocStringFormatter formatter = DocStringFormatter.builder().build();
String format = formatter.format(docString);
assertThat(format, equalTo(
"\"\"\"\n" +
"{\n" +
" \"key1\": \"value1\",\n" +
" \"key2\": \"value2\",\n" +
" \"another1\": \"another2\"\n" +
"}\n" +
"\"\"\"\n"));
}

@Test
void should_print_docstring_with_indentation() {
DocString docString = DocString.create("Hello",
"text/plain");

DocStringFormatter formatter = DocStringFormatter.builder().indentation(" ").build();
String format = formatter.format(docString);
assertThat(format, equalTo(
" \"\"\"text/plain\n" +
" Hello\n" +
" \"\"\"\n"));
}

}
16 changes: 8 additions & 8 deletions docstring/src/test/java/io/cucumber/docstring/DocStringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ void pretty_prints_doc_string_objects() {
"application/json");

assertThat(docString.toString(), is("" +
" \"\"\"application/json\n" +
" {\n" +
" \"hello\":\"world\"\n" +
" }\n" +
" \"\"\""));
"\"\"\"application/json\n" +
"{\n" +
" \"hello\":\"world\"\n" +
"}\n" +
"\"\"\"\n"));
}

@Test
Expand Down Expand Up @@ -60,9 +60,9 @@ void pretty_prints_empty_doc_string_objects() {
"application/json");

assertThat(docString.toString(), is("" +
" \"\"\"application/json\n" +
" \n" +
" \"\"\""));
"\"\"\"application/json\n" +
"\n" +
"\"\"\"\n"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ void throws_when_conversion_fails() {
() -> converter.convert(docString, JsonNode.class));
assertThat(exception.getMessage(), is(equalToCompressingWhiteSpace("" +
"'json' could not transform\n" +
" \"\"\"json\n" +
" {\"hello\":\"world\"}\n" +
" \"\"\"")));
" \"\"\"json\n" +
" {\"hello\":\"world\"}\n" +
" \"\"\"")));
}

@Test
Expand Down
Loading