Skip to content

Commit aca512d

Browse files
committed
[Core] Updated core for the use of Gherkin v4.0.0.
1 parent 51c8656 commit aca512d

File tree

147 files changed

+5930
-3476
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

147 files changed

+5930
-3476
lines changed

core/src/main/java/cucumber/api/DataTable.java

+29-23
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
import cucumber.runtime.table.TableConverter;
77
import cucumber.runtime.table.TableDiffException;
88
import cucumber.runtime.table.TableDiffer;
9+
import cucumber.runtime.table.TablePrinter;
910
import cucumber.runtime.xstream.LocalizedXStreams;
10-
import gherkin.formatter.PrettyFormatter;
11-
import gherkin.formatter.model.DataTableRow;
12-
import gherkin.formatter.model.Row;
11+
import gherkin.pickles.PickleCell;
12+
import gherkin.pickles.PickleRow;
13+
import gherkin.pickles.PickleTable;
1314

1415
import java.util.ArrayList;
1516
import java.util.Collections;
@@ -24,7 +25,7 @@
2425
public class DataTable {
2526

2627
private final List<List<String>> raw;
27-
private final List<DataTableRow> gherkinRows;
28+
private final PickleTable pickleTable;
2829
private final TableConverter tableConverter;
2930

3031
public static DataTable create(List<?> raw) {
@@ -48,17 +49,19 @@ private static DataTable create(List<?> raw, Locale locale, String format, Strin
4849
/**
4950
* Creates a new DataTable. This constructor should not be called by Cucumber users - it's used internally only.
5051
*
51-
* @param gherkinRows the underlying rows.
52+
* @param pickleTable the underlying table.
5253
* @param tableConverter how to convert the rows.
5354
*/
54-
public DataTable(List<DataTableRow> gherkinRows, TableConverter tableConverter) {
55-
this.gherkinRows = gherkinRows;
55+
public DataTable(PickleTable pickleTable, TableConverter tableConverter) {
56+
this.pickleTable = pickleTable;
5657
this.tableConverter = tableConverter;
57-
int columns = gherkinRows.isEmpty() ? 0 : gherkinRows.get(0).getCells().size();
58+
int columns = pickleTable.getRows().isEmpty() ? 0 : pickleTable.getRows().get(0).getCells().size();
5859
List<List<String>> raw = new ArrayList<List<String>>();
59-
for (Row row : gherkinRows) {
60+
for (PickleRow row : pickleTable.getRows()) {
6061
List<String> list = new ArrayList<String>();
61-
list.addAll(row.getCells());
62+
for (PickleCell cell : row.getCells()) {
63+
list.add(cell.getValue());
64+
}
6265
if (columns != row.getCells().size()) {
6366
throw new CucumberException(String.format("Table is unbalanced: expected %s column(s) but found %s.", columns, row.getCells().size()));
6467
}
@@ -67,8 +70,8 @@ public DataTable(List<DataTableRow> gherkinRows, TableConverter tableConverter)
6770
this.raw = Collections.unmodifiableList(raw);
6871
}
6972

70-
private DataTable(List<DataTableRow> gherkinRows, List<List<String>> raw, TableConverter tableConverter) {
71-
this.gherkinRows = gherkinRows;
73+
private DataTable(PickleTable pickleTable, List<List<String>> raw, TableConverter tableConverter) {
74+
this.pickleTable = pickleTable;
7275
this.tableConverter = tableConverter;
7376
this.raw = Collections.unmodifiableList(raw);
7477
}
@@ -206,24 +209,23 @@ public void unorderedDiff(List<?> other) throws TableDiffException {
206209
*
207210
* @return a list of raw rows.
208211
*/
209-
public List<DataTableRow> getGherkinRows() {
210-
return Collections.unmodifiableList(gherkinRows);
212+
public List<PickleRow> getPickleRows() {
213+
return Collections.unmodifiableList(pickleTable.getRows());
211214
}
212215

213216
@Override
214217
public String toString() {
215218
StringBuilder result = new StringBuilder();
216-
PrettyFormatter pf = new PrettyFormatter(result, true, false);
217-
pf.table(getGherkinRows());
218-
pf.eof();
219+
TablePrinter printer = createTablePrinter();
220+
printer.printTable(raw, result);
219221
return result.toString();
220222
}
221223

222224
public List<DiffableRow> diffableRows() {
223225
List<DiffableRow> result = new ArrayList<DiffableRow>();
224226
List<List<String>> convertedRows = raw();
225227
for (int i = 0; i < convertedRows.size(); i++) {
226-
result.add(new DiffableRow(getGherkinRows().get(i), convertedRows.get(i)));
228+
result.add(new DiffableRow(getPickleRows().get(i), convertedRows.get(i)));
227229
}
228230
return result;
229231
}
@@ -234,9 +236,9 @@ public TableConverter getTableConverter() {
234236

235237
public DataTable transpose() {
236238
List<List<String>> transposed = new ArrayList<List<String>>();
237-
for (int i = 0; i < gherkinRows.size(); i++) {
238-
Row gherkinRow = gherkinRows.get(i);
239-
for (int j = 0; j < gherkinRow.getCells().size(); j++) {
239+
for (int i = 0; i < pickleTable.getRows().size(); i++) {
240+
PickleRow pickleRow = pickleTable.getRows().get(i);
241+
for (int j = 0; j < pickleRow.getCells().size(); j++) {
240242
List<String> row = null;
241243
if (j < transposed.size()) {
242244
row = transposed.get(j);
@@ -245,10 +247,10 @@ public DataTable transpose() {
245247
row = new ArrayList<String>();
246248
transposed.add(row);
247249
}
248-
row.add(gherkinRow.getCells().get(j));
250+
row.add(pickleRow.getCells().get(j).getValue());
249251
}
250252
}
251-
return new DataTable(this.gherkinRows, transposed, this.tableConverter);
253+
return new DataTable(this.pickleTable, transposed, this.tableConverter);
252254
}
253255

254256
@Override
@@ -267,4 +269,8 @@ public boolean equals(Object o) {
267269
public int hashCode() {
268270
return raw.hashCode();
269271
}
272+
273+
protected TablePrinter createTablePrinter() {
274+
return new TablePrinter();
275+
}
270276
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package cucumber.api;
2+
3+
public enum HookType {
4+
Before, After;
5+
6+
@Override
7+
public String toString() {
8+
return super.toString().toLowerCase();
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package cucumber.api;
2+
3+
import java.io.PrintWriter;
4+
import java.io.StringWriter;
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
public class Result {
9+
private static final long serialVersionUID = 1L;
10+
11+
private final String status;
12+
private final Long duration;
13+
private final Throwable error;
14+
private final List<String> snippets;
15+
public static final Result SKIPPED = new Result("skipped", null, null);
16+
public static final String UNDEFINED = "undefined";
17+
public static final String PASSED = "passed";
18+
public static final String PENDING = "pending";
19+
public static final String FAILED = "failed";
20+
21+
/**
22+
* Used at runtime
23+
*
24+
* @param status
25+
* @param duration
26+
* @param error
27+
*/
28+
public Result(String status, Long duration, Throwable error) {
29+
this(status, duration, error, Collections.<String>emptyList());
30+
}
31+
32+
/**
33+
* Used at runtime
34+
*
35+
* @param status
36+
* @param duration
37+
* @param error
38+
* @param snippets
39+
*/
40+
public Result(String status, Long duration, Throwable error, List<String> snippets) {
41+
this.status = status;
42+
this.duration = duration;
43+
this.error = error;
44+
this.snippets = snippets;
45+
}
46+
47+
public String getStatus() {
48+
return status;
49+
}
50+
51+
public Long getDuration() {
52+
return duration;
53+
}
54+
55+
public String getErrorMessage() {
56+
return error != null ? getErrorMessage(error) : null;
57+
}
58+
59+
public Throwable getError() {
60+
return error;
61+
}
62+
63+
public List<String> getSnippets() {
64+
return snippets;
65+
}
66+
67+
public boolean isOk(boolean isStrict) {
68+
return hasAlwaysOkStatus() || !isStrict && hasOkWhenNotStrictStatus();
69+
}
70+
71+
private boolean hasAlwaysOkStatus() {
72+
return Result.PASSED.equals(status) || Result.SKIPPED.getStatus().equals(status);
73+
}
74+
75+
private boolean hasOkWhenNotStrictStatus() {
76+
return Result.UNDEFINED.equals(status) || Result.PENDING.equals(status);
77+
}
78+
79+
private String getErrorMessage(Throwable error) {
80+
StringWriter stringWriter = new StringWriter();
81+
PrintWriter printWriter = new PrintWriter(stringWriter);
82+
error.printStackTrace(printWriter);
83+
return stringWriter.getBuffer().toString();
84+
}
85+
}

core/src/main/java/cucumber/api/Scenario.java

-5
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,4 @@ public interface Scenario {
5151
* @return the name of the Scenario
5252
*/
5353
String getName();
54-
55-
/**
56-
* @return the id of the Scenario.
57-
*/
58-
String getId();
5954
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package cucumber.api;
2+
3+
import cucumber.api.event.TestCaseFinished;
4+
import cucumber.api.event.TestCaseStarted;
5+
import cucumber.runner.EventBus;
6+
import cucumber.runtime.ScenarioImpl;
7+
import gherkin.pickles.Pickle;
8+
import gherkin.pickles.PickleLocation;
9+
import gherkin.pickles.PickleTag;
10+
11+
import java.lang.reflect.Field;
12+
import java.util.Collections;
13+
import java.util.List;
14+
15+
public class TestCase {
16+
private final Pickle pickle;
17+
private final List<TestStep> testSteps;
18+
19+
public TestCase(List<TestStep> testSteps, Pickle pickle) {
20+
this.testSteps = testSteps;
21+
this.pickle = pickle;
22+
}
23+
24+
public void run(EventBus bus, String language) {
25+
boolean skipNextStep = false;
26+
bus.send(new TestCaseStarted(this));
27+
ScenarioImpl scenarioResult = new ScenarioImpl(bus, pickle);
28+
for (TestStep step : testSteps) {
29+
Result stepResult = step.run(bus, language, scenarioResult, skipNextStep);
30+
if (stepResult.getStatus() != Result.PASSED) {
31+
skipNextStep = true;
32+
}
33+
scenarioResult.add(stepResult);
34+
}
35+
bus.send(new TestCaseFinished(this, new Result(scenarioResult.getStatus(), null, null)));
36+
}
37+
38+
public String getName() {
39+
return pickle.getName();
40+
}
41+
42+
public String getScenarioDesignation() {
43+
return fileColonLine(pickle.getLocations().get(0)) + " # " + getName();
44+
}
45+
46+
public String getPath() {
47+
return pickle.getLocations().get(0).getPath();
48+
}
49+
50+
public int getLine() {
51+
return pickle.getLocations().get(0).getLine();
52+
}
53+
54+
private String fileColonLine(PickleLocation location) {
55+
return location.getPath() + ":" + Integer.toString(location.getLine());
56+
}
57+
58+
public List<PickleTag> getTags() {
59+
List<PickleTag> tags;
60+
try { // TODO: Fix when Gherkin provide a getter for the tags.
61+
Field f;
62+
f = pickle.getClass().getDeclaredField("tags");
63+
f.setAccessible(true);
64+
tags = (List<PickleTag>) f.get(pickle);
65+
} catch (Exception e) {
66+
tags = Collections.<PickleTag>emptyList();
67+
}
68+
return tags;
69+
}
70+
}

0 commit comments

Comments
 (0)