comparison vendor/nikic/php-parser/test/PhpParser/PrettyPrinterTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 5fb285c0d0e3
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace PhpParser;
4
5 use PhpParser\Comment;
6 use PhpParser\Node\Expr;
7 use PhpParser\Node\Name;
8 use PhpParser\Node\Scalar\DNumber;
9 use PhpParser\Node\Scalar\Encapsed;
10 use PhpParser\Node\Scalar\EncapsedStringPart;
11 use PhpParser\Node\Scalar\LNumber;
12 use PhpParser\Node\Scalar\String_;
13 use PhpParser\Node\Stmt;
14 use PhpParser\PrettyPrinter\Standard;
15
16 require_once __DIR__ . '/CodeTestAbstract.php';
17
18 class PrettyPrinterTest extends CodeTestAbstract
19 {
20 protected function doTestPrettyPrintMethod($method, $name, $code, $expected, $modeLine) {
21 $lexer = new Lexer\Emulative;
22 $parser5 = new Parser\Php5($lexer);
23 $parser7 = new Parser\Php7($lexer);
24
25 list($version, $options) = $this->parseModeLine($modeLine);
26 $prettyPrinter = new Standard($options);
27
28 try {
29 $output5 = canonicalize($prettyPrinter->$method($parser5->parse($code)));
30 } catch (Error $e) {
31 $output5 = null;
32 if ('php7' !== $version) {
33 throw $e;
34 }
35 }
36
37 try {
38 $output7 = canonicalize($prettyPrinter->$method($parser7->parse($code)));
39 } catch (Error $e) {
40 $output7 = null;
41 if ('php5' !== $version) {
42 throw $e;
43 }
44 }
45
46 if ('php5' === $version) {
47 $this->assertSame($expected, $output5, $name);
48 $this->assertNotSame($expected, $output7, $name);
49 } else if ('php7' === $version) {
50 $this->assertSame($expected, $output7, $name);
51 $this->assertNotSame($expected, $output5, $name);
52 } else {
53 $this->assertSame($expected, $output5, $name);
54 $this->assertSame($expected, $output7, $name);
55 }
56 }
57
58 /**
59 * @dataProvider provideTestPrettyPrint
60 * @covers PhpParser\PrettyPrinter\Standard<extended>
61 */
62 public function testPrettyPrint($name, $code, $expected, $mode) {
63 $this->doTestPrettyPrintMethod('prettyPrint', $name, $code, $expected, $mode);
64 }
65
66 /**
67 * @dataProvider provideTestPrettyPrintFile
68 * @covers PhpParser\PrettyPrinter\Standard<extended>
69 */
70 public function testPrettyPrintFile($name, $code, $expected, $mode) {
71 $this->doTestPrettyPrintMethod('prettyPrintFile', $name, $code, $expected, $mode);
72 }
73
74 public function provideTestPrettyPrint() {
75 return $this->getTests(__DIR__ . '/../code/prettyPrinter', 'test');
76 }
77
78 public function provideTestPrettyPrintFile() {
79 return $this->getTests(__DIR__ . '/../code/prettyPrinter', 'file-test');
80 }
81
82 public function testPrettyPrintExpr() {
83 $prettyPrinter = new Standard;
84 $expr = new Expr\BinaryOp\Mul(
85 new Expr\BinaryOp\Plus(new Expr\Variable('a'), new Expr\Variable('b')),
86 new Expr\Variable('c')
87 );
88 $this->assertEquals('($a + $b) * $c', $prettyPrinter->prettyPrintExpr($expr));
89
90 $expr = new Expr\Closure(array(
91 'stmts' => array(new Stmt\Return_(new String_("a\nb")))
92 ));
93 $this->assertEquals("function () {\n return 'a\nb';\n}", $prettyPrinter->prettyPrintExpr($expr));
94 }
95
96 public function testCommentBeforeInlineHTML() {
97 $prettyPrinter = new PrettyPrinter\Standard;
98 $comment = new Comment\Doc("/**\n * This is a comment\n */");
99 $stmts = [new Stmt\InlineHTML('Hello World!', ['comments' => [$comment]])];
100 $expected = "<?php\n\n/**\n * This is a comment\n */\n?>\nHello World!";
101 $this->assertSame($expected, $prettyPrinter->prettyPrintFile($stmts));
102 }
103
104 private function parseModeLine($modeLine) {
105 $parts = explode(' ', $modeLine, 2);
106 $version = isset($parts[0]) ? $parts[0] : 'both';
107 $options = isset($parts[1]) ? json_decode($parts[1], true) : [];
108 return [$version, $options];
109 }
110
111 public function testArraySyntaxDefault() {
112 $prettyPrinter = new Standard(['shortArraySyntax' => true]);
113 $expr = new Expr\Array_([
114 new Expr\ArrayItem(new String_('val'), new String_('key'))
115 ]);
116 $expected = "['key' => 'val']";
117 $this->assertSame($expected, $prettyPrinter->prettyPrintExpr($expr));
118 }
119
120 /**
121 * @dataProvider provideTestKindAttributes
122 */
123 public function testKindAttributes($node, $expected) {
124 $prttyPrinter = new PrettyPrinter\Standard;
125 $result = $prttyPrinter->prettyPrintExpr($node);
126 $this->assertSame($expected, $result);
127 }
128
129 public function provideTestKindAttributes() {
130 $nowdoc = ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR'];
131 $heredoc = ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR'];
132 return [
133 // Defaults to single quoted
134 [new String_('foo'), "'foo'"],
135 // Explicit single/double quoted
136 [new String_('foo', ['kind' => String_::KIND_SINGLE_QUOTED]), "'foo'"],
137 [new String_('foo', ['kind' => String_::KIND_DOUBLE_QUOTED]), '"foo"'],
138 // Fallback from doc string if no label
139 [new String_('foo', ['kind' => String_::KIND_NOWDOC]), "'foo'"],
140 [new String_('foo', ['kind' => String_::KIND_HEREDOC]), '"foo"'],
141 // Fallback if string contains label
142 [new String_("A\nB\nC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'A']), "'A\nB\nC'"],
143 [new String_("A\nB\nC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'B']), "'A\nB\nC'"],
144 [new String_("A\nB\nC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'C']), "'A\nB\nC'"],
145 [new String_("STR;", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']), "'STR;'"],
146 // Doc string if label not contained (or not in ending position)
147 [new String_("foo", $nowdoc), "<<<'STR'\nfoo\nSTR\n"],
148 [new String_("foo", $heredoc), "<<<STR\nfoo\nSTR\n"],
149 [new String_("STRx", $nowdoc), "<<<'STR'\nSTRx\nSTR\n"],
150 [new String_("xSTR", $nowdoc), "<<<'STR'\nxSTR\nSTR\n"],
151 // Empty doc string variations (encapsed variant does not occur naturally)
152 [new String_("", $nowdoc), "<<<'STR'\nSTR\n"],
153 [new String_("", $heredoc), "<<<STR\nSTR\n"],
154 [new Encapsed([new EncapsedStringPart('')], $heredoc), "<<<STR\nSTR\n"],
155 // Encapsed doc string variations
156 [new Encapsed([new EncapsedStringPart('foo')], $heredoc), "<<<STR\nfoo\nSTR\n"],
157 [new Encapsed([new EncapsedStringPart('foo'), new Expr\Variable('y')], $heredoc), "<<<STR\nfoo{\$y}\nSTR\n"],
158 [new Encapsed([new EncapsedStringPart("\nSTR"), new Expr\Variable('y')], $heredoc), "<<<STR\n\nSTR{\$y}\nSTR\n"],
159 [new Encapsed([new EncapsedStringPart("\nSTR"), new Expr\Variable('y')], $heredoc), "<<<STR\n\nSTR{\$y}\nSTR\n"],
160 [new Encapsed([new Expr\Variable('y'), new EncapsedStringPart("STR\n")], $heredoc), "<<<STR\n{\$y}STR\n\nSTR\n"],
161 // Encapsed doc string fallback
162 [new Encapsed([new Expr\Variable('y'), new EncapsedStringPart("\nSTR")], $heredoc), '"{$y}\\nSTR"'],
163 [new Encapsed([new EncapsedStringPart("STR\n"), new Expr\Variable('y')], $heredoc), '"STR\\n{$y}"'],
164 [new Encapsed([new EncapsedStringPart("STR")], $heredoc), '"STR"'],
165 ];
166 }
167
168 /** @dataProvider provideTestUnnaturalLiterals */
169 public function testUnnaturalLiterals($node, $expected) {
170 $prttyPrinter = new PrettyPrinter\Standard;
171 $result = $prttyPrinter->prettyPrintExpr($node);
172 $this->assertSame($expected, $result);
173 }
174
175 public function provideTestUnnaturalLiterals() {
176 return [
177 [new LNumber(-1), '-1'],
178 [new LNumber(-PHP_INT_MAX - 1), '(-' . PHP_INT_MAX . '-1)'],
179 [new LNumber(-1, ['kind' => LNumber::KIND_BIN]), '-0b1'],
180 [new LNumber(-1, ['kind' => LNumber::KIND_OCT]), '-01'],
181 [new LNumber(-1, ['kind' => LNumber::KIND_HEX]), '-0x1'],
182 [new DNumber(\INF), '\INF'],
183 [new DNumber(-\INF), '-\INF'],
184 [new DNumber(-\NAN), '\NAN'],
185 ];
186 }
187
188 /**
189 * @expectedException \LogicException
190 * @expectedExceptionMessage Cannot pretty-print AST with Error nodes
191 */
192 public function testPrettyPrintWithError() {
193 $stmts = [new Expr\PropertyFetch(new Expr\Variable('a'), new Expr\Error())];
194 $prettyPrinter = new PrettyPrinter\Standard;
195 $prettyPrinter->prettyPrint($stmts);
196 }
197
198 /**
199 * @expectedException \LogicException
200 * @expectedExceptionMessage Cannot pretty-print AST with Error nodes
201 */
202 public function testPrettyPrintWithErrorInClassConstFetch() {
203 $stmts = [new Expr\ClassConstFetch(new Name('Foo'), new Expr\Error())];
204 $prettyPrinter = new PrettyPrinter\Standard;
205 $prettyPrinter->prettyPrint($stmts);
206 }
207 }