SlaMidtfyn's Wiki site : TDDKata1

Home :: Categories :: PageIndex :: RecentChanges :: RecentlyCommented :: Login/Register

Revision [180]

Most recent edit made on 2009-10-09 15:06:13 by SlaMidtfyn

Additions:
Description:
 

The Test
The class


Deletions:
Description:

The Test
The class




Revision [179]

Edited on 2009-10-09 15:03:59 by SlaMidtfyn

Additions:





Revision [178]

Edited on 2009-10-09 15:02:04 by SlaMidtfyn

Additions:
Description:


Deletions:
Test criterias




Revision [177]

Edited on 2009-10-09 15:00:07 by SlaMidtfyn

Additions:
Test criterias
# Create a simple String calculator with a method int Add(string numbers)
1. The method can take 0, 1 or 2 numbers, and will return their sum (for an empty string it will return 0) for example “” or “1” or “1,2”
2. Start with the simplest test case of an empty string and move to 1 and two numbers
3. Remember to solve things as simply as possible so that you force yourself to write tests you did not think about
4. Remember to refactor after each passing test




Revision [176]

Edited on 2009-10-09 14:08:35 by SlaMidtfyn

Additions:
http://osherove.com/tdd-kata-1/




Revision [175]

Edited on 2009-10-09 14:04:13 by SlaMidtfyn

Additions:
The class


Deletions:
The class




Revision [174]

Edited on 2009-10-09 14:03:58 by SlaMidtfyn

Additions:
The Test


Deletions:
The Test




Revision [173]

Edited on 2009-10-09 14:03:40 by SlaMidtfyn

Additions:





Revision [172]

The oldest known version of this page was edited on 2009-10-09 13:44:18 by SlaMidtfyn
The Test

[TestMethod]
public void Add_EmptyString_Returns0()
{
int actual = sut.Add("");
int expected = 0;
Assert.AreEqual<int>(expected, actual);
}

[TestMethod]
public void Add_StringWith1Number_ReturnsSameNumber()
{
int actual=sut.Add("1");
int expected = 1;
Assert.AreEqual<int>(expected, actual, "OneNumber");
}

[TestMethod]
public void Add_StringWithLargeNumber_ReturnsSameNumber()
{
int actual = sut.Add("12345");
int expected = 12345;
Assert.AreEqual<int>(expected, actual);
}

[TestMethod]
public void Add_StringWith2numbers_ReturnsSumOfNumbers()
{
int actual = sut.Add("1,2");
int expected = 3;
Assert.AreEqual<int>(expected, actual);
}

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void Add_StringContainsLetters_ThrowsException()
{
sut.Add("FAIL");
}

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void Add_StringContainsLettersAndNumbers_ThrowsException()
{
Fixture setup
sut.Add("1,FAIL");
}
}

The class
public class StringCalculator
{
public int Add(string p)
{
string[] numbers = p.Split(',');
return Sum(numbers);
}

private int Sum(string[] numbers)
{
return numbers.Sum(n => ToInt(n));
}

private int ToInt(string number)
{
int n;
if (!int.TryParse(number, out n) && !string.IsNullOrEmpty(number))
{
throw new ArgumentException("NaN");
}
return n;
}
}
Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by WikkaWiki
Page was generated in 0.1283 seconds