38.4. SummaryIt can be well worthwhile to create custom fixtures so that Fit tables can express tests directly and clearly. We have looked at the development of two example fixtures:
Listing 38.3. ImageFixture.javapublic class ImageFixture extends Fixture { private String[][] actual; public ImageFixture(String[][] actual2DArrayOfImageFileNames) { actual = actual2DArrayOfImageFileNames; } public void doTable(Parse table) { if (actual.length == 0 && table.parts.more == null) right(table.parts); else if (!rowsMatch(actual, table.parts)) addActualRows(table.parts,actual); } private boolean rowsMatch(String[][] actual, Parse lastRow) { boolean matched = true; for (int i = 0; i < actual.length; i++) { lastRow = lastRow.more; if (!cellsMatch(actual[i],lastRow.parts)) matched = false; } return markWrong(lastRow.more, matched); } private boolean cellsMatch(String[] actual, Parse allCells) { Parse cells = allCells; boolean matched = true; for (int i = 0; i < actual.length; i++) { if (!cellMatches(actual[i], cells)) matched = false; cells = cells.more; } return markWrong(cells, matched); } private boolean cellMatches(String actual, Parse cell) { if (cell.body == null) return false; String image = getImageFileName(cell.body); boolean matches = finalFileName(actual).equals(finalFileName(image)); if (matches) right(cell); else wrong(cell); return matches; } // ... } |