InlineTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\Yaml;
  11. use Symfony\Component\Yaml\Yaml;
  12. use Symfony\Component\Yaml\Inline;
  13. class InlineTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function testParse()
  16. {
  17. foreach ($this->getTestsForParse() as $yaml => $value) {
  18. $this->assertEquals($value, Inline::parse($yaml), sprintf('::parse() converts an inline YAML to a PHP structure (%s)', $yaml));
  19. }
  20. }
  21. public function testDump()
  22. {
  23. $testsForDump = $this->getTestsForDump();
  24. foreach ($testsForDump as $yaml => $value) {
  25. $this->assertEquals($yaml, Inline::dump($value), sprintf('::dump() converts a PHP structure to an inline YAML (%s)', $yaml));
  26. }
  27. foreach ($this->getTestsForParse() as $yaml => $value) {
  28. $this->assertEquals($value, Inline::parse(Inline::dump($value)), 'check consistency');
  29. }
  30. foreach ($testsForDump as $yaml => $value) {
  31. $this->assertEquals($value, Inline::parse(Inline::dump($value)), 'check consistency');
  32. }
  33. }
  34. public function testDumpNumericValueWithLocale()
  35. {
  36. $locale = setlocale(LC_NUMERIC, 0);
  37. if (false === $locale) {
  38. $this->markTestSkipped('Your platform does not support locales.');
  39. }
  40. setlocale(LC_ALL, 'fr_FR.UTF-8', 'fr_FR.UTF8', 'fr_FR.utf-8', 'fr_FR.utf8', 'French_France.1252');
  41. $this->assertEquals('1.2', Inline::dump(1.2));
  42. $this->assertContains('fr', strtolower(setlocale(LC_NUMERIC, 0)));
  43. setlocale(LC_ALL, $locale);
  44. }
  45. public function testHashStringsResemblingExponentialNumericsShouldNotBeChangedToINF()
  46. {
  47. $value = '686e444';
  48. $this->assertSame($value, Inline::parse(Inline::dump($value)));
  49. }
  50. protected function getTestsForParse()
  51. {
  52. return array(
  53. '' => '',
  54. 'null' => null,
  55. 'false' => false,
  56. 'true' => true,
  57. '12' => 12,
  58. '"quoted string"' => 'quoted string',
  59. "'quoted string'" => 'quoted string',
  60. '12.30e+02' => 12.30e+02,
  61. '0x4D2' => 0x4D2,
  62. '02333' => 02333,
  63. '.Inf' => -log(0),
  64. '-.Inf' => log(0),
  65. "'686e444'" => '686e444',
  66. '686e444' => 646e444,
  67. '123456789123456789' => '123456789123456789',
  68. '"foo\r\nbar"' => "foo\r\nbar",
  69. "'foo#bar'" => 'foo#bar',
  70. "'foo # bar'" => 'foo # bar',
  71. "'#cfcfcf'" => '#cfcfcf',
  72. '2007-10-30' => mktime(0, 0, 0, 10, 30, 2007),
  73. '2007-10-30T02:59:43Z' => gmmktime(2, 59, 43, 10, 30, 2007),
  74. '2007-10-30 02:59:43 Z' => gmmktime(2, 59, 43, 10, 30, 2007),
  75. '"a \\"string\\" with \'quoted strings inside\'"' => 'a "string" with \'quoted strings inside\'',
  76. "'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'',
  77. // sequences
  78. // urls are no key value mapping. see #3609. Valid yaml "key: value" mappings require a space after the colon
  79. '[foo, http://urls.are/no/mappings, false, null, 12]' => array('foo', 'http://urls.are/no/mappings', false, null, 12),
  80. '[ foo , bar , false , null , 12 ]' => array('foo', 'bar', false, null, 12),
  81. '[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'),
  82. // mappings
  83. '{foo:bar,bar:foo,false:false,null:null,integer:12}' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
  84. '{ foo : bar, bar : foo, false : false, null : null, integer : 12 }' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
  85. '{foo: \'bar\', bar: \'foo: bar\'}' => array('foo' => 'bar', 'bar' => 'foo: bar'),
  86. '{\'foo\': \'bar\', "bar": \'foo: bar\'}' => array('foo' => 'bar', 'bar' => 'foo: bar'),
  87. '{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}' => array('foo\'' => 'bar', "bar\"" => 'foo: bar'),
  88. '{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}' => array('foo: ' => 'bar', "bar: " => 'foo: bar'),
  89. // nested sequences and mappings
  90. '[foo, [bar, foo]]' => array('foo', array('bar', 'foo')),
  91. '[foo, {bar: foo}]' => array('foo', array('bar' => 'foo')),
  92. '{ foo: {bar: foo} }' => array('foo' => array('bar' => 'foo')),
  93. '{ foo: [bar, foo] }' => array('foo' => array('bar', 'foo')),
  94. '[ foo, [ bar, foo ] ]' => array('foo', array('bar', 'foo')),
  95. '[{ foo: {bar: foo} }]' => array(array('foo' => array('bar' => 'foo'))),
  96. '[foo, [bar, [foo, [bar, foo]], foo]]' => array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo')),
  97. '[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'))),
  98. '[foo, bar: { foo: bar }]' => array('foo', '1' => array('bar' => array('foo' => 'bar'))),
  99. );
  100. }
  101. protected function getTestsForDump()
  102. {
  103. return array(
  104. 'null' => null,
  105. 'false' => false,
  106. 'true' => true,
  107. '12' => 12,
  108. "'quoted string'" => 'quoted string',
  109. '12.30e+02' => 12.30e+02,
  110. '1234' => 0x4D2,
  111. '1243' => 02333,
  112. '.Inf' => -log(0),
  113. '-.Inf' => log(0),
  114. "'686e444'" => '686e444',
  115. '.Inf' => 646e444,
  116. '"foo\r\nbar"' => "foo\r\nbar",
  117. "'foo#bar'" => 'foo#bar',
  118. "'foo # bar'" => 'foo # bar',
  119. "'#cfcfcf'" => '#cfcfcf',
  120. "'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'',
  121. // sequences
  122. '[foo, bar, false, null, 12]' => array('foo', 'bar', false, null, 12),
  123. '[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'),
  124. // mappings
  125. '{ foo: bar, bar: foo, \'false\': false, \'null\': null, integer: 12 }' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
  126. '{ foo: bar, bar: \'foo: bar\' }' => array('foo' => 'bar', 'bar' => 'foo: bar'),
  127. // nested sequences and mappings
  128. '[foo, [bar, foo]]' => array('foo', array('bar', 'foo')),
  129. '[foo, [bar, [foo, [bar, foo]], foo]]' => array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo')),
  130. '{ foo: { bar: foo } }' => array('foo' => array('bar' => 'foo')),
  131. '[foo, { bar: foo }]' => array('foo', array('bar' => 'foo')),
  132. '[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'))),
  133. );
  134. }
  135. }