Skip to content

Instantly share code, notes, and snippets.

@jimmyp
Created March 15, 2018 00:25
Show Gist options
  • Save jimmyp/fffb7fac1345cfbd1268b85d56310a1c to your computer and use it in GitHub Desktop.
Save jimmyp/fffb7fac1345cfbd1268b85d56310a1c to your computer and use it in GitHub Desktop.
String CalcTDD Kata with the IQ Team
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using Xunit;
namespace StringCalculator
{
public class CalculatorTests
{
[Fact]
public void Add_EmptyString_ReturnZero()
{
WhenTestingAddWith(String.Empty, expect:0);
}
[Theory]
[InlineData("4", 4)]
[InlineData("7", 7)]
public void Add_SingleStringNumber_ReturnsNumberAsInteger(string input, int expect)
{
WhenTestingAddWith(input, expect);
}
[Theory]
[InlineData("1,2", 3)]
[InlineData("1,2,3", 6)]
public void Add_MultipleStringNumbers_ReturnsSumOfNumbers(string input, int expect)
{
WhenTestingAddWith(input, expect);
}
private static void WhenTestingAddWith(string input, int expect)
{
var actual = Calculator.Add(input);
Assert.Equal(expect, actual);
}
}
public class Calculator
{
public static int Add(string data)
{
//if(IsInvalid(data)) throw new Exception($"WTF? GOt some {data}");
if (string.IsNullOrEmpty(data))
return 0;
if (HasMultipleNumbers(data))
{
return AddMultipleNumbers(data);
}
return ParseNumber(data);
}
private static int ParseNumber(string data) => int.Parse(data);
private static int AddMultipleNumbers(string data) => data.Split(',').Select(ParseNumber).Sum();
private static bool HasMultipleNumbers(string data) => data.Contains(",");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment