Jeroen De Dauw has uploaded a new change for review. https://gerrit.wikimedia.org/r/58867
Change subject: Deprecate ValueParser Result interface in favour of throwing ParseException ...................................................................... Deprecate ValueParser Result interface in favour of throwing ParseException Change-Id: I9d27241701e1387246a3fb56c5d590e5b0f27919 --- M ValueParsers/ValueParsers.classes.php M ValueParsers/ValueParsers.mw.php M ValueParsers/includes/Error.php A ValueParsers/includes/ParseException.php M ValueParsers/includes/Result.php M ValueParsers/includes/ValueParser.php M ValueParsers/includes/api/ApiParseValue.php M ValueParsers/includes/parsers/BoolParser.php M ValueParsers/includes/parsers/FloatParser.php M ValueParsers/includes/parsers/GeoCoordinateParser.php M ValueParsers/includes/parsers/IntParser.php M ValueParsers/includes/parsers/NullParser.php M ValueParsers/includes/parsers/StringValueParser.php M ValueParsers/includes/parsers/TitleParser.php M ValueParsers/tests/includes/parsers/BoolParserTest.php M ValueParsers/tests/includes/parsers/FloatParserTest.php M ValueParsers/tests/includes/parsers/GeoCoordinateParserTest.php M ValueParsers/tests/includes/parsers/IntParserTest.php M ValueParsers/tests/includes/parsers/NullParserTest.php M ValueParsers/tests/includes/parsers/StringValueParserTest.php M ValueParsers/tests/includes/parsers/TitleParserTest.php D ValueParsers/tests/includes/parsers/ValueParserTest.php M ValueParsers/tests/includes/parsers/ValueParserTestBase.php 23 files changed, 256 insertions(+), 208 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/DataValues refs/changes/67/58867/1 diff --git a/ValueParsers/ValueParsers.classes.php b/ValueParsers/ValueParsers.classes.php index bc3eb47..03b9d31 100644 --- a/ValueParsers/ValueParsers.classes.php +++ b/ValueParsers/ValueParsers.classes.php @@ -28,6 +28,7 @@ */ return array( 'ValueParsers\Error' => 'includes/Error.php', + 'ValueParsers\ParseException' => 'includes/ParseException.php', 'ValueParsers\ErrorObject' => 'includes/Error.php', 'ValueParsers\ParserOptions' => 'includes/ParserOptions.php', 'ValueParsers\Result' => 'includes/Result.php', diff --git a/ValueParsers/ValueParsers.mw.php b/ValueParsers/ValueParsers.mw.php index a22c133..9d91a75 100644 --- a/ValueParsers/ValueParsers.mw.php +++ b/ValueParsers/ValueParsers.mw.php @@ -74,7 +74,6 @@ 'includes/parsers/IntParser', 'includes/parsers/NullParser', 'includes/parsers/TitleParser', - 'includes/parsers/ValueParser', 'includes/Error', 'includes/ParserOptions', diff --git a/ValueParsers/includes/Error.php b/ValueParsers/includes/Error.php index 10dd134..96e4c6e 100644 --- a/ValueParsers/includes/Error.php +++ b/ValueParsers/includes/Error.php @@ -21,6 +21,7 @@ * http://www.gnu.org/copyleft/gpl.html * * @since 0.1 + * @deprecated * * @file * @ingroup ValueParsers diff --git a/ValueParsers/includes/ParseException.php b/ValueParsers/includes/ParseException.php new file mode 100644 index 0000000..d57dc8b --- /dev/null +++ b/ValueParsers/includes/ParseException.php @@ -0,0 +1,33 @@ +<?php + +namespace ValueParsers; + +/** + * Parse exception + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @since 0.1 + * + * @file + * @ingroup ValueParsers + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < jeroended...@gmail.com > + */ +class ParseException extends \RuntimeException { + +} diff --git a/ValueParsers/includes/Result.php b/ValueParsers/includes/Result.php index 76a67b4..ab7621b 100644 --- a/ValueParsers/includes/Result.php +++ b/ValueParsers/includes/Result.php @@ -24,6 +24,7 @@ * http://www.gnu.org/copyleft/gpl.html * * @since 0.1 + * @deprecated * * @file * @ingroup ValueParsers diff --git a/ValueParsers/includes/ValueParser.php b/ValueParsers/includes/ValueParser.php index fffc4d7..ae558aa 100644 --- a/ValueParsers/includes/ValueParser.php +++ b/ValueParsers/includes/ValueParser.php @@ -43,7 +43,7 @@ * * @param mixed $value The value to parse * - * @return Result + * @return mixed */ public function parse( $value ); diff --git a/ValueParsers/includes/api/ApiParseValue.php b/ValueParsers/includes/api/ApiParseValue.php index 5d38b9f..937f8bb 100644 --- a/ValueParsers/includes/api/ApiParseValue.php +++ b/ValueParsers/includes/api/ApiParseValue.php @@ -3,6 +3,8 @@ namespace ValueParsers; use ApiBase; +use DataValues\DataValue; +use LogicException; use MWException; /** @@ -57,6 +59,24 @@ * @since 0.1 */ public function execute() { + $parser = $this->getParser(); + + $results = array(); + + $params = $this->extractRequestParams(); + + foreach ( $params['values'] as $value ) { + $results[] = $this->parseValue( $parser, $value ); + } + + $this->outputResults( $results ); + } + + /** + * @return ValueParser + * @throws LogicException + */ + private function getParser() { $params = $this->extractRequestParams(); $options = $this->getOptionsObject( $params['options'] ); @@ -64,36 +84,37 @@ // Paranoid check, should never fail as we only accept registered parsers for the parser parameter. if ( $parser === null ) { - throw new MWException( 'Could not obtain a ValueParser instance' ); + throw new LogicException( 'Could not obtain a ValueParser instance' ); } - $results = array(); + return $parser; + } - foreach ( $params['values'] as $value ) { + private function parseValue( ValueParser $parser, $value ) { + $result = array( + 'raw' => $value + ); + + try { $parseResult = $parser->parse( $value ); - - $result = array( - 'raw' => $value - ); - - if ( $parseResult->isValid() ) { - $value = $parseResult->getValue(); - - if ( $value instanceof \DataValues\DataValue ) { - $result['value'] = $value->getArrayValue(); - $result['type'] = $value->getType(); - } - else { - $result['value'] = $value; - } - } - else { - $result['error'] = $parseResult->getError()->getText(); - } - - $results[] = $result; + } + catch ( ParseException $parsingError ) { + $result['error'] = $parsingError->getMessage(); + return $result; } + if ( $parseResult instanceof DataValue ) { + $result['value'] = $parseResult->getArrayValue(); + $result['type'] = $parseResult->getType(); + } + else { + $result['value'] = $parseResult; + } + + return $result; + } + + private function outputResults( array $results ) { $this->getResult()->setIndexedTagName( $results, 'result' ); $this->getResult()->addValue( diff --git a/ValueParsers/includes/parsers/BoolParser.php b/ValueParsers/includes/parsers/BoolParser.php index 1fa6207..e7df43d 100644 --- a/ValueParsers/includes/parsers/BoolParser.php +++ b/ValueParsers/includes/parsers/BoolParser.php @@ -2,6 +2,8 @@ namespace ValueParsers; +use DataValues\BooleanValue; + /** * ValueParser that parses the string representation of a boolean. * @@ -48,17 +50,17 @@ * * @param string $value * - * @return Result + * @return BooleanValue + * @throws ParseException */ protected function stringParse( $value ) { $value = strtolower( $value ); if ( array_key_exists( $value, $this->values ) ) { - return Result::newSuccess( new \DataValues\BooleanValue( $this->values[$value] ) ); + return new BooleanValue( $this->values[$value] ); } - else { - return $this->newErrorResult( 'Not a boolean' ); - } + + throw new ParseException( 'Not a boolean' ); } } diff --git a/ValueParsers/includes/parsers/FloatParser.php b/ValueParsers/includes/parsers/FloatParser.php index 2efc83a..03409d1 100644 --- a/ValueParsers/includes/parsers/FloatParser.php +++ b/ValueParsers/includes/parsers/FloatParser.php @@ -2,6 +2,8 @@ namespace ValueParsers; +use DataValues\NumberValue; + /** * ValueParser that parses the string representation of a float. * @@ -39,15 +41,15 @@ * * @param string $value * - * @return Result + * @return NumberValue + * @throws ParseException */ protected function stringParse( $value ) { if ( preg_match( '/^(-)?\d+((\.|,)\d+)?$/', $value ) ) { - return Result::newSuccess( new \DataValues\NumberValue( (float)$value ) ); + return new NumberValue( (float)$value ); } - else { - return Result::newErrorText( 'Not a float' ); - } + + throw new ParseException( 'Not a float' ); } } diff --git a/ValueParsers/includes/parsers/GeoCoordinateParser.php b/ValueParsers/includes/parsers/GeoCoordinateParser.php index 9d387e3..9921021 100644 --- a/ValueParsers/includes/parsers/GeoCoordinateParser.php +++ b/ValueParsers/includes/parsers/GeoCoordinateParser.php @@ -2,6 +2,7 @@ namespace ValueParsers; +use DataValues\GeoCoordinateValue; use Exception; use InvalidArgumentException; @@ -103,8 +104,8 @@ * * @param string $value * - * @return Result - * @throws Exception + * @return GeoCoordinateValue + * @throws ParseException */ protected function stringParse( $value ) { $value = $this->getNormalizedNotation( $value ); @@ -112,15 +113,13 @@ $notationType = $this->getCoordinatesType( $value ); if ( $notationType === false ) { - return $this->newErrorResult( 'Not a geographical coordinate' ); + throw new ParseException( 'Not a geographical coordinate' ); } $coordinates = explode( $this->getOption( self::OPT_SEPARATOR_SYMBOL ), $value ); if ( count( $coordinates ) !== 2 ) { - // @codeCoverageIgnoreStart - throw new Exception( 'A coordinates string with an incorrect segment count has made it through validation' ); - // @codeCoverageIgnoreEnd + throw new ParseException( 'A coordinates string with an incorrect segment count has made it through validation' ); } list( $latitude, $longitude ) = $coordinates; @@ -128,9 +127,9 @@ $latitude = $this->getParsedCoordinate( $notationType, $latitude ); $longitude = $this->getParsedCoordinate( $notationType, $longitude ); - $coordinate = new \DataValues\GeoCoordinateValue( $latitude, $longitude ); + $coordinate = new GeoCoordinateValue( $latitude, $longitude ); - return Result::newSuccess( $coordinate ); + return $coordinate; } /** @@ -143,7 +142,7 @@ * * @return float * - * @throws InvalidArgumentException + * @throws ParseException */ protected function getParsedCoordinate( $notationType, $coordinate ) { $coordinate = $this->resolveDirection( $coordinate ); @@ -158,9 +157,7 @@ case self::TYPE_DMS: return $this->parseDMSCoordinate( $coordinate ); default: - // @codeCoverageIgnoreStart - throw new InvalidArgumentException( 'Invalid coordinate type specified' ); - // @codeCoverageIgnoreEnd + throw new ParseException( 'Invalid coordinate type specified' ); } } diff --git a/ValueParsers/includes/parsers/IntParser.php b/ValueParsers/includes/parsers/IntParser.php index c648e24..bdcdef0 100644 --- a/ValueParsers/includes/parsers/IntParser.php +++ b/ValueParsers/includes/parsers/IntParser.php @@ -2,6 +2,8 @@ namespace ValueParsers; +use DataValues\NumberValue; + /** * ValueParser that parses the string representation of an integer. * @@ -37,17 +39,17 @@ * * @param string $value * - * @return Result + * @return NumberValue + * @throws ParseException */ protected function stringParse( $value ) { $positiveValue = strpos( $value, '-' ) === 0 ? substr( $value, 1 ) : $value; if ( ctype_digit( $positiveValue ) ) { - return Result::newSuccess( new \DataValues\NumberValue( (int)$value ) ); + return new NumberValue( (int)$value ); } - else { - return Result::newErrorText( 'Not an integer' ); - } + + throw new ParseException( 'Not an integer' ); } } diff --git a/ValueParsers/includes/parsers/NullParser.php b/ValueParsers/includes/parsers/NullParser.php index 7fd83ec..81302a2 100644 --- a/ValueParsers/includes/parsers/NullParser.php +++ b/ValueParsers/includes/parsers/NullParser.php @@ -2,6 +2,8 @@ namespace ValueParsers; +use DataValues\UnknownValue; + /** * Implementation of the ValueParser interface that does a null parse. * @@ -37,10 +39,10 @@ * * @param mixed $value * - * @return Result + * @return UnknownValue */ public function parse( $value ) { - return Result::newSuccess( new \DataValues\UnknownValue( $value ) ); + return new UnknownValue( $value ); } } diff --git a/ValueParsers/includes/parsers/StringValueParser.php b/ValueParsers/includes/parsers/StringValueParser.php index f1bb408..5180a44 100644 --- a/ValueParsers/includes/parsers/StringValueParser.php +++ b/ValueParsers/includes/parsers/StringValueParser.php @@ -62,14 +62,14 @@ * @param mixed $value * * @return Result + * @throws ParseException */ public function parse( $value ) { if ( is_string( $value ) ) { return $this->stringParse( $value ); } - else { - return Result::newErrorText( 'Not a string' ); // TODO - } + + throw new ParseException( 'Not a string' ); } /** @@ -85,6 +85,7 @@ /** * @since 0.1 + * @deprecated * * @param string $errorMessage * diff --git a/ValueParsers/includes/parsers/TitleParser.php b/ValueParsers/includes/parsers/TitleParser.php index 850acb4..5af9ca3 100644 --- a/ValueParsers/includes/parsers/TitleParser.php +++ b/ValueParsers/includes/parsers/TitleParser.php @@ -2,6 +2,8 @@ namespace ValueParsers; +use DataValues\MediaWikiTitleValue; + /** * ValueParser that parses the string representation of a Title object. * @@ -37,17 +39,17 @@ * * @param string $value * - * @return Result + * @return MediaWikiTitleValue + * @throws ParseException */ protected function stringParse( $value ) { $value = \Title::newFromText( $value ); if ( is_null( $value ) ) { - return $this->newErrorResult( 'Not a title' ); + throw new ParseException( 'Not a title' ); } - else { - return Result::newSuccess( new \DataValues\MediaWikiTitleValue( $value ) ); - } + + return new MediaWikiTitleValue( $value ); } } diff --git a/ValueParsers/tests/includes/parsers/BoolParserTest.php b/ValueParsers/tests/includes/parsers/BoolParserTest.php index edab50f..b9d8ce0 100644 --- a/ValueParsers/tests/includes/parsers/BoolParserTest.php +++ b/ValueParsers/tests/includes/parsers/BoolParserTest.php @@ -2,7 +2,7 @@ namespace ValueParsers\Test; -use ValueParsers\Result; +use DataValues\BooleanValue; /** * Unit test BoolParser class. @@ -36,13 +36,13 @@ class BoolParserTest extends StringValueParserTest { /** - * @see ValueParserTestBase::parseProvider + * @see ValueParserTestBase::validInputProvider * * @since 0.1 * * @return array */ - public function parseProvider() { + public function validInputProvider() { $argLists = array(); $valid = array( @@ -62,9 +62,15 @@ ); foreach ( $valid as $value => $expected ) { - $expected = new \DataValues\BooleanValue( $expected ); - $argLists[] = array( (string)$value, Result::newSuccess( $expected ) ); + $expected = new BooleanValue( $expected ); + $argLists[] = array( (string)$value, $expected ); } + + return $argLists; + } + + public function invalidInputProvider() { + $argLists = parent::invalidInputProvider(); $invalid = array( 'foo', @@ -72,10 +78,10 @@ ); foreach ( $invalid as $value ) { - $argLists[] = array( $value, Result::newErrorText( '' ) ); + $argLists[] = array( $value ); } - return array_merge( $argLists, parent::parseProvider() ); + return $argLists; } /** diff --git a/ValueParsers/tests/includes/parsers/FloatParserTest.php b/ValueParsers/tests/includes/parsers/FloatParserTest.php index e5c667c..8bad46e 100644 --- a/ValueParsers/tests/includes/parsers/FloatParserTest.php +++ b/ValueParsers/tests/includes/parsers/FloatParserTest.php @@ -2,7 +2,7 @@ namespace ValueParsers\Test; -use ValueParsers\Result; +use DataValues\NumberValue; /** * Unit test FloatParser class. @@ -37,13 +37,13 @@ class FloatParserTest extends StringValueParserTest { /** - * @see ValueParserTestBase::parseProvider + * @see ValueParserTestBase::validInputProvider * * @since 0.1 * * @return array */ - public function parseProvider() { + public function validInputProvider() { $argLists = array(); $valid = array( @@ -71,9 +71,15 @@ // Because 1 is an int but will come out as a float $expected = (float)$expected; - $expected = new \DataValues\NumberValue( $expected ); - $argLists[] = array( $value, Result::newSuccess( $expected ) ); + $expected = new NumberValue( $expected ); + $argLists[] = array( $value, $expected ); } + + return $argLists; + } + + public function invalidInputProvider() { + $argLists = parent::invalidInputProvider(); $invalid = array( 'foo', @@ -93,10 +99,10 @@ ); foreach ( $invalid as $value ) { - $argLists[] = array( $value, Result::newErrorText( '' ) ); + $argLists[] = array( $value ); } - return array_merge( $argLists, parent::parseProvider() ); + return $argLists; } /** diff --git a/ValueParsers/tests/includes/parsers/GeoCoordinateParserTest.php b/ValueParsers/tests/includes/parsers/GeoCoordinateParserTest.php index 587aeee..62674e0 100644 --- a/ValueParsers/tests/includes/parsers/GeoCoordinateParserTest.php +++ b/ValueParsers/tests/includes/parsers/GeoCoordinateParserTest.php @@ -2,8 +2,8 @@ namespace ValueParsers\Test; +use DataValues\GeoCoordinateValue; use ValueParsers\GeoCoordinateParser; -use ValueParsers\Result; /** * Unit tests for the GeoCoordinateValue class. @@ -38,13 +38,13 @@ class GeoCoordinateParserTest extends StringValueParserTest { /** - * @see ValueParserTestBase::parseProvider + * @see ValueParserTestBase::validInputProvider * * @since 0.1 * * @return array */ - public function parseProvider() { + public function validInputProvider() { $argLists = array(); // TODO: test with different parser options @@ -88,15 +88,26 @@ ); foreach ( $valid as $value => $expected ) { - $expected = new \DataValues\GeoCoordinateValue( $expected[0], $expected[1] ); - $argLists[] = array( (string)$value, Result::newSuccess( $expected ) ); + $expected = new GeoCoordinateValue( $expected[0], $expected[1] ); + $argLists[] = array( (string)$value, $expected ); } - foreach ( array( '~=[,,_,,]:3', 'ohi there' ) as $invalid ) { - $argLists[] = array( $invalid, Result::newErrorText( 'Not a geographical coordinate' ) ); + return $argLists; + } + + public function invalidInputProvider() { + $argLists = parent::invalidInputProvider(); + + $invalid = array( + '~=[,,_,,]:3', + 'ohi there', + ); + + foreach ( $invalid as $value ) { + $argLists[] = array( $value ); } - return array_merge( $argLists, parent::parseProvider() ); + return $argLists; } /** diff --git a/ValueParsers/tests/includes/parsers/IntParserTest.php b/ValueParsers/tests/includes/parsers/IntParserTest.php index fde7dd8..9fbdbaf 100644 --- a/ValueParsers/tests/includes/parsers/IntParserTest.php +++ b/ValueParsers/tests/includes/parsers/IntParserTest.php @@ -2,7 +2,7 @@ namespace ValueParsers\Test; -use ValueParsers\Result; +use DataValues\NumberValue; /** * Unit test IntParser class. @@ -36,13 +36,13 @@ class IntParserTest extends StringValueParserTest { /** - * @see ValueParserTestBase::parseProvider + * @see ValueParserTestBase::validInputProvider * * @since 0.1 * * @return array */ - public function parseProvider() { + public function validInputProvider() { $argLists = array(); $valid = array( @@ -59,9 +59,15 @@ // Because PHP turns them into ints using black magic $value = (string)$value; - $expected = new \DataValues\NumberValue( $expected ); - $argLists[] = array( $value, Result::newSuccess( $expected ) ); + $expected = new NumberValue( $expected ); + $argLists[] = array( $value, $expected ); } + + return $argLists; + } + + public function invalidInputProvider() { + $argLists = parent::invalidInputProvider(); $invalid = array( 'foo', @@ -82,10 +88,10 @@ ); foreach ( $invalid as $value ) { - $argLists[] = array( $value, Result::newErrorText( '' ) ); + $argLists[] = array( $value ); } - return array_merge( $argLists, parent::parseProvider() ); + return $argLists; } /** diff --git a/ValueParsers/tests/includes/parsers/NullParserTest.php b/ValueParsers/tests/includes/parsers/NullParserTest.php index 5ce4ee9..c050dc8 100644 --- a/ValueParsers/tests/includes/parsers/NullParserTest.php +++ b/ValueParsers/tests/includes/parsers/NullParserTest.php @@ -2,7 +2,8 @@ namespace ValueParsers\Test; -use ValueParsers\Result; +use DataValues\UnknownValue; +use ValueParsers\ValueParser; /** * Unit test NullParser class. @@ -36,13 +37,13 @@ class NullParserTest extends ValueParserTestBase { /** - * @see ValueParserTestBase::parseProvider + * @see ValueParserTestBase::validInputProvider * * @since 0.1 * * @return array */ - public function parseProvider() { + public function validInputProvider() { $argLists = array(); $values = array( @@ -58,7 +59,7 @@ foreach ( $values as $value ) { $argLists[] = array( $value, - Result::newSuccess( new \DataValues\UnknownValue( $value ) ) + new UnknownValue( $value ) ); } @@ -66,6 +67,25 @@ } /** + * @since 0.1 + */ + public function invalidInputProvider() { + return array( array( + 'This sucks; this parser has no invalid inputs, so this test should be skipped.' . + 'Not clear how to do that in a way one does not get a "incomplete test" notice though' + ) ); + } + + /** + * @dataProvider invalidInputProvider + * @param $value + * @param ValueParser $parser + */ + public function testParseWithInvalidInputs( $value, ValueParser $parser = null ) { + $this->assertTrue( true ); + } + + /** * @see ValueParserTestBase::getParserClass * @since 0.1 * @return string diff --git a/ValueParsers/tests/includes/parsers/StringValueParserTest.php b/ValueParsers/tests/includes/parsers/StringValueParserTest.php index 0ad4a03..e837a9d 100644 --- a/ValueParsers/tests/includes/parsers/StringValueParserTest.php +++ b/ValueParsers/tests/includes/parsers/StringValueParserTest.php @@ -2,7 +2,6 @@ namespace ValueParsers\Test; -use ValueParsers\Result; use ValueParsers\StringValueParser; /** @@ -36,14 +35,7 @@ */ abstract class StringValueParserTest extends ValueParserTestBase { - /** - * @see ValueParserTestBase::parseProvider - * - * @since 0.1 - * - * @return array - */ - public function parseProvider() { + public function invalidInputProvider() { $argLists = array(); $invalid = array( @@ -56,7 +48,7 @@ ); foreach ( $invalid as $value ) { - $argLists[] = array( $value, Result::newErrorText( '' ) ); + $argLists[] = array( $value ); } return $argLists; diff --git a/ValueParsers/tests/includes/parsers/TitleParserTest.php b/ValueParsers/tests/includes/parsers/TitleParserTest.php index 6f5baec..997afdb 100644 --- a/ValueParsers/tests/includes/parsers/TitleParserTest.php +++ b/ValueParsers/tests/includes/parsers/TitleParserTest.php @@ -2,7 +2,7 @@ namespace ValueParsers\Test; -use ValueParsers\Result; +use DataValues\MediaWikiTitleValue; /** * Unit test TitleParser class. @@ -36,13 +36,13 @@ class TitleParserTest extends StringValueParserTest { /** - * @see ValueParserTestBase::parseProvider + * @see ValueParserTestBase::validInputProvider * * @since 0.1 * * @return array */ - public function parseProvider() { + public function validInputProvider() { $argLists = array(); $valid = array( @@ -51,10 +51,10 @@ ); foreach ( $valid as $value ) { - $argLists[] = array( $value, Result::newSuccess( new \DataValues\MediaWikiTitleValue( \Title::newFromText( $value ) ) ) ); + $argLists[] = array( $value, new MediaWikiTitleValue( \Title::newFromText( $value ) ) ); } - return array_merge( $argLists, parent::parseProvider() ); + return $argLists; } /** diff --git a/ValueParsers/tests/includes/parsers/ValueParserTest.php b/ValueParsers/tests/includes/parsers/ValueParserTest.php deleted file mode 100644 index b58015e..0000000 --- a/ValueParsers/tests/includes/parsers/ValueParserTest.php +++ /dev/null @@ -1,71 +0,0 @@ -<?php - -namespace ValueParsers\Test; - -use ValueParsers\ValueParser; - -/** - * Unit test for the implementation of the ValueParser interface. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * http://www.gnu.org/copyleft/gpl.html - * - * @file - * @since 0.1 - * - * @ingroup ValueParsersTest - * - * @group ValueParsers - * @group DataValueExtensions - * - * @licence GNU GPL v2+ - * @author Jeroen De Dauw < jeroended...@gmail.com > - */ -class ValueParserTest extends \MediaWikiTestCase { - -// protected function getImplementingClasses( $interfaceName ) { -// foreach ( array_keys( $GLOBALS['wgAutoloadClasses'] ) as $className ) { -// class_exists( $className, true ); -// } -// -// return array_filter( -// get_declared_classes(), -// function( $className ) use ( $interfaceName ) { -// return in_array( $interfaceName, class_implements( $className, true ) ); -// } -// ); -// } - - public function instanceProvider() { - return array_map( - function( $className ) { - return array( new $className() ); - }, - array( - 'ValueParsers\NullParser' - ) - ); - } - - /** - * @dataProvider instanceProvider - */ - public function testParser( ValueParser $parser ) { - foreach ( array( 'foo', 42, array(), false, 'ohi there!' ) as $value ) { - $this->assertInstanceOf( 'ValueParsers\Result', $parser->parse( $value ) ); - } - } - -} diff --git a/ValueParsers/tests/includes/parsers/ValueParserTestBase.php b/ValueParsers/tests/includes/parsers/ValueParserTestBase.php index 6cc8300..f7f5fbe 100644 --- a/ValueParsers/tests/includes/parsers/ValueParserTestBase.php +++ b/ValueParsers/tests/includes/parsers/ValueParserTestBase.php @@ -2,7 +2,7 @@ namespace ValueParsers\Test; -use ValueParsers\Result; +use ValueParsers\ParserOptions; use ValueParsers\ValueParser; /** @@ -38,14 +38,21 @@ /** * @since 0.1 - */ - public abstract function parseProvider(); - - /** - * @since 0.1 * @return string */ protected abstract function getParserClass(); + + /** + * @since 0.1 + */ + public abstract function validInputProvider(); + + /** + * @since 0.1 + */ + public function invalidInputProvider() { + return array(); + } /** * @since 0.1 @@ -57,36 +64,43 @@ } /** - * @dataProvider parseProvider + * @dataProvider validInputProvider * @since 0.1 * @param $value - * @param Result $expected + * @param mixed $expected * @param ValueParser|null $parser */ - public function testParse( $value, Result $expected, ValueParser $parser = null ) { + public function testParseWithValidInputs( $value, $expected, ValueParser $parser = null ) { if ( is_null( $parser ) ) { $parser = $this->getInstance(); } - $result = $parser->parse( $value ); + $this->assertSmartEquals( $expected, $parser->parse( $value ) ); + } - $this->assertEquals( $expected->isValid(), $result->isValid(), 'Validity of the value is not valid. Error: ' . var_export( $result->getError(), true ) ); - - if ( $expected->isValid() ) { - if ( $this->requireDataValue() || $result->getValue() instanceof \Comparable ) { - $this->assertTrue( $expected->getValue()->equals( $result->getValue() ) ); - } - else { - $this->assertEquals( $expected->getValue(), $result->getValue() ); - } - - $this->assertNull( $result->getError() ); + private function assertSmartEquals( $expected, $actual ) { + if ( $this->requireDataValue() || $expected instanceof \Comparable ) { + $this->assertTrue( $expected->equals( $actual ) ); } else { - $this->assertTypeOrValue( '\ValueParsers\Error', $result->getError(), null ); - - $this->assertException( function() use ( $result ) { $result->getValue(); } ); + $this->assertEquals( $expected, $actual ); } + } + + /** + * @dataProvider invalidInputProvider + * @since 0.1 + * @param $value + * @param ValueParser|null $parser + */ + public function testParseWithInvalidInputs( $value, ValueParser $parser = null ) { + if ( is_null( $parser ) ) { + $parser = $this->getInstance(); + } + + $this->setExpectedException( 'ValueParsers\ParseException' ); + + $parser->parse( $value ); } /** @@ -105,10 +119,10 @@ * * @since 0.1 * - * @return \ValueParsers\ParserOptions + * @return ParserOptions */ protected function newParserOptions() { - return new \ValueParsers\ParserOptions(); + return new ParserOptions(); } } -- To view, visit https://gerrit.wikimedia.org/r/58867 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I9d27241701e1387246a3fb56c5d590e5b0f27919 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/DataValues Gerrit-Branch: master Gerrit-Owner: Jeroen De Dauw <jeroended...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits