Test:
it("starts and ends with slash-star", function() {
assert.isTrue(isComment("/**/"), "empty comment");
assert.isFalse(isComment(""), "empty string");
assert.isFalse(isComment("foo"), "not a comment");
});
Code: isComment(text) {
return text.matches(/\/\*\*\//); // excuse any errors; haven't actually tried this
}
That's the first TDD loop. Takes less than a minute. But it's obviously incomplete, so now we build it up, step by step.Add another test:
it("can contain text", function() {
assert.isTrue(isComment("/* foo */"));
}
Modify the code: isComment(text) {
return text.matches(/\/\*.*?\*\//);
}
That also takes less than a minute. We continue in this way, step by step, handling more and more specific edge cases, until we think we've handled all the cases. For example, the next test might be it("can span multiple lines").I've been pondering why this feels so counterintuitive to me. I think it's because, in traditional testing, the test is treated as authoritative. Your test is supposed to be 100% correct, and then you make the code good enough to pass the test. The word "test" suggests a teacher grading an exam: the teacher must be 100% correct.
So, I wonder if it is helpful when explaining this approach to explicitly set aside this assumption. The program and the test can both be incomplete. Would it be an overstatement to say it seems like the test and the program have more of a symmetric relationship, rather than an asymmetric one?
Thanks for taking the time to explain this!
Based on my personal experience and observations of others so far, I think a large fraction of programmers experience unit tests as
(a) taking more time to write than the code under test;
(b) requiring much more code than the code under test; and
(c) relied upon to be correct much more than the code under test.
(To be clear, I'm talking about unit tests, not integration or system tests.)
In order to understand what you meant by TDD, I needed to unlearn all these things, and I suspect others will need to as well.
For instance, how could one possibly write a complete test for the behaviour of a regular expression that matches a C comment in less than 2 minutes or even 5 minutes?
The test has to be understandable to other readers, so I would easily spend a few minutes just documenting it carefully so that other readers could convince themselves that the test is complete.