Wednesday, April 6, 2011

Testing the Fall Method

Today, I'm going to write tests for my new Fall method. A couple of weeks back I created a template for this, so I started by copying it into a new script in PowerShell ISE.


# Testing Fall Method

#

# FUNCTIONS

#

#

# TEST DATA

#

#

# TESTS

#

#

#

# END Testing Fall Method


Here's my first run at it – placing a stick at 4 and calling Fall on it. Expected result: it should drop to position 9.

(Um, I had to write a method in Board to call the fall method in the Node class.)


$testFallMethod = new Board(60)


$testFallMethod.AddStick(4)

display $testFallMethod

$testFallMethod.dropStick(4)

display $testFallMethod


And here is the result:


True

o o o o-o o

o o o o o o

o o o o o o

o o o o o o

o o o o o o

o o o o o o

True

o o o o o o

|

o o o o o o

o o o o o o

o o o o o o

o o o o o o

o o o o o o


OK – that worked as expected. (Sorry things don't line up on the blog, but it is correct.)

Let's try dropping 9 (where the stick landed. Expected: 20.


$testFallMethod.dropStick(9)

True


> display $testFallMethod


o o o o o o

o o o o o o

|

o o o o o o

o o o o o o

o o o o o o

o o o o o o


That worked as expected.

I need to be a little more systematic about this. I'm going to create a function that takes an initial value and an expected value and returns FAIL if it fails.


function testFallMethod ($start, $expected)

{

$testBoard = new Board(60)

if (!($testBoard.addStick($start))) {"FAIL: Could not add stick at position $start."}

if (!($testBoard.dropStick($start))) {"FAIL: Fall method returned value of false for position $start."}

if (!($testBoard.getStick($expected).Value)) {"FAIL: Stick from position $start did not fall to expected position $expected"}

}


I ran this on values 4 and 9 and got no warnings.


Let's try values 4 and 34:


> testFallMethod 4 34

FAIL: Stick from position 4 did not fall to expected position 34


That's the correct FAIL. The function was able to add the stick and drop it.

To test the rest of the function, I added a stick to 4 twice to get the first clause to fail.


I can test the second clause by giving it a value that can't fall, like 59:


> testFallMethod 59 9

FAIL: Stick from position 59 did not fall to expected position 9


That's the wrong FAIL message.

I don't think this is a problem with the test. I think the Fall method must be returning True even if nothing happens. In general, if something doesn't happen in this design, a method returns false.

I can use the display function to get an idea of what's going on:


> $test = new Board(60)


> $test.addStick(59)

True


> display $test

o o o o o o

o o o o o o

o o o o o o

o o o o o o

o o o o o o

o o o o-o o


> $test.dropStick(59)

True


> display $test

o o o o o o

o o o o o o

o o o o o o

o o o o o o

o o o o o o

o o o o o o


Yes, something's wrong with the C# class.

So, more coding tomorrow.


No comments:

Post a Comment