mirror of
https://github.com/danog/psalm.git
synced 2024-11-26 20:34:47 +01:00
bf99b76798
Since heredocs in PHP 7.3+ automatically strip leading whitespace we can fix the formatting for this test.
210 lines
5.0 KiB
PHP
210 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
use PHPUnit\Framework\TestCase as BaseTestCase;
|
|
use PhpParser\Comment\Doc;
|
|
use Psalm\DocComment;
|
|
use Psalm\Internal\RuntimeCaches;
|
|
use Psalm\Internal\Scanner\ParsedDocblock;
|
|
|
|
class DocCommentTest extends BaseTestCase
|
|
{
|
|
public function setUp(): void
|
|
{
|
|
RuntimeCaches::clearAll();
|
|
}
|
|
|
|
public function testNewLineIsAddedBetweenAnnotationsByDefault(): void
|
|
{
|
|
$docComment = new ParsedDocblock(
|
|
'some desc',
|
|
[
|
|
'param' =>
|
|
[
|
|
2 => 'string $bli',
|
|
3 => 'int $bla',
|
|
],
|
|
'throws' =>
|
|
[
|
|
0 => '\Exception',
|
|
],
|
|
'return' =>
|
|
[
|
|
0 => 'bool',
|
|
],
|
|
],
|
|
);
|
|
|
|
$expectedDoc = <<<'PHP'
|
|
/**
|
|
* some desc
|
|
*
|
|
* @param string $bli
|
|
* @param int $bla
|
|
*
|
|
* @throws \Exception
|
|
*
|
|
* @return bool
|
|
*/
|
|
|
|
PHP;
|
|
|
|
$this->assertSame($expectedDoc, $docComment->render(''));
|
|
}
|
|
|
|
public function testNewLineIsNotAddedBetweenAnnotationsIfDisabled(): void
|
|
{
|
|
ParsedDocblock::addNewLineBetweenAnnotations(false);
|
|
|
|
$docComment = new ParsedDocblock(
|
|
'some desc',
|
|
[
|
|
'param' =>
|
|
[
|
|
2 => 'string $bli',
|
|
3 => 'int $bla',
|
|
],
|
|
'throws' =>
|
|
[
|
|
0 => '\Exception',
|
|
],
|
|
'return' =>
|
|
[
|
|
0 => 'bool',
|
|
],
|
|
],
|
|
);
|
|
|
|
$expectedDoc = <<<'PHP'
|
|
/**
|
|
* some desc
|
|
*
|
|
* @param string $bli
|
|
* @param int $bla
|
|
* @throws \Exception
|
|
* @return bool
|
|
*/
|
|
|
|
PHP;
|
|
|
|
$this->assertSame($expectedDoc, $docComment->render(''));
|
|
}
|
|
|
|
public function testNewLineIsAddedBetweenAnnotationsIfEnabled(): void
|
|
{
|
|
ParsedDocblock::addNewLineBetweenAnnotations(true);
|
|
|
|
$docComment = new ParsedDocblock(
|
|
'some desc',
|
|
[
|
|
'param' =>
|
|
[
|
|
2 => 'string $bli',
|
|
3 => 'int $bla',
|
|
],
|
|
'throws' =>
|
|
[
|
|
0 => '\Exception',
|
|
],
|
|
'return' =>
|
|
[
|
|
0 => 'bool',
|
|
],
|
|
],
|
|
);
|
|
|
|
$expectedDoc = <<<'PHP'
|
|
/**
|
|
* some desc
|
|
*
|
|
* @param string $bli
|
|
* @param int $bla
|
|
*
|
|
* @throws \Exception
|
|
*
|
|
* @return bool
|
|
*/
|
|
|
|
PHP;
|
|
|
|
$this->assertSame($expectedDoc, $docComment->render(''));
|
|
}
|
|
|
|
public function testParsingRoundtrip(): void
|
|
{
|
|
ParsedDocblock::addNewLineBetweenAnnotations(true);
|
|
|
|
$expectedDoc = <<<'PHP'
|
|
/**
|
|
* some desc
|
|
*
|
|
* @param string $bli
|
|
* @param int $bla
|
|
*
|
|
* @throws \Exception
|
|
*
|
|
* @return bool
|
|
*/
|
|
|
|
PHP;
|
|
$docComment = DocComment::parsePreservingLength(
|
|
new Doc($expectedDoc),
|
|
);
|
|
|
|
$this->assertSame($expectedDoc, $docComment->render(''));
|
|
}
|
|
|
|
public function testParsingWithIndentation(): void
|
|
{
|
|
ParsedDocblock::addNewLineBetweenAnnotations(true);
|
|
|
|
$expectedDoc = <<<'PHP'
|
|
/**
|
|
* some desc
|
|
*
|
|
* @param string $bli
|
|
* @param int $bla
|
|
*
|
|
* @throws \Exception
|
|
*
|
|
* @return bool
|
|
*/
|
|
|
|
PHP
|
|
. " ";
|
|
|
|
$docComment = DocComment::parsePreservingLength(
|
|
new Doc($expectedDoc),
|
|
);
|
|
|
|
$this->assertSame($expectedDoc, $docComment->render(' '));
|
|
}
|
|
|
|
public function testParsingWithCommonPrefixes(): void
|
|
{
|
|
ParsedDocblock::addNewLineBetweenAnnotations(true);
|
|
|
|
$expectedDoc = <<<'PHP'
|
|
/**
|
|
* some self-referential desc with " * @return bool
|
|
* " as part of it.
|
|
*
|
|
* @param string $bli
|
|
* @param string $bli_this_suffix_is_kept
|
|
* @param int $bla
|
|
*
|
|
* @throws \Exception
|
|
*
|
|
* @return bool
|
|
*/
|
|
|
|
PHP;
|
|
$docComment = DocComment::parsePreservingLength(
|
|
new Doc($expectedDoc),
|
|
);
|
|
|
|
$this->assertSame($expectedDoc, $docComment->render(''));
|
|
}
|
|
}
|