diff --git a/java/src/main/java/gherkin/formatter/PrettyFormatter.java b/java/src/main/java/gherkin/formatter/PrettyFormatter.java index 516a0c0d..7d098e98 100644 --- a/java/src/main/java/gherkin/formatter/PrettyFormatter.java +++ b/java/src/main/java/gherkin/formatter/PrettyFormatter.java @@ -282,7 +282,7 @@ public void table(List rows) { private void prepareTable(List rows) { this.rows = rows; - + // find the largest row int columnCount = 0; for (Row row : rows) { @@ -295,15 +295,10 @@ private void prepareTable(List rows) { maxLengths = new int[columnCount]; for (int rowIndex = 0; rowIndex < rows.size(); rowIndex++) { Row row = rows.get(rowIndex); - List cells = row.getCells(); + final List cells = row.getCells(); for (int colIndex = 0; colIndex < columnCount; colIndex++) { - - // check missing cells - if (colIndex >= cells.size()) { - cells.add(""); - } - String cell = cells.get(colIndex); - int length = escapeCell(cell).length(); + final String cell = getCellSafely(cells, colIndex); + final int length = escapeCell(cell).length(); cellLengths[rowIndex][colIndex] = length; maxLengths[colIndex] = Math.max(maxLengths[colIndex], length); } @@ -311,6 +306,10 @@ private void prepareTable(List rows) { rowIndex = 0; } + private String getCellSafely(final List cells, final int colIndex) { + return (colIndex < cells.size()) ? cells.get(colIndex) : ""; + } + public void row(List cellResults) { Row row = rows.get(rowIndex); if (rowsAbove) { @@ -337,11 +336,11 @@ public void row(List cellResults) { break; } for (int colIndex = 0; colIndex < maxLengths.length; colIndex++) { - String cellText = escapeCell(row.getCells().get(colIndex)); + String cellText = escapeCell(getCellSafely(row.getCells(), colIndex)); String status = null; switch (row.getDiffType()) { case NONE: - status = cellResults.get(colIndex).getStatus(); + status = cellResults.size() < colIndex ? cellResults.get(colIndex).getStatus() : "skipped"; break; case DELETE: status = "skipped"; @@ -471,4 +470,4 @@ private static String indent(String s, String indentation) { private static String escapeTripleQuotes(String s) { return TRIPLE_QUOTES.matcher(s).replaceAll(ESCAPED_TRIPLE_QUOTES); } -} \ No newline at end of file +} diff --git a/java/src/test/java/gherkin/formatter/PrettyFormatterTest.java b/java/src/test/java/gherkin/formatter/PrettyFormatterTest.java index 947c50d0..997b8bcd 100644 --- a/java/src/test/java/gherkin/formatter/PrettyFormatterTest.java +++ b/java/src/test/java/gherkin/formatter/PrettyFormatterTest.java @@ -171,5 +171,5 @@ private List doFormatter(String feature) throws IOException { return lines; } - + }