Skip to content

Commit

Permalink
feature #18785 [Yaml] deprecate comma separators in floats (xabbuh)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.2-dev branch.

Discussion
----------

[Yaml] deprecate comma separators in floats

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | #18178
| License       | MIT
| Doc PR        |

Commits
-------

3f3623d [Yaml] deprecate comma separators in floats
  • Loading branch information
fabpot committed May 24, 2016
2 parents 4c5464c + 3f3623d commit b0a483e
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Component/Yaml/Inline.php
Expand Up @@ -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):
Expand Down
Expand Up @@ -509,37 +509,53 @@ 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
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: |
Expand Down Expand Up @@ -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)
)
Expand Down
44 changes: 40 additions & 4 deletions src/Symfony/Component/Yaml/Tests/Fixtures/YtsTypeTransfers.yml
Expand Up @@ -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
)
---
Expand All @@ -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: >
Expand Down
25 changes: 23 additions & 2 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Expand Up @@ -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()
Expand All @@ -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);
}
}
}
Expand Down

0 comments on commit b0a483e

Please sign in to comment.