Summary


In this chapter, we built a Stack as an example of Test-Driven Development, and our version resulted in 14 tests. Reviewing the code shows that there is much more test code than actual code.

If you study the individual steps, you probably notice that we spent most of our time writing tests instead of writing the Stack code. This is because when we write tests, we focus on what the class does and how it is used instead of how it is implemented. This emphasis is very different from other ways of writing software, in which the code is written and then we figure out how to use it.

Comparing the size of the two classes is interesting. The Stack class is 35 lines of code; the StackFixture class contains 144 lines of code. The test code is more than four times the size of the Stack code. The following is the completed code in its entirety:

StackFixture.cs

 using System; using NUnit.Framework; [TestFixture] public class StackFixture {     private Stack stack;      [SetUp]     public void Init()     {         stack = new Stack();     }     [Test]     public void Empty()     {         Assert.IsTrue(stack.IsEmpty);     }     [Test]     public void PushOne()     {         stack.Push("first element");         Assert.IsFalse(stack.IsEmpty,              "After Push, IsEmpty should be false");     }     [Test]     public void Pop()     {         stack.Push("first element");         stack.Pop();         Assert.IsTrue(stack.IsEmpty,             "After Push - Pop, IsEmpty should be true");     }     [Test]     public void PushPopContentCheck()     {         int expected = 1234;          stack.Push(expected);         int actual = (int)stack.Pop();         Assert.AreEqual(expected, actual);     }     [Test]     public void PushPopMultipleElements()     {         string pushed1 = "1";         stack.Push(pushed1);         string pushed2 = "2";         stack.Push(pushed2);         string pushed3 = "3";         stack.Push(pushed3);         string popped = (string)stack.Pop();         Assert.AreEqual(pushed3, popped);         popped = (string)stack.Pop();         Assert.AreEqual(pushed2, popped);         popped = (string)stack.Pop();         Assert.AreEqual(pushed1, popped);     }     [Test]     [ExpectedException(typeof(InvalidOperationException))]     public void PopEmptyStack()     {         stack.Pop();     }     [Test]     public void PushTop()     {         stack.Push("42");         stack.Top();         Assert.IsFalse(stack.IsEmpty);     }     [Test]     public void PushTopContentCheckOneElement()     {         string pushed = "42";         stack.Push(pushed);         string topped = (string)stack.Top();         Assert.AreEqual(pushed, topped);     }     [Test]     public void PushTopContentCheckMultiples()     {         string pushed3 = "3";         stack.Push(pushed3);         string pushed4 = "4";         stack.Push(pushed4);         string pushed5 = "5";         stack.Push(pushed5);                string topped = (string)stack.Top();         Assert.AreEqual(pushed5, topped);     }     [Test]     public void PushTopNoStackStateChange()     {         string pushed = "44";         stack.Push(pushed);         for(int index = 0; index < 10; index++)         {             string topped = (string)stack.Top();             Assert.AreEqual(pushed, topped);         }     }     [Test]     [ExpectedException(typeof(InvalidOperationException))]     public void TopEmptyStack()     {         stack.Top();     }     [Test]     public void PushNull()     {         stack.Push(null);         Assert.IsFalse(stack.IsEmpty);     }     [Test]     public void PushNullCheckPop()     {         stack.Push(null);         Assert.IsNull(stack.Pop());         Assert.IsTrue(stack.IsEmpty);     }     [Test]     public void PushNullCheckTop()     {         stack.Push(null);         Assert.IsNull(stack.Top());         Assert.IsFalse(stack.IsEmpty);     } } 

Stack.cs

 using System; using System.Collections; public class Stack {     private ArrayList elements = new ArrayList();     public bool IsEmpty     {         get         {             return (elements.Count == 0);          }     }     public void Push(object element)     {         elements.Insert(0, element);     }     public object Pop()     {         object top = Top();         elements.RemoveAt(0);         return top;     }     public object Top()     {         if(IsEmpty)             throw new InvalidOperationException("Stack is Empty");         return elements[0];     } } 



Test-Driven Development in Microsoft .NET
Test-Driven Development in Microsoft .NET (Microsoft Professional)
ISBN: 0735619484
EAN: 2147483647
Year: 2004
Pages: 85

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net