Last active
February 26, 2020 22:47
-
-
Save kid-cavaquinho/b457080cfbfcaffff57c6890e2aa0658 to your computer and use it in GitHub Desktop.
String Calculator Kata (http://osherove.com/tdd-kata-1) This was my first iteration for 15 minutes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class StringCalculator | |
| { | |
| private const string DefaultDelimiters = ",\n"; | |
| public int Add(string numbers) | |
| { | |
| if (string.IsNullOrWhiteSpace(numbers)) | |
| { | |
| return 0; | |
| } | |
| var delimiters = DefaultDelimiters; | |
| if (numbers.StartsWith("//")) | |
| { | |
| delimiters += numbers.Split("\n")[0].Replace("//[", string.Empty).Replace("]", string.Empty); | |
| numbers = numbers.Split("\n")[1]; | |
| } | |
| var items = numbers.Split(delimiters.ToCharArray()).Where(i => !string.IsNullOrEmpty(i)); | |
| IReadOnlyList<int> integers = items.Select(int.Parse).ToList(); | |
| IReadOnlyList<int> negatives = integers.Where(i => i < 0).ToList(); | |
| if (negatives.Count > 0) | |
| { | |
| throw new Exception($"negatives not allowed { string.Join(" ", negatives) }"); | |
| } | |
| return integers.Where(i => i > 0 && i < 1000).Sum(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class StringCalculatorUnitTest | |
| { | |
| private readonly StringCalculator _target; | |
| public StringCalculatorUnitTest() | |
| { | |
| _target = new StringCalculator(); | |
| } | |
| [Theory] | |
| [InlineData("", 0)] | |
| [InlineData(" ", 0)] | |
| [InlineData("1", 1)] | |
| [InlineData("1,2", 3)] | |
| [InlineData("1\n2,3", 6)] | |
| [InlineData("1001,2", 2)] | |
| [InlineData("//[***]\n1***2***3", 6)] | |
| [InlineData("//[*][%]\n1*2%3", 6)] | |
| [InlineData("//;\n1;2", 3)] | |
| public void Add_Should_Return_Correct_Sum(string numbers, int expected) | |
| { | |
| // Arrange | |
| // Act | |
| var actual = _target.Add(numbers); | |
| // Assert | |
| Assert.Equal(expected, actual); | |
| } | |
| [Theory] | |
| [InlineData("-1", "negatives not allowed -1")] | |
| [InlineData("-1,-2", "negatives not allowed -1 -2")] | |
| [InlineData("1,-2", "negatives not allowed -2")] | |
| [InlineData("1\n2,-3", "negatives not allowed -3")] | |
| public void Add_Should_Throw_Exception_When_Negative_Numbers_Are_Added(string numbers, string message) | |
| { | |
| // Arrange | |
| // Act | |
| // Assert | |
| var exception = Assert.Throws<Exception>(() => _target.Add(numbers)); | |
| Assert.Equal(exception.Message, message); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment