From 3f3623df271ba9da658cb3578d79f55316c9884b Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sat, 14 May 2016 18:19:31 +0200 Subject: [PATCH] [Yaml] deprecate comma separators in floats --- src/Symfony/Component/Yaml/Inline.php | 1 + .../Fixtures/YtsSpecificationExamples.yml | 45 +++++++++++++++---- .../Yaml/Tests/Fixtures/YtsTypeTransfers.yml | 44 ++++++++++++++++-- .../Component/Yaml/Tests/ParserTest.php | 25 ++++++++++- 4 files changed, 101 insertions(+), 14 deletions(-) diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index 43da4fd328408..44eb79326185f 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -591,6 +591,7 @@ private static function evaluateScalar($scalar, $flags, $references = array()) case 0 === strpos($scalar, '!!binary '): return self::evaluateBinaryScalar(substr($scalar, 9)); case preg_match('/^(-|\+)?[0-9][0-9,]*(\.[0-9_]+)?$/', $scalar): + @trigger_error('Using the comma as a group separator for floats is deprecated since version 3.2 and will be removed in 4.0.', E_USER_DEPRECATED); case preg_match('/^(-|\+)?[0-9][0-9_]*(\.[0-9_]+)?$/', $scalar): return (float) str_replace(array(',', '_'), '', $scalar); case preg_match(self::getTimestampRegex(), $scalar): diff --git a/src/Symfony/Component/Yaml/Tests/Fixtures/YtsSpecificationExamples.yml b/src/Symfony/Component/Yaml/Tests/Fixtures/YtsSpecificationExamples.yml index ec1c4c3a1ab40..268c02efeb2e8 100644 --- a/src/Symfony/Component/Yaml/Tests/Fixtures/YtsSpecificationExamples.yml +++ b/src/Symfony/Component/Yaml/Tests/Fixtures/YtsSpecificationExamples.yml @@ -509,24 +509,31 @@ test: Integers spec: 2.19 yaml: | canonical: 12345 - decimal: +12,345 octal: 014 hexadecimal: 0xC php: | array( 'canonical' => 12345, - 'decimal' => 12345.0, 'octal' => 014, 'hexadecimal' => 0xC ) --- +test: Decimal Integer +deprecated: true +spec: 2.19 +yaml: | + decimal: +12,345 +php: | + array( + 'decimal' => 12345.0, + ) +--- # FIX: spec shows parens around -inf and NaN test: Floating point spec: 2.20 yaml: | canonical: 1.23015e+3 exponential: 12.3015e+02 - fixed: 1,230.15 negative infinity: -.inf not a number: .NaN float as whole number: !!float 1 @@ -534,12 +541,21 @@ php: | array( 'canonical' => 1230.15, 'exponential' => 1230.15, - 'fixed' => 1230.15, 'negative infinity' => log(0), 'not a number' => -log(0), 'float as whole number' => (float) 1 ) --- +test: Fixed Floating point +deprecated: true +spec: 2.20 +yaml: | + fixed: 1,230.15 +php: | + array( + 'fixed' => 1230.15, + ) +--- test: Miscellaneous spec: 2.21 yaml: | @@ -1532,29 +1548,42 @@ php: | test: Integer yaml: | canonical: 12345 - decimal: +12,345 octal: 014 hexadecimal: 0xC php: | array( 'canonical' => 12345, - 'decimal' => 12345.0, 'octal' => 12, 'hexadecimal' => 12 ) --- +test: Decimal +deprecated: true +yaml: | + decimal: +12,345 +php: | + array( + 'decimal' => 12345.0, + ) +--- +test: Fixed Float +deprecated: true +yaml: | + fixed: 1,230.15 +php: | + array( + 'fixed' => 1230.15, + ) test: Float yaml: | canonical: 1.23015e+3 exponential: 12.3015e+02 - fixed: 1,230.15 negative infinity: -.inf not a number: .NaN php: | array( 'canonical' => 1230.15, 'exponential' => 1230.15, - 'fixed' => 1230.15, 'negative infinity' => log(0), 'not a number' => -log(0) ) diff --git a/src/Symfony/Component/Yaml/Tests/Fixtures/YtsTypeTransfers.yml b/src/Symfony/Component/Yaml/Tests/Fixtures/YtsTypeTransfers.yml index 46c8d4a2c0f0b..8fefa494cfb20 100644 --- a/src/Symfony/Component/Yaml/Tests/Fixtures/YtsTypeTransfers.yml +++ b/src/Symfony/Component/Yaml/Tests/Fixtures/YtsTypeTransfers.yml @@ -176,13 +176,37 @@ brief: > yaml: | zero: 0 simple: 12 - one-thousand: 1,000 - negative one-thousand: -1,000 php: | array( 'zero' => 0, 'simple' => 12, + ) +--- +test: Positive Big Integer +deprecated: true +dump_skip: true +brief: > + An integer is a series of numbers, optionally + starting with a positive or negative sign. Integers + may also contain commas for readability. +yaml: | + one-thousand: 1,000 +php: | + array( 'one-thousand' => 1000.0, + ) +--- +test: Negative Big Integer +deprecated: true +dump_skip: true +brief: > + An integer is a series of numbers, optionally + starting with a positive or negative sign. Integers + may also contain commas for readability. +yaml: | + negative one-thousand: -1,000 +php: | + array( 'negative one-thousand' => -1000.0 ) --- @@ -208,15 +232,27 @@ brief: > positive and negative infinity and "not a number." yaml: | a simple float: 2.00 - larger float: 1,000.09 scientific notation: 1.00009e+3 php: | array( 'a simple float' => 2.0, - 'larger float' => 1000.09, 'scientific notation' => 1000.09 ) --- +test: Larger Float +dump_skip: true +deprecated: true +brief: > + Floats are represented by numbers with decimals, + allowing for scientific notation, as well as + positive and negative infinity and "not a number." +yaml: | + larger float: 1,000.09 +php: | + array( + 'larger float' => 1000.09, + ) +--- test: Time todo: true brief: > diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php index bddf969744eeb..69a1bab2f3c47 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -31,9 +31,30 @@ protected function tearDown() /** * @dataProvider getDataFormSpecifications */ - public function testSpecifications($file, $expected, $yaml, $comment) + public function testSpecifications($file, $expected, $yaml, $comment, $deprecated) { + $deprecations = array(); + + if ($deprecated) { + set_error_handler(function ($type, $msg) use (&$deprecations) { + if (E_USER_DEPRECATED !== $type) { + restore_error_handler(); + + return call_user_func_array('PHPUnit_Util_ErrorHandler::handleError', func_get_args()); + } + + $deprecations[] = $msg; + }); + } + $this->assertEquals($expected, var_export($this->parser->parse($yaml), true), $comment); + + if ($deprecated) { + restore_error_handler(); + + $this->assertCount(1, $deprecations); + $this->assertContains('Using the comma as a group separator for floats is deprecated since version 3.2 and will be removed in 4.0.', $deprecations[0]); + } } public function getDataFormSpecifications() @@ -58,7 +79,7 @@ public function getDataFormSpecifications() } else { eval('$expected = '.trim($test['php']).';'); - $tests[] = array($file, var_export($expected, true), $test['yaml'], $test['test']); + $tests[] = array($file, var_export($expected, true), $test['yaml'], $test['test'], isset($test['deprecated']) ? $test['deprecated'] : false); } } }