暂无描述

LoopContextPassTest.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2018 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Psy\Test\CodeCleaner;
  11. use Psy\CodeCleaner\LoopContextPass;
  12. class LoopContextPassTest extends CodeCleanerTestCase
  13. {
  14. public function setUp()
  15. {
  16. $this->setPass(new LoopContextPass());
  17. }
  18. /**
  19. * @dataProvider invalidStatements
  20. * @expectedException \Psy\Exception\FatalErrorException
  21. */
  22. public function testProcessStatementFails($code)
  23. {
  24. $this->parseAndTraverse($code);
  25. }
  26. public function invalidStatements()
  27. {
  28. return [
  29. ['continue'],
  30. ['break'],
  31. ['if (true) { continue; }'],
  32. ['if (true) { break; }'],
  33. ['if (false) { continue; }'],
  34. ['if (false) { break; }'],
  35. ['function foo() { break; }'],
  36. ['function foo() { continue; }'],
  37. // actually enforce break/continue depth argument
  38. ['do { break 2; } while (true)'],
  39. ['do { continue 2; } while (true)'],
  40. ['for ($a; $b; $c) { break 2; }'],
  41. ['for ($a; $b; $c) { continue 2; }'],
  42. ['foreach ($a as $b) { break 2; }'],
  43. ['foreach ($a as $b) { continue 2; }'],
  44. ['switch (true) { default: break 2; }'],
  45. ['switch (true) { default: continue 2; }'],
  46. ['while (true) { break 2; }'],
  47. ['while (true) { continue 2; }'],
  48. // In PHP 5.4+, only positive literal integers are allowed
  49. ['while (true) { break $n; }'],
  50. ['while (true) { continue $n; }'],
  51. ['while (true) { break N; }'],
  52. ['while (true) { continue N; }'],
  53. ['while (true) { break 0; }'],
  54. ['while (true) { continue 0; }'],
  55. ['while (true) { break -1; }'],
  56. ['while (true) { continue -1; }'],
  57. ['while (true) { break 1.0; }'],
  58. ['while (true) { continue 1.0; }'],
  59. ['while (true) { break 2.0; }'],
  60. ['while (true) { continue 2.0; }'],
  61. // and once with nested loops, just for good measure
  62. ['while (true) { while (true) { break 3; } }'],
  63. ['while (true) { while (true) { continue 3; } }'],
  64. ];
  65. }
  66. /**
  67. * @dataProvider validStatements
  68. */
  69. public function testProcessStatementPasses($code)
  70. {
  71. $this->parseAndTraverse($code);
  72. $this->assertTrue(true);
  73. }
  74. public function validStatements()
  75. {
  76. return [
  77. ['do { break; } while (true)'],
  78. ['do { continue; } while (true)'],
  79. ['for ($a; $b; $c) { break; }'],
  80. ['for ($a; $b; $c) { continue; }'],
  81. ['foreach ($a as $b) { break; }'],
  82. ['foreach ($a as $b) { continue; }'],
  83. ['switch (true) { default: break; }'],
  84. ['switch (true) { default: continue; }'],
  85. ['while (true) { break; }'],
  86. ['while (true) { continue; }'],
  87. // `break 1` is redundant, but not invalid
  88. ['while (true) { break 1; }'],
  89. ['while (true) { continue 1; }'],
  90. // and once with nested loops just for good measure
  91. ['while (true) { while (true) { break 2; } }'],
  92. ['while (true) { while (true) { continue 2; } }'],
  93. ];
  94. }
  95. }