Skip to content

Commit

Permalink
Simplified formatter/reporter API by removing the steps() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed Jan 2, 2011
1 parent 13fc624 commit 3a673de
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 145 deletions.
3 changes: 1 addition & 2 deletions java/Gherkin.iml
Expand Up @@ -10,9 +10,8 @@
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.fusesource.jansi:jansi:1.4" level="project" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.googlecode.json-simple:json-simple:1.1" level="project" />
<orderEntry type="library" name="Maven: net.iharder:base64:2.3.8" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.8.1" level="project" />
Expand Down
3 changes: 0 additions & 3 deletions java/src/main/java/gherkin/formatter/JSONFormatter.java
Expand Up @@ -90,9 +90,6 @@ public void syntaxError(String state, String event, List<String> legalEvents, St
throw new UnsupportedOperationException();
}

public void steps(List<Step> steps) {
}

private List<Object> getFeatureElements() {
List<Object> featureElements = (List) featureHash.get("elements");
if (featureElements == null) {
Expand Down
130 changes: 71 additions & 59 deletions java/src/main/java/gherkin/formatter/PrettyFormatter.java
Expand Up @@ -8,9 +8,7 @@
import java.io.PrintWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.regex.Pattern;

import static gherkin.util.FixJava.join;
Expand All @@ -28,17 +26,13 @@ public class PrettyFormatter implements Reporter {
private final boolean monochrome;
private final boolean executing;

private int maxStepLength = -1;
private int[] stepLengths;
private int stepIndex;
private String uri;
private Mapper tagNameMapper = new Mapper() {
public String map(Object tag) {
return ((Tag) tag).getName();
}
};
private Formats formats;
private Step step;
private Match match;
private int[][] cellLengths;
private int[] maxLengths;
Expand All @@ -47,6 +41,10 @@ public String map(Object tag) {
private Integer rowHeight = null;
private boolean rowsAbove = false;

private List<Step> steps = new ArrayList<Step>();
private List<Integer> indentations = new ArrayList<Integer>();
private DescribedStatement statement;

public PrettyFormatter(Writer out, boolean monochrome, boolean executing) {
this.out = new PrintWriter(out);
this.monochrome = monochrome;
Expand Down Expand Up @@ -79,38 +77,68 @@ public void feature(Feature feature) {
}

public void background(Background background) {
out.println();
printComments(background.getComments(), " ");
printDescribedStatement(background);
replay();
statement = background;
}

public void scenario(Scenario scenario) {
printTagStatement(scenario);
replay();
statement = scenario;
}

public void scenarioOutline(ScenarioOutline scenarioOutline) {
printTagStatement(scenarioOutline);
replay();
statement = scenarioOutline;
}

private void printTagStatement(TagStatement statement) {
out.println();
printComments(statement.getComments(), " ");
printTags(statement.getTags(), " ");
printDescribedStatement(statement);
private void replay() {
printStatement();
printSteps();
}

private void printSteps() {
while(!steps.isEmpty()) {
printStep("skipped", Collections.<Argument>emptyList(), null, true);
}
}

private void printDescribedStatement(DescribedStatement statement) {
private void printStatement() {
if (statement == null) {
return;
}
calculateLocationIndentations();
out.println();
printComments(statement.getComments(), " ");
if (statement instanceof TagStatement) {
printTags(((TagStatement) statement).getTags(), " ");
}
out.print(" ");
out.print(statement.getKeyword());
out.print(": ");
out.print(statement.getName());
printIndentedLocation(statement.getKeyword(), statement.getName(), statement.getLine());
out.println();
String location = executing ? uri + ":" + statement.getLine() : null;
out.println(indentedLocation(location, true));
printDescription(statement.getDescription(), " ", true);
out.flush();
statement = null;
}

private String indentedLocation(String location, boolean proceed) {
StringBuffer sb = new StringBuffer();
int indentation = proceed ? indentations.remove(0) : indentations.get(0);
if(location == null) {
return "";
}
for (int i = 0; i < indentation; i++) {
sb.append(' ');
}
sb.append(' ');
sb.append(getFormat("comment").text("# " + location));
return sb.toString();
}

public void examples(Examples examples) {
replay();
out.println();
printComments(examples.getComments(), " ");
printTags(examples.getTags(), " ");
Expand All @@ -124,41 +152,37 @@ public void examples(Examples examples) {
}

public void step(Step step) {
this.step = step;
stepIndex++;
if (!executing) {
match(Match.NONE);
result(Result.SKIPPED);
}
steps.add(step);
}

public void match(Match match) {
this.match = match;
printStatement();
if (!monochrome) {
printStep("executing", match.getArguments(), match.getLocation());
printStep("executing", match.getArguments(), match.getLocation(), false);
}
}

public void result(Result result) {
if (!monochrome) {
out.print(formats.up(1));
}
printStep(result.getStatus(), match.getArguments(), match.getLocation());

printStep(result.getStatus(), match.getArguments(), match.getLocation(), true);
if (result.getErrorMessage() != null) {
printError(result);
}
}

private void printStep(String status, List<Argument> arguments, String location) {
private void printStep(String status, List<Argument> arguments, String location, boolean proceed) {
Step step = proceed ? steps.remove(0) : steps.get(0);
Format textFormat = getFormat(status);
Format argFormat = getArgFormat(status);

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

out.println();
if (step.getRows() != null) {
Expand Down Expand Up @@ -238,7 +262,7 @@ public void row(List<CellResult> cellResults) {
for (Result result : cellResult.getResults()) {
if (result.getErrorMessage() != null && !seenResults.contains(result)) {
printError(result);
rowHeight += result.getErrorMessage().split("\n").length;
rowHeight += result.getErrorMessage().split("\n").length;
seenResults.add(result);
}
}
Expand Down Expand Up @@ -271,18 +295,26 @@ public void pyString(PyString pyString) {
}

public void eof() {
replay();
out.flush();
}

public void steps(List<Step> steps) {
stepLengths = new int[steps.size()];
private void calculateLocationIndentations() {
int[] lineWidths = new int[steps.size() + 1];
int i = 0;
for (Step step : steps) {
int stepLength = step.getKeyword().length() + step.getName().length();
stepLengths[i++] = stepLength;
maxStepLength = Math.max(maxStepLength, stepLength);

List<BasicStatement> statements = new ArrayList<BasicStatement>();
statements.add(statement);
statements.addAll(steps);
int maxLineWidth = 0;
for (BasicStatement statement : statements) {
int stepWidth = statement.getKeyword().length() + statement.getName().length();
lineWidths[i++] = stepWidth;
maxLineWidth = Math.max(maxLineWidth, stepWidth);
}
for (int lineWidth : lineWidths) {
indentations.add(maxLineWidth - lineWidth);
}
stepIndex = -1;
}

public void embedding(String mimeType, byte[] data) {
Expand All @@ -309,26 +341,6 @@ private void printTags(List<Tag> tags, String indent) {
out.flush();
}

private void printIndentedLocation(String keyword, String name, long line) {
if (maxStepLength == -1) return;
int l = keyword.length() + name.length();
maxStepLength = Math.max(maxStepLength, l);
int indent = maxStepLength - l;

padSpace(indent);
out.append(" ");
out.print(getFormat("comment").text("# " + uri + ":" + line));
}

private void printIndentedStepLocation(String location) {
if (location == null || "".equals(location)) return;
int indent = maxStepLength - stepLengths[stepIndex];

padSpace(indent);
out.append(" ");
out.print(getFormat("comment").text("# " + location));
}

private void printDescription(String description, String indentation, boolean newline) {
if (!"".equals(description)) {
out.println(indent(description, indentation));
Expand Down
1 change: 0 additions & 1 deletion java/src/main/java/gherkin/formatter/Reporter.java
Expand Up @@ -5,7 +5,6 @@
import java.util.List;

public interface Reporter extends Formatter {
void steps(List<Step> steps);
void result(Result result);
void match(Match match);
void embedding(String mimeType, byte[] data);
Expand Down
11 changes: 4 additions & 7 deletions java/src/test/java/gherkin/formatter/PrettyFormatterTest.java
@@ -1,22 +1,19 @@
package gherkin.formatter;

import gherkin.formatter.model.Comment;
import gherkin.formatter.model.Match;
import gherkin.formatter.model.Result;
import gherkin.formatter.model.Step;
import gherkin.formatter.model.*;
import org.junit.Test;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class PrettyFormatterTest {
@Test
public void testShouldPrintNiceColors() throws UnsupportedEncodingException, InterruptedException {
PrettyFormatter f = new PrettyFormatter(System.out, false, false);
Step step = new Step(new ArrayList<Comment>(), "Given ", "I have 6 cukes", 1);
f.steps(Arrays.asList(step));
f.step(step);
f.scenario(new Scenario(Collections.<Comment>emptyList(), Collections.<Tag>emptyList(), "Scenario", "a scenario", "", 1));
f.step(new Step(new ArrayList<Comment>(), "Given ", "I have 6 cukes", 1));
Thread.sleep(1000);
f.match(new Match(Arrays.asList(new Argument(7, "6")), "somewhere.brainfuck"));
Thread.sleep(1000);
Expand Down

0 comments on commit 3a673de

Please sign in to comment.