InlineTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Symfony\Tests\Components\OutputEscaper;
  10. use Symfony\Components\Yaml\Yaml;
  11. use Symfony\Components\Yaml\Inline;
  12. class InlineTest extends \PHPUnit_Framework_TestCase
  13. {
  14. static public function setUpBeforeClass()
  15. {
  16. Yaml::setSpecVersion('1.1');
  17. }
  18. public function testLoad()
  19. {
  20. foreach ($this->getTestsForLoad() as $yaml => $value)
  21. {
  22. $this->assertEquals($value, Inline::load($yaml), sprintf('::load() converts an inline YAML to a PHP structure (%s)', $yaml));
  23. }
  24. }
  25. public function testDump()
  26. {
  27. $testsForDump = $this->getTestsForDump();
  28. foreach ($testsForDump as $yaml => $value)
  29. {
  30. $this->assertEquals($yaml, Inline::dump($value), sprintf('::dump() converts a PHP structure to an inline YAML (%s)', $yaml));
  31. }
  32. foreach ($this->getTestsForLoad() as $yaml => $value)
  33. {
  34. if ($value == 1230)
  35. {
  36. continue;
  37. }
  38. $this->assertEquals($value, Inline::load(Inline::dump($value)), 'check consistency');
  39. }
  40. foreach ($testsForDump as $yaml => $value)
  41. {
  42. if ($value == 1230)
  43. {
  44. continue;
  45. }
  46. $this->assertEquals($value, Inline::load(Inline::dump($value)), 'check consistency');
  47. }
  48. }
  49. protected function getTestsForLoad()
  50. {
  51. return array(
  52. '' => '',
  53. 'null' => null,
  54. 'false' => false,
  55. 'true' => true,
  56. '12' => 12,
  57. '"quoted string"' => 'quoted string',
  58. "'quoted string'" => 'quoted string',
  59. '12.30e+02' => 12.30e+02,
  60. '0x4D2' => 0x4D2,
  61. '02333' => 02333,
  62. '.Inf' => -log(0),
  63. '-.Inf' => log(0),
  64. '123456789123456789' => '123456789123456789',
  65. '"foo\r\nbar"' => "foo\r\nbar",
  66. "'foo#bar'" => 'foo#bar',
  67. "'foo # bar'" => 'foo # bar',
  68. "'#cfcfcf'" => '#cfcfcf',
  69. '2007-10-30' => mktime(0, 0, 0, 10, 30, 2007),
  70. '2007-10-30T02:59:43Z' => gmmktime(2, 59, 43, 10, 30, 2007),
  71. '2007-10-30 02:59:43 Z' => gmmktime(2, 59, 43, 10, 30, 2007),
  72. '"a \\"string\\" with \'quoted strings inside\'"' => 'a "string" with \'quoted strings inside\'',
  73. "'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'',
  74. // sequences
  75. // urls are no key value mapping. see #3609. Valid yaml "key: value" mappings require a space after the colon
  76. '[foo, http://urls.are/no/mappings, false, null, 12]' => array('foo', 'http://urls.are/no/mappings', false, null, 12),
  77. '[ foo , bar , false , null , 12 ]' => array('foo', 'bar', false, null, 12),
  78. '[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'),
  79. // mappings
  80. '{foo:bar,bar:foo,false:false,null:null,integer:12}' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
  81. '{ foo : bar, bar : foo, false : false, null : null, integer : 12 }' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
  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. '{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}' => array('foo\'' => 'bar', "bar\"" => 'foo: bar'),
  85. '{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}' => array('foo: ' => 'bar', "bar: " => 'foo: bar'),
  86. // nested sequences and mappings
  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('foo' => array('bar', 'foo')),
  91. '[ foo, [ bar, foo ] ]' => array('foo', array('bar', 'foo')),
  92. '[{ foo: {bar: foo} }]' => array(array('foo' => array('bar' => 'foo'))),
  93. '[foo, [bar, [foo, [bar, foo]], foo]]' => array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo')),
  94. '[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'))),
  95. '[foo, bar: { foo: bar }]' => array('foo', '1' => array('bar' => array('foo' => 'bar'))),
  96. );
  97. }
  98. protected function getTestsForDump()
  99. {
  100. return array(
  101. 'null' => null,
  102. 'false' => false,
  103. 'true' => true,
  104. '12' => 12,
  105. "'quoted string'" => 'quoted string',
  106. '12.30e+02' => 12.30e+02,
  107. '1234' => 0x4D2,
  108. '1243' => 02333,
  109. '.Inf' => -log(0),
  110. '-.Inf' => log(0),
  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. }