Test Driven Development
By Marcus Baker2004-01-25
Design with working tests
Here is the refactored version...
class ConfigurationParser {
function parse($lines) {
$values = array();
foreach ($lines as $line) {
if (preg_match('/^(.*?)\s+(.*)$/', $line, $matches)) {
$values[$matches[1]] = trim($matches[2]);
}
}
return $values;
}
}
I actually got this right on the first go, but I suspect that this was a fluke. More likely I would have had a failure, such as forgetting to trim the trailing carriage return. In this case just do a hack to add it, rerun the tests, and then just focus on only that issue. It is much easier to shuffle the code about with the tests to protect you.
What I have done here is moved in the smallest possible steps. One of the joys of this process is that we can tune the step size as we go. If we get lot's of easy passes, take bigger steps. If you get a failure you don't expect, slow down and do less work each cycle. The cycle is red, green, refactor.
Tutorial pages:
|
|
|||||||||
You might also want to check these out:
|
Leave a Comment on "Test Driven Development"
You must be logged in to post a comment.
Link to This Tutorial Page!

