ParseTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. require_once ("../Spyc.php");
  3. class ParseTest extends PHPUnit_Framework_TestCase {
  4. protected $yaml;
  5. protected function setUp() {
  6. $this->yaml = spyc_load_file('../spyc.yaml');
  7. }
  8. public function testMergeHashKeys() {
  9. $Expected = array (
  10. array ('step' => array('instrument' => 'Lasik 2000', 'pulseEnergy' => 5.4, 'pulseDuration' => 12, 'repetition' => 1000, 'spotSize' => '1mm')),
  11. array ('step' => array('instrument' => 'Lasik 2000', 'pulseEnergy' => 5.4, 'pulseDuration' => 12, 'repetition' => 1000, 'spotSize' => '2mm')),
  12. );
  13. $Actual = spyc_load_file ('indent_1.yaml');
  14. $this->assertEquals ($Expected, $Actual['steps']);
  15. }
  16. public function testDeathMasks() {
  17. $Expected = array ('sad' => 2, 'magnificent' => 4);
  18. $Actual = spyc_load_file ('indent_1.yaml');
  19. $this->assertEquals ($Expected, $Actual['death masks are']);
  20. }
  21. public function testDevDb() {
  22. $Expected = array ('adapter' => 'mysql', 'host' => 'localhost', 'database' => 'rails_dev');
  23. $Actual = spyc_load_file ('indent_1.yaml');
  24. $this->assertEquals ($Expected, $Actual['development']);
  25. }
  26. public function testNumericKey() {
  27. $this->assertEquals ("Ooo, a numeric key!", $this->yaml[1040]);
  28. }
  29. public function testMappingsString() {
  30. $this->assertEquals ("Anyone's name, really.", $this->yaml['String']);
  31. }
  32. public function testMappingsInt() {
  33. $this->assertSame (13, $this->yaml['Int']);
  34. }
  35. public function testMappingsHex() {
  36. $this->assertSame (243, $this->yaml['Hex']);
  37. $this->assertSame ('f0xf3', $this->yaml['BadHex']);
  38. }
  39. public function testMappingsBooleanTrue() {
  40. $this->assertSame (true, $this->yaml['True']);
  41. }
  42. public function testMappingsBooleanFalse() {
  43. $this->assertSame (false, $this->yaml['False']);
  44. }
  45. public function testMappingsZero() {
  46. $this->assertSame (0, $this->yaml['Zero']);
  47. }
  48. public function testMappingsNull() {
  49. $this->assertSame (null, $this->yaml['Null']);
  50. }
  51. public function testMappingsNotNull() {
  52. $this->assertSame ('null', $this->yaml['NotNull']);
  53. }
  54. public function testMappingsFloat() {
  55. $this->assertSame (5.34, $this->yaml['Float']);
  56. }
  57. public function testMappingsNegative() {
  58. $this->assertSame (-90, $this->yaml['Negative']);
  59. }
  60. public function testMappingsSmallFloat() {
  61. $this->assertSame (0.7, $this->yaml['SmallFloat']);
  62. }
  63. public function testNewline() {
  64. $this->assertSame ('\n', $this->yaml['NewLine']);
  65. }
  66. public function testQuotedNewline() {
  67. $this->assertSame ("\n", $this->yaml['QuotedNewLine']);
  68. }
  69. public function testSeq0() {
  70. $this->assertEquals ("PHP Class", $this->yaml[0]);
  71. }
  72. public function testSeq1() {
  73. $this->assertEquals ("Basic YAML Loader", $this->yaml[1]);
  74. }
  75. public function testSeq2() {
  76. $this->assertEquals ("Very Basic YAML Dumper", $this->yaml[2]);
  77. }
  78. public function testSeq3() {
  79. $this->assertEquals (array("YAML is so easy to learn.",
  80. "Your config files will never be the same."), $this->yaml[3]);
  81. }
  82. public function testSeqMap() {
  83. $this->assertEquals (array("cpu" => "1.5ghz", "ram" => "1 gig",
  84. "os" => "os x 10.4.1"), $this->yaml[4]);
  85. }
  86. public function testMappedSequence() {
  87. $this->assertEquals (array("yaml.org", "php.net"), $this->yaml['domains']);
  88. }
  89. public function testAnotherSequence() {
  90. $this->assertEquals (array("program" => "Adium", "platform" => "OS X",
  91. "type" => "Chat Client"), $this->yaml[5]);
  92. }
  93. public function testFoldedBlock() {
  94. $this->assertEquals ("There isn't any time for your tricks!\nDo you understand?", $this->yaml['no time']);
  95. }
  96. public function testLiteralAsMapped() {
  97. $this->assertEquals ("There is nothing but time\nfor your tricks.", $this->yaml['some time']);
  98. }
  99. public function testCrazy() {
  100. $this->assertEquals (array( array("name" => "spartan", "notes" =>
  101. array( "Needs to be backed up",
  102. "Needs to be normalized" ),
  103. "type" => "mysql" )), $this->yaml['databases']);
  104. }
  105. public function testColons() {
  106. $this->assertEquals ("like", $this->yaml["if: you'd"]);
  107. }
  108. public function testInline() {
  109. $this->assertEquals (array("One", "Two", "Three", "Four"), $this->yaml[6]);
  110. }
  111. public function testNestedInline() {
  112. $this->assertEquals (array("One", array("Two", "And", "Three"), "Four", "Five"), $this->yaml[7]);
  113. }
  114. public function testNestedNestedInline() {
  115. $this->assertEquals (array( "This", array("Is", "Getting", array("Ridiculous", "Guys")),
  116. "Seriously", array("Show", "Mercy")), $this->yaml[8]);
  117. }
  118. public function testInlineMappings() {
  119. $this->assertEquals (array("name" => "chris", "age" => "young", "brand" => "lucky strike"), $this->yaml[9]);
  120. }
  121. public function testNestedInlineMappings() {
  122. $this->assertEquals (array("name" => "mark", "age" => "older than chris",
  123. "brand" => array("marlboro", "lucky strike")), $this->yaml[10]);
  124. }
  125. public function testReferences() {
  126. $this->assertEquals (array('Perl', 'Python', 'PHP', 'Ruby'), $this->yaml['dynamic languages']);
  127. }
  128. public function testReferences2() {
  129. $this->assertEquals (array('C/C++', 'Java'), $this->yaml['compiled languages']);
  130. }
  131. public function testReferences3() {
  132. $this->assertEquals (array(
  133. array('Perl', 'Python', 'PHP', 'Ruby'),
  134. array('C/C++', 'Java')
  135. ), $this->yaml['all languages']);
  136. }
  137. public function testEscapedQuotes() {
  138. $this->assertEquals ("you know, this shouldn't work. but it does.", $this->yaml[11]);
  139. }
  140. public function testEscapedQuotes_2() {
  141. $this->assertEquals ( "that's my value.", $this->yaml[12]);
  142. }
  143. public function testEscapedQuotes_3() {
  144. $this->assertEquals ("again, that's my value.", $this->yaml[13]);
  145. }
  146. public function testQuotes() {
  147. $this->assertEquals ("here's to \"quotes\", boss.", $this->yaml[14]);
  148. }
  149. public function testQuoteSequence() {
  150. $this->assertEquals ( array( 'name' => "Foo, Bar's", 'age' => 20), $this->yaml[15]);
  151. }
  152. public function testShortSequence() {
  153. $this->assertEquals (array( 0 => "a", 1 => array (0 => 1, 1 => 2), 2 => "b"), $this->yaml[16]);
  154. }
  155. public function testQuotedNewlines() {
  156. $this->assertEquals ("First line\nSecond line\nThird line", $this->yaml[17]);
  157. }
  158. public function testHash_1() {
  159. $this->assertEquals ("Hash", $this->yaml['hash_1']);
  160. }
  161. public function testHash_2() {
  162. $this->assertEquals ('Hash #and a comment', $this->yaml['hash_2']);
  163. }
  164. public function testHash_3() {
  165. $this->assertEquals ('Hash (#) can appear in key too', $this->yaml['hash#3']);
  166. }
  167. public function testEndloop() {
  168. $this->assertEquals ("Does this line in the end indeed make Spyc go to an infinite loop?", $this->yaml['endloop']);
  169. }
  170. public function testReallyLargeNumber() {
  171. $this->assertEquals ('115792089237316195423570985008687907853269984665640564039457584007913129639936', $this->yaml['a_really_large_number']);
  172. }
  173. public function testFloatWithZeros() {
  174. $this->assertSame ('1.0', $this->yaml['float_test']);
  175. }
  176. public function testFloatWithQuotes() {
  177. $this->assertSame ('1.0', $this->yaml['float_test_with_quotes']);
  178. }
  179. public function testFloatInverse() {
  180. $this->assertEquals ('001', $this->yaml['float_inverse_test']);
  181. }
  182. public function testIntArray() {
  183. $this->assertEquals (array (1, 2, 3), $this->yaml['int array']);
  184. }
  185. public function testArrayOnSeveralLines() {
  186. $this->assertEquals (array (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19), $this->yaml['array on several lines']);
  187. }
  188. public function testArrayWithCommas() {
  189. $this->assertEquals(array (0, 1), $this->yaml['array with commas']);
  190. }
  191. public function testmoreLessKey() {
  192. $this->assertEquals ('<value>', $this->yaml['morelesskey']);
  193. }
  194. public function testArrayOfZero() {
  195. $this->assertSame (array(0), $this->yaml['array_of_zero']);
  196. }
  197. public function testSophisticatedArrayOfZero() {
  198. $this->assertSame (array('rx' => array ('tx' => array (0))), $this->yaml['sophisticated_array_of_zero']);
  199. }
  200. public function testSwitches() {
  201. $this->assertEquals (array (array ('row' => 0, 'col' => 0, 'func' => array ('tx' => array(0, 1)))), $this->yaml['switches']);
  202. }
  203. public function testEmptySequence() {
  204. $this->assertSame (array(), $this->yaml['empty_sequence']);
  205. }
  206. public function testEmptyHash() {
  207. $this->assertSame (array(), $this->yaml['empty_hash']);
  208. }
  209. public function testEmptykey() {
  210. $this->assertSame (array('' => array ('key' => 'value')), $this->yaml['empty_key']);
  211. }
  212. public function testMultilines() {
  213. $this->assertSame (array(array('type' => 'SomeItem', 'values' => array ('blah', 'blah', 'blah', 'blah'), 'ints' => array(2, 54, 12, 2143))), $this->yaml['multiline_items']);
  214. }
  215. public function testManyNewlines() {
  216. $this->assertSame ('A quick
  217. fox
  218. jumped
  219. over
  220. a lazy
  221. dog', $this->yaml['many_lines']);
  222. }
  223. public function testWerte() {
  224. $this->assertSame (array ('1' => 'nummer 1', '0' => 'Stunde 0'), $this->yaml['werte']);
  225. }
  226. /* public function testNoIndent() {
  227. $this->assertSame (array(
  228. array ('record1'=>'value1'),
  229. array ('record2'=>'value2')
  230. )
  231. , $this->yaml['noindent_records']);
  232. } */
  233. public function testColonsInKeys() {
  234. $this->assertSame (array (1000), $this->yaml['a:1']);
  235. }
  236. public function testColonsInKeys2() {
  237. $this->assertSame (array (2000), $this->yaml['a:2']);
  238. }
  239. public function testUnquotedColonsInKeys() {
  240. $this->assertSame (array (3000), $this->yaml['a:3']);
  241. }
  242. public function testComplicatedKeyWithColon() {
  243. $this->assertSame(array("a:b:''test'" => 'value'), $this->yaml['complex_unquoted_key']);
  244. }
  245. public function testKeysInMappedValueException() {
  246. $this->setExpectedException('Exception');
  247. Spyc::YAMLLoad('x: y: z:');
  248. }
  249. public function testKeysInValueException() {
  250. $this->setExpectedException('Exception');
  251. Spyc::YAMLLoad('x: y: z');
  252. }
  253. public function testSpecialCharacters() {
  254. $this->assertSame ('[{]]{{]]', $this->yaml['special_characters']);
  255. }
  256. public function testAngleQuotes() {
  257. $Quotes = Spyc::YAMLLoad('quotes.yaml');
  258. $this->assertEquals (array ('html_tags' => array ('<br>', '<p>'), 'html_content' => array ('<p>hello world</p>', 'hello<br>world'), 'text_content' => array ('hello world')),
  259. $Quotes);
  260. }
  261. public function testFailingColons() {
  262. $Failing = Spyc::YAMLLoad('failing1.yaml');
  263. $this->assertSame (array ('MyObject' => array ('Prop1' => array ('key1:val1'))),
  264. $Failing);
  265. }
  266. public function testQuotesWithComments() {
  267. $Expected = 'bar';
  268. $Actual = spyc_load_file ('comments.yaml');
  269. $this->assertEquals ($Expected, $Actual['foo']);
  270. }
  271. public function testArrayWithComments() {
  272. $Expected = array ('x', 'y', 'z');
  273. $Actual = spyc_load_file ('comments.yaml');
  274. $this->assertEquals ($Expected, $Actual['arr']);
  275. }
  276. public function testAfterArrayWithKittens() {
  277. $Expected = 'kittens';
  278. $Actual = spyc_load_file ('comments.yaml');
  279. $this->assertEquals ($Expected, $Actual['bar']);
  280. }
  281. // Plain characters http://www.yaml.org/spec/1.2/spec.html#id2789510
  282. public function testKai() {
  283. $Expected = array('-example' => 'value');
  284. $Actual = spyc_load_file ('indent_1.yaml');
  285. $this->assertEquals ($Expected, $Actual['kai']);
  286. }
  287. public function testKaiList() {
  288. $Expected = array ('-item', '-item', '-item');
  289. $Actual = spyc_load_file ('indent_1.yaml');
  290. $this->assertEquals ($Expected, $Actual['kai_list_of_items']);
  291. }
  292. public function testDifferentQuoteTypes() {
  293. $expected = array ('Something', "", "", "Something else");
  294. $this->assertSame ($expected, $this->yaml['invoice']);
  295. }
  296. public function testDifferentQuoteTypes2() {
  297. $expected = array ('Something', "Nothing", "Anything", "Thing");
  298. $this->assertSame ($expected, $this->yaml['quotes']);
  299. }
  300. // Separation spaces http://www.yaml.org/spec/1.2/spec.html#id2778394
  301. public function testMultipleArrays() {
  302. $expected = array(array(array('x')));
  303. $this->assertSame($expected, Spyc::YAMLLoad("- - - x"));
  304. }
  305. }