I have done the following kata in TDD style and would appreciate it if someone could review my code and my tests.
String Calculator
Create a simple
String
calculator with a methodint Add(string numbers)
. 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".Start with the simplest test case of an empty string and move to 1 and two numbers
Remember to solve things as simply as possible so that you force yourself to write tests you did not think about.
Remember to refactor after each passing test.
Allow the
Add
method to handle an unknown amount of numbers.Allow the
Add
method to handle new lines between numbers (instead of commas). The following input is ok: "1\n2,3" (will equal 6). The following input is NOT ok: "1,\n" (not need to prove it - just clarifying).Support different delimiters to change a delimiter, the beginning of the string will contain a separate line that looks like this: "//[delimiter]\n[numbers…]". For example, "//;\n1;2" should return three where the default delimiter is ';'. The first line is optional. All existing scenarios should still be supported.
- Calling
Add
with a negative number will throw an exception "negatives not allowed" - and the negative that was passed. If there are multiple negatives, show all of them in the exception message.
public class StringCalculator
{
public int AddNumbers(string args)
{
if (string.IsNullOrEmpty(args))
{
return 0;
}
var delimeters = new List<char>()
{
'\n',','
};
if (args[0] == '/')
{
var customDelimeter = args[2];
delimeters.Add(customDelimeter);
args = args.Remove(0,
3);
}
var numbers = args.ToCharArray().Where(x => !delimeters.Contains(x)).ToList();
if (numbers.Any(x => x == '-'))
{
StringBuilder stringBuilder= new StringBuilder();
for (int i = 0;
i < numbers.Count;
i++)
{
if (numbers[i] == '-')
{
stringBuilder.Append("-");
stringBuilder.Append(numbers[++i]);
stringBuilder.Append(", ");
}
}
throw new Exception(string.Format("negatives {0} not allowed",stringBuilder.ToString()));
}
var sum = numbers.Sum(x => (int)Char.GetNumericValue(x));
return sum;
}
}
Tests:
[TestFixture]
public class StringCalculatorTests
{
[Test]
public void ShouldReturnZeroForEmptyString()
{
var sut = new StringCalculator();
var result = sut.AddNumbers("");
Assert.AreEqual(0,result);
}
[Test]
[TestCase(1,"1")]
[TestCase(2,"2")]
public void ShouldReturnNumberIfGivenOneNumber(int expected,string arg)
{
var sut = new StringCalculator();
var result = sut.AddNumbers(arg);
Assert.AreEqual(expected,result);
}
[Test]
[TestCase(3, "1,2")]
[TestCase(11, "1,2,3,5")]
public void ShouldReturnSumOfAllNumbers(int expected, string arg)
{
var sut = new StringCalculator();
var result = sut.AddNumbers(arg);
Assert.AreEqual(expected, result);
}
[Test]
[TestCase(3, "1\n2")]
[TestCase(11, "1,2\n3,5")]
public void ShouldAllowNewLineAsASeparator(int expected, string arg)
{
var sut = new StringCalculator();
var result = sut.AddNumbers(arg);
Assert.AreEqual(expected, result);
}
[Test]
[TestCase(3, "//;\n1;2")]
[TestCase(11, "//.\n1.2\n3.5")]
[TestCase(11, "//-\n1-2\n3-5")]
public void ShouldSupportDifferentSeparators(int expected, string arg)
{
var sut = new StringCalculator();
var result = sut.AddNumbers(arg);
Assert.AreEqual(expected, result);
}
[Test]
[TestCase(3, "//;\n-1;2")]
[TestCase(3, "//;\n-1;-2")]
[ExpectedException(typeof(Exception))]
public void ShouldThrowExceptionIfNegativeInArgs(int expected, string arg)
{
var sut = new StringCalculator();
var result = sut.AddNumbers(arg);
Assert.AreEqual(expected, result);
}
}
int Add(string numbers)
;) \$\endgroup\$