PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/php/PHP/Depend/Code/ASTCastExpressionTest.php

https://github.com/Proudio-Interactive/pdepend
PHP | 514 lines | 163 code | 31 blank | 320 comment | 0 complexity | df2504cdee85710851163b28f90423c9 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of PHP_Depend.
  4. *
  5. * PHP Version 5
  6. *
  7. * Copyright (c) 2008-2011, Manuel Pichler <mapi@pdepend.org>.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * * Neither the name of Manuel Pichler nor the names of his
  23. * contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  30. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  32. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * @category PHP
  40. * @package PHP_Depend
  41. * @subpackage Code
  42. * @author Manuel Pichler <mapi@pdepend.org>
  43. * @copyright 2008-2011 Manuel Pichler. All rights reserved.
  44. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  45. * @version SVN: $Id$
  46. * @link http://www.pdepend.org/
  47. */
  48. require_once dirname(__FILE__) . '/ASTNodeTest.php';
  49. /**
  50. * Test case for the {@link PHP_Depend_Code_ASTCastExpression} class.
  51. *
  52. * @category PHP
  53. * @package PHP_Depend
  54. * @subpackage Code
  55. * @author Manuel Pichler <mapi@pdepend.org>
  56. * @copyright 2008-2011 Manuel Pichler. All rights reserved.
  57. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  58. * @version Release: @package_version@
  59. * @link http://www.pdepend.org/
  60. */
  61. class PHP_Depend_Code_ASTCastExpressionTest extends PHP_Depend_Code_ASTNodeTest
  62. {
  63. /**
  64. * testAcceptInvokesVisitOnGivenVisitor
  65. *
  66. * @return void
  67. * @covers PHP_Depend_Code_ASTNode
  68. * @covers PHP_Depend_Code_ASTCastExpression
  69. * @group pdepend
  70. * @group pdepend::ast
  71. * @group unittest
  72. */
  73. public function testAcceptInvokesVisitOnGivenVisitor()
  74. {
  75. $visitor = $this->getMock('PHP_Depend_Code_ASTVisitorI');
  76. $visitor->expects($this->once())
  77. ->method('__call')
  78. ->with($this->equalTo('visitCastExpression'));
  79. $expr = new PHP_Depend_Code_ASTCastExpression('(array)');
  80. $expr->accept($visitor);
  81. }
  82. /**
  83. * testAcceptReturnsReturnValueOfVisitMethod
  84. *
  85. * @return void
  86. * @covers PHP_Depend_Code_ASTNode
  87. * @covers PHP_Depend_Code_ASTCastExpression
  88. * @group pdepend
  89. * @group pdepend::ast
  90. * @group unittest
  91. */
  92. public function testAcceptReturnsReturnValueOfVisitMethod()
  93. {
  94. $visitor = $this->getMock('PHP_Depend_Code_ASTVisitorI');
  95. $visitor->expects($this->once())
  96. ->method('__call')
  97. ->with($this->equalTo('visitCastExpression'))
  98. ->will($this->returnValue(42));
  99. $expr = new PHP_Depend_Code_ASTCastExpression('(array)');
  100. self::assertEquals(42, $expr->accept($visitor));
  101. }
  102. /**
  103. * testNormalizesWhitespacesInCastExpression
  104. *
  105. * @return void
  106. * @covers PHP_Depend_Code_ASTCastExpression
  107. * @group pdepend
  108. * @group pdepend::ast
  109. * @group unittest
  110. */
  111. public function testNormalizesWhitespacesInCastExpression()
  112. {
  113. $expr = new PHP_Depend_Code_ASTCastExpression("\n( float )\t\r");
  114. $this->assertEquals('(float)', $expr->getImage());
  115. }
  116. /**
  117. * testNormalizesCaseInCastExpressionImage
  118. *
  119. * @return void
  120. * @covers PHP_Depend_Code_ASTCastExpression
  121. * @group pdepend
  122. * @group pdepend::ast
  123. * @group unittest
  124. */
  125. public function testNormalizesCaseInCastExpressionImage()
  126. {
  127. $expr = new PHP_Depend_Code_ASTCastExpression("(DouBlE)");
  128. $this->assertEquals('(double)', $expr->getImage());
  129. }
  130. /**
  131. * testIsBooleanReturnsFalseByDefault
  132. *
  133. * @return void
  134. * @covers PHP_Depend_Code_ASTCastExpression
  135. * @group pdepend
  136. * @group pdepend::ast
  137. * @group unittest
  138. */
  139. public function testIsBooleanReturnsFalseByDefault()
  140. {
  141. $expr = new PHP_Depend_Code_ASTCastExpression('');
  142. $this->assertFalse($expr->isBoolean());
  143. }
  144. /**
  145. * testIsBooleanReturnsTrueForShortExpression
  146. *
  147. * @return void
  148. * @covers PHP_Depend_Code_ASTCastExpression
  149. * @group pdepend
  150. * @group pdepend::ast
  151. * @group unittest
  152. */
  153. public function testIsBooleanReturnsTrueForShortExpression()
  154. {
  155. $expr = new PHP_Depend_Code_ASTCastExpression('(bool)');
  156. $this->assertTrue($expr->isBoolean());
  157. }
  158. /**
  159. * testIsBooleanReturnsTrueForLongExpression
  160. *
  161. * @return void
  162. * @covers PHP_Depend_Code_ASTCastExpression
  163. * @group pdepend
  164. * @group pdepend::ast
  165. * @group unittest
  166. */
  167. public function testIsBooleanReturnsTrueForLongExpression()
  168. {
  169. $expr = new PHP_Depend_Code_ASTCastExpression('(boolean)');
  170. $this->assertTrue($expr->isBoolean());
  171. }
  172. /**
  173. * testIsIntegerReturnsFalseByDefault
  174. *
  175. * @return void
  176. * @covers PHP_Depend_Code_ASTCastExpression
  177. * @group pdepend
  178. * @group pdepend::ast
  179. * @group unittest
  180. */
  181. public function testIsIntegerReturnsFalseByDefault()
  182. {
  183. $expr = new PHP_Depend_Code_ASTCastExpression('');
  184. $this->assertFalse($expr->isInteger());
  185. }
  186. /**
  187. * testIsIntegerReturnsTrueForShortNotation
  188. *
  189. * @return void
  190. * @covers PHP_Depend_Code_ASTCastExpression
  191. * @group pdepend
  192. * @group pdepend::ast
  193. * @group unittest
  194. */
  195. public function testIsIntegerReturnsTrueForShortNotation()
  196. {
  197. $expr = new PHP_Depend_Code_ASTCastExpression('(int)');
  198. $this->assertTrue($expr->isInteger());
  199. }
  200. /**
  201. * testIsIntegerReturnsTrueForLongNotation
  202. *
  203. * @return void
  204. * @covers PHP_Depend_Code_ASTCastExpression
  205. * @group pdepend
  206. * @group pdepend::ast
  207. * @group unittest
  208. */
  209. public function testIsIntegerReturnsTrueForLongNotation()
  210. {
  211. $expr = new PHP_Depend_Code_ASTCastExpression('(integer)');
  212. $this->assertTrue($expr->isInteger());
  213. }
  214. /**
  215. * testIsArrayReturnsFalseByDefault
  216. *
  217. * @return void
  218. * @covers PHP_Depend_Code_ASTCastExpression
  219. * @group pdepend
  220. * @group pdepend::ast
  221. * @group unittest
  222. */
  223. public function testIsArrayReturnsFalseByDefault()
  224. {
  225. $expr = new PHP_Depend_Code_ASTCastExpression('');
  226. $this->assertFalse($expr->isArray());
  227. }
  228. /**
  229. * testIsArrayReturnsTrueForArrayCast
  230. *
  231. * @return void
  232. * @covers PHP_Depend_Code_ASTCastExpression
  233. * @group pdepend
  234. * @group pdepend::ast
  235. * @group unittest
  236. */
  237. public function testIsArrayReturnsTrueForArrayCast()
  238. {
  239. $expr = new PHP_Depend_Code_ASTCastExpression('(array)');
  240. $this->assertTrue($expr->isArray());
  241. }
  242. /**
  243. * testIsFloatReturnsFalseByDefault
  244. *
  245. * @return void
  246. * @covers PHP_Depend_Code_ASTCastExpression
  247. * @group pdepend
  248. * @group pdepend::ast
  249. * @group unittest
  250. */
  251. public function testIsFloatReturnsFalseByDefault()
  252. {
  253. $expr = new PHP_Depend_Code_ASTCastExpression('');
  254. $this->assertFalse($expr->isFloat());
  255. }
  256. /**
  257. * testIsFloatReturnsTrueForRealCast
  258. *
  259. * @return void
  260. * @covers PHP_Depend_Code_ASTCastExpression
  261. * @group pdepend
  262. * @group pdepend::ast
  263. * @group unittest
  264. */
  265. public function testIsFloatReturnsTrueForRealCast()
  266. {
  267. $expr = new PHP_Depend_Code_ASTCastExpression('(real)');
  268. $this->assertTrue($expr->isFloat());
  269. }
  270. /**
  271. * testIsFloatReturnsTrueForFloatCast
  272. *
  273. * @return void
  274. * @covers PHP_Depend_Code_ASTCastExpression
  275. * @group pdepend
  276. * @group pdepend::ast
  277. * @group unittest
  278. */
  279. public function testIsFloatReturnsTrueForFloatCast()
  280. {
  281. $expr = new PHP_Depend_Code_ASTCastExpression('(float)');
  282. $this->assertTrue($expr->isFloat());
  283. }
  284. /**
  285. * testIsFloatReturnsTrueForDoubleCast
  286. *
  287. * @return void
  288. * @covers PHP_Depend_Code_ASTCastExpression
  289. * @group pdepend
  290. * @group pdepend::ast
  291. * @group unittest
  292. */
  293. public function testIsFloatReturnsTrueForDoubleCast()
  294. {
  295. $expr = new PHP_Depend_Code_ASTCastExpression('(double)');
  296. $this->assertTrue($expr->isFloat());
  297. }
  298. /**
  299. * testIsStringReturnsFalseByDefault
  300. *
  301. * @return void
  302. * @covers PHP_Depend_Code_ASTCastExpression
  303. * @group pdepend
  304. * @group pdepend::ast
  305. * @group unittest
  306. */
  307. public function testIsStringReturnsFalseByDefault()
  308. {
  309. $expr = new PHP_Depend_Code_ASTCastExpression('( )');
  310. $this->assertFalse($expr->isString());
  311. }
  312. /**
  313. * testIsStringReturnsTrueForStringCast
  314. *
  315. * @return void
  316. * @covers PHP_Depend_Code_ASTCastExpression
  317. * @group pdepend
  318. * @group pdepend::ast
  319. * @group unittest
  320. */
  321. public function testIsStringReturnsTrueForStringCast()
  322. {
  323. $expr = new PHP_Depend_Code_ASTCastExpression('(string)');
  324. $this->assertTrue($expr->isString());
  325. }
  326. /**
  327. * testIsObjectReturnsFalseByDefault
  328. *
  329. * @return void
  330. * @covers PHP_Depend_Code_ASTCastExpression
  331. * @group pdepend
  332. * @group pdepend::ast
  333. * @group unittest
  334. */
  335. public function testIsObjectReturnsFalseByDefault()
  336. {
  337. $expr = new PHP_Depend_Code_ASTCastExpression('( obj )');
  338. $this->assertFalse($expr->isObject());
  339. }
  340. /**
  341. * testIsObjectReturnsTrueForObjectCast
  342. *
  343. * @return void
  344. * @covers PHP_Depend_Code_ASTCastExpression
  345. * @group pdepend
  346. * @group pdepend::ast
  347. * @group unittest
  348. */
  349. public function testIsObjectReturnsTrueForObjectCast()
  350. {
  351. $expr = new PHP_Depend_Code_ASTCastExpression('(object)');
  352. $this->assertTrue($expr->isObject());
  353. }
  354. /**
  355. * testIsUnsetReturnsFalseByDefault
  356. *
  357. * @return void
  358. * @covers PHP_Depend_Code_ASTCastExpression
  359. * @group pdepend
  360. * @group pdepend::ast
  361. * @group unittest
  362. */
  363. public function testIsUnsetReturnsFalseByDefault()
  364. {
  365. $expr = new PHP_Depend_Code_ASTCastExpression('(nu)');
  366. $this->assertFalse($expr->isUnset());
  367. }
  368. /**
  369. * testIsUnsetReturnsTrueForUnsetCast
  370. *
  371. * @return void
  372. * @covers PHP_Depend_Code_ASTCastExpression
  373. * @group pdepend
  374. * @group pdepend::ast
  375. * @group unittest
  376. */
  377. public function testIsUnsetReturnsTrueForUnsetCast()
  378. {
  379. $expr = new PHP_Depend_Code_ASTCastExpression('(unset)');
  380. $this->assertTrue($expr->isUnset());
  381. }
  382. /**
  383. * testParserHandlesNestedCastExpressions
  384. *
  385. * @return void
  386. * @covers PHP_Depend_Parser
  387. * @covers PHP_Depend_Builder_Default
  388. * @covers PHP_Depend_Code_ASTCastExpression
  389. * @group pdepend
  390. * @group pdepend::ast
  391. * @group unittest
  392. */
  393. public function testParserHandlesNestedCastExpressions()
  394. {
  395. $expr = $this->_getFirstCastExpressionInFunction(__METHOD__);
  396. $this->assertGraphEquals(
  397. $expr,
  398. array(
  399. PHP_Depend_Code_ASTCastExpression::CLAZZ,
  400. PHP_Depend_Code_ASTCastExpression::CLAZZ,
  401. PHP_Depend_Code_ASTCastExpression::CLAZZ,
  402. PHP_Depend_Code_ASTVariable::CLAZZ
  403. )
  404. );
  405. }
  406. /**
  407. * testCastExpressionHasExpectedStartLine
  408. *
  409. * @return void
  410. * @covers PHP_Depend_Parser
  411. * @covers PHP_Depend_Builder_Default
  412. * @covers PHP_Depend_Code_ASTCastExpression
  413. * @group pdepend
  414. * @group pdepend::ast
  415. * @group unittest
  416. */
  417. public function testCastExpressionHasExpectedStartLine()
  418. {
  419. $expr = $this->_getFirstCastExpressionInFunction(__METHOD__);
  420. $this->assertEquals(4, $expr->getStartLine());
  421. }
  422. /**
  423. * testCastExpressionHasExpectedStartColumn
  424. *
  425. * @return void
  426. * @covers PHP_Depend_Parser
  427. * @covers PHP_Depend_Builder_Default
  428. * @covers PHP_Depend_Code_ASTCastExpression
  429. * @group pdepend
  430. * @group pdepend::ast
  431. * @group unittest
  432. */
  433. public function testCastExpressionHasExpectedStartColumn()
  434. {
  435. $expr = $this->_getFirstCastExpressionInFunction(__METHOD__);
  436. $this->assertEquals(12, $expr->getStartColumn());
  437. }
  438. /**
  439. * testCastExpressionHasExpectedEndLine
  440. *
  441. * @return void
  442. * @covers PHP_Depend_Parser
  443. * @covers PHP_Depend_Builder_Default
  444. * @covers PHP_Depend_Code_ASTCastExpression
  445. * @group pdepend
  446. * @group pdepend::ast
  447. * @group unittest
  448. */
  449. public function testCastExpressionHasExpectedEndLine()
  450. {
  451. $expr = $this->_getFirstCastExpressionInFunction(__METHOD__);
  452. $this->assertEquals(4, $expr->getEndLine());
  453. }
  454. /**
  455. * testCastExpressionHasExpectedEndColumn
  456. *
  457. * @return void
  458. * @covers PHP_Depend_Parser
  459. * @covers PHP_Depend_Builder_Default
  460. * @covers PHP_Depend_Code_ASTCastExpression
  461. * @group pdepend
  462. * @group pdepend::ast
  463. * @group unittest
  464. */
  465. public function testCastExpressionHasExpectedEndColumn()
  466. {
  467. $expr = $this->_getFirstCastExpressionInFunction(__METHOD__);
  468. $this->assertEquals(26, $expr->getEndColumn());
  469. }
  470. /**
  471. * Returns a node instance for the currently executed test case.
  472. *
  473. * @param string $testCase Name of the calling test case.
  474. *
  475. * @return PHP_Depend_Code_ASTCastExpression
  476. */
  477. private function _getFirstCastExpressionInFunction($testCase)
  478. {
  479. return $this->getFirstNodeOfTypeInFunction(
  480. $testCase, PHP_Depend_Code_ASTCastExpression::CLAZZ
  481. );
  482. }
  483. }