<?php
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;
}
}
?>
<?php
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 messagen")),
array('a' => 'A long message'));
}
function testMultipleKeyValuePairs() {
$parser = &new ConfigurationParser();
$this->assertEqual(
$parser->parse(array("a An", "btBn")),
array('a' => 'A', 'b' => 'B'));
}
function testBlankLinesAreIgnored() {
$parser = &new ConfigurationParser();
$this->assertEqual(
$parser->parse(array("n", "key valuen")),
array('key' => 'value'));
}
function testCommentLinesAreIgnored() {
$parser = &new ConfigurationParser();
$this->assertEqual(
$parser->parse(array("# A commentn", "key valuen")),
array('key' => 'value'));
}
}
?>