Skip to content

Commit

Permalink
Added accessors to TableDiffException. Closes #384.
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed Mar 10, 2013
1 parent 036942b commit 1ecc87d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 16 deletions.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## [Git master](https://github.com/cucumber/cucumber-jvm/compare/v1.1.2...master) (Not released)

* [Core] Added accessors to `TableDiffException`. ([#384](https://github.com/cucumber/cucumber-jvm/issues/384) Aslak Hellesøy)
* [Core] Fixed use of formatter to list all step results in JSON output ([#426](https://github.com/cucumber/cucumber-jvm/pull/426) agattiker)
* [Scala] Add support for DataTable and locale-aware type transformations. ([#443](https://github.com/cucumber/cucumber-jvm/issues/443), [#455](https://github.com/cucumber/cucumber-jvm/pull/455) Matthew Lucas)
* [Groovy] Groovy should throw exception if more then one World registred ([#464](https://github.com/cucumber/cucumber-jvm/pull/464), [#458](https://github.com/cucumber/cucumber-jvm/issues/458) Luxor)
Expand Down
32 changes: 30 additions & 2 deletions core/src/main/java/cucumber/runtime/table/TableDiffException.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,35 @@
import cucumber.api.DataTable;

public class TableDiffException extends RuntimeException {
public TableDiffException(DataTable tableDiff) {
super("Tables were not identical:\n" + tableDiff.toString());
private final DataTable from;
private final DataTable to;
private final DataTable diff;

public TableDiffException(DataTable from, DataTable to, DataTable diff) {
super("Tables were not identical:\n" + diff.toString());
this.from = from;
this.to = to;
this.diff = diff;
}

/**
* @return the left side of the diff
*/
public DataTable getFrom() {
return from;
}

/**
* @return the right side of the diff
*/
public DataTable getTo() {
return to;
}

/**
* @return the diff itself - represented as a table
*/
public DataTable getDiff() {
return diff;
}
}
24 changes: 12 additions & 12 deletions core/src/main/java/cucumber/runtime/table/TableDiffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

public class TableDiffer {

private final DataTable orig;
private final DataTable other;
private final DataTable from;
private final DataTable to;

public TableDiffer(DataTable origTable, DataTable otherTable) {
checkColumns(origTable, otherTable);
this.orig = origTable;
this.other = otherTable;
public TableDiffer(DataTable fromTable, DataTable toTable) {
checkColumns(fromTable, toTable);
this.from = fromTable;
this.to = toTable;
}

private void checkColumns(DataTable a, DataTable b) {
Expand All @@ -30,11 +30,11 @@ private void checkColumns(DataTable a, DataTable b) {
}

public void calculateDiffs() throws TableDiffException {
Patch patch = DiffUtils.diff(orig.diffableRows(), other.diffableRows());
Patch patch = DiffUtils.diff(from.diffableRows(), to.diffableRows());
List<Delta> deltas = patch.getDeltas();
if (!deltas.isEmpty()) {
Map<Integer, Delta> deltasByLine = createDeltasByLine(deltas);
throw new TableDiffException(createTableDiff(deltasByLine));
throw new TableDiffException(from, to, createTableDiff(deltasByLine));
}
}

Expand All @@ -48,18 +48,18 @@ private Map<Integer, Delta> createDeltasByLine(List<Delta> deltas) {

private DataTable createTableDiff(Map<Integer, Delta> deltasByLine) {
List<DataTableRow> diffTableRows = new ArrayList<DataTableRow>();
List<List<String>> rows = orig.raw();
List<List<String>> rows = from.raw();
for (int i = 0; i < rows.size(); i++) {
Delta delta = deltasByLine.get(i);
if (delta == null) {
diffTableRows.add(orig.getGherkinRows().get(i));
diffTableRows.add(from.getGherkinRows().get(i));
} else {
addRowsToTableDiff(diffTableRows, delta);
// skipping lines involved in a delta
if (delta.getType() == Delta.TYPE.CHANGE || delta.getType() == Delta.TYPE.DELETE) {
i += delta.getOriginal().getLines().size() - 1;
} else {
diffTableRows.add(orig.getGherkinRows().get(i));
diffTableRows.add(from.getGherkinRows().get(i));
}
}
}
Expand All @@ -68,7 +68,7 @@ private DataTable createTableDiff(Map<Integer, Delta> deltasByLine) {
if (remainingDelta != null) {
addRowsToTableDiff(diffTableRows, remainingDelta);
}
return new DataTable(diffTableRows, orig.getTableConverter());
return new DataTable(diffTableRows, from.getTableConverter());
}

private void addRowsToTableDiff(List<DataTableRow> diffTableRows, Delta delta) {
Expand Down
23 changes: 21 additions & 2 deletions core/src/test/java/cucumber/runtime/table/TableDifferTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;

public class TableDifferTest {

Expand Down Expand Up @@ -175,7 +176,6 @@ public void should_diff_when_consecutive_changed_lines() {
assertEquals(expected, e.getMessage());
throw e;
}

}

@Test(expected = TableDiffException.class)
Expand All @@ -195,7 +195,27 @@ public void should_diff_when_consecutive_inserted_lines() {
assertEquals(expected, e.getMessage());
throw e;
}
}

@Test(expected = TableDiffException.class)
public void should_return_tables() {
DataTable from = table();
DataTable to = otherTableWithTwoConsecutiveRowsInserted();
try {
from.diff(to);
} catch (TableDiffException e) {
String expected = "" +
" | Aslak | [email protected] | 123 |\n" +
" | Joe | [email protected] | 234 |\n" +
" + | Doe | [email protected] | 234 |\n" +
" + | Foo | [email protected] | 789 |\n" +
" | Bryan | [email protected] | 456 |\n" +
" | Ni | [email protected] | 654 |\n";
assertSame(from, e.getFrom());
assertSame(to, e.getTo());
assertEquals(expected, e.getDiff().toString());
throw e;
}
}

public static class TestPojo {
Expand All @@ -210,7 +230,6 @@ public TestPojo(Integer id, String givenName, int decisionCriteria) {
}
}


@Test
public void diff_with_list_of_pojos_and_camelcase_header_mapping() {
String source = "" +
Expand Down

0 comments on commit 1ecc87d

Please sign in to comment.