Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added table verification: unbalanced tables reported as errors

  • Loading branch information...
commit 6aef2cd6da909e05d4af02e461b178640ba25279 1 parent 77decde
Roberto Lo Giacco authored
View
37 core/src/main/java/cucumber/api/DataTable.java
@@ -1,5 +1,6 @@
package cucumber.api;
+import cucumber.runtime.CucumberException;
import cucumber.runtime.ParameterInfo;
import cucumber.runtime.table.DiffableRow;
import cucumber.runtime.table.TableConverter;
@@ -55,10 +56,14 @@ private static DataTable create(List<?> raw, Locale locale, String format, Strin
public DataTable(List<DataTableRow> gherkinRows, TableConverter tableConverter) {
this.gherkinRows = gherkinRows;
this.tableConverter = tableConverter;
+ int columns = gherkinRows.get(0).getCells().size();
List<List<String>> raw = new ArrayList<List<String>>();
for (Row row : gherkinRows) {
List<String> list = new ArrayList<String>();
list.addAll(row.getCells());
+ if (columns != row.getCells().size()) {
+ throw new CucumberException(String.format("Table is unbalanced: expected %s column(s) but found %s.", columns, row.getCells().size()));
+ }
raw.add(Collections.unmodifiableList(list));
}
this.raw = Collections.unmodifiableList(raw);
@@ -201,24 +206,20 @@ public TableConverter getTableConverter() {
public DataTable transpose() {
List<List<String>> transposed = new ArrayList<List<String>>();
for (int i = 0; i < gherkinRows.size(); i++) {
- Row gherkinRow = gherkinRows.get(i);
- for (int j = 0; j < gherkinRow.getCells().size(); j++) {
- List<String> row = null;
- if (j < transposed.size()) {
- row = transposed.get(j);
- }
- if (row == null) {
- row = new ArrayList<String>();
- transposed.add(row);
- }
- // fixes non rectangular tables
- while (i > row.size()) {
- row.add(new String());
- }
- row.add(gherkinRow.getCells().get(j));
- }
- }
- return new DataTable(this.gherkinRows, transposed, this.tableConverter);
+ Row gherkinRow = gherkinRows.get(i);
+ for (int j = 0; j < gherkinRow.getCells().size(); j++) {
+ List<String> row = null;
+ if (j < transposed.size()) {
+ row = transposed.get(j);
+ }
+ if (row == null) {
+ row = new ArrayList<String>();
+ transposed.add(row);
+ }
+ row.add(gherkinRow.getCells().get(j));
+ }
+ }
+ return new DataTable(this.gherkinRows, transposed, this.tableConverter);
}
@Override
View
31 core/src/test/java/cucumber/runtime/table/DataTableTest.java
@@ -1,6 +1,7 @@
package cucumber.runtime.table;
import cucumber.api.DataTable;
+import cucumber.runtime.CucumberException;
import cucumber.runtime.xstream.LocalizedXStreams;
import gherkin.formatter.model.Comment;
import gherkin.formatter.model.DataTableRow;
@@ -35,15 +36,15 @@ public void transposedRawShouldHaveTwoColumnsAndThreeRows() {
}
}
- @Test
- public void canTransposeNonRectangularTables() {
- List<List<String>> raw = createNonRectangularTable().transpose().raw();
- assertEquals("Rows size", 4, raw.size());
- assertEquals("Cols size: " + raw.get(0), 4, raw.get(0).size());
- assertEquals("Cols size: " + raw.get(1), 4, raw.get(1).size());
- assertEquals("Cols size: " + raw.get(2), 2, raw.get(2).size());
- assertEquals("Cols size: " + raw.get(3), 2, raw.get(3).size());
- }
+ @Test(expected=CucumberException.class)
+ public void canNotSupportNonRectangularTablesMissingColumn() {
+ List<List<String>> raw = createNonRectangularTableMissingColumn().raw();
+ }
+
+ @Test(expected=CucumberException.class)
+ public void canNotSupportNonRectangularTablesExceedingColumn() {
+ List<List<String>> raw = createNonRectangularTableExceedingColumn().raw();
+ }
@Test
public void canCreateTableFromListOfListOfString() {
@@ -108,11 +109,15 @@ public DataTable createSimpleTable() {
return createTable(asList("one", "four", "seven"), asList("4444", "55555", "666666"));
}
- public DataTable createNonRectangularTable() {
+ public DataTable createNonRectangularTableMissingColumn() {
+ return createTable(asList("one", "four", "seven"),
+ asList("a1", "a4444"),
+ asList("b1"));
+ }
+
+ public DataTable createNonRectangularTableExceedingColumn() {
return createTable(asList("one", "four", "seven"),
- asList("a1", "a4444", "a7777777", "zero"),
- asList("b1"),
- asList("c1", "c4444"));
+ asList("a1", "a4444", "b7777777", "zero"));
}
private DataTable createTable(List<String>... rows) {
Please sign in to comment.
Something went wrong with that request. Please try again.