InlineTest.php 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. $required_locales = array('fr_FR.UTF-8', 'fr_FR.UTF8', 'fr_FR.utf-8', 'fr_FR.utf8', 'French_France.1252');
  41. if (false === setlocale(LC_ALL, $required_locales)) {
  42. $this->markTestSkipped('Could not set any of required locales: ' . implode(", ", $required_locales));
  43. }
  44. $this->assertEquals('1.2', Inline::dump(1.2));
  45. $this->assertContains('fr', strtolower(setlocale(LC_NUMERIC, 0)));
  46. setlocale(LC_ALL, $locale);
  47. }
  48. public function testHashStringsResemblingExponentialNumericsShouldNotBeChangedToINF()
  49. {
  50. $value = '686e444';
  51. $this->assertSame($value, Inline::parse(Inline::dump($value)));
  52. }
  53. protected function getTestsForParse()
  54. {
  55. return array(
  56. '' => '',
  57. 'null' => null,
  58. 'false' => false,
  59. 'true' => true,
  60. '12' => 12,
  61. '"quoted string"' => 'quoted string',
  62. "'quoted string'" => 'quoted string',
  63. '12.30e+02' => 12.30e+02,
  64. '0x4D2' => 0x4D2,
  65. '02333' => 02333,
  66. '.Inf' => -log(0),
  67. '-.Inf' => log(0),
  68. "'686e444'" => '686e444',
  69. '686e444' => 646e444,
  70. '123456789123456789' => '123456789123456789',
  71. '"foo\r\nbar"' => "foo\r\nbar",
  72. "'foo#bar'" => 'foo#bar',
  73. "'foo # bar'" => 'foo # bar',
  74. "'#cfcfcf'" => '#cfcfcf',
  75. '2007-10-30' => mktime(0, 0, 0, 10, 30, 2007),
  76. '2007-10-30T02:59:43Z' => gmmktime(2, 59, 43, 10, 30, 2007),
  77. '2007-10-30 02:59:43 Z' => gmmktime(2, 59, 43, 10, 30, 2007),
  78. '"a \\"string\\" with \'quoted strings inside\'"' => 'a "string" with \'quoted strings inside\'',
  79. "'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'',
  80. // sequences
  81. // urls are no key value mapping. see #3609. Valid yaml "key: value" mappings require a space after the colon
  82. '[foo, http://urls.are/no/mappings, false, null, 12]' => array('foo', 'http://urls.are/no/mappings', false, null, 12),
  83. '[ foo , bar , false , null , 12 ]' => array('foo', 'bar', false, null, 12),
  84. '[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'),
  85. // mappings
  86. '{foo:bar,bar:foo,false:false,null:null,integer:12}' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
  87. '{ foo : bar, bar : foo, false : false, null : null, integer : 12 }' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
  88. '{foo: \'bar\', bar: \'foo: bar\'}' => array('foo' => 'bar', 'bar' => 'foo: bar'),
  89. '{\'foo\': \'bar\', "bar": \'foo: bar\'}' => array('foo' => 'bar', 'bar' => 'foo: bar'),
  90. '{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}' => array('foo\'' => 'bar', "bar\"" => 'foo: bar'),
  91. '{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}' => array('foo: ' => 'bar', "bar: " => 'foo: bar'),
  92. // nested sequences and mappings
  93. '[foo, [bar, foo]]' => array('foo', array('bar', 'foo')),
  94. '[foo, {bar: foo}]' => array('foo', array('bar' => 'foo')),
  95. '{ foo: {bar: foo} }' => array('foo' => array('bar' => 'foo')),
  96. '{ foo: [bar, foo] }' => array('foo' => array('bar', 'foo')),
  97. '[ foo, [ bar, foo ] ]' => array('foo', array('bar', 'foo')),
  98. '[{ foo: {bar: foo} }]' => array(array('foo' => array('bar' => 'foo'))),
  99. '[foo, [bar, [foo, [bar, foo]], foo]]' => array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo')),
  100. '[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'))),
  101. '[foo, bar: { foo: bar }]' => array('foo', '1' => array('bar' => array('foo' => 'bar'))),
  102. );
  103. }
  104. protected function getTestsForDump()
  105. {
  106. return array(
  107. 'null' => null,
  108. 'false' => false,
  109. 'true' => true,
  110. '12' => 12,
  111. "'quoted string'" => 'quoted string',
  112. '12.30e+02' => 12.30e+02,
  113. '1234' => 0x4D2,
  114. '1243' => 02333,
  115. '.Inf' => -log(0),
  116. '-.Inf' => log(0),
  117. "'686e444'" => '686e444',
  118. '.Inf' => 646e444,
  119. '"foo\r\nbar"' => "foo\r\nbar",
  120. "'foo#bar'" => 'foo#bar',
  121. "'foo # bar'" => 'foo # bar',
  122. "'#cfcfcf'" => '#cfcfcf',
  123. "'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'',
  124. // sequences
  125. '[foo, bar, false, null, 12]' => array('foo', 'bar', false, null, 12),
  126. '[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'),
  127. // mappings
  128. '{ foo: bar, bar: foo, \'false\': false, \'null\': null, integer: 12 }' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
  129. '{ foo: bar, bar: \'foo: bar\' }' => array('foo' => 'bar', 'bar' => 'foo: bar'),
  130. // nested sequences and mappings
  131. '[foo, [bar, foo]]' => array('foo', array('bar', 'foo')),
  132. '[foo, [bar, [foo, [bar, foo]], foo]]' => array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo')),
  133. '{ foo: { bar: foo } }' => array('foo' => array('bar' => 'foo')),
  134. '[foo, { bar: foo }]' => array('foo', array('bar' => 'foo')),
  135. '[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'))),
  136. );
  137. }
  138. }