/core/src/test/php/net/xp_framework/unittest/tests/AssertionsTest.class.php

https://github.com/treuter/xp-framework · PHP · 617 lines · 205 code · 66 blank · 346 comment · 0 complexity · fa195cc8487953301522175be3cf2c7f MD5 · raw file

  1. <?php
  2. /* This class is part of the XP framework
  3. *
  4. * $Id$
  5. */
  6. uses('unittest.TestCase', 'lang.types.String', 'lang.types.ArrayList');
  7. /**
  8. * Test assertion methods
  9. *
  10. * @purpose Unit Test
  11. */
  12. class AssertionsTest extends TestCase {
  13. /**
  14. * Test assertTrue()
  15. *
  16. */
  17. #[@test]
  18. public function trueIsTrue() {
  19. $this->assertTrue(TRUE);
  20. }
  21. /**
  22. * Test assertTrue()
  23. *
  24. */
  25. #[@test, @expect('unittest.AssertionFailedError')]
  26. public function falseIsNotTrue() {
  27. $this->assertTrue(FALSE);
  28. }
  29. /**
  30. * Test assertFalse()
  31. *
  32. */
  33. #[@test]
  34. public function falseIsFalse() {
  35. $this->assertFalse(FALSE);
  36. }
  37. /**
  38. * Test assertFalse()
  39. *
  40. */
  41. #[@test, @expect('unittest.AssertionFailedError')]
  42. public function trueIsNotFalse() {
  43. $this->assertFalse(TRUE);
  44. }
  45. /**
  46. * Test assertNull()
  47. *
  48. */
  49. #[@test]
  50. public function NullIsNull() {
  51. $this->assertNull(NULL);
  52. }
  53. /**
  54. * Test assertNull()
  55. *
  56. */
  57. #[@test, @expect('unittest.AssertionFailedError')]
  58. public function falseIsNotNull() {
  59. $this->assertNull(FALSE);
  60. }
  61. /**
  62. * Test assertNull()
  63. *
  64. */
  65. #[@test, @expect('unittest.AssertionFailedError')]
  66. public function zeroIsNotNull() {
  67. $this->assertNull(0);
  68. }
  69. /**
  70. * Test assertNull()
  71. *
  72. */
  73. #[@test, @expect('unittest.AssertionFailedError')]
  74. public function emptyStringIsNotNull() {
  75. $this->assertNull('');
  76. }
  77. /**
  78. * Test assertNull()
  79. *
  80. */
  81. #[@test, @expect('unittest.AssertionFailedError')]
  82. public function emptyArrayIsNotNull() {
  83. $this->assertNull(array());
  84. }
  85. /**
  86. * Test assertEquals() and assertNotEquals() invoke equals() methods
  87. * on objects.
  88. *
  89. */
  90. #[@test]
  91. public function equalsMethodIsInvoked() {
  92. $instance= newinstance('lang.Object', array(), '{
  93. public $equalsInvoked= 0;
  94. public function equals($other) {
  95. $this->equalsInvoked++;
  96. return $other instanceof self && $this->equalsInvoked == $other->equalsInvoked;
  97. }
  98. }');
  99. $this->assertEquals($instance, $instance);
  100. $this->assertNotEquals($instance, NULL);
  101. $this->assertEquals(2, $instance->equalsInvoked);
  102. }
  103. /**
  104. * Test assertEquals() for integers
  105. */
  106. #[@test, @values(array(0, 1, -1, LONG_MAX, LONG_MIN))]
  107. public function integersAreEqual($int) {
  108. $this->assertEquals($int, $int);
  109. }
  110. /**
  111. * Test assertEquals() for strings
  112. */
  113. #[@test, @values(array('', 'Hello', 'äöüß'))]
  114. public function stringsAreEqual($str) {
  115. $this->assertEquals($str, $str);
  116. }
  117. /**
  118. * Test assertEquals() for arrays
  119. */
  120. #[@test, @values(array(
  121. # array(array()),
  122. # array(array(1, 2, 3)),
  123. # array(array(array(1), array(), array(-1, 4), array(new String('baz')))
  124. #))]
  125. public function arraysAreEqual($array) {
  126. $this->assertEquals($array, $array);
  127. }
  128. /**
  129. * Test assertEquals() for hashes
  130. */
  131. #[@test, @values(array(
  132. # array(array()),
  133. # array(array('foo' => 2)),
  134. # array(array(array('bar' => 'baz'), array(), array('bool' => TRUE, 'bar' => new String('baz'))))
  135. #))]
  136. public function hashesAreEqual($hash) {
  137. $this->assertEquals($hash, $hash);
  138. }
  139. /**
  140. * Test hash order is not relevant
  141. *
  142. */
  143. #[@test]
  144. public function hashesOrderNotRelevant() {
  145. $hash= array('&' => '&amp;', '"' => '&quot;');
  146. $this->assertEquals($hash, array_reverse($hash, TRUE), xp::stringOf($hash));
  147. }
  148. /**
  149. * Test assertEquals() for lang.types.String objects
  150. */
  151. #[@test, @values(array(new String(''), new String('Hello'), new String('äöüß', 'iso-8859-1')))]
  152. public function stringObjectsAreEqual($str) {
  153. $this->assertEquals($str, $str);
  154. }
  155. /**
  156. * Test assertEquals() fails for FALSE and NULL
  157. *
  158. */
  159. #[@test, @expect('unittest.AssertionFailedError')]
  160. public function differentNotTypesAreNotEqual() {
  161. $this->assertEquals(FALSE, NULL);
  162. }
  163. /**
  164. * Test assertNotEquals() for integers
  165. */
  166. #[@test, @values(array(-1, 1.0, NULL, FALSE, TRUE, '', array(array()), new String('1'))]
  167. public function integersAreNotEqual($cmp) {
  168. $this->assertNotEquals(1, $cmp);
  169. }
  170. /**
  171. * Test assertNotEquals() for strings
  172. */
  173. #[@test, @values(array(-1, 1.0, NULL, FALSE, TRUE, 1, array(array()), new String('1')))]
  174. public function stringsAreNotEqual($cmp) {
  175. $this->assertNotEquals('', $cmp);
  176. }
  177. /**
  178. * Test assertNotEquals() for arrays
  179. */
  180. #[@test, @values(array(-1, 1.0, NULL, FALSE, TRUE, 1, array(1), new String('1')))]
  181. public function arraysAreNotEqual($cmp) {
  182. $this->assertNotEquals(array(), $cmp);
  183. }
  184. /**
  185. * Test assertNotEquals() throws exceptions on equality
  186. *
  187. */
  188. #[@test, @expect('unittest.AssertionFailedError')]
  189. public function sameIntegersAreEqual() {
  190. $this->assertNotEquals(1, 1);
  191. }
  192. /**
  193. * Test assertInstanceOf()
  194. *
  195. */
  196. #[@test]
  197. public function thisIsAnInstanceOfTestCase() {
  198. $this->assertInstanceOf('unittest.TestCase', $this);
  199. }
  200. /**
  201. * Test assertInstanceOf()
  202. *
  203. */
  204. #[@test]
  205. public function thisIsAnInstanceOfTestCaseClass() {
  206. $this->assertInstanceOf(XPClass::forName('unittest.TestCase'), $this);
  207. }
  208. /**
  209. * Test assertInstanceOf()
  210. *
  211. */
  212. #[@test]
  213. public function thisIsAnInstanceOfObject() {
  214. $this->assertInstanceOf('lang.Object', $this);
  215. }
  216. /**
  217. * Test assertInstanceOf()
  218. *
  219. */
  220. #[@test]
  221. public function objectIsAnInstanceOfObject() {
  222. $this->assertInstanceOf('lang.Object', new Object());
  223. }
  224. /**
  225. * Test assertInstanceOf()
  226. *
  227. */
  228. #[@test, @expect('unittest.AssertionFailedError')]
  229. public function objectIsNotAnInstanceOfString() {
  230. $this->assertInstanceOf('lang.types.String', new Object());
  231. }
  232. /**
  233. * Test assertInstanceOf()
  234. *
  235. */
  236. #[@test, @expect('unittest.AssertionFailedError')]
  237. public function zeroIsNotAnInstanceOfGeneric() {
  238. $this->assertInstanceOf('lang.Generic', 0);
  239. }
  240. /**
  241. * Test assertInstanceOf()
  242. *
  243. */
  244. #[@test, @expect('unittest.AssertionFailedError')]
  245. public function nullIsNotAnInstanceOfGeneric() {
  246. $this->assertInstanceOf('lang.Generic', NULL);
  247. }
  248. /**
  249. * Test assertInstanceOf()
  250. *
  251. */
  252. #[@test, @expect('unittest.AssertionFailedError')]
  253. public function xpNullIsNotAnInstanceOfGeneric() {
  254. $this->assertInstanceOf('lang.Generic', xp::null());
  255. }
  256. /**
  257. * Test assertInstanceOf()
  258. *
  259. */
  260. #[@test, @expect('unittest.AssertionFailedError')]
  261. public function thisIsNotAnInstanceOfString() {
  262. $this->assertInstanceOf('lang.types.String', $this);
  263. }
  264. /**
  265. * Test assertInstanceOf()
  266. *
  267. */
  268. #[@test]
  269. public function thisIsAnInstanceOfGeneric() {
  270. $this->assertInstanceOf('lang.Generic', $this);
  271. }
  272. /**
  273. * Test assertInstanceOf()
  274. *
  275. */
  276. #[@test]
  277. public function zeroIsInstanceOfInt() {
  278. $this->assertInstanceOf('int', 0);
  279. }
  280. /**
  281. * Test assertInstanceOf()
  282. *
  283. */
  284. #[@test, @expect('unittest.AssertionFailedError')]
  285. public function zeroPointZeroIsNotInstanceOfInt() {
  286. $this->assertInstanceOf('int', 0.0);
  287. }
  288. /**
  289. * Test assertInstanceOf()
  290. *
  291. */
  292. #[@test]
  293. public function nullIsInstanceOfVar() {
  294. $this->assertInstanceOf(Type::$VAR, NULL);
  295. }
  296. /**
  297. * Test assertInstanceOf()
  298. *
  299. */
  300. #[@test, @expect('unittest.AssertionFailedError')]
  301. public function nullIsNotInstanceOfVoidType() {
  302. $this->assertInstanceOf(Type::$VOID, NULL);
  303. }
  304. /**
  305. * Test assertInstanceOf()
  306. *
  307. */
  308. #[@test, @expect('unittest.AssertionFailedError')]
  309. public function nullIsNotInstanceOfVoid() {
  310. $this->assertInstanceOf('void', NULL);
  311. }
  312. /**
  313. * Test assertInstanceOf()
  314. *
  315. */
  316. #[@test]
  317. public function emptyArrayIsInstanceOfArray() {
  318. $this->assertInstanceOf('array', array());
  319. }
  320. /**
  321. * Test assertInstanceOf()
  322. *
  323. */
  324. #[@test]
  325. public function intArrayIsInstanceOfArray() {
  326. $this->assertInstanceOf('array', array(1, 2, 3));
  327. }
  328. /**
  329. * Test assertInstanceOf()
  330. *
  331. */
  332. #[@test, @expect('unittest.AssertionFailedError')]
  333. public function hashIsNotInstanceOfArray() {
  334. $this->assertInstanceOf('array', array('color' => 'green'));
  335. }
  336. /**
  337. * Test assertInstanceOf()
  338. *
  339. */
  340. #[@test, @expect('unittest.AssertionFailedError')]
  341. public function nullIsNotInstanceOfArray() {
  342. $this->assertInstanceOf('array', NULL);
  343. }
  344. /**
  345. * Test assertInstanceOf()
  346. *
  347. */
  348. #[@test, @expect('unittest.AssertionFailedError')]
  349. public function arrayListIsNotInstanceOfArray() {
  350. $this->assertInstanceOf('array', new ArrayList(1, 2, 3));
  351. }
  352. /**
  353. * Test assertInstanceOf() for strings
  354. *
  355. */
  356. #[@test, @expect('unittest.AssertionFailedError')]
  357. public function primitiveIsNotAnInstanceOfStringClass() {
  358. $this->assertInstanceOf('string', new String());
  359. }
  360. /**
  361. * Test assertEmpty() for an empty array
  362. *
  363. * @deprecated
  364. */
  365. #[@test]
  366. public function emptyArrayEmpty() {
  367. $this->assertEmpty(array());
  368. }
  369. /**
  370. * Test assertEmpty() for a non-empty array
  371. *
  372. * @deprecated
  373. */
  374. #[@test, @expect('unittest.AssertionFailedError')]
  375. public function nonEmptyArrayEmpty() {
  376. $this->assertEmpty(array(1));
  377. }
  378. /**
  379. * Test assertNotEmpty() for a non-empty array
  380. *
  381. * @deprecated
  382. */
  383. #[@test]
  384. public function nonEmptyArrayNotEmpty() {
  385. $this->assertNotEmpty(array(0));
  386. }
  387. /**
  388. * Test assertNotEmpty() for an empty array
  389. *
  390. * @deprecated
  391. */
  392. #[@test, @expect('unittest.AssertionFailedError')]
  393. public function emptyArrayNotEmpty() {
  394. $this->assertNotEmpty(array());
  395. }
  396. /**
  397. * Test assertClass() for NULLs
  398. *
  399. * @deprecated
  400. */
  401. #[@test, @expect('unittest.AssertionFailedError')]
  402. public function nullIsNotAClass() {
  403. $this->assertClass(NULL, 'lang.Object');
  404. }
  405. /**
  406. * Test assertClass() for strings
  407. *
  408. * @deprecated
  409. */
  410. #[@test, @expect('unittest.AssertionFailedError')]
  411. public function primitiveIsNotOfStringClass() {
  412. $this->assertClass('string', 'lang.types.String');
  413. }
  414. /**
  415. * Test assertClass() for lang.Object
  416. *
  417. * @deprecated
  418. */
  419. #[@test]
  420. public function objectIsOfObjectClass() {
  421. $this->assertClass(new Object(), 'lang.Object');
  422. }
  423. /**
  424. * Test assertClass() for this
  425. *
  426. * @deprecated
  427. */
  428. #[@test, @expect('unittest.AssertionFailedError')]
  429. public function thisIsOfNotObjectClass() {
  430. $this->assertClass($this, 'lang.Object');
  431. }
  432. /**
  433. * Test assertSubclass() for NULLs
  434. *
  435. * @deprecated
  436. */
  437. #[@test, @expect('unittest.AssertionFailedError')]
  438. public function nullIsNotASubClass() {
  439. $this->assertClass(NULL, 'lang.Object');
  440. }
  441. /**
  442. * Test assertSubclass() for lang.Object
  443. *
  444. * @deprecated
  445. */
  446. #[@test]
  447. public function objectIsOfObjectSubclass() {
  448. $this->assertClass(new Object(), 'lang.Object');
  449. }
  450. /**
  451. * Test assertSubclass() for this
  452. *
  453. * @deprecated
  454. */
  455. #[@test]
  456. public function thisIsOfObjectSubclass() {
  457. $this->assertSubClass($this, 'lang.Object');
  458. }
  459. /**
  460. * Test assertSubclass() for strings
  461. *
  462. * @deprecated
  463. */
  464. #[@test, @expect('unittest.AssertionFailedError')]
  465. public function primitiveIsNotOfStringSubclass() {
  466. $this->assertClass('string', 'lang.types.String');
  467. }
  468. /**
  469. * Test assertObject() for this
  470. *
  471. * @deprecated
  472. */
  473. #[@test]
  474. public function thisIsAnObject() {
  475. $this->assertObject($this);
  476. }
  477. /**
  478. * Test assertObject() for NULL
  479. *
  480. * @deprecated
  481. */
  482. #[@test, @expect('unittest.AssertionFailedError')]
  483. public function nullIsNotAnObject() {
  484. $this->assertObject(NULL);
  485. }
  486. /**
  487. * Test assertObject() for primitives
  488. *
  489. * @deprecated
  490. */
  491. #[@test, @expect('unittest.AssertionFailedError')]
  492. public function primitiveIsNotAnObject() {
  493. $this->assertObject('string');
  494. }
  495. /**
  496. * Test assertArray() for an empty array
  497. *
  498. * @deprecated
  499. */
  500. #[@test]
  501. public function emptyArrayIsAnArray() {
  502. $this->assertArray(array());
  503. }
  504. /**
  505. * Test assertArray() for a non-empty array
  506. *
  507. * @deprecated
  508. */
  509. #[@test]
  510. public function arrayIsAnArray() {
  511. $this->assertArray(array(1, 2, 3));
  512. }
  513. /**
  514. * Test assertArray() for an associative array
  515. *
  516. * @deprecated
  517. */
  518. #[@test]
  519. public function hashIsAnArray() {
  520. $this->assertArray(array('key' => 'value'));
  521. }
  522. /**
  523. * Test assertArray() for a lang.types.ArrayList
  524. *
  525. * @deprecated
  526. */
  527. #[@test]
  528. public function arrayListIsAnArray() {
  529. $this->assertArray(new ArrayList());
  530. }
  531. /**
  532. * Test assertArray() for a lang.Object
  533. *
  534. * @deprecated
  535. */
  536. #[@test, @expect('unittest.AssertionFailedError')]
  537. public function objectIsNotAnArray() {
  538. $this->assertArray(new Object());
  539. }
  540. /**
  541. * Test assertArray() for NULL
  542. *
  543. * @deprecated
  544. */
  545. #[@test, @expect('unittest.AssertionFailedError')]
  546. public function nullIsNotAnArray() {
  547. $this->assertArray(NULL);
  548. }
  549. }
  550. ?>