Skip to content

Commit

Permalink
Merge pull request #23 from DataValues/unitOption1
Browse files Browse the repository at this point in the history
Allow non-conflicting unit option
  • Loading branch information
Daniel Kinzler committed Mar 6, 2015
2 parents ca5ee56 + e3e8a98 commit 2af1de9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
9 changes: 2 additions & 7 deletions src/ValueParsers/QuantityParser.php
Expand Up @@ -67,12 +67,8 @@ protected function stringParse( $value ) {
$unitOption = $this->getOption( self::OPT_UNIT );

if ( $unit === null ) {
if ( $unitOption !== null ) {
$unit = $unitOption;
} else {
$unit = '1';
}
} elseif ( $unitOption !== null ) {
$unit = $unitOption !== null ? $unitOption : '1';
} elseif ( $unitOption !== null && $unit !== $unitOption ) {
throw new ParseException( 'Cannot specify a unit in input if a unit was fixed via options.' );
}

Expand Down Expand Up @@ -115,7 +111,6 @@ private function newQuantityFromParts( $amount, $exactness, $margin, $unit ) {
}

return $quantity;

}

/**
Expand Down
36 changes: 29 additions & 7 deletions tests/ValueParsers/QuantityParserTest.php
Expand Up @@ -196,24 +196,46 @@ function( $number ) use ( $charmap ) {
$this->assertEquals( 'a~b', $quantity->getUnit() );
}

public function testUnitOption() {
/**
* @dataProvider unitOptionProvider
*/
public function testUnitOption( $value, $unit ) {
$options = new ParserOptions();
$options->setOption( QuantityParser::OPT_UNIT, 'kittens' );
$options->setOption( QuantityParser::OPT_UNIT, $unit );

$parser = new QuantityParser( $options );

$quantity = $parser->parse( '17' );
$this->assertEquals( 'kittens', $quantity->getUnit() );
$quantity = $parser->parse( $value );
$this->assertEquals( $unit, $quantity->getUnit() );
}

public function unitOptionProvider() {
return array(
array( '17', 'kittens' ),
array( '17 kittens', 'kittens' ),
array( '17m', 'm' ),
);
}

public function testUnitOption_conflict() {
/**
* @dataProvider conflictingUnitOptionProvider
*/
public function testConflictingUnitOption( $value, $unit ) {
$options = new ParserOptions();
$options->setOption( QuantityParser::OPT_UNIT, 'kittens' );
$options->setOption( QuantityParser::OPT_UNIT, $unit );

$parser = new QuantityParser( $options );

$this->setExpectedException( 'ValueParsers\ParseException' );
$parser->parse( '17m' );
$parser->parse( $value );
}

public function conflictingUnitOptionProvider() {
return array(
array( '17 kittens', 'm' ),
array( '17m', 'kittens' ),
array( '17m', '' ),
);
}

}

0 comments on commit 2af1de9

Please sign in to comment.