菜谱项目

PrettyPrinterTest.php 8.7KB

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