expectation_test.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. // $Id: expectation_test.php 1788 2008-04-27 11:01:59Z pp11 $
  3. require_once(dirname(__FILE__) . '/../autorun.php');
  4. require_once(dirname(__FILE__) . '/../expectation.php');
  5. class TestOfEquality extends UnitTestCase {
  6. function testBoolean() {
  7. $is_true = new EqualExpectation(true);
  8. $this->assertTrue($is_true->test(true));
  9. $this->assertFalse($is_true->test(false));
  10. }
  11. function testStringMatch() {
  12. $hello = new EqualExpectation("Hello");
  13. $this->assertTrue($hello->test("Hello"));
  14. $this->assertFalse($hello->test("Goodbye"));
  15. }
  16. function testInteger() {
  17. $fifteen = new EqualExpectation(15);
  18. $this->assertTrue($fifteen->test(15));
  19. $this->assertFalse($fifteen->test(14));
  20. }
  21. function testFloat() {
  22. $pi = new EqualExpectation(3.14);
  23. $this->assertTrue($pi->test(3.14));
  24. $this->assertFalse($pi->test(3.15));
  25. }
  26. function testArray() {
  27. $colours = new EqualExpectation(array("r", "g", "b"));
  28. $this->assertTrue($colours->test(array("r", "g", "b")));
  29. $this->assertFalse($colours->test(array("g", "b", "r")));
  30. }
  31. function testHash() {
  32. $is_blue = new EqualExpectation(array("r" => 0, "g" => 0, "b" => 255));
  33. $this->assertTrue($is_blue->test(array("r" => 0, "g" => 0, "b" => 255)));
  34. $this->assertFalse($is_blue->test(array("r" => 0, "g" => 255, "b" => 0)));
  35. }
  36. function testHashWithOutOfOrderKeysShouldStillMatch() {
  37. $any_order = new EqualExpectation(array('a' => 1, 'b' => 2));
  38. $this->assertTrue($any_order->test(array('b' => 2, 'a' => 1)));
  39. }
  40. }
  41. class TestOfWithin extends UnitTestCase {
  42. function testWithinFloatingPointMargin() {
  43. $within = new WithinMarginExpectation(1.0, 0.2);
  44. $this->assertFalse($within->test(0.7));
  45. $this->assertTrue($within->test(0.8));
  46. $this->assertTrue($within->test(0.9));
  47. $this->assertTrue($within->test(1.1));
  48. $this->assertTrue($within->test(1.2));
  49. $this->assertFalse($within->test(1.3));
  50. }
  51. function testOutsideFloatingPointMargin() {
  52. $within = new OutsideMarginExpectation(1.0, 0.2);
  53. $this->assertTrue($within->test(0.7));
  54. $this->assertFalse($within->test(0.8));
  55. $this->assertFalse($within->test(1.2));
  56. $this->assertTrue($within->test(1.3));
  57. }
  58. }
  59. class TestOfInequality extends UnitTestCase {
  60. function testStringMismatch() {
  61. $not_hello = new NotEqualExpectation("Hello");
  62. $this->assertTrue($not_hello->test("Goodbye"));
  63. $this->assertFalse($not_hello->test("Hello"));
  64. }
  65. }
  66. class RecursiveNasty {
  67. private $me;
  68. function RecursiveNasty() {
  69. $this->me = $this;
  70. }
  71. }
  72. class TestOfIdentity extends UnitTestCase {
  73. function testType() {
  74. $string = new IdenticalExpectation("37");
  75. $this->assertTrue($string->test("37"));
  76. $this->assertFalse($string->test(37));
  77. $this->assertFalse($string->test("38"));
  78. }
  79. function _testNastyPhp5Bug() {
  80. $this->assertFalse(new RecursiveNasty() != new RecursiveNasty());
  81. }
  82. function _testReallyHorribleRecursiveStructure() {
  83. $hopeful = new IdenticalExpectation(new RecursiveNasty());
  84. $this->assertTrue($hopeful->test(new RecursiveNasty()));
  85. }
  86. }
  87. class DummyReferencedObject{}
  88. class TestOfReference extends UnitTestCase {
  89. function testReference() {
  90. $foo = "foo";
  91. $ref = &$foo;
  92. $not_ref = $foo;
  93. $bar = "bar";
  94. $expect = new ReferenceExpectation($foo);
  95. $this->assertTrue($expect->test($ref));
  96. $this->assertFalse($expect->test($not_ref));
  97. $this->assertFalse($expect->test($bar));
  98. }
  99. }
  100. class TestOfNonIdentity extends UnitTestCase {
  101. function testType() {
  102. $string = new NotIdenticalExpectation("37");
  103. $this->assertTrue($string->test("38"));
  104. $this->assertTrue($string->test(37));
  105. $this->assertFalse($string->test("37"));
  106. }
  107. }
  108. class TestOfPatterns extends UnitTestCase {
  109. function testWanted() {
  110. $pattern = new PatternExpectation('/hello/i');
  111. $this->assertTrue($pattern->test("Hello world"));
  112. $this->assertFalse($pattern->test("Goodbye world"));
  113. }
  114. function testUnwanted() {
  115. $pattern = new NoPatternExpectation('/hello/i');
  116. $this->assertFalse($pattern->test("Hello world"));
  117. $this->assertTrue($pattern->test("Goodbye world"));
  118. }
  119. }
  120. class ExpectedMethodTarget {
  121. function hasThisMethod() {}
  122. }
  123. class TestOfMethodExistence extends UnitTestCase {
  124. function testHasMethod() {
  125. $instance = new ExpectedMethodTarget();
  126. $expectation = new MethodExistsExpectation('hasThisMethod');
  127. $this->assertTrue($expectation->test($instance));
  128. $expectation = new MethodExistsExpectation('doesNotHaveThisMethod');
  129. $this->assertFalse($expectation->test($instance));
  130. }
  131. }
  132. class TestOfIsA extends UnitTestCase {
  133. function testString() {
  134. $expectation = new IsAExpectation('string');
  135. $this->assertTrue($expectation->test('Hello'));
  136. $this->assertFalse($expectation->test(5));
  137. }
  138. function testBoolean() {
  139. $expectation = new IsAExpectation('boolean');
  140. $this->assertTrue($expectation->test(true));
  141. $this->assertFalse($expectation->test(1));
  142. }
  143. function testBool() {
  144. $expectation = new IsAExpectation('bool');
  145. $this->assertTrue($expectation->test(true));
  146. $this->assertFalse($expectation->test(1));
  147. }
  148. function testDouble() {
  149. $expectation = new IsAExpectation('double');
  150. $this->assertTrue($expectation->test(5.0));
  151. $this->assertFalse($expectation->test(5));
  152. }
  153. function testFloat() {
  154. $expectation = new IsAExpectation('float');
  155. $this->assertTrue($expectation->test(5.0));
  156. $this->assertFalse($expectation->test(5));
  157. }
  158. function testReal() {
  159. $expectation = new IsAExpectation('real');
  160. $this->assertTrue($expectation->test(5.0));
  161. $this->assertFalse($expectation->test(5));
  162. }
  163. function testInteger() {
  164. $expectation = new IsAExpectation('integer');
  165. $this->assertTrue($expectation->test(5));
  166. $this->assertFalse($expectation->test(5.0));
  167. }
  168. function testInt() {
  169. $expectation = new IsAExpectation('int');
  170. $this->assertTrue($expectation->test(5));
  171. $this->assertFalse($expectation->test(5.0));
  172. }
  173. }
  174. class TestOfNotA extends UnitTestCase {
  175. function testString() {
  176. $expectation = new NotAExpectation('string');
  177. $this->assertFalse($expectation->test('Hello'));
  178. $this->assertTrue($expectation->test(5));
  179. }
  180. }
  181. ?>