Chris@13: parseModeLine($modeLine); Chris@0: $prettyPrinter = new Standard($options); Chris@0: Chris@0: try { Chris@0: $output5 = canonicalize($prettyPrinter->$method($parser5->parse($code))); Chris@0: } catch (Error $e) { Chris@0: $output5 = null; Chris@0: if ('php7' !== $version) { Chris@0: throw $e; Chris@0: } Chris@0: } Chris@0: Chris@0: try { Chris@0: $output7 = canonicalize($prettyPrinter->$method($parser7->parse($code))); Chris@0: } catch (Error $e) { Chris@0: $output7 = null; Chris@0: if ('php5' !== $version) { Chris@0: throw $e; Chris@0: } Chris@0: } Chris@0: Chris@0: if ('php5' === $version) { Chris@0: $this->assertSame($expected, $output5, $name); Chris@0: $this->assertNotSame($expected, $output7, $name); Chris@13: } elseif ('php7' === $version) { Chris@0: $this->assertSame($expected, $output7, $name); Chris@0: $this->assertNotSame($expected, $output5, $name); Chris@0: } else { Chris@0: $this->assertSame($expected, $output5, $name); Chris@0: $this->assertSame($expected, $output7, $name); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * @dataProvider provideTestPrettyPrint Chris@13: * @covers \PhpParser\PrettyPrinter\Standard Chris@0: */ Chris@0: public function testPrettyPrint($name, $code, $expected, $mode) { Chris@0: $this->doTestPrettyPrintMethod('prettyPrint', $name, $code, $expected, $mode); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @dataProvider provideTestPrettyPrintFile Chris@13: * @covers \PhpParser\PrettyPrinter\Standard Chris@0: */ Chris@0: public function testPrettyPrintFile($name, $code, $expected, $mode) { Chris@0: $this->doTestPrettyPrintMethod('prettyPrintFile', $name, $code, $expected, $mode); Chris@0: } Chris@0: Chris@0: public function provideTestPrettyPrint() { Chris@0: return $this->getTests(__DIR__ . '/../code/prettyPrinter', 'test'); Chris@0: } Chris@0: Chris@0: public function provideTestPrettyPrintFile() { Chris@0: return $this->getTests(__DIR__ . '/../code/prettyPrinter', 'file-test'); Chris@0: } Chris@0: Chris@0: public function testPrettyPrintExpr() { Chris@0: $prettyPrinter = new Standard; Chris@0: $expr = new Expr\BinaryOp\Mul( Chris@0: new Expr\BinaryOp\Plus(new Expr\Variable('a'), new Expr\Variable('b')), Chris@0: new Expr\Variable('c') Chris@0: ); Chris@0: $this->assertEquals('($a + $b) * $c', $prettyPrinter->prettyPrintExpr($expr)); Chris@0: Chris@13: $expr = new Expr\Closure([ Chris@13: 'stmts' => [new Stmt\Return_(new String_("a\nb"))] Chris@13: ]); Chris@0: $this->assertEquals("function () {\n return 'a\nb';\n}", $prettyPrinter->prettyPrintExpr($expr)); Chris@0: } Chris@0: Chris@0: public function testCommentBeforeInlineHTML() { Chris@0: $prettyPrinter = new PrettyPrinter\Standard; Chris@0: $comment = new Comment\Doc("/**\n * This is a comment\n */"); Chris@0: $stmts = [new Stmt\InlineHTML('Hello World!', ['comments' => [$comment]])]; Chris@0: $expected = "\nHello World!"; Chris@0: $this->assertSame($expected, $prettyPrinter->prettyPrintFile($stmts)); Chris@0: } Chris@0: Chris@0: private function parseModeLine($modeLine) { Chris@13: $parts = explode(' ', (string) $modeLine, 2); Chris@13: $version = $parts[0] ?? 'both'; Chris@0: $options = isset($parts[1]) ? json_decode($parts[1], true) : []; Chris@0: return [$version, $options]; Chris@0: } Chris@0: Chris@0: public function testArraySyntaxDefault() { Chris@0: $prettyPrinter = new Standard(['shortArraySyntax' => true]); Chris@0: $expr = new Expr\Array_([ Chris@0: new Expr\ArrayItem(new String_('val'), new String_('key')) Chris@0: ]); Chris@0: $expected = "['key' => 'val']"; Chris@0: $this->assertSame($expected, $prettyPrinter->prettyPrintExpr($expr)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @dataProvider provideTestKindAttributes Chris@0: */ Chris@0: public function testKindAttributes($node, $expected) { Chris@0: $prttyPrinter = new PrettyPrinter\Standard; Chris@0: $result = $prttyPrinter->prettyPrintExpr($node); Chris@0: $this->assertSame($expected, $result); Chris@0: } Chris@0: Chris@0: public function provideTestKindAttributes() { Chris@0: $nowdoc = ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']; Chris@0: $heredoc = ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']; Chris@0: return [ Chris@0: // Defaults to single quoted Chris@0: [new String_('foo'), "'foo'"], Chris@0: // Explicit single/double quoted Chris@0: [new String_('foo', ['kind' => String_::KIND_SINGLE_QUOTED]), "'foo'"], Chris@0: [new String_('foo', ['kind' => String_::KIND_DOUBLE_QUOTED]), '"foo"'], Chris@0: // Fallback from doc string if no label Chris@0: [new String_('foo', ['kind' => String_::KIND_NOWDOC]), "'foo'"], Chris@0: [new String_('foo', ['kind' => String_::KIND_HEREDOC]), '"foo"'], Chris@0: // Fallback if string contains label Chris@0: [new String_("A\nB\nC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'A']), "'A\nB\nC'"], Chris@0: [new String_("A\nB\nC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'B']), "'A\nB\nC'"], Chris@0: [new String_("A\nB\nC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'C']), "'A\nB\nC'"], Chris@0: [new String_("STR;", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']), "'STR;'"], Chris@0: // Doc string if label not contained (or not in ending position) Chris@0: [new String_("foo", $nowdoc), "<<<'STR'\nfoo\nSTR\n"], Chris@0: [new String_("foo", $heredoc), "<<prettyPrintExpr($node); Chris@0: $this->assertSame($expected, $result); Chris@0: } Chris@0: Chris@0: public function provideTestUnnaturalLiterals() { Chris@0: return [ Chris@0: [new LNumber(-1), '-1'], Chris@0: [new LNumber(-PHP_INT_MAX - 1), '(-' . PHP_INT_MAX . '-1)'], Chris@0: [new LNumber(-1, ['kind' => LNumber::KIND_BIN]), '-0b1'], Chris@0: [new LNumber(-1, ['kind' => LNumber::KIND_OCT]), '-01'], Chris@0: [new LNumber(-1, ['kind' => LNumber::KIND_HEX]), '-0x1'], Chris@0: [new DNumber(\INF), '\INF'], Chris@0: [new DNumber(-\INF), '-\INF'], Chris@0: [new DNumber(-\NAN), '\NAN'], Chris@0: ]; Chris@0: } Chris@0: Chris@0: public function testPrettyPrintWithError() { Chris@17: $this->expectException(\LogicException::class); Chris@17: $this->expectExceptionMessage('Cannot pretty-print AST with Error nodes'); Chris@13: $stmts = [new Stmt\Expression( Chris@13: new Expr\PropertyFetch(new Expr\Variable('a'), new Expr\Error()) Chris@13: )]; Chris@0: $prettyPrinter = new PrettyPrinter\Standard; Chris@0: $prettyPrinter->prettyPrint($stmts); Chris@0: } Chris@0: Chris@0: public function testPrettyPrintWithErrorInClassConstFetch() { Chris@17: $this->expectException(\LogicException::class); Chris@17: $this->expectExceptionMessage('Cannot pretty-print AST with Error nodes'); Chris@13: $stmts = [new Stmt\Expression( Chris@13: new Expr\ClassConstFetch(new Name('Foo'), new Expr\Error()) Chris@13: )]; Chris@0: $prettyPrinter = new PrettyPrinter\Standard; Chris@0: $prettyPrinter->prettyPrint($stmts); Chris@0: } Chris@13: Chris@16: public function testPrettyPrintEncapsedStringPart() { Chris@17: $this->expectException(\LogicException::class); Chris@17: $this->expectExceptionMessage('Cannot directly print EncapsedStringPart'); Chris@16: $expr = new Node\Scalar\EncapsedStringPart('foo'); Chris@16: $prettyPrinter = new PrettyPrinter\Standard; Chris@16: $prettyPrinter->prettyPrintExpr($expr); Chris@16: } Chris@16: Chris@16: /** Chris@13: * @dataProvider provideTestFormatPreservingPrint Chris@13: * @covers \PhpParser\PrettyPrinter\Standard Chris@13: */ Chris@13: public function testFormatPreservingPrint($name, $code, $modification, $expected, $modeLine) { Chris@13: $lexer = new Lexer\Emulative([ Chris@13: 'usedAttributes' => [ Chris@13: 'comments', Chris@13: 'startLine', 'endLine', Chris@13: 'startTokenPos', 'endTokenPos', Chris@13: ], Chris@13: ]); Chris@13: Chris@13: $parser = new Parser\Php7($lexer); Chris@13: $traverser = new NodeTraverser(); Chris@13: $traverser->addVisitor(new NodeVisitor\CloningVisitor()); Chris@13: Chris@13: $printer = new PrettyPrinter\Standard(); Chris@13: Chris@13: $oldStmts = $parser->parse($code); Chris@13: $oldTokens = $lexer->getTokens(); Chris@13: Chris@13: $newStmts = $traverser->traverse($oldStmts); Chris@13: Chris@13: /** @var callable $fn */ Chris@13: eval(<<printFormatPreserving($newStmts, $oldStmts, $oldTokens); Chris@13: $this->assertSame(canonicalize($expected), canonicalize($newCode), $name); Chris@13: } Chris@13: Chris@13: public function provideTestFormatPreservingPrint() { Chris@13: return $this->getTests(__DIR__ . '/../code/formatPreservation', 'test', 3); Chris@13: } Chris@13: Chris@13: /** Chris@13: * @dataProvider provideTestRoundTripPrint Chris@13: * @covers \PhpParser\PrettyPrinter\Standard Chris@13: */ Chris@13: public function testRoundTripPrint($name, $code, $expected, $modeLine) { Chris@13: /** Chris@13: * This test makes sure that the format-preserving pretty printer round-trips for all Chris@13: * the pretty printer tests (i.e. returns the input if no changes occurred). Chris@13: */ Chris@13: Chris@13: list($version) = $this->parseModeLine($modeLine); Chris@13: Chris@13: $lexer = new Lexer\Emulative([ Chris@13: 'usedAttributes' => [ Chris@13: 'comments', Chris@13: 'startLine', 'endLine', Chris@13: 'startTokenPos', 'endTokenPos', Chris@13: ], Chris@13: ]); Chris@13: Chris@13: $parserClass = $version === 'php5' ? Parser\Php5::class : Parser\Php7::class; Chris@13: /** @var Parser $parser */ Chris@13: $parser = new $parserClass($lexer); Chris@13: Chris@13: $traverser = new NodeTraverser(); Chris@13: $traverser->addVisitor(new NodeVisitor\CloningVisitor()); Chris@13: Chris@13: $printer = new PrettyPrinter\Standard(); Chris@13: Chris@13: try { Chris@13: $oldStmts = $parser->parse($code); Chris@13: } catch (Error $e) { Chris@13: // Can't do a format-preserving print on a file with errors Chris@13: return; Chris@13: } Chris@13: Chris@13: $oldTokens = $lexer->getTokens(); Chris@13: Chris@13: $newStmts = $traverser->traverse($oldStmts); Chris@13: Chris@13: $newCode = $printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens); Chris@13: $this->assertSame(canonicalize($code), canonicalize($newCode), $name); Chris@13: } Chris@13: Chris@13: public function provideTestRoundTripPrint() { Chris@13: return array_merge( Chris@13: $this->getTests(__DIR__ . '/../code/prettyPrinter', 'test'), Chris@13: $this->getTests(__DIR__ . '/../code/parser', 'test') Chris@13: ); Chris@13: } Chris@0: }