InlineTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. require_once __DIR__.'/../../bootstrap.php';
  11. use Symfony\Components\Yaml\Yaml;
  12. use Symfony\Components\Yaml\Inline;
  13. class InlineTest extends \PHPUnit_Framework_TestCase
  14. {
  15. static public function setUpBeforeClass()
  16. {
  17. Yaml::setSpecVersion('1.1');
  18. }
  19. public function testLoad()
  20. {
  21. foreach ($this->getTestsForLoad() as $yaml => $value)
  22. {
  23. $this->assertEquals($value, Inline::load($yaml), sprintf('::load() converts an inline YAML to a PHP structure (%s)', $yaml));
  24. }
  25. }
  26. public function testDump()
  27. {
  28. $testsForDump = $this->getTestsForDump();
  29. foreach ($testsForDump as $yaml => $value)
  30. {
  31. $this->assertEquals($yaml, Inline::dump($value), sprintf('::dump() converts a PHP structure to an inline YAML (%s)', $yaml));
  32. }
  33. foreach ($this->getTestsForLoad() as $yaml => $value)
  34. {
  35. if ($value == 1230)
  36. {
  37. continue;
  38. }
  39. $this->assertEquals($value, Inline::load(Inline::dump($value)), 'check consistency');
  40. }
  41. foreach ($testsForDump as $yaml => $value)
  42. {
  43. if ($value == 1230)
  44. {
  45. continue;
  46. }
  47. $this->assertEquals($value, Inline::load(Inline::dump($value)), 'check consistency');
  48. }
  49. }
  50. protected function getTestsForLoad()
  51. {
  52. return array(
  53. '' => '',
  54. 'null' => null,
  55. 'false' => false,
  56. 'true' => true,
  57. '12' => 12,
  58. '"quoted string"' => 'quoted string',
  59. "'quoted string'" => 'quoted string',
  60. '12.30e+02' => 12.30e+02,
  61. '0x4D2' => 0x4D2,
  62. '02333' => 02333,
  63. '.Inf' => -log(0),
  64. '-.Inf' => log(0),
  65. '123456789123456789' => '123456789123456789',
  66. '"foo\r\nbar"' => "foo\r\nbar",
  67. "'foo#bar'" => 'foo#bar',
  68. "'foo # bar'" => 'foo # bar',
  69. "'#cfcfcf'" => '#cfcfcf',
  70. '2007-10-30' => mktime(0, 0, 0, 10, 30, 2007),
  71. '2007-10-30T02:59:43Z' => gmmktime(2, 59, 43, 10, 30, 2007),
  72. '2007-10-30 02:59:43 Z' => gmmktime(2, 59, 43, 10, 30, 2007),
  73. '"a \\"string\\" with \'quoted strings inside\'"' => 'a "string" with \'quoted strings inside\'',
  74. "'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'',
  75. // sequences
  76. // urls are no key value mapping. see #3609. Valid yaml "key: value" mappings require a space after the colon
  77. '[foo, http://urls.are/no/mappings, false, null, 12]' => array('foo', 'http://urls.are/no/mappings', false, null, 12),
  78. '[ foo , bar , false , null , 12 ]' => array('foo', 'bar', false, null, 12),
  79. '[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'),
  80. // mappings
  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, false : false, null : null, integer : 12 }' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
  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. '{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}' => array('foo: ' => 'bar', "bar: " => 'foo: bar'),
  87. // nested sequences and mappings
  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('foo', array('bar', 'foo')),
  93. '[{ foo: {bar: foo} }]' => array(array('foo' => array('bar' => 'foo'))),
  94. '[foo, [bar, [foo, [bar, foo]], foo]]' => array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo')),
  95. '[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'))),
  96. '[foo, bar: { foo: bar }]' => array('foo', '1' => array('bar' => array('foo' => 'bar'))),
  97. );
  98. }
  99. protected function getTestsForDump()
  100. {
  101. return array(
  102. 'null' => null,
  103. 'false' => false,
  104. 'true' => true,
  105. '12' => 12,
  106. "'quoted string'" => 'quoted string',
  107. '12.30e+02' => 12.30e+02,
  108. '1234' => 0x4D2,
  109. '1243' => 02333,
  110. '.Inf' => -log(0),
  111. '-.Inf' => log(0),
  112. '"foo\r\nbar"' => "foo\r\nbar",
  113. "'foo#bar'" => 'foo#bar',
  114. "'foo # bar'" => 'foo # bar',
  115. "'#cfcfcf'" => '#cfcfcf',
  116. "'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'',
  117. // sequences
  118. '[foo, bar, false, null, 12]' => array('foo', 'bar', false, null, 12),
  119. '[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'),
  120. // mappings
  121. '{ foo: bar, bar: foo, \'false\': false, \'null\': null, integer: 12 }' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
  122. '{ foo: bar, bar: \'foo: bar\' }' => array('foo' => 'bar', 'bar' => 'foo: bar'),
  123. // nested sequences and mappings
  124. '[foo, [bar, foo]]' => array('foo', array('bar', 'foo')),
  125. '[foo, [bar, [foo, [bar, foo]], foo]]' => array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo')),
  126. '{ foo: { bar: foo } }' => array('foo' => array('bar' => 'foo')),
  127. '[foo, { bar: foo }]' => array('foo', array('bar' => 'foo')),
  128. '[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'))),
  129. );
  130. }
  131. }