暂无描述

StringDescriptionTest.php 5.3KB

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