Arsalan's Musings on Software

Software design and development techniques, ideas, and source code snippets…

Writing unit tests for the Jumble problem

leave a comment »

Let’s write some unit tests for the jumble problem solved in the previous post.

We will create some setup and teardown code that will be executed before and after running each test, respectively. We will use the NUnit framework to write our tests. To run the tests we can either use the NUnit Graphical User Interface or the excellent Visual Studio plugin TestDriven.Net which allows inline debugging of our tests. Way cool!

#region ----- Private Members -----
JumbleSolver simpleJumble;
JumbleSolver jumbleWithWordDictionaryFile;
const string dictionaryFilePath = @"dictionary.txt";
#endregion

#region ----- Setup and Teardown -----
[SetUp]
public void Setup()
{
this.simpleJumble = new JumbleSolver();
this.jumbleWithWordDictionaryFile = new JumbleSolver(dictionaryFilePath);
}

[TearDown]
public void TearDown()
{
this.simpleJumble = null;
this.jumbleWithWordDictionaryFile = null;
}
#endregion

Tests

I often like to divide the tests into Happy Path and Negative Test regions.

Happy Path Tests

#region ---------- Happy Path Tests ----------
[Test]
public void SimpleTestIsWordAMatch3Letters()
{
// Arrange
string word = "ran";
string jumble = "rndlae";

// Act
bool isMatch = simpleJumble.IsWordAMatch(word, jumble);

// Assert
Assert.AreEqual(true, isMatch, "The IsWordAMatch method failed for a 3 letter combination");
}

[Test]
public void SimpleTestIsWordAMatch4Letters()
{
string word = "lean";
string jumble = "rndlae";
bool isMatch = simpleJumble.IsWordAMatch(word, jumble);
Assert.AreEqual(true, isMatch, "The IsWordAMatch method failed for a 3 letter combination");
}

[Test]
public void SimpleTestIsWordAMatch5Letters()
{
string word = "ldean";
string jumble = "rndlae";
bool isMatch = simpleJumble.IsWordAMatch(word, jumble);
Assert.AreEqual(true, isMatch, "The IsWordAMatch method failed for a 3 letter combination");
}

[Test]
public void SimpleTestIsWordAMatch6Letters()
{
string word = "lander";
string jumble = "rndlae";
bool isMatch = simpleJumble.IsWordAMatch(word, jumble);
Assert.AreEqual(true, isMatch, "The IsWordAMatch method failed for a 3 letter combination");
}

[Test]
public void SimpleTestCountMatchingWords()
{
List dictionary = new List();
dictionary.Add("ace");
dictionary.Add("are");
dictionary.Add("dak");
dictionary.Add("ross");
dictionary.Add("rosy");
dictionary.Add("rynd");
dictionary.Add("rddnol");;
dictionary.Add("rndle");
dictionary.Add("rlead");

simpleJumble.CurrentJumble = "rndlae";
simpleJumble.WordDictionary = dictionary;
bool didCountSucceed = simpleJumble.CountMatchingWords();
Assert.AreEqual(true, didCountSucceed, "The CountMatchingWords method failed for the jumble 'rndlae'");
}

[Test]
public void MajorTestCountMatchingWords1()
{
jumbleWithWordDictionaryFile.CurrentJumble = "nuetbr";
bool didCountSucceed = jumbleWithWordDictionaryFile.CountMatchingWords();
Assert.AreEqual(true, didCountSucceed, "The CountMatchingWords method failed for the jumble 'rndlae'");

string expectedOutput = "23, 29, 12, 4";
string actualOutput = jumbleWithWordDictionaryFile.MatchCount3Letters.ToString();
actualOutput += ", ";
actualOutput += jumbleWithWordDictionaryFile.MatchCount4Letters.ToString();
actualOutput += ", ";
actualOutput += jumbleWithWordDictionaryFile.MatchCount5Letters.ToString();
actualOutput += ", ";
actualOutput += jumbleWithWordDictionaryFile.MatchCount6Letters;
Assert.AreEqual(expectedOutput, actualOutput, "The CountMatchingOutput method calculated in accurate results.");
}

[Test]
public void MajorTestCountMatchingWords2()
{
jumbleWithWordDictionaryFile.CurrentJumble = "zrftmx";
bool didCountSucceed = jumbleWithWordDictionaryFile.CountMatchingWords();
Assert.AreEqual(true, didCountSucceed, "The CountMatchingWords method failed for the jumble 'rndlae'");

string expectedOutput = "0, 0, 0, 0";
string actualOutput = jumbleWithWordDictionaryFile.MatchCount3Letters.ToString();
actualOutput += ", ";
actualOutput += jumbleWithWordDictionaryFile.MatchCount4Letters.ToString();
actualOutput += ", ";
actualOutput += jumbleWithWordDictionaryFile.MatchCount5Letters.ToString();
actualOutput += ", ";
actualOutput += jumbleWithWordDictionaryFile.MatchCount6Letters;
Assert.AreEqual(expectedOutput, actualOutput, "The CountMatchingOutput method calculated inaccurate results.");
}

[Test]
public void SimpleTestCountMatchingWordsNoMatchesFound()
{
List dictionary = new List();
dictionary.Add("ace");
dictionary.Add("are");
dictionary.Add("dak");
dictionary.Add("ross");
dictionary.Add("rosy");
dictionary.Add("rynd");
dictionary.Add("rddnol"); ;
dictionary.Add("rndle");
dictionary.Add("rlead");

simpleJumble.CurrentJumble = "xyzzqw";
simpleJumble.WordDictionary = dictionary;
bool didCountSucceed = simpleJumble.CountMatchingWords();
Assert.AreEqual(true, didCountSucceed, "The CountMatchingWords method failed for the jumble 'xyzzqw'");

string expectedOutput = "0, 0, 0, 0";
string actualOutput = jumbleWithWordDictionaryFile.MatchCount3Letters.ToString();
actualOutput += ", ";
actualOutput += jumbleWithWordDictionaryFile.MatchCount4Letters.ToString();
actualOutput += ", ";
actualOutput += jumbleWithWordDictionaryFile.MatchCount5Letters.ToString();
actualOutput += ", ";
actualOutput += jumbleWithWordDictionaryFile.MatchCount6Letters;
Assert.AreEqual(expectedOutput, actualOutput, "The CountMatchingOutput method calculated inaccurate results.");
}

[Test]
public void MajorTestCountMatchingWordsNoMatchesFound()
{
jumbleWithWordDictionaryFile.CurrentJumble = "xyzzqw";
bool didCountSucceed = jumbleWithWordDictionaryFile.CountMatchingWords();
Assert.AreEqual(true, didCountSucceed, "The CountMatchingWords method failed for the jumble 'xyzzqw'");

string expectedOutput = "0, 0, 0, 0";
string actualOutput = jumbleWithWordDictionaryFile.MatchCount3Letters.ToString();
actualOutput += ", ";
actualOutput += jumbleWithWordDictionaryFile.MatchCount4Letters.ToString();
actualOutput += ", ";
actualOutput += jumbleWithWordDictionaryFile.MatchCount5Letters.ToString();
actualOutput += ", ";
actualOutput += jumbleWithWordDictionaryFile.MatchCount6Letters;
Assert.AreEqual(expectedOutput, actualOutput, "The CountMatchingOutput method calculated inaccurate results.");
}
#endregion

Negative Tests

#region ---------- Negative Tests ----------
[Test]
public void SimpleNegativeTestCountMatchingWordsSpaceInJumble()
{
List dictionary = new List();
dictionary.Add("ace");
dictionary.Add("are");
dictionary.Add("dak");
dictionary.Add("ross");
dictionary.Add("rosy");
dictionary.Add("rynd");
dictionary.Add("rddnol"); ;
dictionary.Add("rndle");
dictionary.Add("rlead");

// this assignment should be denied leaving the CurrentJumble property pointing to an empty string
simpleJumble.CurrentJumble = "rn dal@#$@$ae";
simpleJumble.WordDictionary = dictionary;
bool didCountSucceed = simpleJumble.CountMatchingWords();
Assert.AreEqual(false, didCountSucceed, "The CountMatchingWords method failed to recognize malformed jumble 'rn dal@#$@$ae'");
}

[Test]
public void SimpleNegativeTestCountMatchingWordsSpaceInWord()
{
List dictionary = new List();
dictionary.Add("ace");
dictionary.Add("are");
dictionary.Add("dak");
dictionary.Add("ross");
dictionary.Add("rosy");
dictionary.Add("ry nd");
dictionary.Add("rddnol"); ;
dictionary.Add("rndle");
dictionary.Add("rlead");

simpleJumble.CurrentJumble = "naetcr";
simpleJumble.WordDictionary = dictionary;
bool didCountSucceed = simpleJumble.CountMatchingWords();
Assert.AreEqual(false, didCountSucceed, "The CountMatchingWords method failed to recognize malformed word 'ry nd'");
}

[Test]
public void SimpleNegativeTestCountMatchingWordsCommaEndedJumble()
{
List dictionary = new List();
dictionary.Add("ace");
dictionary.Add("are");
dictionary.Add("dak");
dictionary.Add("ronss");
dictionary.Add("roeesy");
dictionary.Add("ryndw");
dictionary.Add("rddsnol"); ;
dictionary.Add("rgdlfe");
dictionary.Add("rleqad");

// this assignment should be denied leaving the CurrentJumble property pointing to an empty string
simpleJumble.CurrentJumble = "rdle,";
simpleJumble.WordDictionary = dictionary;
bool didCountSucceed = simpleJumble.CountMatchingWords();
Assert.AreEqual(true, didCountSucceed, "The CountMatchingWords method failed to remove comma at the end of jumble 'rndle,'");
}
#endregion
Advertisements

Written by Arsalan A.

July 7, 2009 at 10:24 am

Posted in 1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: