Create a gist now

Instantly share code, notes, and snippets.

anonymous /StringCalculatorTests.cs
Created Mar 6, 2013

What would you like to do?
Given this simple implementation of string calculator, mid way, how would you refactor this, based on SOLID principals? and how far would you go?
using NUnit.Framework;
namespace StringCalculatorTests
{
[TestFixture]
public class StringCalculatorTests
{
[TestCase("",0)]
[TestCase("1",1)]
[TestCase("1,2",3)]
public void Add_Empty_DefaultResults(string input,int expected)
{
StringCalculator sc = new StringCalculator();
int result = sc.Add(input);
Assert.AreEqual(result, expected);
}
}
public class StringCalculator
{
public int DEFAULT_RESULT = 0;
public int Add(string input)
{
if (input==string.Empty)
{
return DEFAULT_RESULT;
}
if (input.Contains(","))
{
return handleMultiple(input);
}
return ParseSingle(input);
}
private static int ParseSingle(string input)
{
return int.Parse(input);
}
private int handleMultiple(string input)
{
string[] numbers = input.Split(',');
return Add(numbers[0]) + Add(numbers[1]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment