Вход Регистрация
Файл: gapps/vendor/nikic/php-parser/test/PhpParser/PrettyPrinterTest.php
Строк: 366
<?php

namespace PhpParser;

use 
PhpParserComment;
use 
PhpParserNodeExpr;
use 
PhpParserNodeScalarEncapsed;
use 
PhpParserNodeScalarEncapsedStringPart;
use 
PhpParserNodeScalarString_;
use 
PhpParserNodeStmt;
use 
PhpParserPrettyPrinterStandard;

require_once 
__DIR__ . '/CodeTestAbstract.php';

class 
PrettyPrinterTest extends CodeTestAbstract
{
    protected function 
doTestPrettyPrintMethod($method, $name, $code, $expected, $modeLine) {
        
$lexer = new LexerEmulative;
        
$parser5 = new ParserPhp5($lexer);
        
$parser7 = new ParserPhp7($lexer);

        list(
$version, $options) = $this->parseModeLine($modeLine);
        
$prettyPrinter = new Standard($options);

        try {
            
$output5 = canonicalize($prettyPrinter->$method($parser5->parse($code)));
        } catch (
Error $e) {
            
$output5 = null;
            if (
'php7' !== $version) {
                throw 
$e;
            }
        }

        try {
            
$output7 = canonicalize($prettyPrinter->$method($parser7->parse($code)));
        } catch (
Error $e) {
            
$output7 = null;
            if (
'php5' !== $version) {
                throw 
$e;
            }
        }

        if (
'php5' === $version) {
            
$this->assertSame($expected, $output5, $name);
            
$this->assertNotSame($expected, $output7, $name);
        } else if (
'php7' === $version) {
            
$this->assertSame($expected, $output7, $name);
            
$this->assertNotSame($expected, $output5, $name);
        } else {
            
$this->assertSame($expected, $output5, $name);
            
$this->assertSame($expected, $output7, $name);
        }
    }

    
/**
     * @dataProvider provideTestPrettyPrint
     * @covers PhpParserPrettyPrinterStandard<extended>
     */
    
public function testPrettyPrint($name, $code, $expected, $mode) {
        
$this->doTestPrettyPrintMethod('prettyPrint', $name, $code, $expected, $mode);
    }

    
/**
     * @dataProvider provideTestPrettyPrintFile
     * @covers PhpParserPrettyPrinterStandard<extended>
     */
    
public function testPrettyPrintFile($name, $code, $expected, $mode) {
        
$this->doTestPrettyPrintMethod('prettyPrintFile', $name, $code, $expected, $mode);
    }

    public function 
provideTestPrettyPrint() {
        return 
$this->getTests(__DIR__ . '/../code/prettyPrinter', 'test');
    }

    public function 
provideTestPrettyPrintFile() {
        return 
$this->getTests(__DIR__ . '/../code/prettyPrinter', 'file-test');
    }

    public function 
testPrettyPrintExpr() {
        
$prettyPrinter = new Standard;
        
$expr = new ExprBinaryOpMul(
            new 
ExprBinaryOpPlus(new ExprVariable('a'), new ExprVariable('b')),
            new 
ExprVariable('c')
        );
        
$this->assertEquals('($a + $b) * $c', $prettyPrinter->prettyPrintExpr($expr));

        
$expr = new ExprClosure(array(
            
'stmts' => array(new StmtReturn_(new String_("anb")))
        ));
        
$this->assertEquals("function () {n    return 'anb';n}", $prettyPrinter->prettyPrintExpr($expr));
    }

    public function 
testCommentBeforeInlineHTML() {
        
$prettyPrinter = new PrettyPrinterStandard;
        
$comment = new CommentDoc("/**n * This is a commentn */");
        
$stmts = [new StmtInlineHTML('Hello World!', ['comments' => [$comment]])];
        
$expected = "<?phpnn/**n * This is a commentn */n?>nHello World!";
        
$this->assertSame($expected, $prettyPrinter->prettyPrintFile($stmts));
    }

    private function 
parseModeLine($modeLine) {
        
$parts = explode(' ', $modeLine, 2);
        
$version = isset($parts[0]) ? $parts[0] : 'both';
        
$options = isset($parts[1]) ? json_decode($parts[1], true) : [];
        return [
$version, $options];
    }

    public function 
testArraySyntaxDefault() {
        
$prettyPrinter = new Standard(['shortArraySyntax' => true]);
        
$expr = new ExprArray_([
            new 
ExprArrayItem(new String_('val'), new String_('key'))
        ]);
        
$expected = "['key' => 'val']";
        
$this->assertSame($expected, $prettyPrinter->prettyPrintExpr($expr));
    }

    
/**
     * @dataProvider provideTestKindAttributes
     */
    
public function testKindAttributes($node, $expected) {
        
$prttyPrinter = new PrettyPrinterStandard;
        
$result = $prttyPrinter->prettyPrintExpr($node);
        
$this->assertSame($expected, $result);
    }

    public function 
provideTestKindAttributes() {
        
$nowdoc = ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR'];
        
$heredoc = ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR'];
        return [
            
// Defaults to single quoted
            
[new String_('foo'), "'foo'"],
            
// Explicit single/double quoted
            
[new String_('foo', ['kind' => String_::KIND_SINGLE_QUOTED]), "'foo'"],
            [new 
String_('foo', ['kind' => String_::KIND_DOUBLE_QUOTED]), '"foo"'],
            
// Fallback from doc string if no label
            
[new String_('foo', ['kind' => String_::KIND_NOWDOC]), "'foo'"],
            [new 
String_('foo', ['kind' => String_::KIND_HEREDOC]), '"foo"'],
            
// Fallback if string contains label
            
[new String_("AnBnC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'A']), "'AnBnC'"],
            [new 
String_("AnBnC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'B']), "'AnBnC'"],
            [new 
String_("AnBnC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'C']), "'AnBnC'"],
            [new 
String_("STR;", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']), "'STR;'"],
            
// Doc string if label not contained (or not in ending position)
            
[new String_("foo", $nowdoc), "<<<'STR'nfoonSTRn"],
            [new 
String_("foo", $heredoc), "<<<STRnfoonSTRn"],
            [new 
String_("STRx", $nowdoc), "<<<'STR'nSTRxnSTRn"],
            [new 
String_("xSTR", $nowdoc), "<<<'STR'nxSTRnSTRn"],
            
// Empty doc string variations (encapsed variant does not occur naturally)
            
[new String_("", $nowdoc), "<<<'STR'nSTRn"],
            [new 
String_("", $heredoc), "<<<STRnSTRn"],
            [new 
Encapsed([new EncapsedStringPart('')], $heredoc), "<<<STRnSTRn"],
            
// Encapsed doc string variations
            
[new Encapsed([new EncapsedStringPart('foo')], $heredoc), "<<<STRnfoonSTRn"],
            [new 
Encapsed([new EncapsedStringPart('foo'), new ExprVariable('y')], $heredoc), "<<<STRnfoo{$y}nSTRn"],
            [new 
Encapsed([new EncapsedStringPart("nSTR"), new ExprVariable('y')], $heredoc), "<<<STRnnSTR{$y}nSTRn"],
            [new 
Encapsed([new EncapsedStringPart("nSTR"), new ExprVariable('y')], $heredoc), "<<<STRnnSTR{$y}nSTRn"],
            [new 
Encapsed([new ExprVariable('y'), new EncapsedStringPart("STRn")], $heredoc), "<<<STRn{$y}STRnnSTRn"],
            
// Encapsed doc string fallback
            
[new Encapsed([new ExprVariable('y'), new EncapsedStringPart("nSTR")], $heredoc), '"{$y}\nSTR"'],
            [new 
Encapsed([new EncapsedStringPart("STRn"), new ExprVariable('y')], $heredoc), '"STR\n{$y}"'],
            [new 
Encapsed([new EncapsedStringPart("STR")], $heredoc), '"STR"'],
        ];
    }
}
Онлайн: 0
Реклама