Wednesday, April 27, 2011

Some Tests for the Stick Class

I decided to post some of my test scripts. I didn't want to post all of them – that's a little much to dig through. I'm going to include the tests for the Stick class. This is the smallest of the classes.

Here's one of the constructors:


# Testing Stick Class Constructor bool


# Test True:

$testStick = new Stick(1 -eq 1)

if (!($testSTick.Value)) { "FAIL: Stick should be able to take value of True." }


# Test False:

$testStick = new Stick(1 -eq 2)

if ($testSTick.Value) {"FAIL: Stick should be able to take value of False." }



#

# END Testing Stick Class Constructor bool


I didn't see much to test, here. It takes a Boolean and there are only two values, both of which are acceptable.


Here's the other constructor:

# Testing Stick Class Constructor INT



# Test normal values (1 to 60):


for ($location = 1; $location -lt 61; $location++)

{

$testStick = new Stick($location)

if (!($testStick.Location -eq $location)) { "FAIL: Stick should be able to take value of $location" }


}


$testStick = new Stick(-1)

if (!($testStick.Location -eq -1)) {"FAIL: Stick is not validated to prohibit a value of -1." }

$testStick = new Stick(0)

if (!($testStick.Location -eq 0)) {"FAIL: Stick is not validated to prohibit a value of 0." }

$testStick = new Stick(61)

if (!($testStick.Location -eq 61)) {"FAIL: Stick is not validated to prohibit a value of 61." }

#

# END Testing Stick Class Constructor INT


There's no validation of locations going in to this constructor. Validation is done somewhere else. I wanted to leave this out of the Stick class, if I could, in anticipation of changing the board, making it bigger, in the future.

This did lead me to a little confusion, however, over tests for the Location property:

# Testing Location Property


$testLocationStick = new Stick(0)


for ($a = 0; $a -lt 1000; $a++)

{

$testLocationStick.Location = $a

if (!($testLocationStick.Location -eq $a)) {"FAIL: Location not set correctly."}

}


try

{

$testLocationStick.Location = -1

{"FAIL: -1 is not valid value for Location."}

}

catch {}

# END Testing Location Property


Again, this test simply tries a bunch of values that won't fail, but look at the last test. That one tries to assign a value of -1 to this property and fails. In the previous test script, the value of -1 was assigned without error.

The reason for this in the code is that there is validation on the Location property. The constructor sets the value directly, not though this property.

A value of -1 is used for locations that don't exist in individual nodes. That is, a node might be associated with position 1 and position 6 (as node 0 is) but no other positions. The other positions in that node's position array have their values set to -1.

Those aren't stick values, though.

Ideally, if an object can't have a value, it shouldn’t be able to get that value. That's almost tautological. This design can accommodate that, simply by using a call to the Location property in the constructor.

But will that break anything? I can try it and run the tests.

That change only caused the FAIL in the constructor test. As long as the tests cover all the expected behavior of the system, I can re-write that test as follows:


try {

$testStick = new Stick(-1)

{"FAIL: -1 is not valid value for Location."}

}

catch {}

Finally, here's the test script for the Value property:

# Testing Value Property



$testLocationStick = new Stick(1 -eq 1)

if (!($testLocationStick.Value)) {"FAIL: Stick should have a value of True."}


$testLocationStick = new Stick(1 -ne 1)

if ($testLocationStick.Value) {"FAIL: Stick should have a value of False."}


# END Testing Value Property

There's only two values, both of them acceptable.

Now that I think about it, I could just use the isValid method to validate the Location property. It works by polling the node array. That wouldn't require hard-coding any values into Stick.

No comments:

Post a Comment