| [ next ] [ prev ] [ contents ] [ up to More Tests for Story Three ] | XP-Cinti TDD Workshop |
Our score_at method takes whatever coorindates we pass in and checks locations above, below and to the sides. If we call score_at(1,1), then it will attempt to reference locations outside the playing board. Early, when asked, our customer said that referencing out of bounds locations is undefined, so we never covered this in any tests. Since score-at now depends upon out of bounds references, we had better make sure it works.
In Ruby referencing beyond the end of an array is not a problem. Ruby will just return a nil object for beyond the end references.
But, negative references are different. A negative reference in a ruby array will count backward from the end of an array. So array[-1] is the last element of the array and array[-2] is the next to the last element.
Lets design a test to check for this using score_at.
# file: testnet.rb
...
def test_score_at_boundary
@net[1,2] = :BLACK
@net[2,1] = :BLACK
@net[1,5] = :BLACK
@net[5,1] = :BLACK
assert_equal 0, @net.score_at(1,1,:BLACK)
end
...
|
Running our tests show ...
$ ruby testnet.rb Loaded suite testnet Started ....... Failure!!! test_score_at_boundary(TestNet) [testnet.rb:72]: <0> expected but was <1> .. Finished in 0.025289 seconds. 8 tests, 33 assertions, 1 failures, 0 errors |
Good, we expected that error. Now lets fix it.
| [ next ] [ prev ] [ contents ] [ up to More Tests for Story Three ] | Copyright 2003 by Jim Weirich.![]() |