# TDD Kata | String Calculator

Kata is a Japanese word describing detailed choreographed patterns of movements practiced either solo or in pairs. most commonly known for the presence in the martial arts.
The idea is if you practice something regularly, at some point it will be come natural. (As they call it “Muscle memory” :)).
Even though Test Driven Development has been preached as best practice for years now, there is still resistance in many IT shops to adapt TDD (Test Driven Developer). So the best way to teach your team to start making TDD part of their daily development practice is by running small hands on sessions, KATA.
Yesterday, the architect (a very passionate developer and lead) at my job took the team through one 1hr journey of solving a simple but yet complex enough problem to have the taste of TDD (Red-Green-Re factor) .
You can find the problem definition at Royâ€™s String Calculator TDD Kata.

## String Calculator

The following is a TDD Kata- an exercise in coding, refactoring and test-first, that you should apply daily for at least 15 minutes (I do 30).

### Before you start:

• Do one task at a time. The trick is to learn to work incrementally.
• Make sure you only test for correct inputs. there is no need to test for invalid inputs for this kata

### String Calculator

1. 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
2. Allow the Add method to handle an unknown amount of numbers
3. Allow the Add method to handle new lines between numbers (instead of commas).
1. the following input is ok:  â€œ1\n2,3â€  (will equal 6)
2. the following input is NOT ok:  â€œ1,\nâ€ (not need to prove it – just clarifying)
1. Support different delimiters

2. 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 â€˜;â€™ .
3. the first line is optional. all existing scenarios should still be supported
4. 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

stop here if you are a beginner. Continue if you can finish the steps so far in less than 30 minutes.

5. Numbers bigger than 1000 should be ignored, so adding 2 + 1001  = 2
6. Delimiters can be of any length with the following format:  â€œ//[delimiter]\nâ€ for example: â€œ//[***]\n1***2***3â€ should return 6
7. Allow multiple delimiters like this:  â€œ//[delim1][delim2]\nâ€ for example â€œ//[*][%]\n1*2%3â€ should return 6.
8. make sure you can also handle multiple delimiters with length longer than one char

I am sharing my first attempt to develop the String Calculator add functionality using TDD.

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace TDDFun
{
public class StringCalculator
{
{
Double sum = 0;
if (string.IsNullOrEmpty(numbers))
{
sum = 0;
}
else
{
const string regex1 = â€œ(\\//)â€;
const string regex2 = â€œ(.*?)â€;
const string regex3 = â€œ(\\n)â€;
const string regex4 = â€œ(.*?\\d)â€;
var delimiters = new List<string>();
var r = new Regex(regex1 + regex2 + regex3 + regex4, RegexOptions.IgnoreCase | RegexOptions.Singleline);
Match m = r.Match(numbers);
if (m.Success)
{
string delimiterCollection = m.Groups[2].ToString();
int numberStartIndex = m.Groups[3].Index;
const string re5 = â€œ(\\[.*?\\])â€;
var r2 = new Regex(re5, RegexOptions.IgnoreCase | RegexOptions.Singleline);
MatchCollection m2 = r2.Matches(delimiterCollection);
if (m2.Count > 0)
{
foreach (Match x in m2)
{
}
}
else
{
}
numbers = numbers.Remove(0, numberStartIndex + 1);
}
else
{
}
string[] splittedNumbers = numbers.Split(delimiters.ToArray(), StringSplitOptions.None);
ValiateNumbers(splittedNumbers);
foreach (string s in splittedNumbers)
{
double ss = Double.Parse(s);
sum += ss <= 1000 ? ss : 0;
}
}
return sum;
}
private static void ValiateNumbers(IEnumerable<string> numbers)
{
double x;
var negativeNumbers = new List<string>();
foreach (string s in numbers)
{
Validator.IsRequiredField(Double.TryParse(s, out x), â€œValidation Errorâ€);
if (Double.Parse(s) < 0)
{
}
}
Validator.IsRequiredField(negativeNumbers.Count <= 0,
â€œNegatives not allowed â€œ + ShowAllNegatives(negativeNumbers));
}
private static string ShowAllNegatives(List<string> negativeNumbers)
{
var sb = new StringBuilder();
int counter = 0;
negativeNumbers.ForEach(k =>
{
if (counter == 0)
{
sb.Append(k);
counter++;
}
else
{
sb.Append(â€œ;â€ + k);
}
});
return sb.ToString();
}
}
public static class Validator
{
public static void IsRequiredField(bool criteria, string message)
{
if (!criteria)
{
throw new ValidationException(message);
}
}
}
public class ValidationException : ApplicationException
{
public ValidationException(string message) : base(message)
{
}
}
}

using NUnit.Framework;
namespace TDDFun
{
[TestFixture]
public class StringCalculatorTest
{
private StringCalculator calculator;
[SetUp]
public void SetUp()
{
calculator = new StringCalculator();
}
[Test]
{
}
[Test]
{
}
[Test]
{
}
[Test]
{
}
[Test]
{
}
[Test]
{
}
[Test]
{
}
[Test]
{
}
[Test]
{
Assert.IsTrue(calculator.Add(â€œ//[***][kkk]\n1.1   ***  1.2kkk   2â€³) == 4.3);
}
[Test]
{
var exception = Assert.Throws<ValidationException>(() => calculator.Add(â€œ//[***]\n-1***-2â€³));
Assert.AreEqual(exception.Message, â€œNegatives not allowed -1;-2â€³);
}
[Test]
{
}
[Test]
{
var exception = Assert.Throws<ValidationException>(() => calculator.Add(â€œ-1,-2â€³));
Assert.AreEqual(exception.Message, â€œNegatives not allowed -1;-2â€³);
}
[Test]
{
}
[Test]
{
}
[Test]
{
var exception = Assert.Throws<ValidationException>(() => calculator.Add(â€œ//;\n-1;-2â€³));
Assert.AreEqual(exception.Message, â€œNegatives not allowed -1;-2â€³);
}
[Test]
{
}
[Test]
{
}
[Test]
{
}
[Test]
{
}
[Test]
{
}
[Test]
{
}
[Test]
{