Leight-Weight Introduction to TDD:
Step 2: Adding a Simple Count Property

Let's look at our task list as it stands now:

  1. Add an element on its top. This method is usually called Push().
  2. Remove the element from the top. This method is usually called Pop().
  3. Ability to determine whether the stack is empty. A property IsEmpty that returns a boolean value.
  4. A Count property to determine the number of elements on the stack.
  5. A Clear() method for removing all elements from the stack.
  6. Test IsEmpty returns false if stack contains one or more elements.

Next, we'll implement the Count property. We can safely assume that this is a read-only property so we don't need an accessor that writes to it.

Here is the test for it:

1: [Test]
2: public void CountWithEmptyStack() {
3: Stack stack = new Stack();
4: Assert.Equals(0, stack.Count); 5: }

Running the compiler for this will result in a compile error since the Count property doesn't exist yet. No surprise here as this additional test represents an additional requirement for the Stack class, which it doesn't meet yet. Or in other words we have just expanded the specifications for the Stack class.

Again, it's not a failing test that force us. It's even earlier. The compiler wouldn't even compile the test. So here is a simple addition to the Stack class (bold items what has been added):

 1: public class Stack {
 2:   public Stack() {
 3:   }
 4:   public bool IsEmpty {
 5:      get {
 6:         return true;
 7:      }
 8:   }
 9:   public int Count {
10:      get {
11:         return 0;
12:      }
13:   }
14: }

Note that again we have just hard coded an extremely simple version of this property. Admittedly this is a simplistic case but the same principle applies for more complicated real world implementations as well: "The simplest thing that could possibly work" (TSTTCPW).

Now it not only compiles nicely but it even passes all tests!

Ok, apparently our Count implementation is not correct. So what should we do? There is nothing else we can do with the class which may lead to a different result for the Count property. So we check our change set in and add an item to our task list:

  1. Add an element on its top. This method is usually called Push().
  2. Remove the element from the top. This method is usually called Pop().
  3. Ability to determine whether the stack is empty. A property IsEmpty that returns a boolean value.
  4. A Count property to determine the number of elements on the stack.
  5. A Clear() method for removing all elements from the stack.
  6. Test IsEmpty returns false if stack contains one or more elements.
  7. Test Count for non-empty stack.

Step 3: Improving the Count property implementation

Back to beginning of this tutorial

Home

Download

Documentation

Features

Tutorials

Support

Suggest A Feature

Report A Bug

About



Web csunit.org

Sponsors:

Extreme Simplicity Logo

Agile Utilities Logo

Blue Note Ventures Logo


Sources hosted by
Get csUnit - unit testing for .NET at SourceForge.net. Fast, secure and Free Open Source software downloads



Copyright © 2002-2008 by Agile Utilities NZ Ltd. All rights reserved. Site design by Andreas Weiss. This site is protected by bot traps.