Updated from some -dev modules to alpha, beta or full releases
[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             ]);
60         }
61
62         return array_merge($invalidExpr, [
63             ['list("a" => _) = array("a" => 1)', $errorPhpParserSyntax],
64             ['["a"] = [1]', $errorNonVariableAssign],
65             ['[] = []', $errorEmptyList],
66         ]);
67     }
68
69     /**
70      * @dataProvider validStatements
71      */
72     public function testProcessValidStatement($code)
73     {
74         $stmts = $this->parse($code);
75         $this->traverser->traverse($stmts);
76         $this->assertTrue(true);
77     }
78
79     public function validStatements()
80     {
81         $validExpr = [
82             ['list($a) = array(1)'],
83             ['list($x, $y) = array(1, 2)'],
84         ];
85
86         if (version_compare(PHP_VERSION, '7.1', '>=')) {
87             return array_merge($validExpr, [
88                 ['[$a] = array(1)'],
89                 ['list($b) = [2]'],
90                 ['[$x, $y] = array(1, 2)'],
91                 ['[$a] = [1]'],
92                 ['[$x, $y] = [1, 2]'],
93                 ['["_" => $v] = ["_" => 1]'],
94             ]);
95         }
96
97         return $validExpr;
98     }
99 }