comparison vendor/psy/psysh/test/Psy/Test/CodeCleaner/LoopContextPassTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 /*
4 * This file is part of Psy Shell.
5 *
6 * (c) 2012-2017 Justin Hileman
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Psy\Test\CodeCleaner;
13
14 use PhpParser\NodeTraverser;
15 use Psy\CodeCleaner\LoopContextPass;
16
17 class LoopContextPassTest extends CodeCleanerTestCase
18 {
19 public function setUp()
20 {
21 $this->pass = new LoopContextPass();
22 $this->traverser = new NodeTraverser();
23 $this->traverser->addVisitor($this->pass);
24 }
25
26 /**
27 * @dataProvider invalidStatements
28 * @expectedException \Psy\Exception\FatalErrorException
29 */
30 public function testProcessStatementFails($code)
31 {
32 $stmts = $this->parse($code);
33 $this->traverser->traverse($stmts);
34 }
35
36 public function invalidStatements()
37 {
38 return array(
39 array('continue'),
40 array('break'),
41 array('if (true) { continue; }'),
42 array('if (true) { break; }'),
43 array('if (false) { continue; }'),
44 array('if (false) { break; }'),
45 array('function foo() { break; }'),
46 array('function foo() { continue; }'),
47
48 // actually enforce break/continue depth argument
49 array('do { break 2; } while (true)'),
50 array('do { continue 2; } while (true)'),
51 array('for ($a; $b; $c) { break 2; }'),
52 array('for ($a; $b; $c) { continue 2; }'),
53 array('foreach ($a as $b) { break 2; }'),
54 array('foreach ($a as $b) { continue 2; }'),
55 array('switch (true) { default: break 2; }'),
56 array('switch (true) { default: continue 2; }'),
57 array('while (true) { break 2; }'),
58 array('while (true) { continue 2; }'),
59
60 // invalid in 5.4+ because they're floats
61 // ... in 5.3 because the number is too big
62 array('while (true) { break 2.0; }'),
63 array('while (true) { continue 2.0; }'),
64
65 // and once with nested loops, just for good measure
66 array('while (true) { while (true) { break 3; } }'),
67 array('while (true) { while (true) { continue 3; } }'),
68 );
69 }
70
71 /**
72 * @dataProvider invalidPHP54Statements
73 * @expectedException \Psy\Exception\FatalErrorException
74 */
75 public function testPHP54ProcessStatementFails($code)
76 {
77 if (version_compare(PHP_VERSION, '5.4.0', '<')) {
78 $this->markTestSkipped();
79 }
80
81 $stmts = $this->parse($code);
82 $this->traverser->traverse($stmts);
83 }
84
85 public function invalidPHP54Statements()
86 {
87 return array(
88 // In PHP 5.4+, only positive literal integers are allowed
89 array('while (true) { break $n; }'),
90 array('while (true) { continue $n; }'),
91 array('while (true) { break N; }'),
92 array('while (true) { continue N; }'),
93 array('while (true) { break 0; }'),
94 array('while (true) { continue 0; }'),
95 array('while (true) { break -1; }'),
96 array('while (true) { continue -1; }'),
97 array('while (true) { break 1.0; }'),
98 array('while (true) { continue 1.0; }'),
99 );
100 }
101
102 /**
103 * @dataProvider validStatements
104 */
105 public function testProcessStatementPasses($code)
106 {
107 $stmts = $this->parse($code);
108 $this->traverser->traverse($stmts);
109 }
110
111 public function validStatements()
112 {
113 return array(
114 array('do { break; } while (true)'),
115 array('do { continue; } while (true)'),
116 array('for ($a; $b; $c) { break; }'),
117 array('for ($a; $b; $c) { continue; }'),
118 array('foreach ($a as $b) { break; }'),
119 array('foreach ($a as $b) { continue; }'),
120 array('switch (true) { default: break; }'),
121 array('switch (true) { default: continue; }'),
122 array('while (true) { break; }'),
123 array('while (true) { continue; }'),
124
125 // `break 1` is redundant, but not invalid
126 array('while (true) { break 1; }'),
127 array('while (true) { continue 1; }'),
128
129 // and once with nested loops just for good measure
130 array('while (true) { while (true) { break 2; } }'),
131 array('while (true) { while (true) { continue 2; } }'),
132 );
133 }
134 }