comparison vendor/psy/psysh/test/CodeCleaner/ListPassTest.php @ 16:c2387f117808

Routine composer update
author Chris Cannam
date Tue, 10 Jul 2018 15:07:59 +0100
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
15:e200cb7efeb3 16:c2387f117808
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 ['[,] = [1,2]', $errorEmptyList],
67 ]);
68 }
69
70 /**
71 * @dataProvider validStatements
72 */
73 public function testProcessValidStatement($code)
74 {
75 $stmts = $this->parse($code);
76 $this->traverser->traverse($stmts);
77 $this->assertTrue(true);
78 }
79
80 public function validStatements()
81 {
82 $validExpr = [
83 ['list($a) = array(1)'],
84 ['list($x, $y) = array(1, 2)'],
85 ];
86
87 if (version_compare(PHP_VERSION, '7.1', '>=')) {
88 return array_merge($validExpr, [
89 ['[$a] = array(1)'],
90 ['list($b) = [2]'],
91 ['[$x, $y] = array(1, 2)'],
92 ['[$a] = [1]'],
93 ['[$x, $y] = [1, 2]'],
94 ['["_" => $v] = ["_" => 1]'],
95 ['[$a,] = [1,2,3]'],
96 ['[,$b] = [1,2,3]'],
97 ['[$a,,$c] = [1,2,3]'],
98 ['[$a,,,] = [1,2,3]'],
99 ]);
100 }
101
102 return $validExpr;
103 }
104 }