diff --git a/core/src/main/java/cucumber/runtime/table/TableDiffer.java b/core/src/main/java/cucumber/runtime/table/TableDiffer.java index a5b533882c..eee679067d 100644 --- a/core/src/main/java/cucumber/runtime/table/TableDiffer.java +++ b/core/src/main/java/cucumber/runtime/table/TableDiffer.java @@ -38,6 +38,14 @@ public void calculateDiffs() throws TableDiffException { } } + private Map createDeltasByLine(List deltas) { + Map deltasByLine = new HashMap(); + for (Delta delta : deltas) { + deltasByLine.put(delta.getOriginal().getPosition(), delta); + } + return deltasByLine; + } + private DataTable createTableDiff(Map deltasByLine) { List diffTableRows = new ArrayList(); List> rows = orig.raw(); @@ -47,6 +55,12 @@ private DataTable createTableDiff(Map deltasByLine) { diffTableRows.add(orig.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)); + } } } // Can have new lines at end @@ -58,23 +72,21 @@ private DataTable createTableDiff(Map deltasByLine) { } private void addRowsToTableDiff(List diffTableRows, Delta delta) { - if (delta.getType() == Delta.TYPE.CHANGE || delta.getType() == Delta.TYPE.DELETE) { - List deletedLines = (List) delta.getOriginal().getLines(); - for (DiffableRow row : deletedLines) { - diffTableRows.add(new DataTableRow(row.row.getComments(), row.row.getCells(), row.row.getLine(), Row.DiffType.DELETE)); - } + markChangedAndDeletedRowsInOriginalAsMissing(diffTableRows, delta); + markChangedAndInsertedRowsInRevisedAsNew(diffTableRows, delta); + } + + private void markChangedAndDeletedRowsInOriginalAsMissing(List diffTableRows, Delta delta) { + List deletedLines = (List) delta.getOriginal().getLines(); + for (DiffableRow row : deletedLines) { + diffTableRows.add(new DataTableRow(row.row.getComments(), row.row.getCells(), row.row.getLine(), Row.DiffType.DELETE)); } + } + + private void markChangedAndInsertedRowsInRevisedAsNew(List diffTableRows, Delta delta) { List insertedLines = (List) delta.getRevised().getLines(); for (DiffableRow row : insertedLines) { diffTableRows.add(new DataTableRow(row.row.getComments(), row.row.getCells(), row.row.getLine(), Row.DiffType.INSERT)); } } - - private Map createDeltasByLine(List deltas) { - Map deltasByLine = new HashMap(); - for (Delta delta : deltas) { - deltasByLine.put(delta.getOriginal().getPosition(), delta); - } - return deltasByLine; - } } diff --git a/core/src/test/java/cucumber/runtime/table/TableDifferTest.java b/core/src/test/java/cucumber/runtime/table/TableDifferTest.java index 7c650c885e..b1a14acba8 100755 --- a/core/src/test/java/cucumber/runtime/table/TableDifferTest.java +++ b/core/src/test/java/cucumber/runtime/table/TableDifferTest.java @@ -20,6 +20,34 @@ private DataTable table() { return TableParser.parse(source, null); } + private DataTable otherTableWithTwoConsecutiveRowsDeleted() { + String source = "" + + "| Aslak | aslak@email.com | 123 |\n" + + "| Ni | ni@email.com | 654 |\n"; + return TableParser.parse(source, null); + + } + + private DataTable otherTableWithTwoConsecutiveRowsChanged() { + String source = "" + + "| Aslak | aslak@email.com | 123 |\n" + + "| Joe | joe@NOSPAM.com | 234 |\n" + + "| Bryan | bryan@NOSPAM.org | 456 |\n" + + "| Ni | ni@email.com | 654 |\n"; + return TableParser.parse(source, null); + } + + private DataTable otherTableWithTwoConsecutiveRowsInserted() { + String source = "" + + "| Aslak | aslak@email.com | 123 |\n" + + "| Joe | joe@email.com | 234 |\n" + + "| Doe | joe@email.com | 234 |\n" + + "| Foo | schnickens@email.net | 789 |\n" + + "| Bryan | bryan@email.org | 456 |\n" + + "| Ni | ni@email.com | 654 |\n"; + return TableParser.parse(source, null); + } + private DataTable otherTableWithDeletedAndInserted() { String source = "" + "| Aslak | aslak@email.com | 123 |\n" + @@ -59,6 +87,7 @@ public void shouldFindDifferences() { } } + @Test(expected = TableDiffException.class) public void shouldFindNewLinesAtEnd() { try { @@ -105,21 +134,68 @@ public void shouldFindNewLinesAtEndWhenUsingDiff() { public void should_not_fail_with_out_of_memory() { DataTable expected = TableParser.parse("" + "| I'm going to work |\n", null); - List> actual = new ArrayList>(); - actual.add(asList("I just woke up")); actual.add(asList("I'm going to work")); + expected.diff(actual); + } + + @Test(expected = TableDiffException.class) + public void should_diff_when_consecutive_deleted_lines() { + try { + List> other = otherTableWithTwoConsecutiveRowsDeleted().raw(); + table().diff(other); + } catch (TableDiffException e) { + String expected = "" + + "Tables were not identical:\n" + + " | Aslak | aslak@email.com | 123 |\n" + + " - | Joe | joe@email.com | 234 |\n" + + " - | Bryan | bryan@email.org | 456 |\n" + + " | Ni | ni@email.com | 654 |\n"; + assertEquals(expected, e.getMessage()); + throw e; + } + + } + + @Test(expected = TableDiffException.class) + public void should_diff_when_consecutive_changed_lines() { + try { + List> other = otherTableWithTwoConsecutiveRowsChanged().raw(); + table().diff(other); + } catch (TableDiffException e) { + String expected = "" + + "Tables were not identical:\n" + + " | Aslak | aslak@email.com | 123 |\n" + + " - | Joe | joe@email.com | 234 |\n" + + " - | Bryan | bryan@email.org | 456 |\n" + + " + | Joe | joe@NOSPAM.com | 234 |\n" + + " + | Bryan | bryan@NOSPAM.org | 456 |\n" + + " | Ni | ni@email.com | 654 |\n"; + assertEquals(expected, e.getMessage()); + throw e; + } + + } + @Test(expected = TableDiffException.class) + public void should_diff_when_consecutive_inserted_lines() { try { - expected.diff(actual); + List> other = otherTableWithTwoConsecutiveRowsInserted().raw(); + table().diff(other); } catch (TableDiffException e) { - String expectedDiff = "" + + String expected = "" + "Tables were not identical:\n" + - " + | I just woke up |\n"; - assertEquals(expectedDiff, e.getMessage()); + " | Aslak | aslak@email.com | 123 |\n" + + " | Joe | joe@email.com | 234 |\n" + + " + | Doe | joe@email.com | 234 |\n" + + " + | Foo | schnickens@email.net | 789 |\n" + + " | Bryan | bryan@email.org | 456 |\n" + + " | Ni | ni@email.com | 654 |\n"; + assertEquals(expected, e.getMessage()); throw e; } + } public static class TestPojo { @@ -134,6 +210,7 @@ public TestPojo(Integer id, String givenName, int decisionCriteria) { } } + @Test public void diff_with_list_of_pojos_and_camelcase_header_mapping() { String source = "" +