Today's exercise is to just work my way through some of the methods to be tested and documented, checking off what I've done as I go. When I started, I had done 6 and, about an hour of coding and scripting later, I'm just getting up to 10.
I'm glad I'm doing the whole thing. Look at this method:
/*
Whether two locations are offset by 90 degrees around this node.
origin: One of the two locations.
destination: One of the two locations.
returns: true if and only if the two locations are 90 degrees offset.
*/
private bool offset90(int origin, int destination)
{
if ((nodePosition(origin) == 0) && (nodePosition(destination) == 2)) return false;
if ((nodePosition(origin) == 1) && (nodePosition(destination) == 3)) return false;
if ((nodePosition(origin) == 2) && (nodePosition(destination) == 0)) return false;
if ((nodePosition(origin) == 3) && (nodePosition(destination) == 1)) return false;
return true;
}
I started writing tests for this. I had to create a test_ wrapper, so I could call it from the scripts:
/* WRAPS offset90 FOR TESTING */
public bool test_offset90(int origin, int destination)
{
return offset90(origin, destination);
}
Then I got to work on a test, starting with a testing function:
function testOffset ($a, $b, $expected = 1) {
if ($expected -eq 1) { #if this is supposed to work
if (!($testNode.test_offset90($a, $b))) {"FAIL: Values $a and $b should be offset 90 degrees."}
}
else {
if ($testNode.test_offset90($a, $b)) {"FAIL: Values $a and $b are not offset 90 degrees or are not valid."}
}
}
So, the following values should work:
testOffset 7 12
testOffset 7 13
testOffset 12 18
testOffset 12 7
testOffset 13 7
testOffset 13 18
testOffset 18 12
testOffset 18 13
These shouldn't – and won't raise a fail because they have a 0 value for expected:
testOffset 7 18 0
testOffset 12 13 0
testOffset 13 12 0
testOffset 18 7 0
OK, so far, so good. How about these?
testOffset 7 7 0
testOffset 12 12 0
testOffset 13 13 0
testOffset 18 18 0
These threw FAILs. And it got worse:
#Invalid values
testOffset 7 -1 0
testOffset 7 0 0
testOffset 7 61 0
testOffset -1 7 0
testOffset 0 7 0
testOffset 61 7 0
# Not part of same node
testOffset 7 33 0
testOffset 33 7 0
All of these threw FAILs.
Why?
I'd written the method to exclude positions that were opposite of each other with respect to this node. Everything else returns true.
When I was writing it, it probably seemed shorter and easier. It never gave any errors or problems before because there is other validation before this call is made – but that's not how OO should work. Newer versions of this program should be able to count on this method doing what it says it does.
Here's my revised code:
private bool offset90(int origin, int destination)
{
if ((nodePosition(origin) == 0) && (nodePosition(destination) == 1)) return true; // Check for 90 offset
if ((nodePosition(origin) == 0) && (nodePosition(destination) == 3)) return true;
if ((nodePosition(origin) == 1) && (nodePosition(destination) == 0)) return true;
if ((nodePosition(origin) == 1) && (nodePosition(destination) == 2)) return true;
if ((nodePosition(origin) == 2) && (nodePosition(destination) == 1)) return true;
if ((nodePosition(origin) == 2) && (nodePosition(destination) == 3)) return true;
if ((nodePosition(origin) == 3) && (nodePosition(destination) == 2)) return true;
if ((nodePosition(origin) == 3) && (nodePosition(destination) == 0)) return true;
return false;
}
More tomorrow.
No comments:
Post a Comment