InlineTest.php 6.5KB

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