Skip to content

Commit

Permalink
PrinterTest - parse the new printed node again and verify the AST is …
Browse files Browse the repository at this point in the history
…the same
  • Loading branch information
ondrejmirtes committed Apr 28, 2023
1 parent 0b4de96 commit cf694fd
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion tests/PHPStan/Printer/PrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\PhpDocParser\Printer;

use PHPStan\PhpDocParser\Ast\AbstractNodeVisitor;
use PHPStan\PhpDocParser\Ast\Attribute;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprArrayItemNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprArrayNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
Expand Down Expand Up @@ -1173,7 +1174,37 @@ public function testPrintFormatPreserving(string $phpDoc, string $expectedResult
[$newNode] = $changingTraverser->traverse($newNodes);

$printer = new Printer();
$this->assertSame($expectedResult, $printer->printFormatPreserving($newNode, $phpDocNode, $tokens));
$newPhpDoc = $printer->printFormatPreserving($newNode, $phpDocNode, $tokens);
$this->assertSame($expectedResult, $newPhpDoc);

$newTokens = new TokenIterator($lexer->tokenize($newPhpDoc));
$this->assertEquals(
$this->unsetAttributes($newNode),
$this->unsetAttributes($phpDocParser->parse($newTokens))
);
}

private function unsetAttributes(PhpDocNode $node): PhpDocNode
{
$visitor = new class extends AbstractNodeVisitor {

public function enterNode(Node $node)
{
$node->setAttribute(Attribute::START_LINE, null);
$node->setAttribute(Attribute::END_LINE, null);
$node->setAttribute(Attribute::START_INDEX, null);
$node->setAttribute(Attribute::END_INDEX, null);
$node->setAttribute(Attribute::ORIGINAL_NODE, null);

return $node;
}

};

$traverser = new NodeTraverser([$visitor]);

/** @var PhpDocNode */
return $traverser->traverse([$node])[0];
}

}

0 comments on commit cf694fd

Please sign in to comment.