I'm performing this TDD kata excercise: http://ift.tt/1dG3gvX
I produced following code (points from 1 to 5 in this excercise - I have unit tests for it):
public class StringCalculator
{
private readonly string[] _defaultSeparators = { ",", "\n" };
public int Add(string numbers)
{
// Parser section (string to list of ints)
var separators = _defaultSeparators;
var isSeparatorDefinitionSpecified = numbers.StartsWith("//");
if (isSeparatorDefinitionSpecified)
{
var endOfSeparatorDefinition = numbers.IndexOf('\n');
var separator = numbers.Substring(2, endOfSeparatorDefinition - 2);
numbers = numbers.Substring(endOfSeparatorDefinition);
separators = new[] { separator };
}
var numbersArray = numbers.Split(separators, StringSplitOptions.RemoveEmptyEntries);
var numbersArrayAsInts = numbersArray.Select(int.Parse).ToArray();
// Validator section
var negativeNumbers = numbersArrayAsInts.Where(c => c < 0).ToArray();
if (negativeNumbers.Any())
{
throw new Exception(string.Format("negatives not allowed ({0})", string.Join(", ", negativeNumbers)));
}
return numbersArrayAsInts.Sum();
}
}
Now I want to refactor code to something like this:
public int Add(string numbers)
{
var numbersAsInts = CalculatorNumbersParser.Parse(numbers);
CalculatorNumbersValidator.Validate(numbersAsInts);
return numbersAsInts.Sum();
}
How I should plan refactor to properly refactor my code and unit tests?
I think that I should move part of tests to new created implementations classes tests (CalculatorNumbersParserTests and CalculatorNumbersValidatorTests), change some existing tests and add tests for Parse and Validate method execution.
But what is the correct way to do this without breaking the tests?
Aucun commentaire:
Enregistrer un commentaire