Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / nikic / php-parser / test / PhpParser / PrettyPrinterTest.php
index 908c18b2bb0f86b386480e82901bc962b8eb45d3..e47a9b9b3bf57e03c79d8e81ad48b1994f1630b3 100644 (file)
@@ -1,8 +1,7 @@
-<?php
+<?php declare(strict_types=1);
 
 namespace PhpParser;
 
-use PhpParser\Comment;
 use PhpParser\Node\Expr;
 use PhpParser\Node\Name;
 use PhpParser\Node\Scalar\DNumber;
@@ -46,7 +45,7 @@ class PrettyPrinterTest extends CodeTestAbstract
         if ('php5' === $version) {
             $this->assertSame($expected, $output5, $name);
             $this->assertNotSame($expected, $output7, $name);
-        } else if ('php7' === $version) {
+        } elseif ('php7' === $version) {
             $this->assertSame($expected, $output7, $name);
             $this->assertNotSame($expected, $output5, $name);
         } else {
@@ -57,7 +56,7 @@ class PrettyPrinterTest extends CodeTestAbstract
 
     /**
      * @dataProvider provideTestPrettyPrint
-     * @covers PhpParser\PrettyPrinter\Standard<extended>
+     * @covers \PhpParser\PrettyPrinter\Standard<extended>
      */
     public function testPrettyPrint($name, $code, $expected, $mode) {
         $this->doTestPrettyPrintMethod('prettyPrint', $name, $code, $expected, $mode);
@@ -65,7 +64,7 @@ class PrettyPrinterTest extends CodeTestAbstract
 
     /**
      * @dataProvider provideTestPrettyPrintFile
-     * @covers PhpParser\PrettyPrinter\Standard<extended>
+     * @covers \PhpParser\PrettyPrinter\Standard<extended>
      */
     public function testPrettyPrintFile($name, $code, $expected, $mode) {
         $this->doTestPrettyPrintMethod('prettyPrintFile', $name, $code, $expected, $mode);
@@ -87,9 +86,9 @@ class PrettyPrinterTest extends CodeTestAbstract
         );
         $this->assertEquals('($a + $b) * $c', $prettyPrinter->prettyPrintExpr($expr));
 
-        $expr = new Expr\Closure(array(
-            'stmts' => array(new Stmt\Return_(new String_("a\nb")))
-        ));
+        $expr = new Expr\Closure([
+            'stmts' => [new Stmt\Return_(new String_("a\nb"))]
+        ]);
         $this->assertEquals("function () {\n    return 'a\nb';\n}", $prettyPrinter->prettyPrintExpr($expr));
     }
 
@@ -102,8 +101,8 @@ class PrettyPrinterTest extends CodeTestAbstract
     }
 
     private function parseModeLine($modeLine) {
-        $parts = explode(' ', $modeLine, 2);
-        $version = isset($parts[0]) ? $parts[0] : 'both';
+        $parts = explode(' ', (string) $modeLine, 2);
+        $version = $parts[0] ?? 'both';
         $options = isset($parts[1]) ? json_decode($parts[1], true) : [];
         return [$version, $options];
     }
@@ -190,7 +189,9 @@ class PrettyPrinterTest extends CodeTestAbstract
      * @expectedExceptionMessage Cannot pretty-print AST with Error nodes
      */
     public function testPrettyPrintWithError() {
-        $stmts = [new Expr\PropertyFetch(new Expr\Variable('a'), new Expr\Error())];
+        $stmts = [new Stmt\Expression(
+            new Expr\PropertyFetch(new Expr\Variable('a'), new Expr\Error())
+        )];
         $prettyPrinter = new PrettyPrinter\Standard;
         $prettyPrinter->prettyPrint($stmts);
     }
@@ -200,8 +201,105 @@ class PrettyPrinterTest extends CodeTestAbstract
      * @expectedExceptionMessage Cannot pretty-print AST with Error nodes
      */
     public function testPrettyPrintWithErrorInClassConstFetch() {
-        $stmts = [new Expr\ClassConstFetch(new Name('Foo'), new Expr\Error())];
+        $stmts = [new Stmt\Expression(
+            new Expr\ClassConstFetch(new Name('Foo'), new Expr\Error())
+        )];
         $prettyPrinter = new PrettyPrinter\Standard;
         $prettyPrinter->prettyPrint($stmts);
     }
+
+    /**
+     * @dataProvider provideTestFormatPreservingPrint
+     * @covers \PhpParser\PrettyPrinter\Standard<extended>
+     */
+    public function testFormatPreservingPrint($name, $code, $modification, $expected, $modeLine) {
+        $lexer = new Lexer\Emulative([
+            'usedAttributes' => [
+                'comments',
+                'startLine', 'endLine',
+                'startTokenPos', 'endTokenPos',
+            ],
+        ]);
+
+        $parser = new Parser\Php7($lexer);
+        $traverser = new NodeTraverser();
+        $traverser->addVisitor(new NodeVisitor\CloningVisitor());
+
+        $printer = new PrettyPrinter\Standard();
+
+        $oldStmts = $parser->parse($code);
+        $oldTokens = $lexer->getTokens();
+
+        $newStmts = $traverser->traverse($oldStmts);
+
+        /** @var callable $fn */
+        eval(<<<CODE
+use PhpParser\Comment;
+use PhpParser\Node;
+use PhpParser\Node\Expr;
+use PhpParser\Node\Scalar;
+use PhpParser\Node\Stmt;
+\$fn = function(&\$stmts) { $modification };
+CODE
+        );
+        $fn($newStmts);
+
+        $newCode = $printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens);
+        $this->assertSame(canonicalize($expected), canonicalize($newCode), $name);
+    }
+
+    public function provideTestFormatPreservingPrint() {
+        return $this->getTests(__DIR__ . '/../code/formatPreservation', 'test', 3);
+    }
+
+    /**
+     * @dataProvider provideTestRoundTripPrint
+     * @covers \PhpParser\PrettyPrinter\Standard<extended>
+     */
+    public function testRoundTripPrint($name, $code, $expected, $modeLine) {
+        /**
+         * This test makes sure that the format-preserving pretty printer round-trips for all
+         * the pretty printer tests (i.e. returns the input if no changes occurred).
+         */
+
+        list($version) = $this->parseModeLine($modeLine);
+
+        $lexer = new Lexer\Emulative([
+            'usedAttributes' => [
+                'comments',
+                'startLine', 'endLine',
+                'startTokenPos', 'endTokenPos',
+            ],
+        ]);
+
+        $parserClass = $version === 'php5' ? Parser\Php5::class : Parser\Php7::class;
+        /** @var Parser $parser */
+        $parser = new $parserClass($lexer);
+
+        $traverser = new NodeTraverser();
+        $traverser->addVisitor(new NodeVisitor\CloningVisitor());
+
+        $printer = new PrettyPrinter\Standard();
+
+        try {
+            $oldStmts = $parser->parse($code);
+        } catch (Error $e) {
+            // Can't do a format-preserving print on a file with errors
+            return;
+        }
+
+        $oldTokens = $lexer->getTokens();
+
+        $newStmts = $traverser->traverse($oldStmts);
+
+        $newCode = $printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens);
+        $this->assertSame(canonicalize($code), canonicalize($newCode), $name);
+    }
+
+    public function provideTestRoundTripPrint() {
+        return array_merge(
+            $this->getTests(__DIR__ . '/../code/prettyPrinter', 'test'),
+            $this->getTests(__DIR__ . '/../code/parser', 'test')
+        );
+    }
 }