菜谱项目

StringDescriptionTest.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace Hamcrest;
  3. class SampleSelfDescriber implements \Hamcrest\SelfDescribing
  4. {
  5. private $_text;
  6. public function __construct($text)
  7. {
  8. $this->_text = $text;
  9. }
  10. public function describeTo(\Hamcrest\Description $description)
  11. {
  12. $description->appendText($this->_text);
  13. }
  14. }
  15. class StringDescriptionTest extends \PhpUnit_Framework_TestCase
  16. {
  17. private $_description;
  18. public function setUp()
  19. {
  20. $this->_description = new \Hamcrest\StringDescription();
  21. }
  22. public function testAppendTextAppendsTextInformation()
  23. {
  24. $this->_description->appendText('foo')->appendText('bar');
  25. $this->assertEquals('foobar', (string) $this->_description);
  26. }
  27. public function testAppendValueCanAppendTextTypes()
  28. {
  29. $this->_description->appendValue('foo');
  30. $this->assertEquals('"foo"', (string) $this->_description);
  31. }
  32. public function testSpecialCharactersAreEscapedForStringTypes()
  33. {
  34. $this->_description->appendValue("foo\\bar\"zip\r\n");
  35. $this->assertEquals('"foo\\bar\\"zip\r\n"', (string) $this->_description);
  36. }
  37. public function testIntegerValuesCanBeAppended()
  38. {
  39. $this->_description->appendValue(42);
  40. $this->assertEquals('<42>', (string) $this->_description);
  41. }
  42. public function testFloatValuesCanBeAppended()
  43. {
  44. $this->_description->appendValue(42.78);
  45. $this->assertEquals('<42.78F>', (string) $this->_description);
  46. }
  47. public function testNullValuesCanBeAppended()
  48. {
  49. $this->_description->appendValue(null);
  50. $this->assertEquals('null', (string) $this->_description);
  51. }
  52. public function testArraysCanBeAppended()
  53. {
  54. $this->_description->appendValue(array('foo', 42.78));
  55. $this->assertEquals('["foo", <42.78F>]', (string) $this->_description);
  56. }
  57. public function testObjectsCanBeAppended()
  58. {
  59. $this->_description->appendValue(new \stdClass());
  60. $this->assertEquals('<stdClass>', (string) $this->_description);
  61. }
  62. public function testBooleanValuesCanBeAppended()
  63. {
  64. $this->_description->appendValue(false);
  65. $this->assertEquals('<false>', (string) $this->_description);
  66. }
  67. public function testListsOfvaluesCanBeAppended()
  68. {
  69. $this->_description->appendValue(array('foo', 42.78));
  70. $this->assertEquals('["foo", <42.78F>]', (string) $this->_description);
  71. }
  72. public function testIterableOfvaluesCanBeAppended()
  73. {
  74. $items = new \ArrayObject(array('foo', 42.78));
  75. $this->_description->appendValue($items);
  76. $this->assertEquals('["foo", <42.78F>]', (string) $this->_description);
  77. }
  78. public function testIteratorOfvaluesCanBeAppended()
  79. {
  80. $items = new \ArrayObject(array('foo', 42.78));
  81. $this->_description->appendValue($items->getIterator());
  82. $this->assertEquals('["foo", <42.78F>]', (string) $this->_description);
  83. }
  84. public function testListsOfvaluesCanBeAppendedManually()
  85. {
  86. $this->_description->appendValueList('@start@', '@sep@ ', '@end@', array('foo', 42.78));
  87. $this->assertEquals('@start@"foo"@sep@ <42.78F>@end@', (string) $this->_description);
  88. }
  89. public function testIterableOfvaluesCanBeAppendedManually()
  90. {
  91. $items = new \ArrayObject(array('foo', 42.78));
  92. $this->_description->appendValueList('@start@', '@sep@ ', '@end@', $items);
  93. $this->assertEquals('@start@"foo"@sep@ <42.78F>@end@', (string) $this->_description);
  94. }
  95. public function testIteratorOfvaluesCanBeAppendedManually()
  96. {
  97. $items = new \ArrayObject(array('foo', 42.78));
  98. $this->_description->appendValueList('@start@', '@sep@ ', '@end@', $items->getIterator());
  99. $this->assertEquals('@start@"foo"@sep@ <42.78F>@end@', (string) $this->_description);
  100. }
  101. public function testSelfDescribingObjectsCanBeAppended()
  102. {
  103. $this->_description
  104. ->appendDescriptionOf(new \Hamcrest\SampleSelfDescriber('foo'))
  105. ->appendDescriptionOf(new \Hamcrest\SampleSelfDescriber('bar'))
  106. ;
  107. $this->assertEquals('foobar', (string) $this->_description);
  108. }
  109. public function testSelfDescribingObjectsCanBeAppendedAsLists()
  110. {
  111. $this->_description->appendList('@start@', '@sep@ ', '@end@', array(
  112. new \Hamcrest\SampleSelfDescriber('foo'),
  113. new \Hamcrest\SampleSelfDescriber('bar')
  114. ));
  115. $this->assertEquals('@start@foo@sep@ bar@end@', (string) $this->_description);
  116. }
  117. public function testSelfDescribingObjectsCanBeAppendedAsIteratedLists()
  118. {
  119. $items = new \ArrayObject(array(
  120. new \Hamcrest\SampleSelfDescriber('foo'),
  121. new \Hamcrest\SampleSelfDescriber('bar')
  122. ));
  123. $this->_description->appendList('@start@', '@sep@ ', '@end@', $items);
  124. $this->assertEquals('@start@foo@sep@ bar@end@', (string) $this->_description);
  125. }
  126. public function testSelfDescribingObjectsCanBeAppendedAsIterators()
  127. {
  128. $items = new \ArrayObject(array(
  129. new \Hamcrest\SampleSelfDescriber('foo'),
  130. new \Hamcrest\SampleSelfDescriber('bar')
  131. ));
  132. $this->_description->appendList('@start@', '@sep@ ', '@end@', $items->getIterator());
  133. $this->assertEquals('@start@foo@sep@ bar@end@', (string) $this->_description);
  134. }
  135. }