| [ next ] [ prev ] [ contents ] [ up to Some Loose Ends ] | XP-Cinti TDD Workshop |
Here's another loose end. We notice that our story specifies a 5x5 matrix. We only really test location (1,1) and there is nothing in the tests to verify that the matrix can really be 5x5. Perhaps we should add a test to check this.
# file: testnet.rb
...
def test_max_matrix_size
check_location(1,1)
check_location(1,5)
check_location(5,1)
check_location(5,5)
end
def check_location(x,y)
assert_equal true, @net.untied?(x,y)
@net.tie(x,y)
assert_equal false, @net.untied?(x,y)
@net.untie(x,y)
assert_equal true, @net.untied?(x,y)
end
...
|
We expect this one to pass, but when we run it we get ...
$ ruby testnet.rb Loaded suite testnet Started... .. Error occurred in test_max_matrix_size(TestNet): NameError: undefined method `[]' for nil ./net.rb:14:in `untied?' testnet.rb:41:in `check_location' testnet.rb:36:in `test_max_matrix_size' testnet.rb:40 .... Finished in 0.067089 seconds. 5 runs, 13 assertions, 0 failures, 1 errors |
What? It failed?! Why?
Oh yeah. Ruby arrays are indexed starting with 0. Our net matrix indices start with 1. We have a typical off-by-one boundary error.
That's easy to fix ...
# file: net.rb
...
def untied?(x,y)
! @tied[x-1][y-1]
end
def tie(x,y)
@tied[x-1][y-1] = true
end
def untie(x,y)
@tied[x-1][y-1] = false
end
...
|
And now our test passes again!
| [ next ] [ prev ] [ contents ] [ up to Some Loose Ends ] | Copyright 2003 by Jim Weirich.![]() |