PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/webapp/_lib/extlib/simpletest/test/expectation_test.php

https://github.com/devsatish/ThinkUp
PHP | 299 lines | 237 code | 57 blank | 5 comment | 1 complexity | 0515302d516cefe4948e370657ad3ffc MD5 | raw file
  1. <?php
  2. // $Id: expectation_test.php 1905 2009-07-29 13:54:00Z dgheath $
  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 OpaqueContainer {
  73. private $stuff;
  74. private $value;
  75. public function __construct($value) {
  76. $this->value = $value;
  77. }
  78. }
  79. class DerivedOpaqueContainer extends OpaqueContainer {
  80. // Deliberately have a variable whose name with the same suffix as a later
  81. // variable
  82. private $new_value = 1;
  83. // Deliberately obscures the variable of the same name in the base
  84. // class.
  85. private $value;
  86. public function __construct($value, $base_value) {
  87. parent::__construct($base_value);
  88. $this->value = $value;
  89. }
  90. }
  91. class TestOfIdentity extends UnitTestCase {
  92. function testType() {
  93. $string = new IdenticalExpectation("37");
  94. $this->assertTrue($string->test("37"));
  95. $this->assertFalse($string->test(37));
  96. $this->assertFalse($string->test("38"));
  97. }
  98. function _testNastyPhp5Bug() {
  99. $this->assertFalse(new RecursiveNasty() != new RecursiveNasty());
  100. }
  101. function _testReallyHorribleRecursiveStructure() {
  102. $hopeful = new IdenticalExpectation(new RecursiveNasty());
  103. $this->assertTrue($hopeful->test(new RecursiveNasty()));
  104. }
  105. function testCanComparePrivateMembers() {
  106. $expectFive = new IdenticalExpectation(new OpaqueContainer(5));
  107. $this->assertTrue($expectFive->test(new OpaqueContainer(5)));
  108. $this->assertFalse($expectFive->test(new OpaqueContainer(6)));
  109. }
  110. function testCanComparePrivateMembersOfObjectsInArrays() {
  111. $expectFive = new IdenticalExpectation(array(new OpaqueContainer(5)));
  112. $this->assertTrue($expectFive->test(array(new OpaqueContainer(5))));
  113. $this->assertFalse($expectFive->test(array(new OpaqueContainer(6))));
  114. }
  115. function testCanComparePrivateMembersOfObjectsWherePrivateMemberOfBaseClassIsObscured() {
  116. $expectFive = new IdenticalExpectation(array(new DerivedOpaqueContainer(1,2)));
  117. $this->assertTrue($expectFive->test(array(new DerivedOpaqueContainer(1,2))));
  118. $this->assertFalse($expectFive->test(array(new DerivedOpaqueContainer(0,2))));
  119. $this->assertFalse($expectFive->test(array(new DerivedOpaqueContainer(0,9))));
  120. $this->assertFalse($expectFive->test(array(new DerivedOpaqueContainer(1,0))));
  121. }
  122. }
  123. class TransparentContainer {
  124. public $value;
  125. public function __construct($value) {
  126. $this->value = $value;
  127. }
  128. }
  129. class TestOfMemberComparison extends UnitTestCase {
  130. function testMemberExpectationCanMatchPublicMember() {
  131. $expect_five = new MemberExpectation('value', 5);
  132. $this->assertTrue($expect_five->test(new TransparentContainer(5)));
  133. $this->assertFalse($expect_five->test(new TransparentContainer(8)));
  134. }
  135. function testMemberExpectationCanMatchPrivateMember() {
  136. $expect_five = new MemberExpectation('value', 5);
  137. $this->assertTrue($expect_five->test(new OpaqueContainer(5)));
  138. $this->assertFalse($expect_five->test(new OpaqueContainer(8)));
  139. }
  140. function testMemberExpectationCanMatchPrivateMemberObscuredByDerivedClass() {
  141. $expect_five = new MemberExpectation('value', 5);
  142. $this->assertTrue($expect_five->test(new DerivedOpaqueContainer(5,8)));
  143. $this->assertTrue($expect_five->test(new DerivedOpaqueContainer(5,5)));
  144. $this->assertFalse($expect_five->test(new DerivedOpaqueContainer(8,8)));
  145. $this->assertFalse($expect_five->test(new DerivedOpaqueContainer(8,5)));
  146. }
  147. }
  148. class DummyReferencedObject{}
  149. class TestOfReference extends UnitTestCase {
  150. function testReference() {
  151. $foo = "foo";
  152. $ref = &$foo;
  153. $not_ref = $foo;
  154. $bar = "bar";
  155. $expect = new ReferenceExpectation($foo);
  156. $this->assertTrue($expect->test($ref));
  157. $this->assertFalse($expect->test($not_ref));
  158. $this->assertFalse($expect->test($bar));
  159. }
  160. }
  161. class TestOfNonIdentity extends UnitTestCase {
  162. function testType() {
  163. $string = new NotIdenticalExpectation("37");
  164. $this->assertTrue($string->test("38"));
  165. $this->assertTrue($string->test(37));
  166. $this->assertFalse($string->test("37"));
  167. }
  168. }
  169. class TestOfPatterns extends UnitTestCase {
  170. function testWanted() {
  171. $pattern = new PatternExpectation('/hello/i');
  172. $this->assertTrue($pattern->test("Hello world"));
  173. $this->assertFalse($pattern->test("Goodbye world"));
  174. }
  175. function testUnwanted() {
  176. $pattern = new NoPatternExpectation('/hello/i');
  177. $this->assertFalse($pattern->test("Hello world"));
  178. $this->assertTrue($pattern->test("Goodbye world"));
  179. }
  180. }
  181. class ExpectedMethodTarget {
  182. function hasThisMethod() {}
  183. }
  184. class TestOfMethodExistence extends UnitTestCase {
  185. function testHasMethod() {
  186. $instance = new ExpectedMethodTarget();
  187. $expectation = new MethodExistsExpectation('hasThisMethod');
  188. $this->assertTrue($expectation->test($instance));
  189. $expectation = new MethodExistsExpectation('doesNotHaveThisMethod');
  190. $this->assertFalse($expectation->test($instance));
  191. }
  192. }
  193. class TestOfIsA extends UnitTestCase {
  194. function testString() {
  195. $expectation = new IsAExpectation('string');
  196. $this->assertTrue($expectation->test('Hello'));
  197. $this->assertFalse($expectation->test(5));
  198. }
  199. function testBoolean() {
  200. $expectation = new IsAExpectation('boolean');
  201. $this->assertTrue($expectation->test(true));
  202. $this->assertFalse($expectation->test(1));
  203. }
  204. function testBool() {
  205. $expectation = new IsAExpectation('bool');
  206. $this->assertTrue($expectation->test(true));
  207. $this->assertFalse($expectation->test(1));
  208. }
  209. function testDouble() {
  210. $expectation = new IsAExpectation('double');
  211. $this->assertTrue($expectation->test(5.0));
  212. $this->assertFalse($expectation->test(5));
  213. }
  214. function testFloat() {
  215. $expectation = new IsAExpectation('float');
  216. $this->assertTrue($expectation->test(5.0));
  217. $this->assertFalse($expectation->test(5));
  218. }
  219. function testReal() {
  220. $expectation = new IsAExpectation('real');
  221. $this->assertTrue($expectation->test(5.0));
  222. $this->assertFalse($expectation->test(5));
  223. }
  224. function testInteger() {
  225. $expectation = new IsAExpectation('integer');
  226. $this->assertTrue($expectation->test(5));
  227. $this->assertFalse($expectation->test(5.0));
  228. }
  229. function testInt() {
  230. $expectation = new IsAExpectation('int');
  231. $this->assertTrue($expectation->test(5));
  232. $this->assertFalse($expectation->test(5.0));
  233. }
  234. }
  235. class TestOfNotA extends UnitTestCase {
  236. function testString() {
  237. $expectation = new NotAExpectation('string');
  238. $this->assertFalse($expectation->test('Hello'));
  239. $this->assertTrue($expectation->test(5));
  240. }
  241. }
  242. ?>