Section 3

Here's the test grid shown visually.

Here is the first unit test. We'll start with the blank cell in the upper left corner (1@1) and prove that it doesn't become a non-blank cell with any of the four direction pushes.

In the above test we re-fetch the cell each time right after the push because, for mirror cells, we expect the cell to be different. So we'll establish a testing pattern here which we'll replicate in the other push tests. We can construct a very similar test to ensure the target cell doesn't move.

testPushTargetCell
    | grid cell |
    grid := self generateDemoGrid.
    grid pushCellSouthFromLocation: 5@1.
    cell := grid at: 5@1.
    self should: [cell class = TargetCell].
    grid pushCellNorthFromLocation: 5@1.
    cell := grid at: 5@1.
    self should: [cell class = TargetCell].
    grid pushCellWestFromLocation: 5@1.
    cell := grid at: 5@1.
    self should: [cell class = TargetCell].
    grid pushCellEastFromLocation: 5@1.
    cell := grid at: 5@1.
    self should: [cell class = TargetCell].

And of course if we run our unit tests they currently pass. No surprise, since we haven't written code to move any cell yet.

So what about the mirror cells? We should test individual mirror cells, as well as mirror cells against the edges of the grid, and adjacent to other mirror cells, and adjacent to the target cell. We will write several methods to test scenarios for mirror cells. Refer to the graphic diagram above if you have trouble following what these tests are doing.

testPushIsolatedMirrorCellNorthCase1
    | grid cell |
    grid := self generateDemoGrid.
    grid pushCellNorthFromLocation: 1@2.
    cell := grid at: 1@1.
    self should: [cell class = MirrorCell].
    cell := grid at: 1@2.
    self should: [cell class = BlankCell].
    cell := grid at: 2@2.
    self should: [cell class = BlankCell].
    cell := grid at: 1@3.
    self should: [cell class = BlankCell].

testPushIsolatedMirrorCellNorthCase2
    | grid cell |
    grid := self generateDemoGrid.
    grid pushCellNorthFromLocation: 3@3.
    cell := grid at: 3@2.
    self should: [cell class = MirrorCell].
    cell := grid at: 3@3.
    self should: [cell class = BlankCell].
    cell := grid at: 2@3.
    self should: [cell class = MirrorCell].
    cell := grid at: 3@4.
    self should: [cell class = MirrorCell].
    cell := grid at: 4@3.
    self should: [cell class = BlankCell].

testPushIsolatedMirrorCellEastCase1
    | grid cell |
    grid := self generateDemoGrid.
    grid pushCellEastFromLocation: 1@2.
    cell := grid at: 1@1.
    self should: [cell class = BlankCell].
    cell := grid at: 1@2.
    self should: [cell class = BlankCell].
    cell := grid at: 2@2.
    self should: [cell class = MirrorCell].
    cell := grid at: 1@3.
    self should: [cell class = BlankCell].

testPushIsolatedMirrorCellEastCase2
    | grid cell |
    grid := self generateDemoGrid.
    grid pushCellEastFromLocation: 3@3.
    cell := grid at: 3@2.
    self should: [cell class = BlankCell].
    cell := grid at: 3@3.
    self should: [cell class = BlankCell].
    cell := grid at: 2@3.
    self should: [cell class = MirrorCell].
    cell := grid at: 3@4.
    self should: [cell class = MirrorCell].
    cell := grid at: 4@3.
    self should: [cell class = MirrorCell].

testPushIsolatedMirrorCellSouth
    | grid cell |
    grid := self generateDemoGrid.
    grid pushCellSouthFromLocation: 3@3.
    cell := grid at: 3@2.
    self should: [cell class = BlankCell].
    cell := grid at: 3@3.
    self should: [cell class = MirrorCell].
    cell := grid at: 2@3.
    self should: [cell class = MirrorCell].
    cell := grid at: 3@4.
    self should: [cell class = MirrorCell].
    cell := grid at: 4@3.
    self should: [cell class = BlankCell].

testPushIsolatedMirrorCellWest
    | grid cell |
    grid := self generateDemoGrid.
    grid pushCellWestFromLocation: 3@3.
    cell := grid at: 3@2.
    self should: [cell class = BlankCell].
    cell := grid at: 3@3.
    self should: [cell class = MirrorCell].
    cell := grid at: 2@3.
    self should: [cell class = MirrorCell].
    cell := grid at: 3@4.
    self should: [cell class = MirrorCell].
    cell := grid at: 4@3.
    self should: [cell class = BlankCell].

And of course if we run our unit tests now, they will fail for these mirror push test cases. Actually, only the unit test cases where the mirror was expected to move will fail. For some of the test scenarios the mirror isn't supposed to move and of course it didn't. So those tests pass. After we implement the real mirror cell push code we expect them to continue to pass.

Index Page Next Page

Copyright © 2007, 2008, 2009, 2010 Stephan B Wessels    stevewessels@me.com