.
Developer Spot - Web Development Tutorials
 


Web Hosting Directory
Budget Web Hosting Linux Web Hosting Small Business Hosting
Windows Web Hosting Reseller Web Hosting Web Hosting Articles

Test Driven Development

By Marcus Baker
2004-01-25
Reader Rating: 5 out of 5
Bookmark Print Version
Tests as documentation

We repeat the cycle until we cannot think of any more sensible tests to add. With five more cycles we get the following test case...

class ConfigurationTest extends UnitTestCase {
    function ConfigurationTest() {
        $this->UnitTestCase();
    }
    function testNoLinesGivesEmptyHash() {
        $parser = &new ConfigurationParser();
        $this->assertIdentical($parser->parse(array()), array());
    }
    function testKeyValuePair() {
        $parser = &new ConfigurationParser();
        $this->assertEqual(
            $parser->parse(array("a A long message\n")),
            array('a' => 'A long message'));
    }
    function testMultipleKeyValuePairs() {
        $parser = &new ConfigurationParser();
        $this->assertEqual(
            $parser->parse(array("a A\n", "b\tB\n")),
            array('a' => 'A', 'b' => 'B'));
    }
    function testBlankLinesAreIgnored() {
        $parser = &new ConfigurationParser();
        $this->assertEqual(
            $parser->parse(array("\n", "key value\n")),
            array('key' => 'value'));
    }
    function testCommentLinesAreIgnored() {
        $parser = &new ConfigurationParser();
        $this->assertEqual(
            $parser->parse(array("# A comment\n", "key value\n")),
            array('key' => 'value'));
    }
}

Notice how the test case describes the behaviour. Once you are used to reading test cases you can use them as an executable specification of the code. Unlike coments they cannot lie.

Now look what has happened to the code...

class ConfigurationParser {
    function parse($lines) {
        $values = array();
        foreach ($lines as $line) {
            if (preg_match('/^\s*#/', $line)) {
                continue;
            }
            if (preg_match('/^(\w+)\s+(.*)/', $line, $matches)) {
                $values[$matches[1]] = trim($matches[2]);
            }
        }
        return $values;
    }
}

That crucial regular expression has gone through several refinements. Even though I was able to code the first version without breaking the tests, I found plenty of bugs when I added blank lines and comments into the mix. Just goes to show what happens if you don't test.



Article Pages:
Test Driven Development
Writing a Test
A Simple Test Case
Our first test (at last)
Minimal Code
Design with working tests
Tests as documentation
Test then Code then Design

 Rate this article:   Poor          Excellent 


If you found this article interesting, you may want to read these as well:

» Protecting your PHP and HTML Source Code

» Publishing Newsletters Using PHP & MySQL - 4

» Publishing Newsletters Using PHP & MySQL - 3

» Publishing Newsletter Using PHP & MySQL - 2

» Publishing Newsletters Using PHP & MySQL

» Unix Webserver Crontab Basics



 
Development Tutorials
ASP
CGI & Perl
CSS
HTML
Java
JavaScript
Linux
PHP
XML




More Resources
Web Hosting Articles
Development Tutorials: CGI & Perl - CSS - HTML - Java - JavaScript - Linux - PHP - XML