I usually don't do blog posts on weekend, but I just can't keep myself from getting started.
I'm going to start with my Odometer side project again, fitting it into the new methodology. See the March 19 post for this project.
In the short time since I wrote the last post, I've managed to forget the name of the function that I created to create new folders. I could look it up on my blog, but PowerShell is much easier than that:
prompt> Get-ChildItem function:*
It's called “newProject”.
newproject C:\Programming\PowerShellProg odometer
Then I copied all the stub programs into the relevant directories and now I need to change a little code here and there.
Now I can copy the existing code into the stub. The new file looks like this:
Add-Type @'
#define DEBUG
public class Wheel
{
// FIELDS
private int state = 0;
private Wheel next = null;
// CONSTRUCTORS
// PROPERTIES
public int Value
{
get
{
int temp = 0;
if (next != null)
{
temp = (next.Value * 10);
}
temp = temp + state;
return temp;
}
}
// METHODS - public
public int addOne()
{
click();
return Value;
}
// METHODS - private
private void click()
{
state++;
if (state > 9)
{
if (next != null)
{
next.click();
}
else
{
next = new Wheel();
next.click();
}
state = 0;
}
}
#if DEBUG
// TESTING CODE
#endif
// OVERRIDES
}
'@
My tests for this version of the project were pretty rudimentary. I'll put them in a script called legacy.ps1 and see if things run.
With a little work, everything did run. That is, the main script, in the odometer directory, loaded and ran the code and the tests.
I'm going to close out PowerShellISE and start it again, make sure it's working.
Sweet.
But this project never had that many tests and no documentation, so let's spend a little more time.
First of all, the tests I need to write are for the following:
- Value
- addOne()
- click()
Click is private, so I’ll write a public wrapper for it in the testing code.
Here are my tests:
# Testing Value property
#
$testWheel = new Wheel
for ($i = 1; $i -lt 100; $i++)
{
$temp = $testWheel.addOne()
if ($testWheel.Value -ne $i) {"FAIL: Value should be $i."}
}
# END Testing Value property
# Testing click private method
#
$testWheel = new Wheel
for ($i = 1; $i -lt 1000; $i++)
{
$testWheel.test_click()
if ($testWheel.Value -ne $i) {"FAIL: Value should be $i."}
}
# END Testing click private method
# Testing addOne Method
#
$testWheel = new Wheel
for ($i = 1; $i -lt 1000; $i++)
{
if ($testWheel.addOne() -ne $i) {"FAIL: Method addOne should return $i."}
}
# END Testing addOne Method
I'm not sure how to get more failures. This code does not pass a lot of parameters around. It makes for a tight design, but boring tests.
Well, let's do the method signature documentation while we're here.
Here's the signature level documentation I wrote, using the PowerShell code documentation function I wrote in the April 28 post.
START DESCRIPTION
This class models an odometer.
Each instance of the class models a single wheel on an odometer
such that it can be incremented from 0 to 9. Once it reaches 9,
the wheel returns to 0 and increments the next wheel in the odometer.
The first wheel is the only one the user accessess directly. The first
wheel provides an interface to the user, creates the next wheel,
when necessary, and polls the next wheel for its value.
The recursive design creates as many new wheels as necessary, each 10
times larger than the wheel creating it.
END DESCRIPTION
public class Wheel
START DESCRIPTION
The value of the odometer. This is compiled by polling all the wheels
created for their values and adding them together.
END DESCRIPTION
public int Value
// METHODS - public
START DESCRIPTION
Clicks the current wheel object and returns the value of the
odometer.
END DESCRIPTION
public int addOne()
START DESCRIPTION
Changes the state of the current wheel and, if necessary,
creates or calls the next wheel in the progression to change its state.
END DESCRIPTION
public void test_click() // Wraps click()
And the verdict is:
All my prep work behaves as expected. That is, when I run the odometer.ps1 script, it loads the class and runs the tests, as expected.