annotate vendor/nikic/php-parser/test/PhpParser/PrettyPrinterTest.php @ 16:c2387f117808

Routine composer update
author Chris Cannam
date Tue, 10 Jul 2018 15:07:59 +0100
parents 5fb285c0d0e3
children 129ea1e6d783
rev   line source
Chris@13 1 <?php declare(strict_types=1);
Chris@0 2
Chris@0 3 namespace PhpParser;
Chris@0 4
Chris@0 5 use PhpParser\Node\Expr;
Chris@0 6 use PhpParser\Node\Name;
Chris@0 7 use PhpParser\Node\Scalar\DNumber;
Chris@0 8 use PhpParser\Node\Scalar\Encapsed;
Chris@0 9 use PhpParser\Node\Scalar\EncapsedStringPart;
Chris@0 10 use PhpParser\Node\Scalar\LNumber;
Chris@0 11 use PhpParser\Node\Scalar\String_;
Chris@0 12 use PhpParser\Node\Stmt;
Chris@0 13 use PhpParser\PrettyPrinter\Standard;
Chris@0 14
Chris@0 15 require_once __DIR__ . '/CodeTestAbstract.php';
Chris@0 16
Chris@0 17 class PrettyPrinterTest extends CodeTestAbstract
Chris@0 18 {
Chris@0 19 protected function doTestPrettyPrintMethod($method, $name, $code, $expected, $modeLine) {
Chris@0 20 $lexer = new Lexer\Emulative;
Chris@0 21 $parser5 = new Parser\Php5($lexer);
Chris@0 22 $parser7 = new Parser\Php7($lexer);
Chris@0 23
Chris@0 24 list($version, $options) = $this->parseModeLine($modeLine);
Chris@0 25 $prettyPrinter = new Standard($options);
Chris@0 26
Chris@0 27 try {
Chris@0 28 $output5 = canonicalize($prettyPrinter->$method($parser5->parse($code)));
Chris@0 29 } catch (Error $e) {
Chris@0 30 $output5 = null;
Chris@0 31 if ('php7' !== $version) {
Chris@0 32 throw $e;
Chris@0 33 }
Chris@0 34 }
Chris@0 35
Chris@0 36 try {
Chris@0 37 $output7 = canonicalize($prettyPrinter->$method($parser7->parse($code)));
Chris@0 38 } catch (Error $e) {
Chris@0 39 $output7 = null;
Chris@0 40 if ('php5' !== $version) {
Chris@0 41 throw $e;
Chris@0 42 }
Chris@0 43 }
Chris@0 44
Chris@0 45 if ('php5' === $version) {
Chris@0 46 $this->assertSame($expected, $output5, $name);
Chris@0 47 $this->assertNotSame($expected, $output7, $name);
Chris@13 48 } elseif ('php7' === $version) {
Chris@0 49 $this->assertSame($expected, $output7, $name);
Chris@0 50 $this->assertNotSame($expected, $output5, $name);
Chris@0 51 } else {
Chris@0 52 $this->assertSame($expected, $output5, $name);
Chris@0 53 $this->assertSame($expected, $output7, $name);
Chris@0 54 }
Chris@0 55 }
Chris@0 56
Chris@0 57 /**
Chris@0 58 * @dataProvider provideTestPrettyPrint
Chris@13 59 * @covers \PhpParser\PrettyPrinter\Standard<extended>
Chris@0 60 */
Chris@0 61 public function testPrettyPrint($name, $code, $expected, $mode) {
Chris@0 62 $this->doTestPrettyPrintMethod('prettyPrint', $name, $code, $expected, $mode);
Chris@0 63 }
Chris@0 64
Chris@0 65 /**
Chris@0 66 * @dataProvider provideTestPrettyPrintFile
Chris@13 67 * @covers \PhpParser\PrettyPrinter\Standard<extended>
Chris@0 68 */
Chris@0 69 public function testPrettyPrintFile($name, $code, $expected, $mode) {
Chris@0 70 $this->doTestPrettyPrintMethod('prettyPrintFile', $name, $code, $expected, $mode);
Chris@0 71 }
Chris@0 72
Chris@0 73 public function provideTestPrettyPrint() {
Chris@0 74 return $this->getTests(__DIR__ . '/../code/prettyPrinter', 'test');
Chris@0 75 }
Chris@0 76
Chris@0 77 public function provideTestPrettyPrintFile() {
Chris@0 78 return $this->getTests(__DIR__ . '/../code/prettyPrinter', 'file-test');
Chris@0 79 }
Chris@0 80
Chris@0 81 public function testPrettyPrintExpr() {
Chris@0 82 $prettyPrinter = new Standard;
Chris@0 83 $expr = new Expr\BinaryOp\Mul(
Chris@0 84 new Expr\BinaryOp\Plus(new Expr\Variable('a'), new Expr\Variable('b')),
Chris@0 85 new Expr\Variable('c')
Chris@0 86 );
Chris@0 87 $this->assertEquals('($a + $b) * $c', $prettyPrinter->prettyPrintExpr($expr));
Chris@0 88
Chris@13 89 $expr = new Expr\Closure([
Chris@13 90 'stmts' => [new Stmt\Return_(new String_("a\nb"))]
Chris@13 91 ]);
Chris@0 92 $this->assertEquals("function () {\n return 'a\nb';\n}", $prettyPrinter->prettyPrintExpr($expr));
Chris@0 93 }
Chris@0 94
Chris@0 95 public function testCommentBeforeInlineHTML() {
Chris@0 96 $prettyPrinter = new PrettyPrinter\Standard;
Chris@0 97 $comment = new Comment\Doc("/**\n * This is a comment\n */");
Chris@0 98 $stmts = [new Stmt\InlineHTML('Hello World!', ['comments' => [$comment]])];
Chris@0 99 $expected = "<?php\n\n/**\n * This is a comment\n */\n?>\nHello World!";
Chris@0 100 $this->assertSame($expected, $prettyPrinter->prettyPrintFile($stmts));
Chris@0 101 }
Chris@0 102
Chris@0 103 private function parseModeLine($modeLine) {
Chris@13 104 $parts = explode(' ', (string) $modeLine, 2);
Chris@13 105 $version = $parts[0] ?? 'both';
Chris@0 106 $options = isset($parts[1]) ? json_decode($parts[1], true) : [];
Chris@0 107 return [$version, $options];
Chris@0 108 }
Chris@0 109
Chris@0 110 public function testArraySyntaxDefault() {
Chris@0 111 $prettyPrinter = new Standard(['shortArraySyntax' => true]);
Chris@0 112 $expr = new Expr\Array_([
Chris@0 113 new Expr\ArrayItem(new String_('val'), new String_('key'))
Chris@0 114 ]);
Chris@0 115 $expected = "['key' => 'val']";
Chris@0 116 $this->assertSame($expected, $prettyPrinter->prettyPrintExpr($expr));
Chris@0 117 }
Chris@0 118
Chris@0 119 /**
Chris@0 120 * @dataProvider provideTestKindAttributes
Chris@0 121 */
Chris@0 122 public function testKindAttributes($node, $expected) {
Chris@0 123 $prttyPrinter = new PrettyPrinter\Standard;
Chris@0 124 $result = $prttyPrinter->prettyPrintExpr($node);
Chris@0 125 $this->assertSame($expected, $result);
Chris@0 126 }
Chris@0 127
Chris@0 128 public function provideTestKindAttributes() {
Chris@0 129 $nowdoc = ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR'];
Chris@0 130 $heredoc = ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR'];
Chris@0 131 return [
Chris@0 132 // Defaults to single quoted
Chris@0 133 [new String_('foo'), "'foo'"],
Chris@0 134 // Explicit single/double quoted
Chris@0 135 [new String_('foo', ['kind' => String_::KIND_SINGLE_QUOTED]), "'foo'"],
Chris@0 136 [new String_('foo', ['kind' => String_::KIND_DOUBLE_QUOTED]), '"foo"'],
Chris@0 137 // Fallback from doc string if no label
Chris@0 138 [new String_('foo', ['kind' => String_::KIND_NOWDOC]), "'foo'"],
Chris@0 139 [new String_('foo', ['kind' => String_::KIND_HEREDOC]), '"foo"'],
Chris@0 140 // Fallback if string contains label
Chris@0 141 [new String_("A\nB\nC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'A']), "'A\nB\nC'"],
Chris@0 142 [new String_("A\nB\nC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'B']), "'A\nB\nC'"],
Chris@0 143 [new String_("A\nB\nC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'C']), "'A\nB\nC'"],
Chris@0 144 [new String_("STR;", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']), "'STR;'"],
Chris@0 145 // Doc string if label not contained (or not in ending position)
Chris@0 146 [new String_("foo", $nowdoc), "<<<'STR'\nfoo\nSTR\n"],
Chris@0 147 [new String_("foo", $heredoc), "<<<STR\nfoo\nSTR\n"],
Chris@0 148 [new String_("STRx", $nowdoc), "<<<'STR'\nSTRx\nSTR\n"],
Chris@0 149 [new String_("xSTR", $nowdoc), "<<<'STR'\nxSTR\nSTR\n"],
Chris@0 150 // Empty doc string variations (encapsed variant does not occur naturally)
Chris@0 151 [new String_("", $nowdoc), "<<<'STR'\nSTR\n"],
Chris@0 152 [new String_("", $heredoc), "<<<STR\nSTR\n"],
Chris@0 153 [new Encapsed([new EncapsedStringPart('')], $heredoc), "<<<STR\nSTR\n"],
Chris@0 154 // Encapsed doc string variations
Chris@0 155 [new Encapsed([new EncapsedStringPart('foo')], $heredoc), "<<<STR\nfoo\nSTR\n"],
Chris@0 156 [new Encapsed([new EncapsedStringPart('foo'), new Expr\Variable('y')], $heredoc), "<<<STR\nfoo{\$y}\nSTR\n"],
Chris@0 157 [new Encapsed([new EncapsedStringPart("\nSTR"), new Expr\Variable('y')], $heredoc), "<<<STR\n\nSTR{\$y}\nSTR\n"],
Chris@0 158 [new Encapsed([new EncapsedStringPart("\nSTR"), new Expr\Variable('y')], $heredoc), "<<<STR\n\nSTR{\$y}\nSTR\n"],
Chris@0 159 [new Encapsed([new Expr\Variable('y'), new EncapsedStringPart("STR\n")], $heredoc), "<<<STR\n{\$y}STR\n\nSTR\n"],
Chris@0 160 // Encapsed doc string fallback
Chris@0 161 [new Encapsed([new Expr\Variable('y'), new EncapsedStringPart("\nSTR")], $heredoc), '"{$y}\\nSTR"'],
Chris@0 162 [new Encapsed([new EncapsedStringPart("STR\n"), new Expr\Variable('y')], $heredoc), '"STR\\n{$y}"'],
Chris@0 163 [new Encapsed([new EncapsedStringPart("STR")], $heredoc), '"STR"'],
Chris@0 164 ];
Chris@0 165 }
Chris@0 166
Chris@0 167 /** @dataProvider provideTestUnnaturalLiterals */
Chris@0 168 public function testUnnaturalLiterals($node, $expected) {
Chris@0 169 $prttyPrinter = new PrettyPrinter\Standard;
Chris@0 170 $result = $prttyPrinter->prettyPrintExpr($node);
Chris@0 171 $this->assertSame($expected, $result);
Chris@0 172 }
Chris@0 173
Chris@0 174 public function provideTestUnnaturalLiterals() {
Chris@0 175 return [
Chris@0 176 [new LNumber(-1), '-1'],
Chris@0 177 [new LNumber(-PHP_INT_MAX - 1), '(-' . PHP_INT_MAX . '-1)'],
Chris@0 178 [new LNumber(-1, ['kind' => LNumber::KIND_BIN]), '-0b1'],
Chris@0 179 [new LNumber(-1, ['kind' => LNumber::KIND_OCT]), '-01'],
Chris@0 180 [new LNumber(-1, ['kind' => LNumber::KIND_HEX]), '-0x1'],
Chris@0 181 [new DNumber(\INF), '\INF'],
Chris@0 182 [new DNumber(-\INF), '-\INF'],
Chris@0 183 [new DNumber(-\NAN), '\NAN'],
Chris@0 184 ];
Chris@0 185 }
Chris@0 186
Chris@0 187 /**
Chris@0 188 * @expectedException \LogicException
Chris@0 189 * @expectedExceptionMessage Cannot pretty-print AST with Error nodes
Chris@0 190 */
Chris@0 191 public function testPrettyPrintWithError() {
Chris@13 192 $stmts = [new Stmt\Expression(
Chris@13 193 new Expr\PropertyFetch(new Expr\Variable('a'), new Expr\Error())
Chris@13 194 )];
Chris@0 195 $prettyPrinter = new PrettyPrinter\Standard;
Chris@0 196 $prettyPrinter->prettyPrint($stmts);
Chris@0 197 }
Chris@0 198
Chris@0 199 /**
Chris@0 200 * @expectedException \LogicException
Chris@0 201 * @expectedExceptionMessage Cannot pretty-print AST with Error nodes
Chris@0 202 */
Chris@0 203 public function testPrettyPrintWithErrorInClassConstFetch() {
Chris@13 204 $stmts = [new Stmt\Expression(
Chris@13 205 new Expr\ClassConstFetch(new Name('Foo'), new Expr\Error())
Chris@13 206 )];
Chris@0 207 $prettyPrinter = new PrettyPrinter\Standard;
Chris@0 208 $prettyPrinter->prettyPrint($stmts);
Chris@0 209 }
Chris@13 210
Chris@13 211 /**
Chris@16 212 * @expectedException \LogicException
Chris@16 213 * @expectedExceptionMessage Cannot directly print EncapsedStringPart
Chris@16 214 */
Chris@16 215 public function testPrettyPrintEncapsedStringPart() {
Chris@16 216 $expr = new Node\Scalar\EncapsedStringPart('foo');
Chris@16 217 $prettyPrinter = new PrettyPrinter\Standard;
Chris@16 218 $prettyPrinter->prettyPrintExpr($expr);
Chris@16 219 }
Chris@16 220
Chris@16 221 /**
Chris@13 222 * @dataProvider provideTestFormatPreservingPrint
Chris@13 223 * @covers \PhpParser\PrettyPrinter\Standard<extended>
Chris@13 224 */
Chris@13 225 public function testFormatPreservingPrint($name, $code, $modification, $expected, $modeLine) {
Chris@13 226 $lexer = new Lexer\Emulative([
Chris@13 227 'usedAttributes' => [
Chris@13 228 'comments',
Chris@13 229 'startLine', 'endLine',
Chris@13 230 'startTokenPos', 'endTokenPos',
Chris@13 231 ],
Chris@13 232 ]);
Chris@13 233
Chris@13 234 $parser = new Parser\Php7($lexer);
Chris@13 235 $traverser = new NodeTraverser();
Chris@13 236 $traverser->addVisitor(new NodeVisitor\CloningVisitor());
Chris@13 237
Chris@13 238 $printer = new PrettyPrinter\Standard();
Chris@13 239
Chris@13 240 $oldStmts = $parser->parse($code);
Chris@13 241 $oldTokens = $lexer->getTokens();
Chris@13 242
Chris@13 243 $newStmts = $traverser->traverse($oldStmts);
Chris@13 244
Chris@13 245 /** @var callable $fn */
Chris@13 246 eval(<<<CODE
Chris@13 247 use PhpParser\Comment;
Chris@13 248 use PhpParser\Node;
Chris@13 249 use PhpParser\Node\Expr;
Chris@13 250 use PhpParser\Node\Scalar;
Chris@13 251 use PhpParser\Node\Stmt;
Chris@13 252 \$fn = function(&\$stmts) { $modification };
Chris@13 253 CODE
Chris@13 254 );
Chris@13 255 $fn($newStmts);
Chris@13 256
Chris@13 257 $newCode = $printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens);
Chris@13 258 $this->assertSame(canonicalize($expected), canonicalize($newCode), $name);
Chris@13 259 }
Chris@13 260
Chris@13 261 public function provideTestFormatPreservingPrint() {
Chris@13 262 return $this->getTests(__DIR__ . '/../code/formatPreservation', 'test', 3);
Chris@13 263 }
Chris@13 264
Chris@13 265 /**
Chris@13 266 * @dataProvider provideTestRoundTripPrint
Chris@13 267 * @covers \PhpParser\PrettyPrinter\Standard<extended>
Chris@13 268 */
Chris@13 269 public function testRoundTripPrint($name, $code, $expected, $modeLine) {
Chris@13 270 /**
Chris@13 271 * This test makes sure that the format-preserving pretty printer round-trips for all
Chris@13 272 * the pretty printer tests (i.e. returns the input if no changes occurred).
Chris@13 273 */
Chris@13 274
Chris@13 275 list($version) = $this->parseModeLine($modeLine);
Chris@13 276
Chris@13 277 $lexer = new Lexer\Emulative([
Chris@13 278 'usedAttributes' => [
Chris@13 279 'comments',
Chris@13 280 'startLine', 'endLine',
Chris@13 281 'startTokenPos', 'endTokenPos',
Chris@13 282 ],
Chris@13 283 ]);
Chris@13 284
Chris@13 285 $parserClass = $version === 'php5' ? Parser\Php5::class : Parser\Php7::class;
Chris@13 286 /** @var Parser $parser */
Chris@13 287 $parser = new $parserClass($lexer);
Chris@13 288
Chris@13 289 $traverser = new NodeTraverser();
Chris@13 290 $traverser->addVisitor(new NodeVisitor\CloningVisitor());
Chris@13 291
Chris@13 292 $printer = new PrettyPrinter\Standard();
Chris@13 293
Chris@13 294 try {
Chris@13 295 $oldStmts = $parser->parse($code);
Chris@13 296 } catch (Error $e) {
Chris@13 297 // Can't do a format-preserving print on a file with errors
Chris@13 298 return;
Chris@13 299 }
Chris@13 300
Chris@13 301 $oldTokens = $lexer->getTokens();
Chris@13 302
Chris@13 303 $newStmts = $traverser->traverse($oldStmts);
Chris@13 304
Chris@13 305 $newCode = $printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens);
Chris@13 306 $this->assertSame(canonicalize($code), canonicalize($newCode), $name);
Chris@13 307 }
Chris@13 308
Chris@13 309 public function provideTestRoundTripPrint() {
Chris@13 310 return array_merge(
Chris@13 311 $this->getTests(__DIR__ . '/../code/prettyPrinter', 'test'),
Chris@13 312 $this->getTests(__DIR__ . '/../code/parser', 'test')
Chris@13 313 );
Chris@13 314 }
Chris@0 315 }