From cf694fda69b63cfa5ee61e9e657b6ab7efdf5229 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Fri, 28 Apr 2023 09:50:38 +0200 Subject: [PATCH] PrinterTest - parse the new printed node again and verify the AST is the same --- tests/PHPStan/Printer/PrinterTest.php | 33 ++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/tests/PHPStan/Printer/PrinterTest.php b/tests/PHPStan/Printer/PrinterTest.php index 69acfc55..b689b440 100644 --- a/tests/PHPStan/Printer/PrinterTest.php +++ b/tests/PHPStan/Printer/PrinterTest.php @@ -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; @@ -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]; } }