DumpTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. require_once ("../Spyc.php");
  3. class DumpTest extends PHPUnit_Framework_TestCase {
  4. private $files_to_test = array();
  5. public function setUp() {
  6. $this->files_to_test = array ('../spyc.yaml', 'failing1.yaml', 'indent_1.yaml', 'quotes.yaml');
  7. }
  8. public function testShortSyntax() {
  9. $dump = spyc_dump(array ('item1', 'item2', 'item3'));
  10. $awaiting = "- item1\n- item2\n- item3\n";
  11. $this->assertEquals ($awaiting, $dump);
  12. }
  13. public function testDump() {
  14. foreach ($this->files_to_test as $file) {
  15. $yaml = spyc_load(file_get_contents($file));
  16. $dump = Spyc::YAMLDump ($yaml);
  17. $yaml_after_dump = Spyc::YAMLLoad ($dump);
  18. $this->assertEquals ($yaml, $yaml_after_dump);
  19. }
  20. }
  21. public function testDumpWithQuotes() {
  22. $Spyc = new Spyc();
  23. $Spyc->setting_dump_force_quotes = true;
  24. foreach ($this->files_to_test as $file) {
  25. $yaml = $Spyc->load(file_get_contents($file));
  26. $dump = $Spyc->dump ($yaml);
  27. $yaml_after_dump = Spyc::YAMLLoad ($dump);
  28. $this->assertEquals ($yaml, $yaml_after_dump);
  29. }
  30. }
  31. public function testDumpArrays() {
  32. $dump = Spyc::YAMLDump(array ('item1', 'item2', 'item3'));
  33. $awaiting = "---\n- item1\n- item2\n- item3\n";
  34. $this->assertEquals ($awaiting, $dump);
  35. }
  36. public function testNull() {
  37. $dump = Spyc::YAMLDump(array('a' => 1, 'b' => null, 'c' => 3));
  38. $awaiting = "---\na: 1\nb: null\nc: 3\n";
  39. $this->assertEquals ($awaiting, $dump);
  40. }
  41. public function testNext() {
  42. $array = array("aaa", "bbb", "ccc");
  43. #set arrays internal pointer to next element
  44. next($array);
  45. $dump = Spyc::YAMLDump($array);
  46. $awaiting = "---\n- aaa\n- bbb\n- ccc\n";
  47. $this->assertEquals ($awaiting, $dump);
  48. }
  49. public function testDumpingMixedArrays() {
  50. $array = array();
  51. $array[] = 'Sequence item';
  52. $array['The Key'] = 'Mapped value';
  53. $array[] = array('A sequence','of a sequence');
  54. $array[] = array('first' => 'A sequence','second' => 'of mapped values');
  55. $array['Mapped'] = array('A sequence','which is mapped');
  56. $array['A Note'] = 'What if your text is too long?';
  57. $array['Another Note'] = 'If that is the case, the dumper will probably fold your text by using a block. Kinda like this.';
  58. $array['The trick?'] = 'The trick is that we overrode the default indent, 2, to 4 and the default wordwrap, 40, to 60.';
  59. $array['Old Dog'] = "And if you want\n to preserve line breaks, \ngo ahead!";
  60. $array['key:withcolon'] = "Should support this to";
  61. $yaml = Spyc::YAMLDump($array,4,60);
  62. }
  63. public function testMixed() {
  64. $dump = Spyc::YAMLDump(array(0 => 1, 'b' => 2, 1 => 3));
  65. $awaiting = "---\n0: 1\nb: 2\n1: 3\n";
  66. $this->assertEquals ($awaiting, $dump);
  67. }
  68. public function testDumpNumerics() {
  69. $dump = Spyc::YAMLDump(array ('404', '405', '500'));
  70. $awaiting = "---\n- \"404\"\n- \"405\"\n- \"500\"\n";
  71. $this->assertEquals ($awaiting, $dump);
  72. }
  73. public function testDumpAsterisks() {
  74. $dump = Spyc::YAMLDump(array ('*'));
  75. $awaiting = "---\n- '*'\n";
  76. $this->assertEquals ($awaiting, $dump);
  77. }
  78. public function testDumpAmpersands() {
  79. $dump = Spyc::YAMLDump(array ('some' => '&foo'));
  80. $awaiting = "---\nsome: '&foo'\n";
  81. $this->assertEquals ($awaiting, $dump);
  82. }
  83. public function testDumpExclamations() {
  84. $dump = Spyc::YAMLDump(array ('some' => '!foo'));
  85. $awaiting = "---\nsome: '!foo'\n";
  86. $this->assertEquals ($awaiting, $dump);
  87. }
  88. public function testDumpExclamations2() {
  89. $dump = Spyc::YAMLDump(array ('some' => 'foo!'));
  90. $awaiting = "---\nsome: foo!\n";
  91. $this->assertEquals ($awaiting, $dump);
  92. }
  93. public function testDumpApostrophes() {
  94. $dump = Spyc::YAMLDump(array ('some' => "'Biz' pimpt bedrijventerreinen"));
  95. $awaiting = "---\nsome: \"'Biz' pimpt bedrijventerreinen\"\n";
  96. $this->assertEquals ($awaiting, $dump);
  97. }
  98. public function testDumpNumericHashes() {
  99. $dump = Spyc::YAMLDump(array ("titel"=> array("0" => "", 1 => "Dr.", 5 => "Prof.", 6 => "Prof. Dr.")));
  100. $awaiting = "---\ntitel:\n 0: \"\"\n 1: Dr.\n 5: Prof.\n 6: Prof. Dr.\n";
  101. $this->assertEquals ($awaiting, $dump);
  102. }
  103. public function testEmpty() {
  104. $dump = Spyc::YAMLDump(array("foo" => array()));
  105. $awaiting = "---\nfoo: [ ]\n";
  106. $this->assertEquals ($awaiting, $dump);
  107. }
  108. public function testHashesInKeys() {
  109. $dump = Spyc::YAMLDump(array ('#color' => '#ffffff'));
  110. $awaiting = "---\n\"#color\": '#ffffff'\n";
  111. $this->assertEquals ($awaiting, $dump);
  112. }
  113. }