InlineTest.php 5.2 KB

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