Skip to content

Commit

Permalink
Ensure PrettyFormatter outputs whole lines and flushed in between
Browse files Browse the repository at this point in the history
  • Loading branch information
semistrict committed Sep 30, 2013
1 parent 1551c60 commit da93b41
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 50 deletions.
28 changes: 24 additions & 4 deletions java/src/main/java/gherkin/formatter/NiceAppendable.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public NiceAppendable(Appendable out) {
public NiceAppendable append(CharSequence csq) {
try {
out.append(csq);
tryFlush();
return this;
} catch (IOException e) {
throw new RuntimeException(e);
Expand All @@ -27,6 +28,7 @@ public NiceAppendable append(CharSequence csq) {
public NiceAppendable append(CharSequence csq, int start, int end) {
try {
out.append(csq, start, end);
tryFlush();
return this;
} catch (IOException e) {
throw new RuntimeException(e);
Expand All @@ -36,6 +38,7 @@ public NiceAppendable append(CharSequence csq, int start, int end) {
public NiceAppendable append(char c) {
try {
out.append(c);
tryFlush();
return this;
} catch (IOException e) {
throw new RuntimeException(e);
Expand All @@ -47,19 +50,36 @@ public NiceAppendable println() {
}

public NiceAppendable println(CharSequence csq) {
return append(csq).println();
try {
StringBuilder buffer = new StringBuilder();
buffer.append(csq);
buffer.append(NL);
out.append(buffer.toString());
tryFlush();
return this;
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public void close() {
try {
if (out instanceof Flushable) {
((Flushable) out).flush();
}
tryFlush();
if (out instanceof Closeable) {
((Closeable) out).close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private void tryFlush() {
if (!(out instanceof Flushable))
return;
try {
((Flushable) out).flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
73 changes: 34 additions & 39 deletions java/src/main/java/gherkin/formatter/PrettyFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,13 @@ private void printStatement() {
if (statement instanceof TagStatement) {
printTags(((TagStatement) statement).getTags(), " ");
}
out.append(" ");
out.append(statement.getKeyword());
out.append(": ");
out.append(statement.getName());
StringBuilder buffer = new StringBuilder(" ");
buffer.append(statement.getKeyword());
buffer.append(": ");
buffer.append(statement.getName());
String location = executing ? uri + ":" + statement.getLine() : null;
out.println(indentedLocation(location));
buffer.append(indentedLocation(location));
out.println(buffer);
printDescription(statement.getDescription(), " ", true);
statement = null;
}
Expand All @@ -166,11 +167,7 @@ public void examples(Examples examples) {
out.println();
printComments(examples.getComments(), " ");
printTags(examples.getTags(), " ");
out.append(" ");
out.append(examples.getKeyword());
out.append(": ");
out.append(examples.getName());
out.println();
out.println(" " + examples.getKeyword() + ": " + examples.getName());
printDescription(examples.getDescription(), " ", true);
table(examples.getRows());
}
Expand Down Expand Up @@ -230,11 +227,8 @@ private void printHookFailure(Match match, Result result, boolean isBefore) {
}
context.append(" hook:");

out.append(format.text(context.toString()));
out.append(format.text(match.getLocation()));
out.println();
out.append(format.text("Message: "));
out.append(format.text(result.getErrorMessage()));
out.println(format.text(context.toString()) + format.text(match.getLocation()));
out.println(format.text("Message: ") + format.text(result.getErrorMessage()));

if (result.getError() != null) {
printError(result);
Expand All @@ -249,12 +243,13 @@ private void printStep(String status, List<Argument> arguments, String location)
Format argFormat = getArgFormat(status);

printComments(step.getComments(), " ");
out.append(" ");
out.append(textFormat.text(step.getKeyword()));
stepPrinter.writeStep(out, textFormat, argFormat, step.getName(), arguments);
out.append(indentedLocation(location));

out.println();
StringBuilder buffer = new StringBuilder(" ");
buffer.append(textFormat.text(step.getKeyword()));
stepPrinter.writeStep(new NiceAppendable(buffer), textFormat, argFormat, step.getName(), arguments);
buffer.append(indentedLocation(location));

out.println(buffer);
if (step.getRows() != null) {
table(step.getRows());
} else if (step.getDocString() != null) {
Expand Down Expand Up @@ -311,28 +306,30 @@ private String getCellSafely(final List<String> cells, final int colIndex) {
}

public void row(List<CellResult> cellResults) {
StringBuilder buffer = new StringBuilder();
Row row = rows.get(rowIndex);
if (rowsAbove) {
out.append(formats.up(rowHeight));
buffer.append(formats.up(rowHeight));
} else {
rowsAbove = true;
}
rowHeight = 1;

for (Comment comment : row.getComments()) {
out.append(" ");
out.println(comment.getValue());
buffer.append(" ");
buffer.append(comment.getValue());
buffer.append("\n");
rowHeight++;
}
switch (row.getDiffType()) {
case NONE:
out.append(" | ");
buffer.append(" | ");
break;
case DELETE:
out.append(" ").append(formats.get("skipped").text("-")).append(" | ");
buffer.append(" ").append(formats.get("skipped").text("-")).append(" | ");
break;
case INSERT:
out.append(" ").append(formats.get("comment").text("+")).append(" | ");
buffer.append(" ").append(formats.get("comment").text("+")).append(" | ");
break;
}
for (int colIndex = 0; colIndex < maxLengths.length; colIndex++) {
Expand All @@ -350,16 +347,16 @@ public void row(List<CellResult> cellResults) {
break;
}
Format format = formats.get(status);
out.append(format.text(cellText));
buffer.append(format.text(cellText));
int padding = maxLengths[colIndex] - cellLengths[rowIndex][colIndex];
padSpace(padding);
padSpace(buffer, padding);
if (colIndex < maxLengths.length - 1) {
out.append(" | ");
buffer.append(" | ");
} else {
out.append(" |");
buffer.append(" |");
}
}
out.println();
out.println(buffer);
rowHeight++;
Set<Result> seenResults = new HashSet<Result>();
for (CellResult cellResult : cellResults) {
Expand Down Expand Up @@ -406,8 +403,8 @@ private String escapeCell(String cell) {

public void docString(DocString docString) {
out.println(" \"\"\"");
out.append(escapeTripleQuotes(indent(docString.getValue(), " ")));
out.println("\n \"\"\"");
out.println(escapeTripleQuotes(indent(docString.getValue(), " ")));
out.println(" \"\"\"");
}

public void eof() {
Expand All @@ -432,23 +429,21 @@ private void calculateLocationIndentations() {
}
}

private void padSpace(int indent) {
private void padSpace(StringBuilder buffer, int indent) {
for (int i = 0; i < indent; i++) {
out.append(" ");
buffer.append(" ");
}
}

private void printComments(List<Comment> comments, String indent) {
for (Comment comment : comments) {
out.append(indent);
out.println(comment.getValue());
out.println(indent + comment.getValue());
}
}

private void printTags(List<Tag> tags, String indent) {
if (tags.isEmpty()) return;
out.append(indent);
out.println(join(map(tags, tagNameMapper), " "));
out.println(indent + join(map(tags, tagNameMapper), " "));
}

private void printDescription(String description, String indentation, boolean newline) {
Expand Down
61 changes: 54 additions & 7 deletions java/src/test/java/gherkin/formatter/PrettyFormatterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
Expand All @@ -16,13 +17,7 @@
import static org.mockito.Mockito.verify;
import gherkin.parser.Parser;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -104,6 +99,58 @@ public void shouldFormatAsDesigned() throws IOException {

}

@Test
public void shouldAppendOnlyCompleteLinesAndFlushBetween() throws IOException {

StringBuilder featureBuilder = new StringBuilder();
featureBuilder.append("Feature: PrettyFormatter\n");
featureBuilder.append("Scenario: Formmat beautifully\n");
featureBuilder.append("When I have this table:\n");
featureBuilder.append("\t|name|value|\n");
featureBuilder.append("\t|a|b|\n");
featureBuilder.append("Then should formatt beautifully.\n");
String feature = featureBuilder.toString();

Formatter formatter = new PrettyFormatter(new CheckingAppendable(), false, false);
new Parser(formatter).parse(feature, "", 0);
formatter.close();
}

class CheckingAppendable implements Appendable, Flushable {
boolean flushed = true;

void checkAppend(CharSequence csq) {
if (!flushed) {
fail("No flush before append: " + csq);
}
assertThat("Must only append complete lines", csq.toString(), endsWith("\n"));
flushed = false;
}

@Override
public Appendable append(CharSequence csq) throws IOException {
checkAppend(csq);
return this;
}

@Override
public Appendable append(CharSequence csq, int start, int end) throws IOException {
checkAppend(csq.subSequence(start, end));
return this;
}

@Override
public Appendable append(char c) throws IOException {
checkAppend(String.valueOf(c));
return this;
}

@Override
public void flush() throws IOException {
flushed = true;
}
}

@Test
public void whenIMissSomeCellsInARowShouldFill() throws IOException {

Expand Down

0 comments on commit da93b41

Please sign in to comment.