4 * This file is part of Psy Shell.
6 * (c) 2012-2018 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\Test\CodeCleaner;
14 use Psy\CodeCleaner\ListPass;
16 class ListPassTest extends CodeCleanerTestCase
18 public function setUp()
20 $this->setPass(new ListPass());
24 * @dataProvider invalidStatements
25 * @expectedException \Psy\Exception\ParseErrorException
27 public function testProcessInvalidStatement($code, $expectedMessage)
29 if (\method_exists($this, 'setExpectedException')) {
30 $this->setExpectedException('Psy\Exception\ParseErrorException', $expectedMessage);
32 $this->expectExceptionMessage($expectedMessage);
35 $stmts = $this->parse($code);
36 $this->traverser->traverse($stmts);
39 public function invalidStatements()
41 // Not typo. It is ambiguous whether "Syntax" or "syntax".
42 $errorShortListAssign = "yntax error, unexpected '='";
43 $errorEmptyList = 'Cannot use empty list';
44 $errorAssocListAssign = 'Syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting \',\' or \')\'';
45 $errorNonVariableAssign = 'Assignments can only happen to writable values';
46 $errorPhpParserSyntax = 'PHP Parse error: Syntax error, unexpected';
49 ['list() = array()', $errorEmptyList],
50 ['list("a") = array(1)', $errorPhpParserSyntax],
53 if (\version_compare(PHP_VERSION, '7.1', '<')) {
54 return \array_merge($invalidExpr, [
55 ['list("a" => _) = array("a" => 1)', $errorPhpParserSyntax],
56 ['[] = []', $errorShortListAssign],
57 ['[$a] = [1]', $errorShortListAssign],
58 ['list("a" => $a) = array("a" => 1)', $errorAssocListAssign],
59 ['[$a[0], $a[1]] = [1, 2]', $errorShortListAssign],
60 ['[$a->b, $a->c] = [1, 2]', $errorShortListAssign],
64 return \array_merge($invalidExpr, [
65 ['list("a" => _) = array("a" => 1)', $errorPhpParserSyntax],
66 ['["a"] = [1]', $errorNonVariableAssign],
67 ['[] = []', $errorEmptyList],
68 ['[,] = [1,2]', $errorEmptyList],
69 ['[,,] = [1,2,3]', $errorEmptyList],
74 * @dataProvider validStatements
76 public function testProcessValidStatement($code)
78 $stmts = $this->parse($code);
79 $this->traverser->traverse($stmts);
80 $this->assertTrue(true);
83 public function validStatements()
86 ['list($a) = array(1)'],
87 ['list($x, $y) = array(1, 2)'],
90 if (\version_compare(PHP_VERSION, '7.1', '>=')) {
91 return \array_merge($validExpr, [
94 ['[$x, $y] = array(1, 2)'],
96 ['[$x, $y] = [1, 2]'],
97 ['["_" => $v] = ["_" => 1]'],
100 ['[$a,,$c] = [1,2,3]'],
101 ['[$a,,,] = [1,2,3]'],
102 ['[$a[0], $a[1]] = [1, 2]'],
103 ['[$a[0][0][0], $a[0][0][1]] = [1, 2]'],
104 ['[$a->b, $a->c] = [1, 2]'],
105 ['[$a->b[0], $a->c[1]] = [1, 2]'],
106 ['[$a[0]->b[0], $a[0]->c[1]] = [1, 2]'],
107 ['[$a[$b->c + $b->d]] = [1]'],
108 ['[$a->c()->d, $a->c()->e] = [1, 2]'],
109 ['[x()->a, x()->b] = [1, 2]'],