So, today I started with addStick. I saved the original tests script at testing\board_addStick.ps1 and got to work, clearing out everything that isn't about that.
When I got down to the tests for addStick, there were pretty simple:
# Testing Board AddStick Method
$TestAddStick = new Board(4)
# Test adding inside bounds of board:
if (!($TestAddStick.addStick(1))) {"FAIL: Position is valid for a stick to be added."}
if (!($TestAddStick.addStick(33))) {"FAIL: Position is valid for a stick to be added."}
if (!($TestAddStick.addStick(34))) {"FAIL: Position is valid for a stick to be added."}
if (!($TestAddStick.addStick(60))) {"FAIL: Position is valid for a stick to be added."}
# Test adding outside bounds of board:
if ($TestAddStick.addStick(-1)) {"FAIL: Stick cannot be added outside board."}
if ($TestAddStick.addStick(0)) {"FAIL: Stick cannot be added outside board."}
if ($TestAddStick.addStick(61)) {"FAIL: Stick cannot be added outside board."}
# Test adding to a filled position:
if ($TestAddStick.addStick(1)) {"FAIL: Stick cannot be added on filled position."}
# Text exceeding size of board (max 4 in this test):
if ($TestAddStick.addStick(35)) {"FAIL: Stick # 5 cannot be added to this board."}
#
#
# END Testing Board AddStick Method
I decided, first, to get rid of the last test. The stick-limit is functionality that I haven't implemented properly. It should be tested with – well, I was going to say that it should be tested in the Board constructor, but that's not true. The fact that you can't add another stick is a thing about adding sticks.
OK, here's my revised tests:
# Testing Board AddStick Method
$TestAddStick = new Board(60)
# Test acceptable and edge values
for ($temp = 1; $temp -lt 61; $temp++)
{
if (!($TestAddStick.addStick($temp))) {"FAIL: Location $temp is valid for a stick to be added."}
}
# Test adding outside bounds of board:
if ($TestAddStick.addStick(-1)) {"FAIL: Stick cannot be added outside board."}
if ($TestAddStick.addStick(0)) {"FAIL: Stick cannot be added outside board."}
if ($TestAddStick.addStick(61)) {"FAIL: Stick cannot be added outside board."}
# Test adding to a filled position:
for ($temp = 1; $temp -lt 61; $temp++)
{
if ($TestAddStick.addStick($temp)) {"FAIL: Stick should not be added to filled location $temp."}
}
#
#
# END Testing Board AddStick Method
I've tested the acceptable and edge cases all together, adding to every spot on the board. I added several sticks outside the board. Then I add sticks to the existing board, which is filled with sticks.
I'm going to go write a test for the stick limit.
# Test stick limit:
$TestAddStick = new Board(4)
if (!($TestAddStick.addStick(1))) {"FAIL: Location 1 is valid for a stick to be added."}
if (!($TestAddStick.addStick(2))) {"FAIL: Location 2 is valid for a stick to be added."}
if (!($TestAddStick.addStick(3))) {"FAIL: Location 3 is valid for a stick to be added."}
if (!($TestAddStick.addStick(4))) {"FAIL: Location 4 is valid for a stick to be added."}
if ($TestAddStick.addStick(5)) {"FAIL: Fifth stick should not be added to this board: defined as 4 sticks."}
So, now I can check off Acceptable values, Edge values, and Unacceptable values.
Let's look a the code:
/*
Sets the value of a stick at the specified location to true.
location: The stick to be changed.
returns: True on sucess. False if the location is not valid or if the value
of the stick is already true.
*/
public bool addStick(int location)
{
if (!(isValid(location))) return false;
if (getStick(location).Value) return false;
if (this.Value >= max) return false;
stickList.Add(new Stick(location));
return true;
}
Signature is documented, with the exception of a spelling error. I'll check Signature documented on the spreadsheet.
How about some code comments?
public bool addStick(int location)
{
if (!(isValid(location))) return false; // Verify location is valid.
if (getStick(location).Value) return false; // Verify location is currently false.
if (this.Value >= max) return false; // Verify that adding a stick will not exceed the stick limit for this board instance.
stickList.Add(new Stick(location)); // Add stick object to stickList
return true; // NOTE FOR FUTURE: does Add return a true for success? If so, can this be merged with next line?
} // true for an added stick.
And I can check off code commented.
OK, one down. Twenty-eight to go.
No comments:
Post a Comment