InlineTest.php 6.3 KB

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