Additions:
Description:
The Test
The class
Deletions:
Description:
The Test
The class
Additions:
Additions:
Description:
Deletions:
Test criterias
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
Additions:
Additions:
The class
Deletions:
The class
Additions:
The Test
Deletions:
The Test
Additions:
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);
}
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)
{
}
}