This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/tests/Symfony/Tests/Component/Yaml/InlineTest.php
Fabien Potencier 2a0178c2b5 merged branch jmikola/yaml-numeric-strings (PR #1688)
Commits
-------

05cc24c [Yaml] Wrap numeric strings in quotes when dumping

Discussion
----------

[Yaml] Wrap numeric strings in quotes when dumping

This addresses an obscure case where a hash string (actually a commit-ish, "686e444") was dumped to YAML as an unquoted string value. It was later parsed from YAML as an exponential numeric and changed to ".Inf".

This commit should not change the existing behavior when dumping non-string numerics. It also doesn't appear to disturb any of the other test cases. I realize it's a huge edge case, so I'm open to discussion.

The alternative to this fix was an ugly `preg_replace()` to apply quoting around the commit-ish after dumping. I would look forward to removing that :)
2011-07-15 08:39:27 +02:00

155 lines
6.5 KiB
PHP

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Component\Yaml;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Inline;
class InlineTest extends \PHPUnit_Framework_TestCase
{
public function testParse()
{
foreach ($this->getTestsForParse() as $yaml => $value) {
$this->assertEquals($value, Inline::parse($yaml), sprintf('::parse() converts an inline YAML to a PHP structure (%s)', $yaml));
}
}
public function testDump()
{
$testsForDump = $this->getTestsForDump();
foreach ($testsForDump as $yaml => $value) {
$this->assertEquals($yaml, Inline::dump($value), sprintf('::dump() converts a PHP structure to an inline YAML (%s)', $yaml));
}
foreach ($this->getTestsForParse() as $yaml => $value) {
$this->assertEquals($value, Inline::parse(Inline::dump($value)), 'check consistency');
}
foreach ($testsForDump as $yaml => $value) {
$this->assertEquals($value, Inline::parse(Inline::dump($value)), 'check consistency');
}
}
public function testHashStringsResemblingExponentialNumericsShouldNotBeChangedToINF()
{
$value = '686e444';
$this->assertSame($value, Inline::parse(Inline::dump($value)));
}
protected function getTestsForParse()
{
return array(
'' => '',
'null' => null,
'false' => false,
'true' => true,
'12' => 12,
'"quoted string"' => 'quoted string',
"'quoted string'" => 'quoted string',
'12.30e+02' => 12.30e+02,
'0x4D2' => 0x4D2,
'02333' => 02333,
'.Inf' => -log(0),
'-.Inf' => log(0),
"'686e444'" => '686e444',
'686e444' => 646e444,
'123456789123456789' => '123456789123456789',
'"foo\r\nbar"' => "foo\r\nbar",
"'foo#bar'" => 'foo#bar',
"'foo # bar'" => 'foo # bar',
"'#cfcfcf'" => '#cfcfcf',
'2007-10-30' => mktime(0, 0, 0, 10, 30, 2007),
'2007-10-30T02:59:43Z' => gmmktime(2, 59, 43, 10, 30, 2007),
'2007-10-30 02:59:43 Z' => gmmktime(2, 59, 43, 10, 30, 2007),
'"a \\"string\\" with \'quoted strings inside\'"' => 'a "string" with \'quoted strings inside\'',
"'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'',
// sequences
// urls are no key value mapping. see #3609. Valid yaml "key: value" mappings require a space after the colon
'[foo, http://urls.are/no/mappings, false, null, 12]' => array('foo', 'http://urls.are/no/mappings', false, null, 12),
'[ foo , bar , false , null , 12 ]' => array('foo', 'bar', false, null, 12),
'[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'),
// mappings
'{foo:bar,bar:foo,false:false,null:null,integer:12}' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
'{ foo : bar, bar : foo, false : false, null : null, integer : 12 }' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
'{foo: \'bar\', bar: \'foo: bar\'}' => array('foo' => 'bar', 'bar' => 'foo: bar'),
'{\'foo\': \'bar\', "bar": \'foo: bar\'}' => array('foo' => 'bar', 'bar' => 'foo: bar'),
'{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}' => array('foo\'' => 'bar', "bar\"" => 'foo: bar'),
'{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}' => array('foo: ' => 'bar', "bar: " => 'foo: bar'),
// nested sequences and mappings
'[foo, [bar, foo]]' => array('foo', array('bar', 'foo')),
'[foo, {bar: foo}]' => array('foo', array('bar' => 'foo')),
'{ foo: {bar: foo} }' => array('foo' => array('bar' => 'foo')),
'{ foo: [bar, foo] }' => array('foo' => array('bar', 'foo')),
'[ foo, [ bar, foo ] ]' => array('foo', array('bar', 'foo')),
'[{ foo: {bar: foo} }]' => array(array('foo' => array('bar' => 'foo'))),
'[foo, [bar, [foo, [bar, foo]], foo]]' => array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo')),
'[foo, {bar: foo, foo: [foo, {bar: foo}]}, [foo, {bar: foo}]]' => array('foo', array('bar' => 'foo', 'foo' => array('foo', array('bar' => 'foo'))), array('foo', array('bar' => 'foo'))),
'[foo, bar: { foo: bar }]' => array('foo', '1' => array('bar' => array('foo' => 'bar'))),
);
}
protected function getTestsForDump()
{
return array(
'null' => null,
'false' => false,
'true' => true,
'12' => 12,
"'quoted string'" => 'quoted string',
'12.30e+02' => 12.30e+02,
'1234' => 0x4D2,
'1243' => 02333,
'.Inf' => -log(0),
'-.Inf' => log(0),
"'686e444'" => '686e444',
'.Inf' => 646e444,
'"foo\r\nbar"' => "foo\r\nbar",
"'foo#bar'" => 'foo#bar',
"'foo # bar'" => 'foo # bar',
"'#cfcfcf'" => '#cfcfcf',
"'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'',
// sequences
'[foo, bar, false, null, 12]' => array('foo', 'bar', false, null, 12),
'[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'),
// mappings
'{ foo: bar, bar: foo, \'false\': false, \'null\': null, integer: 12 }' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
'{ foo: bar, bar: \'foo: bar\' }' => array('foo' => 'bar', 'bar' => 'foo: bar'),
// nested sequences and mappings
'[foo, [bar, foo]]' => array('foo', array('bar', 'foo')),
'[foo, [bar, [foo, [bar, foo]], foo]]' => array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo')),
'{ foo: { bar: foo } }' => array('foo' => array('bar' => 'foo')),
'[foo, { bar: foo }]' => array('foo', array('bar' => 'foo')),
'[foo, { bar: foo, foo: [foo, { bar: foo }] }, [foo, { bar: foo }]]' => array('foo', array('bar' => 'foo', 'foo' => array('foo', array('bar' => 'foo'))), array('foo', array('bar' => 'foo'))),
);
}
}