1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00
psalm/tests/DocCommentTest.php
lhchavez ba63ccb825
Improve \Psalm\Internal\Scanner\DocblockParser::parse() (#3736)
This change avoids calling `str_replace()` on the original docblock and
instead only operates on the parsed (and modified) lines. This now makes
it so that if there are substrings of the docblock that match a tag
match, it won't get prematurely removed, therefore avoiding mangling of
the parsed docblock's description.

Fixes: #3735
2020-07-02 17:55:57 -04:00

188 lines
4.1 KiB
PHP

<?php
namespace Psalm\Tests;
use PHPUnit\Framework\TestCase as BaseTestCase;
use Psalm\DocComment;
use Psalm\Internal\Scanner\ParsedDocblock;
class DocCommentTest extends BaseTestCase
{
public function testNewLineIsAddedBetweenAnnotationsByDefault(): void
{
$docComment = new ParsedDocblock(
'some desc',
[
'param' =>
[
2 => 'string $bli',
3 => 'int $bla',
],
'throws' =>
[
0 => '\Exception',
],
'return' =>
[
0 => 'bool',
],
]
);
$expectedDoc = '/**
* some desc
*
* @param string $bli
* @param int $bla
*
* @throws \Exception
*
* @return bool
*/
';
$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 = '/**
* some desc
*
* @param string $bli
* @param int $bla
* @throws \Exception
* @return bool
*/
';
$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 = '/**
* some desc
*
* @param string $bli
* @param int $bla
*
* @throws \Exception
*
* @return bool
*/
';
$this->assertSame($expectedDoc, $docComment->render(''));
}
public function testParsingRoundtrip(): void
{
ParsedDocblock::addNewLineBetweenAnnotations(true);
$expectedDoc = '/**
* some desc
*
* @param string $bli
* @param int $bla
*
* @throws \Exception
*
* @return bool
*/
';
$docComment = DocComment::parsePreservingLength(
new \PhpParser\Comment\Doc($expectedDoc)
);
$this->assertSame($expectedDoc, $docComment->render(''));
}
public function testParsingWithIndentation(): void
{
ParsedDocblock::addNewLineBetweenAnnotations(true);
$expectedDoc = '/**
* some desc
*
* @param string $bli
* @param int $bla
*
* @throws \Exception
*
* @return bool
*/
';
$docComment = DocComment::parsePreservingLength(
new \PhpParser\Comment\Doc($expectedDoc)
);
$this->assertSame($expectedDoc, $docComment->render(' '));
}
public function testParsingWithCommonPrefixes(): void
{
ParsedDocblock::addNewLineBetweenAnnotations(true);
$expectedDoc = '/**
* 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
*/
';
$docComment = DocComment::parsePreservingLength(
new \PhpParser\Comment\Doc($expectedDoc)
);
$this->assertSame($expectedDoc, $docComment->render(''));
}
}