29.3. Two-Dimensional ImagesSection 11.3 on p. 83 introduced the use of images in a two-dimensional structure. The fixture StartSokoban for this is shown in Listing 29.3. The method board() carries out the following sequence:
ImageFixture takes a String[][] as an expected argument and attempts to match the elements against the files names of the images in the cells in the rest of the Fit table. If they match, the test succeeds. Otherwise, the ImageFixture adds rows to the table in the report to show the actual array of images. The ImageFixture class is covered in Section 38.3 on p. 314. Listing 29.3. StartSokoban.javapublic class StartSokoban extends DoFixture { private static final String PREFIX = "/files/images/"; private Sokoban sokoban = new Sokoban(); public Fixture board() { int[][] pieces = sokoban.ge2DArrayOfValues(); String[][]imageFileName = new String[pieces.length][]; for (int row = 0; row < pieces.length; row++) { imageFileName[row] = new String[pieces[row].length]; for (int col = 0; col < pieces[row].length; col++) imageFileName[row][col] = PREFIX+map(pieces[row][col]); } return new fit.ImageFixture(imageFileName); } private String map(int piece) { switch(piece) { case Sokoban.s: return "space.jpg"; case Sokoban.e: return "shelf.jpg"; case Sokoban.f: return "shelfwithbox.jpg"; case Sokoban.b: return "box.jpg"; case Sokoban.w: return "worker.jpg"; case Sokoban.W: return "wall.jpg"; } throw new RuntimeException("Unknown piece"); } } |