A Project in .NET
Object design in C# using the PowerShell IDE as a development and testing environment.
Saturday, December 17, 2011
Backups
Saturday, December 10, 2011
Some setup
I've created a new directory and created a new script file for my finances functions.
Back in my profile, I made a little function that will change to the new directory:
function do-finances {
cd MyDir
$a = (Get-Host).UI.RawUI
$a.ForegroundColor = "blue"
$a.BackgroundColor = "white"
clear
}
set-alias df do-finances
So, if I type "df", I'm moved to the right directory - and that other stuff? Well, if I'm actually going to try this as a place where I keep my account information, it would be nice to know, for sure, that I'm using PS for something different than I usually do.
And it gives me another option.
I can add the following lines to the new-transaction function from a couple of days ago:
$a = (Get-Host).UI.RawUI
$a.ForegroundColor = "magenta"
Which means that every time I have unsaved data - the transaction just created - the shell will have a different appearance.
Next time, persisting data.
Thursday, December 8, 2011
A Pilot Project
- It assumes $total will be blank.
- It assumes that the amount in the first entry of $checkbook will be a negative number.
Wednesday, December 7, 2011
Everything we need is here
I don't mind spreadsheet programs, but there is a lot of mousing around - and I wonder if that's all that necessary. Then it occurred to me that PowerShell has the kinds of data types necessary to keep track of this without adding anything fundamental.
So, I'll see what something like that would look like.
We need a record. That is, every transaction needs to have a certain amount type of information. A hash table stores key value pairs. How about this:
PowerShell: $a = {month=0;day=0;payee=blank;amount=0;category=blank;year=0000}
PowerShell: $a
month=0;day=0;payee=blank;amount=0;category=blank;year=0000
Some design decisions:
Why month and day instead of a date object? Seems like overkill. I guess I can implement it with date objects later.
Why is the year last? I plan on using default values, so that will only have to be set once per year.
So, let's make a function out of this:
PowerShell: function new-transaction ($month = 0, $day=0, $payee="blank", $amount=0, $cat="blank", $year=2011)
>> {
>> @{month=$month;day=$day;payee=$payee;amount=$amount;cat=$cat;year=$year}
>> }
>>
And how does it work?
PowerShell: $a = new-transaction
PowerShell: $a
Name Value
---- -----
year 2011
payee blank
amount 0
cat blank
month 0
day 0
PowerShell: $a = new-transaction 12 8 test 4.35 groceries
PowerShell: $a
Name Value
---- -----
year 2011
payee test
amount 4.35
cat groceries
month 12
day 8
And now this needs to live in a script file - my profile, for now.
Here's what I put in:
function new-transaction ($month = 0, $day=0, $payee="blank", $amount=0, $cat="blank", $year=2011)
{
<#
.SYNOPSIS
Create a new transaction hash table.
.DESCRIPTION
Creates a new hash table with vaues for required parameters. Default values are
provided.
.PARAMETER month
Month from 1-12. No verification
.PARAMETER day
Day from 1-31. No verification.
.PARAMETER payee
Payee string.
.PARAMETER amount
Amount.
.PARAMETER cat
Spending category.
.PARAMETER year
Year.
#>
@{month=$month;day=$day;payee=$payee;amount=$amount;cat=$cat;year=$year}
}
set-alias nt new-transaction
Yes, I wrote help for my function - you should, too.
Here's what the output looks like:
PowerShell: nt
Name Value
---- -----
year 2011
payee blank
amount 0
cat blank
month 0
day 0
PowerShell: get-help nt
NAME
new-transaction
SYNOPSIS
Create a new transaction hash table.
SYNTAX
new-transaction [[-month] Object>] [[-day] Object>] [[-payee]
Object>] [[-amount] Object>] [[-cat] Object>] [[
-year] Object>] [
DESCRIPTION
Creates a new hash table with vaues for required parameters. Default values are provided.
RELATED LINKS
REMARKS
To see the examples, type: "get-help new-transaction -examples".
For more information, type: "get-help new-transaction -detailed".
For technical information, type: "get-help new-transaction -full".
PowerShell:
Next up: Put the transaction records into an array.
Tuesday, August 30, 2011
CQ Simulation: Fixing the HasAbandoned Problem
So, I've been struggling with some code for the last couple of weeks. My problem was that I was using a foreach loop in a context where the contents of the loop might change the iteration list.
Because foreach loops do not guarantee the order in which things are done, it's not valid to have the list change halfway though.
My solution – or hack – was to assign each member of the list that matched a certain criterion – the “has abandoned” method – to a second list and then cycle through the second list, deleting items from the first list.
Here's the code:
private List[LESSTHAN]DataUser[GREATERTHAN] tempList;
...
public void tick()
{
...
foreach (DataUser a in computerQueue) {
a.tick(simTick, results);
if (a.hasAbandoned(simTick))
{
tempList.Add(a);
}
}
...
foreach (DataUser d in tempList)
{
computerQueue.Remove(d);
}
As I implied, it seems like a hack to me.
The real problem, I suppose, is that I used C# lists which don't play nicely with foreach in these circumstances.
But I don't want to re-architect the thing, so let's leave well enough alone and go back to working on the program.
Tuesday, August 23, 2011
CQ Simulation: More Tests 2
More method tests today.
I wanted to make sure that these tests are testing the methods themselves, without mixing up the different things to be tested. This means some of these are quite boring:
# Test the timeLeft method
$testSeat = new Seat(60)
if (!($testSeat.timeLeft())) {"FAIL: Time Left should be true."}
Well, maybe there's more later, but for now, next: DataUser constructor:
# queue up time, patience time, desired time.
$testDataUser = new DataUser(1, 2, 3)
And now for 0 values:
$testDataUser = new DataUser(0, 0, 0)
This one caused an exception. Just one problem – well, look at the code:
if (queueUpTime [LESSTHAN] 0) { throw new Exception(); }
if (patienceTime [LESSTHAN] 0) { throw new Exception(); }
if (desiredTime [LESSTHAN] 0) { throw new Exception(); }
Zero is not less than zero. Here – let's try something:
// if (queueUpTime [LESSTHAN] 0) { throw new Exception(); }
// if (patienceTime [LESSTHAN] 0) { throw new Exception(); }
// if (desiredTime [LESSTHAN] 0) { throw new Exception(); }
Same error:
New-Object : Exception calling ".ctor" with "3" argument(s): "Exception of type 'System.Exception' was thrown."
I suspected as much. It seems there is another place in the code that is throwing this exception.
Oh, I was looking at the wrong place in the code. Here's the code for verifying the constructor:
if (X_queueUpTime [LESSTHAN] 0) { throw new Exception(); }
if (X_patienceTime [LESSTHAN] 1) { throw new Exception(); }
if (X_desiredTime [LESSTHAN] 1) { throw new Exception(); }
So, let's make this into an expected fail and we're done.
try {
$testDataUser = new DataUser(0, 0, 1)
"FAIL: Accepting input outside acceptable range."
} catch {}
try {
$testDataUser = new DataUser(0, 1, 0)
"FAIL: Accepting input outside acceptable range."
} catch {}
More next time.
Monday, August 22, 2011
CQ Simulation: Revising and Extending Tests
Over the last couple of weeks, I've letting coding get ahead of testing. The point of this methodology – with testing proceeding at the same time as coding – is to let the testing inform the coding.
I've decided to get back to it. I'm going to make sure I have tests for each method and constructor. I'm also going to make sure they are in files that are clearly named and have enough comments to follow the intent.
For instance, here is ClassSeatConstructor.ps1:
# Testing Seat Constructor
#
#
# Expected success:
$testSeat = new Seat(60)
# Test the Free property: On creation, should be Free.
$testSeat.Free = $true
if (!($testSeat.Free)) {"FAIL: Free should be True."}
$testSeat.Free = $false
if ($testSeat.Free) {"FAIL: Free should be False."}
# unacceptable values: 0, -1
try {
$testSeat = new Seat(0)
"FAIL: Invalid value did not cause exception."
}
catch {}
try {
$testSeat = new Seat(-1)
"FAIL: Invalid value did not cause exception."
}
catch {}
This script verifies that the constructor works. The initial state of the object has Free = True.
Then it tests to see that two unacceptable values are not permitted.
That test relies on testing code, so I should note that in the script.
What this test doesn't do is verify that the initial value is assigned. That will involve more testing code:
public int getUseTime()
{
return useTime;
}
And the test:
#verify initial state:
#USES TESTING CODE
$testSeat = new Seat(20)
if (!($testSeat.getUseTime() -eq 20)) {"FAIL: Initial state of Seat object should be 20."}
One down, 16 to go.