38.3. ImageFixtureAs we saw in Section 11.3 on p. 83, Fit table cells can contain images as well as text. In this section, we look briefly at the code for ImageFixture, a general-purpose fixture to test such tables against a list that is supplied to it by the system under test. Listing 38.2. SetUpFixture.javapublic class SetUpFixture extends CalculateFixture { private MethodTarget target; public void doTable(Parse table) { try { setUp(); super.doTable(table); tearDown(); } catch (Exception e) { exception(table.at(0, 0, 0),e); } } protected void bind(Parse headerRow) { Parse cells = headerRow; argCount = cells.size(); String argNames = ""; while (cells != null) { argNames += " " + cells.text(); cells = cells.more; } String methodName = ExtendedCamelCase.camel(argNames); try { target = findMethod(methodName,argCount); boundOK = true; } catch (Exception e) { exception(headerRow, e); } } public void doRow(Parse row) { if (!boundOK) { ignore(row.parts); return; } if (row.parts.size() != argCount) { exception(row.parts,"Row should be "+argCount+" cells wide"); return; } try { target.invoke(row.parts); } catch (Exception e) { exception(row.parts,e); } } protected void setUp() throws Exception { } protected void tearDown() throws Exception { } } Note FixtureFixture tables can be used in the test-driven development of custom Fit fixtures [Mug04]. FixtureFixture has been used to define the core and FitLibrary fixtures. The core fixtures are also defined with fat; see [Cun]. Class ImageFixture compares a 2D array of the names of image files, based on the actual data, against the referenced images in the Fit table. The actual data is supplied through the constructor. Part of the class ImageFixture is shown in Listing 38.3.[1] Some of the error-handling code is not shown in these methods.
The method doTable() first handles the case in which the collections are both emptytable.parts.more refers to the second row of the tablein which case it colors the header row green through a call to the Fixture method right(). Otherwise, doTable() calls rowsMatch() to match the actual and expected. If the match fails, addActualRows() (not shown) appends all the actual data to the bottom of the table. It is easier to understand the mismatches when the actual images are shown underneath the expected ones, rather than interleaved with the expected rows. The rowsMatch() method in Listing 38.3 calls cellsMatch() to check each actual row against what's expected. This method in turn calls cellMatches() to check each individual cell, by accessing the HTML text in the cell to get the image reference, such as img src="/books/4/59/1/html/2/wall.gif" width=32 height=32. In cellMatches(), we see the use of the Fixture method wrong() to color a table cell red. |