CSharp: Testing a Generic Class
I just posted this question to StackOverflow and I am repeating it here for posterity as the pastebin code expires after a month:
More than a question, per se, this is an attempt to compare noteswith other people. I wrote a generic History class that emulatesthe functionality of a browser's history. I am trying to wrap myhead around how far to go when writing unit tests for it. I amusing NUnit. Please share your testing approaches below.
The full code for the History class is at pastebin.com: http://pastebin.com/ZGKK2V84. A full version of the code is also available below:
using System; using System.Collections.Generic; namespace Gorauskas.DataStructures { /// /// A history data structure that provides the same functionality /// as a browser history, but allows you to use any type /// /// The type parameter public class History<T> { private Stack<T> _forward = new Stack<T>(); private Stack<T> _backward = new Stack<T>(); private T _current; public History() { } /// /// This contructor set the Current pointer to an initial object of type T /// /// An item of type T public History(T item) { this._current = item; } /// /// Returns the current item that it is pointing to /// Pushes the state of the current pointer into the backward stack /// and sets the new current item. If the forward stack has items in it /// then clear that too. /// public T Current { get { return this._current; } set { if(this.CanMoveForward) this._forward.Clear(); if (!(this._current == null)) this._backward.Push(this._current); this._current = value; } } /// /// Tells if the current pointer can be moved forward in history. /// public bool CanMoveForward { get { return this._forward.Count > 0; } } /// /// Tells if the current pointer can be moved backward in history. /// public bool CanMoveBackward { get { return this._backward.Count > 0; } } /// /// Moves the current pointer one item back in history /// /// The current item public T Back() { if (this.CanMoveBackward) { this._forward.Push(this._current); this._current = this._backward.Pop(); } return this._current; } /// /// Moves the current pointer one item forward in history /// /// The current item public T Forward() { if (this.CanMoveForward) { this._backward.Push(this._current); this._current = this._forward.Pop(); } return this._current; } /// /// Adds the entire history in chronological order to a list of T /// /// A ordered list of history items public List<T> Dump() { List<T> l = new List<T>(); if (this.CanMoveForward) l.AddRange(this._forward.ToList<T>().Reverse<T>()); if (!(this.Current == null)) l.Add(this._current); if (this.CanMoveBackward) l.AddRange(this._backward.ToList<T>()); return l; } } }
Comments
Comments powered by Disqus