Chris@0: 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@0: } else if ('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@0: * @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@0: * @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@0: $expr = new Expr\Closure(array( Chris@0: 'stmts' => array(new Stmt\Return_(new String_("a\nb"))) Chris@0: )); 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@0: $parts = explode(' ', $modeLine, 2); Chris@0: $version = isset($parts[0]) ? $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: /** Chris@0: * @expectedException \LogicException Chris@0: * @expectedExceptionMessage Cannot pretty-print AST with Error nodes Chris@0: */ Chris@0: public function testPrettyPrintWithError() { Chris@0: $stmts = [new Expr\PropertyFetch(new Expr\Variable('a'), new Expr\Error())]; Chris@0: $prettyPrinter = new PrettyPrinter\Standard; Chris@0: $prettyPrinter->prettyPrint($stmts); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @expectedException \LogicException Chris@0: * @expectedExceptionMessage Cannot pretty-print AST with Error nodes Chris@0: */ Chris@0: public function testPrettyPrintWithErrorInClassConstFetch() { Chris@0: $stmts = [new Expr\ClassConstFetch(new Name('Foo'), new Expr\Error())]; Chris@0: $prettyPrinter = new PrettyPrinter\Standard; Chris@0: $prettyPrinter->prettyPrint($stmts); Chris@0: } Chris@0: }