Real Test Driven Development – Thinking of Money 2

 

The next test we need to write to to create the engine class

        [Test]
        public void Create()
        {
            Assert.IsNotNull(new MoneyEngine.Engine());
        }

 

this test will fail at first, as we don’t have an engine class, so after creating that the test passes and we are off.

next we will need a balance,

 

        [Test]
        public void Balance()
        {
            Assert.AreEqual(0, new MoneyEngine.Engine().Balance);
        }
the test fails so we create the balance code in the engine class
using System;

namespace MoneyEngine
{
    public class Engine
    {
        private double _Balance = 0;
        public double Balance
        {
            get
            {
                return _Balance;
            }
        }
    }
}
the test passes, we need to refractor the test class as we have each test creating the engine. which cant be good,
So I have created a module level variable of type and a setup method, this has the [SetUp] attribute, which
causes NUnit to call this function before running each test, so each test is called with a known starting state.
        MoneyEngine.Engine _Engine;

        [SetUp]
        public void SetupForTest()
        {
            _Engine = new Engine();    
        }

        [Test]
        public void Create()
        {
            Assert.IsNotNull(_Engine);
        }

        [Test]
        public void Balance()
        {
            Assert.AreEqual(0, _Engine.Balance);
        }

        [Test]
        public void Hook()
        {
            Assert.IsTrue(true);
        }
next we need to be able to add to the balance, 
thinking about the future we will need some information about the addition and subtraction, such as a reason for 
the item, a category which we should be able to add to, the date of the event and a unique id. so we are going to
need some sort of collection(s) of items that are additions and subtractions, 
so we are going to need a new test class for the item, and the item class, the item class itself is going to be
just a collection of properties at present so the test cases simply ensure we get out what we have put in. I will
therefore show the completed class and tests in full.
using System;
using NUnit.Framework;

namespace MoneyEngine.UnitTests
{
    [TestFixture]
    public class Tests
    {
        MoneyEngine.MoneyTransaction _MoneyTrans;
        private DateTime _TestDate = DateTime.Now;

        [SetUp]
        public void SetupForTests()
        {
            _MoneyTrans = new MoneyTransaction(10.0, "Category", "Description", _TestDate, TransactionType.AdditionTransaction);
        }

        [Test]
        public void Hook()
        {
            Assert.IsTrue(true);
        }

        [Test]
        public void Create()
        {
            Assert.IsNotNull(_MoneyTrans);
        }

        [Test]
        public void Amount()
        {
            Assert.AreEqual(10.0, _MoneyTrans.TransactionAmount);
        }

        [Test]
        public void Category()
        {
            Assert.AreEqual("Category", _MoneyTrans.TransactionCategory);
        }

        [Test]
        public void Description()
        {
            Assert.AreEqual("Description", _MoneyTrans.TransactionDescription);
        }

        [Test]
        public void TransactionDate()
        {
            Assert.AreEqual(_TestDate, _MoneyTrans.TransactionDate);
        }

        [Test]
        public void TransType()
        {
            Assert.AreEqual(TransactionType.AdditionTransaction, _MoneyTrans.TransType);
        }
    }
}

using System;

namespace MoneyEngine
{
    public enum TransactionType
    {
        AdditionTransaction,
        SubtractionTransaction
    }

    class MoneyTransaction
    {        
        /// <summary>
        /// Initializes a new instance of the MoneyTransaction class.
        /// </summary>
        /// <param name="transactionAmount"></param>
        /// <param name="transactionCategory"></param>
        /// <param name="transactionDescription"></param>
        /// <param name="transactionDate"></param>
        /// <param name="transType"></param>
        public MoneyTransaction(double transactionAmount, string transactionCategory, string transactionDescription, DateTime transactionDate, TransactionType transType)
        {
            _TransactionAmount = transactionAmount;
            _TransactionCategory = transactionCategory;
            _TransactionDescription = transactionDescription;
            _TransactionDate = transactionDate;
            _TransType = transType;
        }
        
        private double _TransactionAmount;
        public double TransactionAmount
        {
            get
            {
                return _TransactionAmount;
            }
        }

        private string _TransactionCategory;
        public string TransactionCategory
        {
            get
            {
                return _TransactionCategory;
            }
        }

        private string _TransactionDescription;
        public string TransactionDescription
        {
            get
            {
                return _TransactionDescription;
            }
        }

        private DateTime _TransactionDate;
        public DateTime TransactionDate
        {
            get
            {
                return _TransactionDate;
            }
        }

        private TransactionType _TransType;
        public TransactionType TransType
        {
            get
            {
                return _TransType;
            }
        }
    }
}

 
 
 

Posted in Projects | Leave a comment

Real Test Driven Development – Thinking of money

 

Right, so the program outline is

I run a balance sheet, so I know how much should be in my account at the bank, (they are never up to date) its a simple list of additions and subtractions with totals along the way.  The list only lasts a month, so its a sort of quick and dirty, balance sheet.

I want the program to reflect this, a way to input additions and subtractions, and display a balance. also it might be helpful if there was a way to have the standard bills, so at the start of the month I can request that the standard bills are added automatically.

Display should be simple, as I don’t want to have to write a help file.

as we are not going to store account details, numbers etc, I think that a simple database back end would be great, as in the future I want to have other programs that use this data for budgeting.

So its time to fire up visual studio.

I like to start with the business layer, its the easiest layer to test so its a good place to start. and by doing this I don’t have to think about how the interface or the storage will work.

after creating a c# class library project, I add a new folder to the project, called Unit Tests, I like to store the unit test classes with the project, I enclose the class definition within a #if(DEBUG) so that they are not included within a release version.

I then write a simple test, this is a safety net, if this fails then the setup for the unit tests is failing or they are not running correctly!,

I use nunit and Test Driven, to run my tests.  In visual studio I link the run tests command to [control][shift][T]. this means that my main commands are [control][shift][D] for Ghost doc, [control][shift][B] to build the project and  [control][shift][T] to run the tests. with all this I can just about develop without using the mouse.

ok so far I have a project with a single class in looking like

using NUnit.Framework;

namespace MoneyEngine.UnitTests
{
    [TestFixture]
    public class TestMoneyEngine 
    {
        [Test]
        public void Hook()
        {
            Assert.IsTrue(true);
        }
    }
}
I use this format for every test class I create. so when this passes we are ready to start creating.
 

Posted in Projects | Leave a comment

Real Test Driven Development

 

Time to put my thoughts on the line, I have read many posts and blogs about test driven development (TDD), both for and against.  I have tried to write my code at work in a test driven manor. So I thought I would put my thoughts down on paper, and develop a small program here in public view to show how I go about things.

Fell free to comment, not only on the process but on the resulting code, and tests, my aim is to learn, maybe convert a few to looking at this methodology in a new light, and to try to do things in a real world way.

Is this going to be a text book example, no, I am a single man, I have done pair programming in the past, for quick projects or to problem solve in a larger application, but not as a full time process.  I have no client to talk to about specs etc, and delivery will be when its done. so this won’t be a pure extreme programing example.

So what will this be, I want to show how I have used TDD, how I develop and idea, how I have taken the bits of extreme programming and test driven development, and adapted them to suite a single developer, both inside and outside of a company.  I am lucky where I work we are given great leeway in how we develop our code, its all web and windows development, using dot net, most use c# as the preferred programming language, but that is not mandatory.

I like to code along with the things I read, one of the best examples of this, and one of my favorite books is Extreme Programming Adventures in C# (DV-Microsoft Professional) by Ron Jeffries.

OK enough of this preamble, lets get down to business.

Posted in Projects | Leave a comment

CultureInfo

Problem:
 
I am working on  a web form that has a date input field, the date is entered as a string and then converted to a date for storage.
 
This is ok if the users only enter dates in one format, that of the hosting server, but the page is on the internet and could be viewed anywhere, it would be nice if users say in the USA could enter their dates in their own format.
 
The server is always in uk format, so any culture changes wil have to be in the UK
 
What I have Found
 
CultureInfo class can be used to format an item to a specific item.
 

[Test]

public void ConvertString()

{

    string usaDate = "12/30/2005";

    DateTime ukDate = Convert.ToDateTime("30/12/2005"); 

    DateTime nDate = Convert.ToDateTime(usaDate, new CultureInfo("en-US"));

 

    Console.WriteLine("USA Date: {0}", nDate.ToString(new CultureInfo("en-US")));

    Console.WriteLine("UKDate: {0}", nDate.ToString(new CultureInfo("en-GB")));

 

    Assert.AreEqual(nDate, ukDate);

}

 

So if the input string is in USA format to put it into a datetime object on the server you use

DateTime date = Convert.ToDateTime(usaDateString(new CultureInfo("en-US")));

printing out the date object will print in uk format, as thats what the computer is setup as.

 

to output back to the form in usa format use

txtDate.Text = date.ToString(new CultureInfo("en-US"));

 

CultureInfo implements the IFormatProvider

 

 

 

 

Posted in Code | Leave a comment

Finally Got Thre

I have been looking at this for a few weeks now off and on, I need for work a way of alterting prople on our website to a service, sort of one way message box.  well I wanted to poll for a request which sort of lead to Ajax type of thing, and then I saw Anthem.
 
It took a while but I have the pooling mechenasim now in place, finaly got the trigger to fire on the timer, the code for this is here
 
 
So its just the background stuff to do now and of cause the display box etc 😉
 
Overall Anthem looks like a winner for me, the way it allows you to treat controls, in the same way as normal web controls is great, hiding all the java script makes life easier.
 
I hope to do a better review of the controls and workings with maybe some of my own code at a later time.
 
 
 
 
Posted in Anthem.Net | Leave a comment

Grasshopper thoughts

Ok so its march, where is the timegoing, there have been new versions of Code Rush, Refactor Pro, Test Driven and Nunit, we have code coverage now with Test Driven,so meny toys.
 
Anyway this was to say I am still here, the Pile database is moving forward but I have little to report, other than its more complex than I thought, although storing the data in a simular way to old dos stores stuff on a hard drive looks interesting as this uses a binary tree.
 
Other projects are a dvd database and a recipie database, both of which are long term and going forward at a snales pace, stoped projects are the simple financial thing, which I should get back to. 
 
I have also been looking at the windows workflow stuff, which could be good, Its one of those technologies that I look at and go great, now where can I use it.  and why,  lots to think of there.
 
so loads of grasshopper thoughts and things to do. 
 
Oh one thing I did learn.  how to enumerate over a generic dictionary of type dictionary<string, string>
 
like most programming things its simple when you know how
 

foreach(string key in _dictionary.Keys)

{

temp += _dictionary[key] + ", ";

}

 

The above stores a comma seperated list of the values stored in _dictionary, this is one of those things that has bugged me for ages, and it is so simple.
 
So thats it till next time.
 
Posted in General Brain Dump | Leave a comment

Relations and data

I have recently been reading Ralf Sudelbucher’s blog about Pile, a concept of storing the relationship between data insted of the data itself, the data can always be reconstructed from the relationships.
 
 
Anyway after having a play with his version of the pile engine, and agent I have decided to have a go myself.  To be honest it started after I had copied in Ralfs code, following his design and writing a few unit tests to ensure I understood what he was doing.  and found that the engine worked, but I could not find my data, I can reconstruct the string when giving the agent the document handle but I could not find any test using the search.
 
So after looking I have decided to have a go myself.  as things progress here I will post bits and pieces and we shall see how we go, hopefully by the end we should have a working engine, wihich can use disk space insted of just memory.
 
So the plan is to construct a working pile engine, and text agent (think I will design the engine to store text, this may help in storage etc.)
 
fully functional search
 
OK that will do for the minuite.
Posted in Pile Database | Leave a comment

Windows Vista on a Virtual PC

Ok so what have I been upto, there was an update to windows vista build recently, and it coencided with a link to a blog that I hadn’t seen before
 
 
I had never installed vista until now, fallintg at the first hurdle, not having a DVD writer and not being able to get the virtual machine to read the iso image.  blog "virtual vista" takes you through each step, and supprise supprist it all worked as advertised.  even running virtual pc on a windows home installation.
 
It took three days in total (well evenings to be honest) one to download the iso for the latest ctp build, and the next to start the install, which went without a hitch, although it did take an age, (all night) for it to install, the following evening was installing the virtual server additions and setting everything up. 
 
I at the moment cant get vista to see any other machine on my network, (even its host) but It can get to the internet, and happly downloaded the Microsoft updates.  I suppose now I will have to install visual studio and see how it feels to develop in this new environment. 
 
Looking at vista itself, currently (and this is only a ctp remember) it feels very much like xp, the style is different, and there is certinally a lot more security (dialogs keep poping up asking is its ok to run this or that), but finding my way around was not a problem, and it seams that a lot of effert has been made to make the basic setup as simple as possible.
 
As a developer i know that a lot of the changes will be invisible to most, as they are in the way things work, and the support for new devices etc, but I am still looking forward to installing this on a real (as opposed to virtual) machine, maybe in the next ctp.
 
for thos who have the msdn subscription, you rearly do need over 512k to make the virtual machine work with vista, as on my machine (p4 266 with 768 ram) it is quite slow at times, I suppose an upgrade is in order now.
 
All I need to do now is sort out why the install cant see the host machine, any ideas anyone?
 
Posted in Windows Vista | Leave a comment

Ncover and other updates

Ok so I am suffering form a state of update itus, caused when you have lots of ideas and little inclination to start a project, so you look down the list of tools and realise that they all have updates, happily I am now in that state of nirvana where everything seams to be functioning together. 

 

The latest NUnit integrates completely with the latest TestDriven which works great with VS2005, and to top it all I have added a new tool to the armoury NCover now up to this point I had not used code coverage, it is only available in the team system version of Visual Studio, and don’t get me started on why I have not installed that, even without the server backend.  Anyway not using a tool to check, I have found that things just go unchecked, I didn’t realise just how much of my code was not being covered by unit testing. It turned out to be around 12% in a large project, which can be quite a chunk of code.

 

So having now updated everything and taken all the new stuff for a test ride, I suppose it time to get down to some coding. 

Posted in Uncategorized | Leave a comment

New Stuff

OK for those who didn’t know the January CTP of the Expression group of programs is now out, including the all new Interface designer (Microsoft Expression Interactive Designer) argh what was wrong with Sparkle.
 
 
Sparkle comes with some nice tutorials which are worth playing with, well done everyone there it all installed without problems, now I just have to get down to some coding in anger.
Other things being installed here are NUnit which I would never be without, the latest version is just out, I am not sure that even team system will drag me away from this little baby, the number of times it has saved me I just can’t count.
 
 
And to go along with it TestDriven, now this is fairly new to me I only moved to using it when I upgraded to VS 2005, as I found NUnit at first was a bit unstable with the new platform on my setup.  But the two make a perfect paring with VS 2005, with no problems at all, testing has become so much quicker.
 
 
Well for today that’s about it, I am off to ensure that winFX has installed correctly on my machines, and maybe get a bit of work done!.
Posted in Uncategorized | Leave a comment