PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/symfony/yaml/Symfony/Component/Yaml/Tests/InlineTest.php

https://bitbucket.org/alexpozdnyakov/kohana
PHP | 211 lines | 167 code | 24 blank | 20 comment | 2 complexity | 7e12103349d22af73c1d8f8b880568df MD5 | raw file
Possible License(s): BSD-3-Clause
  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->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. $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. public function testParseScalarWithCorrectlyQuotedStringShouldReturnString()
  78. {
  79. $value = "'don''t do somthin'' like that'";
  80. $expect = "don't do somthin' like that";
  81. $this->assertSame($expect, Inline::parseScalar($value));
  82. }
  83. protected function getTestsForParse()
  84. {
  85. return array(
  86. '' => '',
  87. 'null' => null,
  88. 'false' => false,
  89. 'true' => true,
  90. '12' => 12,
  91. '"quoted string"' => 'quoted string',
  92. "'quoted string'" => 'quoted string',
  93. '12.30e+02' => 12.30e+02,
  94. '0x4D2' => 0x4D2,
  95. '02333' => 02333,
  96. '.Inf' => -log(0),
  97. '-.Inf' => log(0),
  98. "'686e444'" => '686e444',
  99. '686e444' => 646e444,
  100. '123456789123456789' => '123456789123456789',
  101. '"foo\r\nbar"' => "foo\r\nbar",
  102. "'foo#bar'" => 'foo#bar',
  103. "'foo # bar'" => 'foo # bar',
  104. "'#cfcfcf'" => '#cfcfcf',
  105. '::form_base.html.twig' => '::form_base.html.twig',
  106. '2007-10-30' => mktime(0, 0, 0, 10, 30, 2007),
  107. '2007-10-30T02:59:43Z' => gmmktime(2, 59, 43, 10, 30, 2007),
  108. '2007-10-30 02:59:43 Z' => gmmktime(2, 59, 43, 10, 30, 2007),
  109. '"a \\"string\\" with \'quoted strings inside\'"' => 'a "string" with \'quoted strings inside\'',
  110. "'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'',
  111. // sequences
  112. // urls are no key value mapping. see #3609. Valid yaml "key: value" mappings require a space after the colon
  113. '[foo, http://urls.are/no/mappings, false, null, 12]' => array('foo', 'http://urls.are/no/mappings', false, null, 12),
  114. '[ foo , bar , false , null , 12 ]' => array('foo', 'bar', false, null, 12),
  115. '[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'),
  116. // mappings
  117. '{foo:bar,bar:foo,false:false,null:null,integer:12}' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
  118. '{ foo : bar, bar : foo, false : false, null : null, integer : 12 }' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
  119. '{foo: \'bar\', bar: \'foo: bar\'}' => array('foo' => 'bar', 'bar' => 'foo: bar'),
  120. '{\'foo\': \'bar\', "bar": \'foo: bar\'}' => array('foo' => 'bar', 'bar' => 'foo: bar'),
  121. '{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}' => array('foo\'' => 'bar', "bar\"" => 'foo: bar'),
  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}]' => array('foo', array('bar' => 'foo')),
  126. '{ foo: {bar: foo} }' => array('foo' => array('bar' => 'foo')),
  127. '{ foo: [bar, foo] }' => array('foo' => array('bar', 'foo')),
  128. '[ foo, [ bar, foo ] ]' => array('foo', array('bar', 'foo')),
  129. '[{ foo: {bar: foo} }]' => array(array('foo' => array('bar' => 'foo'))),
  130. '[foo, [bar, [foo, [bar, foo]], foo]]' => array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo')),
  131. '[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'))),
  132. '[foo, bar: { foo: bar }]' => array('foo', '1' => array('bar' => array('foo' => 'bar'))),
  133. '[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',),
  134. );
  135. }
  136. protected function getTestsForDump()
  137. {
  138. return array(
  139. 'null' => null,
  140. 'false' => false,
  141. 'true' => true,
  142. '12' => 12,
  143. "'quoted string'" => 'quoted string',
  144. '12.30e+02' => 12.30e+02,
  145. '1234' => 0x4D2,
  146. '1243' => 02333,
  147. '.Inf' => -log(0),
  148. '-.Inf' => log(0),
  149. "'686e444'" => '686e444',
  150. '.Inf' => 646e444,
  151. '"foo\r\nbar"' => "foo\r\nbar",
  152. "'foo#bar'" => 'foo#bar',
  153. "'foo # bar'" => 'foo # bar',
  154. "'#cfcfcf'" => '#cfcfcf',
  155. "'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'',
  156. // sequences
  157. '[foo, bar, false, null, 12]' => array('foo', 'bar', false, null, 12),
  158. '[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'),
  159. // mappings
  160. '{ foo: bar, bar: foo, \'false\': false, \'null\': null, integer: 12 }' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
  161. '{ foo: bar, bar: \'foo: bar\' }' => array('foo' => 'bar', 'bar' => 'foo: bar'),
  162. // nested sequences and mappings
  163. '[foo, [bar, foo]]' => array('foo', array('bar', 'foo')),
  164. '[foo, [bar, [foo, [bar, foo]], foo]]' => array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo')),
  165. '{ foo: { bar: foo } }' => array('foo' => array('bar' => 'foo')),
  166. '[foo, { bar: foo }]' => array('foo', array('bar' => 'foo')),
  167. '[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'))),
  168. '[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',),
  169. );
  170. }
  171. }