我目前正在将最初用 ruby 编写的测试套件移植到 java。
列表<字符串[]>
我尝试移植的第一步将 CSV 数据解析为List<String[]>
@Then("test 1")
public void test1( DataTable expectedTable ) {
List<String[]> tableData = getCsvData( fileName );
// I have also tried List<Map<String,String>> here.
// when List<String[]> includes the column names in element 0, TableConverter.toTable()
// for List<Map<String,String>>, TableConverter.toTable() ends up with
// writer: { columnNames:(as provided in element 0),
// fieldNames: ["entry", "entry", "entry"...]
// fieldValues: [colName0, row1Value0, colName1, row1Value1...] }
// and then ComplexTypeWriter.getValues() calls
// int index = fieldNames.indexOf(converter.map(columnName));
// where columnName is correct, but index is evaluated as -1, so getValues() returns
// [, , , ,...]
// so .diff() displays a table of empty strings.
expectedTable.diff( tableData );
}
...cucumber-jvm 无法正确显示实际的 CSV 数据。
列表<地图<字符串,字符串>>
在我们的 ruby 实现中,其他测试步骤用于Cucumber::Ast::Table.diff!
显示失败的原因:
failure = {'line number' => line, 'reason' => 'bad data in column 2', 'data' => column2}
failures.push failure
Cucumber::Ast::Table.new([[]]).diff!( failures, {surplus_col: true, surplus_row: true} ) unless failures.empty?
我尝试使用 将其移植到 java java.util.Map
,如下所示。问题在于,尽管 cucumber-jvm 识别出s 的 emptyDataTable
和 my之间存在差异,但它并不能正确解析(或显示)my。List
Map
List<Map>
Map<String,String> failure = new HashMap<String,String>();
failure.put("line number", Integer.toString(line));
failure.put("reason", "bad data in column 2");
failure.put("data", Arrays.toString(column2));
List<Map<String,String> failures = new ArrayList<Map<String,String>>();
failures.add(failure);
// We're expecting an empty list of failures, so create one to compare against.
String[] columnNames = failures.get(0).keySet().toArray(new String[]{});
ArrayList<Map<String, String>> emptyList = new ArrayList<Map<String,String>>();
HashMap<String, String> emptyData = new HashMap<String, String>();
for( String columnName : failures.get(0).keySet() ) {
emptyData.put(columnName, null);
}
emptyList.add(emptyData);
DataTable empty = DataTable.create( emptyList, Locale.getDefault(), columnNames );
empty.diff( failures );