b7a360d69070cc782e7c5855636180a45515f746
[yaffs-website] / vendor / psy / psysh / test / CodeCleaner / ListPassTest.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2018 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 Psy\CodeCleaner\ListPass;
15
16 class ListPassTest extends CodeCleanerTestCase
17 {
18     public function setUp()
19     {
20         $this->setPass(new ListPass());
21     }
22
23     /**
24      * @dataProvider invalidStatements
25      * @expectedException \Psy\Exception\ParseErrorException
26      */
27     public function testProcessInvalidStatement($code, $expectedMessage)
28     {
29         if (\method_exists($this, 'setExpectedException')) {
30             $this->setExpectedException('Psy\Exception\ParseErrorException', $expectedMessage);
31         } else {
32             $this->expectExceptionMessage($expectedMessage);
33         }
34
35         $stmts = $this->parse($code);
36         $this->traverser->traverse($stmts);
37     }
38
39     public function invalidStatements()
40     {
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';
47
48         $invalidExpr = [
49             ['list() = array()', $errorEmptyList],
50             ['list("a") = array(1)', $errorPhpParserSyntax],
51         ];
52
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],
61             ]);
62         }
63
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],
70         ]);
71     }
72
73     /**
74      * @dataProvider validStatements
75      */
76     public function testProcessValidStatement($code)
77     {
78         $stmts = $this->parse($code);
79         $this->traverser->traverse($stmts);
80         $this->assertTrue(true);
81     }
82
83     public function validStatements()
84     {
85         $validExpr = [
86             ['list($a) = array(1)'],
87             ['list($x, $y) = array(1, 2)'],
88         ];
89
90         if (\version_compare(PHP_VERSION, '7.1', '>=')) {
91             return \array_merge($validExpr, [
92                 ['[$a] = array(1)'],
93                 ['list($b) = [2]'],
94                 ['[$x, $y] = array(1, 2)'],
95                 ['[$a] = [1]'],
96                 ['[$x, $y] = [1, 2]'],
97                 ['["_" => $v] = ["_" => 1]'],
98                 ['[$a,] = [1,2,3]'],
99                 ['[,$b] = [1,2,3]'],
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]'],
110             ]);
111         }
112
113         return $validExpr;
114     }
115 }