PageRenderTime 39ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/pgfouine-1.2/tests/simpletest/test/visual_test.php

#
PHP | 386 lines | 317 code | 59 blank | 10 comment | 6 complexity | 87e9be1d8d9301627c99b519284d8181 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0
  1. <?php
  2. // $Id: visual_test.php,v 1.1 2005/11/09 23:41:18 gsmet Exp $
  3. // NOTE:
  4. // Some of these tests are designed to fail! Do not be alarmed.
  5. // ----------------
  6. // The following tests are a bit hacky. Whilst Kent Beck tried to
  7. // build a unit tester with a unit tester I am not that brave.
  8. // Instead I have just hacked together odd test scripts until
  9. // I have enough of a tester to procede more formally.
  10. //
  11. // The proper tests start in all_tests.php
  12. require_once('../unit_tester.php');
  13. require_once('../shell_tester.php');
  14. require_once('../mock_objects.php');
  15. require_once('../reporter.php');
  16. require_once('../xml.php');
  17. class TestDisplayClass {
  18. var $_a;
  19. function TestDisplayClass($a) {
  20. $this->_a = $a;
  21. }
  22. }
  23. class TestOfUnitTestCaseOutput extends UnitTestCase {
  24. function testOfResults() {
  25. $this->pass('Pass');
  26. $this->fail('Fail'); // Fail.
  27. }
  28. function testTrue() {
  29. $this->assertTrue(true);
  30. $this->assertTrue(false); // Fail.
  31. }
  32. function testFalse() {
  33. $this->assertFalse(true); // Fail.
  34. $this->assertFalse(false);
  35. }
  36. function testExpectation() {
  37. $expectation = &new EqualExpectation(25, 'My expectation message: %s');
  38. $this->assertExpectation($expectation, 25, 'My assert message : %s');
  39. $this->assertExpectation($expectation, 24, 'My assert message : %s'); // Fail.
  40. }
  41. function testNull() {
  42. $this->assertNull(null, "%s -> Pass");
  43. $this->assertNull(false, "%s -> Fail"); // Fail.
  44. $this->assertNotNull(null, "%s -> Fail"); // Fail.
  45. $this->assertNotNull(false, "%s -> Pass");
  46. }
  47. function testType() {
  48. $this->assertIsA("hello", "string", "%s -> Pass");
  49. $this->assertIsA(14, "string", "%s -> Fail"); // Fail.
  50. $this->assertIsA($this, "TestOfUnitTestCaseOutput", "%s -> Pass");
  51. $this->assertIsA($this, "UnitTestCase", "%s -> Pass");
  52. $this->assertIsA(14, "TestOfUnitTestCaseOutput", "%s -> Fail"); // Fail.
  53. $this->assertIsA($this, "TestReporter", "%s -> Fail"); // Fail.
  54. }
  55. function testTypeEquality() {
  56. $this->assertEqual("0", 0, "%s -> Pass");
  57. $this->assertNotEqual("0", 0, "%s -> Fail"); // Fail.
  58. }
  59. function testNullEquality() {
  60. $this->assertEqual(null, 1, "%s -> Fail"); // Fail.
  61. $this->assertNotEqual(null, 1, "%s -> Pass");
  62. $this->assertEqual(1, null, "%s -> Fail"); // Fail.
  63. $this->assertNotEqual(1, null, "%s -> Pass");
  64. }
  65. function testIntegerEquality() {
  66. $this->assertEqual(1, 2, "%s -> Fail"); // Fail.
  67. $this->assertNotEqual(1, 2, "%s -> Pass");
  68. }
  69. function testStringEquality() {
  70. $this->assertEqual("a", "a", "%s -> Pass");
  71. $this->assertNotEqual("a", "a", "%s -> Fail"); // Fail.
  72. $this->assertEqual("aa", "ab", "%s -> Fail"); // Fail.
  73. $this->assertNotEqual("aa", "ab", "%s -> Pass");
  74. }
  75. function testHashEquality() {
  76. $this->assertEqual(array("a" => "A", "b" => "B"), array("b" => "B", "a" => "A"), "%s -> Pass");
  77. $this->assertEqual(array("a" => "A", "b" => "B"), array("b" => "B", "a" => "Z"), "%s -> Pass");
  78. }
  79. function testStringIdentity() {
  80. $a = "fred";
  81. $b = $a;
  82. $this->assertIdentical($a, $b, "%s -> Pass");
  83. $this->assertNotIdentical($a, $b, "%s -> Fail"); // Fail.
  84. }
  85. function testTypeIdentity() {
  86. $a = "0";
  87. $b = 0;
  88. $this->assertIdentical($a, $b, "%s -> Fail"); // Fail.
  89. $this->assertNotIdentical($a, $b, "%s -> Pass");
  90. }
  91. function testNullIdentity() {
  92. $this->assertIdentical(null, 1, "%s -> Fail"); // Fail.
  93. $this->assertNotIdentical(null, 1, "%s -> Pass");
  94. $this->assertIdentical(1, null, "%s -> Fail"); // Fail.
  95. $this->assertNotIdentical(1, null, "%s -> Pass");
  96. }
  97. function testHashIdentity() {
  98. $this->assertIdentical(array("a" => "A", "b" => "B"), array("b" => "B", "a" => "A"), "%s -> fail"); // Fail.
  99. }
  100. function testObjectEquality() {
  101. $this->assertEqual(new TestDisplayClass(4), new TestDisplayClass(4), "%s -> Pass");
  102. $this->assertNotEqual(new TestDisplayClass(4), new TestDisplayClass(4), "%s -> Fail"); // Fail.
  103. $this->assertEqual(new TestDisplayClass(4), new TestDisplayClass(5), "%s -> Fail"); // Fail.
  104. $this->assertNotEqual(new TestDisplayClass(4), new TestDisplayClass(5), "%s -> Pass");
  105. }
  106. function testObjectIndentity() {
  107. $this->assertIdentical(new TestDisplayClass(false), new TestDisplayClass(false), "%s -> Pass");
  108. $this->assertNotIdentical(new TestDisplayClass(false), new TestDisplayClass(false), "%s -> Fail"); // Fail.
  109. $this->assertIdentical(new TestDisplayClass(false), new TestDisplayClass(0), "%s -> Fail"); // Fail.
  110. $this->assertNotIdentical(new TestDisplayClass(false), new TestDisplayClass(0), "%s -> Pass");
  111. }
  112. function testReference() {
  113. $a = "fred";
  114. $b = &$a;
  115. $this->assertReference($a, $b, "%s -> Pass");
  116. $this->assertCopy($a, $b, "%s -> Fail"); // Fail.
  117. $c = "Hello";
  118. $this->assertReference($a, $c, "%s -> Fail"); // Fail.
  119. $this->assertCopy($a, $c, "%s -> Pass");
  120. }
  121. function testPatterns() {
  122. $this->assertWantedPattern('/hello/i', "Hello there", "%s -> Pass");
  123. $this->assertNoUnwantedPattern('/hello/', "Hello there", "%s -> Pass");
  124. $this->assertWantedPattern('/hello/', "Hello there", "%s -> Fail"); // Fail.
  125. $this->assertNoUnwantedPattern('/hello/i', "Hello there", "%s -> Fail"); // Fail.
  126. }
  127. function testLongStrings() {
  128. $text = "";
  129. for ($i = 0; $i < 10; $i++) {
  130. $text .= "0123456789";
  131. }
  132. $this->assertEqual($text, $text);
  133. $this->assertEqual($text . $text, $text . "a" . $text); // Fail.
  134. }
  135. function testErrorDisplay() {
  136. trigger_error('Default'); // Exception.
  137. trigger_error('Error', E_USER_ERROR); // Exception.
  138. trigger_error('Warning', E_USER_WARNING); // Exception.
  139. trigger_error('Notice', E_USER_NOTICE); // Exception.
  140. }
  141. function testErrorTrap() {
  142. $this->assertNoErrors("%s -> Pass");
  143. $this->assertError(); // Fail.
  144. trigger_error('Error 1');
  145. $this->assertNoErrors("%s -> Fail"); // Fail.
  146. $this->assertError();
  147. $this->assertNoErrors("%s -> Pass at end");
  148. }
  149. function testErrorText() {
  150. trigger_error('Error 2');
  151. $this->assertError('Error 2', "%s -> Pass");
  152. trigger_error('Error 3');
  153. $this->assertError('Error 2', "%s -> Fail"); // Fail.
  154. }
  155. function testErrorPatterns() {
  156. trigger_error('Error 2');
  157. $this->assertErrorPattern('/Error 2/', "%s -> Pass");
  158. trigger_error('Error 3');
  159. $this->assertErrorPattern('/Error 2/', "%s -> Fail"); // Fail.
  160. }
  161. function testDumping() {
  162. $this->dump(array("Hello"), "Displaying a variable");
  163. }
  164. function testSignal() {
  165. $fred = "signal as a string";
  166. $this->signal("Signal", $fred); // Signal.
  167. }
  168. }
  169. class Dummy {
  170. function Dummy() {
  171. }
  172. function a() {
  173. }
  174. }
  175. Mock::generate('Dummy');
  176. class TestOfMockObjectsOutput extends UnitTestCase {
  177. function testCallCounts() {
  178. $dummy = &new MockDummy($this);
  179. $dummy->expectCallCount('a', 1, 'My message: %s');
  180. $dummy->a();
  181. $dummy->tally();
  182. $dummy->a();
  183. $dummy->tally();
  184. }
  185. function testMinimumCallCounts() {
  186. $dummy = &new MockDummy($this);
  187. $dummy->expectMinimumCallCount('a', 2, 'My message: %s');
  188. $dummy->a();
  189. $dummy->tally();
  190. $dummy->a();
  191. $dummy->tally();
  192. }
  193. function testEmptyMatching() {
  194. $dummy = &new MockDummy($this);
  195. $dummy->expectArguments('a', array());
  196. $dummy->a();
  197. $dummy->a(null); // Fail.
  198. }
  199. function testEmptyMatchingWithCustomMessage() {
  200. $dummy = &new MockDummy($this);
  201. $dummy->expectArguments('a', array(), 'My expectation message: %s');
  202. $dummy->a();
  203. $dummy->a(null); // Fail.
  204. }
  205. function testNullMatching() {
  206. $dummy = &new MockDummy($this);
  207. $dummy->expectArguments('a', array(null));
  208. $dummy->a(null);
  209. $dummy->a(); // Fail.
  210. }
  211. function testBooleanMatching() {
  212. $dummy = &new MockDummy($this);
  213. $dummy->expectArguments('a', array(true, false));
  214. $dummy->a(true, false);
  215. $dummy->a(true, true); // Fail.
  216. }
  217. function testIntegerMatching() {
  218. $dummy = &new MockDummy($this);
  219. $dummy->expectArguments('a', array(32, 33));
  220. $dummy->a(32, 33);
  221. $dummy->a(32, 34); // Fail.
  222. }
  223. function testFloatMatching() {
  224. $dummy = &new MockDummy($this);
  225. $dummy->expectArguments('a', array(3.2, 3.3));
  226. $dummy->a(3.2, 3.3);
  227. $dummy->a(3.2, 3.4); // Fail.
  228. }
  229. function testStringMatching() {
  230. $dummy = &new MockDummy($this);
  231. $dummy->expectArguments('a', array('32', '33'));
  232. $dummy->a('32', '33');
  233. $dummy->a('32', '34'); // Fail.
  234. }
  235. function testEmptyMatchingWithCustomExpectationMessage() {
  236. $dummy = &new MockDummy($this);
  237. $dummy->expectArguments(
  238. 'a',
  239. array(new EqualExpectation('A', 'My part expectation message: %s')),
  240. 'My expectation message: %s');
  241. $dummy->a('A');
  242. $dummy->a('B'); // Fail.
  243. }
  244. function testArrayMatching() {
  245. $dummy = &new MockDummy($this);
  246. $dummy->expectArguments('a', array(array(32), array(33)));
  247. $dummy->a(array(32), array(33));
  248. $dummy->a(array(32), array('33')); // Fail.
  249. }
  250. function testObjectMatching() {
  251. $a = new Dummy();
  252. $a->a = 'a';
  253. $b = new Dummy();
  254. $b->b = 'b';
  255. $dummy = &new MockDummy($this);
  256. $dummy->expectArguments('a', array($a, $b));
  257. $dummy->a($a, $b);
  258. $dummy->a($a, $a); // Fail.
  259. }
  260. function testBigList() {
  261. $dummy = &new MockDummy($this);
  262. $dummy->expectArguments('a', array(false, 0, 1, 1.0));
  263. $dummy->a(false, 0, 1, 1.0);
  264. $dummy->a(true, false, 2, 2.0); // Fail.
  265. }
  266. }
  267. class TestOfPastBugs extends UnitTestCase {
  268. function testMixedTypes() {
  269. $this->assertEqual(array(), null, "%s -> Pass");
  270. $this->assertIdentical(array(), null, "%s -> Fail"); // Fail.
  271. }
  272. function testMockWildcards() {
  273. $dummy = &new MockDummy($this);
  274. $dummy->expectArguments('a', array('*', array(33)));
  275. $dummy->a(array(32), array(33));
  276. $dummy->a(array(32), array('33')); // Fail.
  277. }
  278. }
  279. class TestOfVisualShell extends ShellTestCase {
  280. function testDump() {
  281. $this->execute('ls');
  282. $this->dumpOutput();
  283. $this->execute('dir');
  284. $this->dumpOutput();
  285. }
  286. function testDumpOfList() {
  287. $this->execute('ls');
  288. $this->dump($this->getOutputAsList());
  289. }
  290. }
  291. class AllOutputReporter extends HtmlReporter {
  292. function _getCss() {
  293. return parent::_getCss() . ' .pass { color: darkgreen; }';
  294. }
  295. function paintPass($message) {
  296. parent::paintPass($message);
  297. print "<span class=\"pass\">Pass</span>: ";
  298. $breadcrumb = $this->getTestList();
  299. array_shift($breadcrumb);
  300. print implode(" -&gt; ", $breadcrumb);
  301. print " -&gt; " . htmlentities($message) . "<br />\n";
  302. }
  303. function paintSignal($type, &$payload) {
  304. print "<span class=\"fail\">$type</span>: ";
  305. $breadcrumb = $this->getTestList();
  306. array_shift($breadcrumb);
  307. print implode(" -&gt; ", $breadcrumb);
  308. print " -&gt; " . htmlentities(serialize($payload)) . "<br />\n";
  309. }
  310. }
  311. $test = &new GroupTest("Visual test with 49 passes, 49 fails and 4 exceptions");
  312. $test->addTestCase(new TestOfUnitTestCaseOutput());
  313. $test->addTestCase(new TestOfMockObjectsOutput());
  314. $test->addTestCase(new TestOfPastBugs());
  315. $test->addTestCase(new TestOfVisualShell());
  316. if (isset($_GET['xml']) || in_array('xml', (isset($argv) ? $argv : array()))) {
  317. $reporter = &new XmlReporter();
  318. } elseif(SimpleReporter::inCli()) {
  319. $reporter = &new TextReporter();
  320. } else {
  321. $reporter = &new AllOutputReporter();
  322. }
  323. if (isset($_GET['dry']) || in_array('dry', (isset($argv) ? $argv : array()))) {
  324. $reporter->makeDry();
  325. }
  326. exit ($test->run($reporter) ? 0 : 1);
  327. ?>