InlineTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. if ($value == 1230) {
  29. continue;
  30. }
  31. $this->assertEquals($value, Inline::parse(Inline::dump($value)), 'check consistency');
  32. }
  33. foreach ($testsForDump as $yaml => $value) {
  34. if ($value == 1230) {
  35. continue;
  36. }
  37. $this->assertEquals($value, Inline::parse(Inline::dump($value)), 'check consistency');
  38. }
  39. }
  40. public function testHashStringsResemblingExponentialNumericsShouldNotBeChangedToINF()
  41. {
  42. $value = '686e444';
  43. $this->assertSame($value, Inline::parse(Inline::dump($value)));
  44. }
  45. protected function getTestsForParse()
  46. {
  47. return array(
  48. '' => '',
  49. 'null' => null,
  50. 'false' => false,
  51. 'true' => true,
  52. '12' => 12,
  53. '"quoted string"' => 'quoted string',
  54. "'quoted string'" => 'quoted string',
  55. '12.30e+02' => 12.30e+02,
  56. '0x4D2' => 0x4D2,
  57. '02333' => 02333,
  58. '.Inf' => -log(0),
  59. '-.Inf' => log(0),
  60. "'686e444'" => '686e444',
  61. '686e444' => 646e444,
  62. '123456789123456789' => '123456789123456789',
  63. '"foo\r\nbar"' => "foo\r\nbar",
  64. "'foo#bar'" => 'foo#bar',
  65. "'foo # bar'" => 'foo # bar',
  66. "'#cfcfcf'" => '#cfcfcf',
  67. '2007-10-30' => mktime(0, 0, 0, 10, 30, 2007),
  68. '2007-10-30T02:59:43Z' => gmmktime(2, 59, 43, 10, 30, 2007),
  69. '2007-10-30 02:59:43 Z' => gmmktime(2, 59, 43, 10, 30, 2007),
  70. '"a \\"string\\" with \'quoted strings inside\'"' => 'a "string" with \'quoted strings inside\'',
  71. "'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'',
  72. // sequences
  73. // urls are no key value mapping. see #3609. Valid yaml "key: value" mappings require a space after the colon
  74. '[foo, http://urls.are/no/mappings, false, null, 12]' => array('foo', 'http://urls.are/no/mappings', false, null, 12),
  75. '[ foo , bar , false , null , 12 ]' => array('foo', 'bar', false, null, 12),
  76. '[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'),
  77. // mappings
  78. '{foo:bar,bar:foo,false:false,null:null,integer:12}' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
  79. '{ foo : bar, bar : foo, false : false, null : null, integer : 12 }' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
  80. '{foo: \'bar\', bar: \'foo: bar\'}' => array('foo' => 'bar', 'bar' => 'foo: bar'),
  81. '{\'foo\': \'bar\', "bar": \'foo: bar\'}' => array('foo' => 'bar', 'bar' => 'foo: bar'),
  82. '{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}' => array('foo\'' => 'bar', "bar\"" => 'foo: bar'),
  83. '{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}' => array('foo: ' => 'bar', "bar: " => 'foo: bar'),
  84. // nested sequences and mappings
  85. '[foo, [bar, foo]]' => array('foo', array('bar', 'foo')),
  86. '[foo, {bar: foo}]' => array('foo', array('bar' => 'foo')),
  87. '{ foo: {bar: foo} }' => array('foo' => array('bar' => 'foo')),
  88. '{ foo: [bar, foo] }' => array('foo' => array('bar', 'foo')),
  89. '[ foo, [ bar, foo ] ]' => array('foo', array('bar', 'foo')),
  90. '[{ foo: {bar: foo} }]' => array(array('foo' => array('bar' => 'foo'))),
  91. '[foo, [bar, [foo, [bar, foo]], foo]]' => array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo')),
  92. '[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'))),
  93. '[foo, bar: { foo: bar }]' => array('foo', '1' => array('bar' => array('foo' => 'bar'))),
  94. );
  95. }
  96. protected function getTestsForDump()
  97. {
  98. return array(
  99. 'null' => null,
  100. 'false' => false,
  101. 'true' => true,
  102. '12' => 12,
  103. "'quoted string'" => 'quoted string',
  104. '12.30e+02' => 12.30e+02,
  105. '1234' => 0x4D2,
  106. '1243' => 02333,
  107. '.Inf' => -log(0),
  108. '-.Inf' => log(0),
  109. "'686e444'" => '686e444',
  110. '.Inf' => 646e444,
  111. '"foo\r\nbar"' => "foo\r\nbar",
  112. "'foo#bar'" => 'foo#bar',
  113. "'foo # bar'" => 'foo # bar',
  114. "'#cfcfcf'" => '#cfcfcf',
  115. "'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'',
  116. // sequences
  117. '[foo, bar, false, null, 12]' => array('foo', 'bar', false, null, 12),
  118. '[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'),
  119. // mappings
  120. '{ foo: bar, bar: foo, \'false\': false, \'null\': null, integer: 12 }' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
  121. '{ foo: bar, bar: \'foo: bar\' }' => array('foo' => 'bar', 'bar' => 'foo: bar'),
  122. // nested sequences and mappings
  123. '[foo, [bar, foo]]' => array('foo', array('bar', 'foo')),
  124. '[foo, [bar, [foo, [bar, foo]], foo]]' => array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo')),
  125. '{ foo: { bar: foo } }' => array('foo' => array('bar' => 'foo')),
  126. '[foo, { bar: foo }]' => array('foo', array('bar' => 'foo')),
  127. '[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'))),
  128. );
  129. }
  130. }