Skip to content

Commit

Permalink
#154 - distinguish between feature file formatting and description bl…
Browse files Browse the repository at this point in the history
…ock formatting
  • Loading branch information
andrewesweet authored and rmpestano committed May 16, 2020
1 parent 22640b7 commit aacfb46
Show file tree
Hide file tree
Showing 10 changed files with 452 additions and 95 deletions.
17 changes: 17 additions & 0 deletions cukedoctor-converter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>perf</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public String renderFeature(Feature feature) {

}
if (hasText(feature.getDescription())) {
final String description = trimAllLines(feature.getDescription()).replaceAll("\\\\", "").replaceAll("\\n", newLine());
final String description = trimAllLines(feature.getDescription()).replaceAll("\\\\", "");
renderDescription(builder, description);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,96 @@

public class StringUtil {

private StringUtil() {}

public static String trimAllLines(String text) {
if (hasText(text)) {
StringBuilder trimmedDescription = new StringBuilder();
String[] lines = text.split(newLine());
boolean shouldTrim = true;
for (int i = 0; i < lines.length; i++) {
if (lines[i].trim().startsWith(Constants.Markup.listing())) {
if (shouldTrim) {
shouldTrim = false;//remove trimming on start listing
} else {
shouldTrim = true; //enable trimming on end listing
}
if (!hasText(text)) {
return text;
}

StringBuilder trimmedDescription = new StringBuilder();
String[] lines = normaliseLineEndings(text).split(newLine());
boolean shouldTrim = true;
boolean foundFirstNotEmptyLine = false;
int leadingWhitespaceCharsToTrim = 0;
for (int i = 0; i < lines.length; i++) {
String line = lines[i];

if (!foundFirstNotEmptyLine) {
if (line != null && !line.isEmpty()) {
leadingWhitespaceCharsToTrim = countLeadingWhitespace(line);
foundFirstNotEmptyLine = true;
}
}

line = trimStart(line, leadingWhitespaceCharsToTrim);

if (line.trim().startsWith(Constants.Markup.listing())) {
if (shouldTrim) {
trimmedDescription.append(lines[i].trim());
shouldTrim = false;//remove trimming on start listing
} else {
trimmedDescription.append(lines[i]);
}
if (i < lines.length - 1) {
trimmedDescription.append(newLine());
shouldTrim = true; //enable trimming on end listing
}
}
return trimmedDescription.toString();

if (shouldTrim) {
trimmedDescription.append(trimEnd(line));
} else {
trimmedDescription.append(line);
}

if (i < lines.length - 1) {
trimmedDescription.append(newLine());
}
}
return text;

return trimmedDescription.toString();
}

}
static int countLeadingWhitespace(String text) {
if (text == null) return 0;

int i = 0;
while (i < text.length()) {
char c = text.charAt(i);
if (c != ' ' && c != '\t') {
break;
}

i += Character.charCount(c);
}

return i;
}

static String trimStart(String text, int count) {
if (count < 1) return text;

if (text == null) return null;

int i = 0;
while (i < text.length() && i < count) {
char c = text.charAt(i);
if (c != ' ' && c != '\t') {
break;
}

i += Character.charCount(c);
}

return text.substring(i);
}

static String trimEnd(String text) {
if (text == null) return null;

// https://stackoverflow.com/a/48053234
return text.replaceFirst("\\s++$", "");
}

public static String normaliseLineEndings(String s) {
if (s == null) return null;

return s.replaceAll("\\r\\n|\\r|\\n", System.lineSeparator());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
import com.github.cukedoctor.builder.CukedoctorDocumentBuilderImpl;
import com.github.cukedoctor.parser.FeatureParser;
import com.github.cukedoctor.renderer.CukedoctorFeatureRenderer;
import com.github.cukedoctor.util.MetaCuke;
import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

import java.io.IOException;
import java.net.URL;
import java.util.List;

Expand All @@ -17,9 +22,27 @@
* Created by pestano on 11/03/16.
*/
public class EnrichmentSteps {

private final MetaCuke metaCuke = new MetaCuke();
String documentation;

public EnrichmentSteps() throws IOException {
}

@Before
public void before() throws IOException {
metaCuke.setUp();
}

@After
public void after() throws IOException {
metaCuke.tearDown();
}

@Given("^the feature:$")
public void the_feature(String featureText) throws Throwable {
assertThat(featureText).isNotNull();
metaCuke.addFeature(featureText);
}

@When("^I convert docstring enriched json output activated with a step comment using cukedoctor converter$")
public void I_convert_docstring_enriched_json_output_activated_with_a_step_comment_using_cukedoctor_converter() throws Throwable {
Expand All @@ -41,16 +64,23 @@ public void I_convert_docstring_enriched_json_output_activiated_with_a_scenario_
getFeatureFixture("/json-output/enrichment/table-and-source-scenario-tag.json");
}

@Then("^DocString asciidoc output must be rendered in my documentation$")
public void DocString_asciidoc_output_must_be_rendered_in_my_documentation(String expected) throws Throwable {
assertThat(documentation.replaceAll("\r","")).contains((expected.replaceAll("\r","")));
}

@When("^I convert enriched feature json output using cukedoctor$")
public void I_convert_enriched_feature_json_output_using_cukedoctor() throws Throwable {
getFeatureFixture("/json-output/enrichment/calc.json");
}

@When("^I convert it$")
public void I_convert_it() {
metaCuke.runCucumber("com.care.dont");
List<Feature> features = FeatureParser.parse(metaCuke.getReport().getAbsolutePath());
documentation = new CukedoctorFeatureRenderer((DocumentAttributes) null).renderFeatures(features, new CukedoctorDocumentBuilderImpl().createNestedBuilder());
}

@Then("^DocString asciidoc output must be rendered in my documentation$")
@Then("^it should be rendered in AsciiDoc as$")
public void DocString_asciidoc_output_must_be_rendered_in_my_documentation(String expected) throws Throwable {
assertThat(documentation.replaceAll("\r","")).contains((expected.replaceAll("\r","")));
}

private void getFeatureFixture(String fixturePath) {
URL featureFile = getClass().getResource(fixturePath);
Expand All @@ -59,4 +89,4 @@ private void getFeatureFixture(String fixturePath) {
assertThat(features).isNotNull().hasSize(1);
documentation = new CukedoctorFeatureRenderer((DocumentAttributes) null).renderFeatures(features, new CukedoctorDocumentBuilderImpl().createNestedBuilder());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.github.cukedoctor.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.util.LinkedList;

public class MetaCuke {
private final LinkedList<File> featureFiles = new LinkedList<>();
private File featureDirectory;
private File reportFile;

public MetaCuke() throws IOException {
featureDirectory = Files.createTempDirectory("Features").toFile();
featureDirectory.deleteOnExit();
}

public void setUp() throws IOException {
featureFiles.clear();
reportFile = createTempFile("Report", ".json");
}

public void addFeature(String featureText) throws IOException {
File featureFile = createTempFile("Scenario", ".feature");
writeFeatureToFile(featureText, featureFile);
featureFiles.add(featureFile);
}

public <T> void runCucumber(Class<T> stepDefs) {
runCucumber(stepDefs.getPackage().getName());
}

public void runCucumber(String stepDefinitionPath) {
io.cucumber.core.cli.Main.run(
new String[]{
"--glue",
stepDefinitionPath,
// The below lines are helpful for debugging, but severely confuse general test output
// "--plugin",
// "pretty",
"--plugin",
"json:" + reportFile.getAbsolutePath(),
featureDirectory.getAbsolutePath(),
},
Thread.currentThread().getContextClassLoader());
}

public File getReport() {
return reportFile;
}

public void tearDown() throws IOException {
for (File feature : featureFiles) {
Files.deleteIfExists(feature.toPath());
}

Files.deleteIfExists(reportFile.toPath());
}

private File createTempFile(String prefix, String suffix) throws IOException {
File tempFile = File.createTempFile(prefix, suffix, featureDirectory);
tempFile.deleteOnExit();
return tempFile;
}

private static void writeFeatureToFile(String featureText, File featureFile) throws IOException {
FileWriter fileWriter = new FileWriter(featureFile);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(featureText);
bufferedWriter.close();
fileWriter.close();
}
}
Loading

0 comments on commit aacfb46

Please sign in to comment.