InlineTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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\Component\Yaml\Tests;
  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->assertSame($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 testDumpNumericValueWithLocale()
  35. {
  36. $locale = setlocale(LC_NUMERIC, 0);
  37. if (false === $locale) {
  38. $this->markTestSkipped('Your platform does not support locales.');
  39. }
  40. $required_locales = array('fr_FR.UTF-8', 'fr_FR.UTF8', 'fr_FR.utf-8', 'fr_FR.utf8', 'French_France.1252');
  41. if (false === setlocale(LC_ALL, $required_locales)) {
  42. $this->markTestSkipped('Could not set any of required locales: '.implode(", ", $required_locales));
  43. }
  44. $this->assertEquals('1.2', Inline::dump(1.2));
  45. $this->assertContains('fr', strtolower(setlocale(LC_NUMERIC, 0)));
  46. setlocale(LC_ALL, $locale);
  47. }
  48. public function testHashStringsResemblingExponentialNumericsShouldNotBeChangedToINF()
  49. {
  50. $value = '686e444';
  51. $this->assertSame($value, Inline::parse(Inline::dump($value)));
  52. }
  53. /**
  54. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  55. */
  56. public function testParseScalarWithIncorrectlyQuotedStringShouldThrowException()
  57. {
  58. $value = "'don't do somthin' like that'";
  59. Inline::parse($value);
  60. }
  61. /**
  62. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  63. */
  64. public function testParseScalarWithIncorrectlyDoubleQuotedStringShouldThrowException()
  65. {
  66. $value = '"don"t do somthin" like that"';
  67. Inline::parse($value);
  68. }
  69. /**
  70. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  71. */
  72. public function testParseInvalidMappingKeyShouldThrowException()
  73. {
  74. $value = '{ "foo " bar": "bar" }';
  75. Inline::parse($value);
  76. }
  77. /**
  78. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  79. */
  80. public function testParseInvalidMappingShouldThrowException()
  81. {
  82. Inline::parse('[foo] bar');
  83. }
  84. /**
  85. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  86. */
  87. public function testParseInvalidSequenceShouldThrowException()
  88. {
  89. Inline::parse('{ foo: bar } bar');
  90. }
  91. public function testParseScalarWithCorrectlyQuotedStringShouldReturnString()
  92. {
  93. $value = "'don''t do somthin'' like that'";
  94. $expect = "don't do somthin' like that";
  95. $this->assertSame($expect, Inline::parseScalar($value));
  96. }
  97. protected function getTestsForParse()
  98. {
  99. return array(
  100. '' => '',
  101. 'null' => null,
  102. 'false' => false,
  103. 'true' => true,
  104. '12' => 12,
  105. '-12' => -12,
  106. '"quoted string"' => 'quoted string',
  107. "'quoted string'" => 'quoted string',
  108. '12.30e+02' => 12.30e+02,
  109. '0x4D2' => 0x4D2,
  110. '02333' => 02333,
  111. '.Inf' => -log(0),
  112. '-.Inf' => log(0),
  113. "'686e444'" => '686e444',
  114. '686e444' => 646e444,
  115. '123456789123456789123456789123456789' => '123456789123456789123456789123456789',
  116. '"foo\r\nbar"' => "foo\r\nbar",
  117. "'foo#bar'" => 'foo#bar',
  118. "'foo # bar'" => 'foo # bar',
  119. "'#cfcfcf'" => '#cfcfcf',
  120. '::form_base.html.twig' => '::form_base.html.twig',
  121. '2007-10-30' => mktime(0, 0, 0, 10, 30, 2007),
  122. '2007-10-30T02:59:43Z' => gmmktime(2, 59, 43, 10, 30, 2007),
  123. '2007-10-30 02:59:43 Z' => gmmktime(2, 59, 43, 10, 30, 2007),
  124. '1960-10-30 02:59:43 Z' => gmmktime(2, 59, 43, 10, 30, 1960),
  125. '1730-10-30T02:59:43Z' => gmmktime(2, 59, 43, 10, 30, 1730),
  126. '"a \\"string\\" with \'quoted strings inside\'"' => 'a "string" with \'quoted strings inside\'',
  127. "'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'',
  128. // sequences
  129. // urls are no key value mapping. see #3609. Valid yaml "key: value" mappings require a space after the colon
  130. '[foo, http://urls.are/no/mappings, false, null, 12]' => array('foo', 'http://urls.are/no/mappings', false, null, 12),
  131. '[ foo , bar , false , null , 12 ]' => array('foo', 'bar', false, null, 12),
  132. '[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'),
  133. // mappings
  134. '{foo:bar,bar:foo,false:false,null:null,integer:12}' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
  135. '{ foo : bar, bar : foo, false : false, null : null, integer : 12 }' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
  136. '{foo: \'bar\', bar: \'foo: bar\'}' => array('foo' => 'bar', 'bar' => 'foo: bar'),
  137. '{\'foo\': \'bar\', "bar": \'foo: bar\'}' => array('foo' => 'bar', 'bar' => 'foo: bar'),
  138. '{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}' => array('foo\'' => 'bar', "bar\"" => 'foo: bar'),
  139. '{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}' => array('foo: ' => 'bar', "bar: " => 'foo: bar'),
  140. // nested sequences and mappings
  141. '[foo, [bar, foo]]' => array('foo', array('bar', 'foo')),
  142. '[foo, {bar: foo}]' => array('foo', array('bar' => 'foo')),
  143. '{ foo: {bar: foo} }' => array('foo' => array('bar' => 'foo')),
  144. '{ foo: [bar, foo] }' => array('foo' => array('bar', 'foo')),
  145. '[ foo, [ bar, foo ] ]' => array('foo', array('bar', 'foo')),
  146. '[{ foo: {bar: foo} }]' => array(array('foo' => array('bar' => 'foo'))),
  147. '[foo, [bar, [foo, [bar, foo]], foo]]' => array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo')),
  148. '[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'))),
  149. '[foo, bar: { foo: bar }]' => array('foo', '1' => array('bar' => array('foo' => 'bar'))),
  150. '[foo, \'@foo.baz\', { \'%foo%\': \'foo is %foo%\', bar: \'%foo%\' }, true, \'@service_container\']' => array('foo', '@foo.baz', array('%foo%' => 'foo is %foo%', 'bar' => '%foo%',), true, '@service_container',),
  151. );
  152. }
  153. protected function getTestsForDump()
  154. {
  155. return array(
  156. 'null' => null,
  157. 'false' => false,
  158. 'true' => true,
  159. '12' => 12,
  160. "'quoted string'" => 'quoted string',
  161. '12.30e+02' => 12.30e+02,
  162. '1234' => 0x4D2,
  163. '1243' => 02333,
  164. '.Inf' => -log(0),
  165. '-.Inf' => log(0),
  166. "'686e444'" => '686e444',
  167. '.Inf' => 646e444,
  168. '"foo\r\nbar"' => "foo\r\nbar",
  169. "'foo#bar'" => 'foo#bar',
  170. "'foo # bar'" => 'foo # bar',
  171. "'#cfcfcf'" => '#cfcfcf',
  172. "'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'',
  173. // sequences
  174. '[foo, bar, false, null, 12]' => array('foo', 'bar', false, null, 12),
  175. '[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'),
  176. // mappings
  177. '{ foo: bar, bar: foo, \'false\': false, \'null\': null, integer: 12 }' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
  178. '{ foo: bar, bar: \'foo: bar\' }' => array('foo' => 'bar', 'bar' => 'foo: bar'),
  179. // nested sequences and mappings
  180. '[foo, [bar, foo]]' => array('foo', array('bar', 'foo')),
  181. '[foo, [bar, [foo, [bar, foo]], foo]]' => array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo')),
  182. '{ foo: { bar: foo } }' => array('foo' => array('bar' => 'foo')),
  183. '[foo, { bar: foo }]' => array('foo', array('bar' => 'foo')),
  184. '[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'))),
  185. '[foo, \'@foo.baz\', { \'%foo%\': \'foo is %foo%\', bar: \'%foo%\' }, true, \'@service_container\']' => array('foo', '@foo.baz', array('%foo%' => 'foo is %foo%', 'bar' => '%foo%',), true, '@service_container',),
  186. );
  187. }
  188. }