Skip to content

Commit

Permalink
Adding test for parsing multiple options.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Oct 14, 2010
1 parent 2c2c9a3 commit 07bda82
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
6 changes: 3 additions & 3 deletions cake/console/console_option_parser.php
Expand Up @@ -175,10 +175,10 @@ protected function _parseShortOption($option, $params) {
protected function _parseOptionName($name, $params) {
$definition = $this->_options[$name];
$nextValue = $this->_nextToken();
if (empty($nextValue)) {
$value = $definition['default'];
} else if ($nextValue{0} != '-') {
if (!empty($nextValue) && $nextValue{0} != '-') {
$value = $nextValue;
} else {
$value = $definition['default'];
}
$params[$name] = $value;
return $params;
Expand Down
24 changes: 20 additions & 4 deletions cake/tests/cases/console/console_option_parser.test.php
Expand Up @@ -70,7 +70,7 @@ function testAddOptionLong() {
'shortcut' => 't'
));
$result = $parser->parse(array('--test', 'value'));
$this->assertEqual(array('test' => 'value'), $result[0], 'Long parameter did not parse out');
$this->assertEquals(array('test' => 'value'), $result[0], 'Long parameter did not parse out');
}

/**
Expand All @@ -84,7 +84,7 @@ function testAddOptionLongEquals() {
'shortcut' => 't'
));
$result = $parser->parse(array('--test=value'));
$this->assertEqual(array('test' => 'value'), $result[0], 'Long parameter did not parse out');
$this->assertEquals(array('test' => 'value'), $result[0], 'Long parameter did not parse out');
}

/**
Expand All @@ -98,7 +98,7 @@ function testAddOptionDefault() {
'default' => 'default value',
));
$result = $parser->parse(array('--test'));
$this->assertEqual(array('test' => 'default value'), $result[0], 'Default value did not parse out');
$this->assertEquals(array('test' => 'default value'), $result[0], 'Default value did not parse out');
}

/**
Expand All @@ -112,6 +112,22 @@ function testAddOptionShortcut() {
'shortcut' => 't'
));
$result = $parser->parse(array('-t', 'value'));
$this->assertEqual(array('test' => 'value'), $result[0], 'Short parameter did not parse out');
$this->assertEquals(array('test' => 'value'), $result[0], 'Short parameter did not parse out');
}

/**
* test multiple options at once.
*
* @return void
*/
function testMultipleOptions() {
$parser = new ConsoleOptionParser();
$parser->addOption('test')
->addOption('connection')
->addOption('table', array('shortcut' => 't'));

$result = $parser->parse(array('--test', 'value', '-t', '--connection', 'postgres'));
$expected = array('test' => 'value', 'table' => true, 'connection' => 'postgres');
$this->assertEquals($expected, $result[0], 'multiple options did not parse');
}
}

0 comments on commit 07bda82

Please sign in to comment.