Saturday, January 8, 2011

Roy Osherove's String Calculator Kata - My first version

I recently heard about programming kata. So I tried Roy Osherove's String Calculator Kata .

This was my first time and will try it probably more because I like to make it into a video.

The class.

```///
/// http://www.osherove.com/tdd-kata-1/
///
class Calculator {

private static string[] Split(string value, string [] splitingWords) {

return value.Split(splitingWords, StringSplitOptions.None);
}
private static string [] Split(string value, string splitingWord){

return value.Split(new string[] { splitingWord }, StringSplitOptions.None);
}
private static bool IsNumber(string n) {
int a = 0;
return int.TryParse(n, out a);
}
public static int Add(string numbers) {

string delimiter    = ",";
string[] delimiters = null;

if (numbers.StartsWith("//[")) {

var newLinePos  = numbers.IndexOf("\n");
delimiters      = numbers.Substring(3, newLinePos - 4).Replace("[", "").Split(']');
numbers         = numbers.Substring(newLinePos + 1);
}
else if (numbers.StartsWith("//")) {

var newLinePos = numbers.IndexOf("\n");
if (newLinePos == -1) throw new System.FormatException(String.Format("Invalid custom delimiters in expression '{0}'", numbers));
delimiter = numbers[newLinePos - 1].ToString();
numbers   = numbers.Substring(newLinePos + 1);
}

numbers = numbers.Replace("\n", delimiter);

if (numbers.Contains(delimiter + delimiter))
throw new System.FormatException(String.Format("Invalid expression '{0}'", numbers));

string[] values;

if (delimiters != null) {
values = Split(numbers, delimiters);
}
else if (delimiter.Length > 1) {
values = Split(numbers, delimiter);
}
else {
values = numbers.Split(delimiter[0]);
}
int sum    = 0;
var negativeNumbers = "";

foreach (var i in values) {

if ((i != "") && (int.Parse(i) < 0)) {
negativeNumbers += i + ",";
}
if ((i != "") && (int.Parse(i) >= 1000)) {
}
else {
sum += int.Parse(i == "" ? "0" : i);
}
}
if(!String.IsNullOrEmpty(negativeNumbers))
throw new System.ArgumentException(String.Format("negatives not allowed '{0}'", negativeNumbers));

return sum;
}
}

```

The unit tests.
```[TestClass]
public class CalculatorUnitTests {

}

}
[TestMethod]

}
[TestMethod]

}
[TestMethod]

}
[TestMethod]

}
[TestMethod, ExpectedException(typeof(System.FormatException))]

}

}
[TestMethod, ExpectedException(typeof(System.ArgumentException))]

}
[TestMethod, ExpectedException(typeof(System.ArgumentException))]

}
[TestMethod]

}
[TestMethod]

}
[TestMethod]

}
[TestMethod]