Skip to content

Commit cf694fd

Browse files
committed
PrinterTest - parse the new printed node again and verify the AST is the same
1 parent 0b4de96 commit cf694fd

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

Diff for: tests/PHPStan/Printer/PrinterTest.php

+32-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPStan\PhpDocParser\Printer;
44

55
use PHPStan\PhpDocParser\Ast\AbstractNodeVisitor;
6+
use PHPStan\PhpDocParser\Ast\Attribute;
67
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprArrayItemNode;
78
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprArrayNode;
89
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
@@ -1173,7 +1174,37 @@ public function testPrintFormatPreserving(string $phpDoc, string $expectedResult
11731174
[$newNode] = $changingTraverser->traverse($newNodes);
11741175

11751176
$printer = new Printer();
1176-
$this->assertSame($expectedResult, $printer->printFormatPreserving($newNode, $phpDocNode, $tokens));
1177+
$newPhpDoc = $printer->printFormatPreserving($newNode, $phpDocNode, $tokens);
1178+
$this->assertSame($expectedResult, $newPhpDoc);
1179+
1180+
$newTokens = new TokenIterator($lexer->tokenize($newPhpDoc));
1181+
$this->assertEquals(
1182+
$this->unsetAttributes($newNode),
1183+
$this->unsetAttributes($phpDocParser->parse($newTokens))
1184+
);
1185+
}
1186+
1187+
private function unsetAttributes(PhpDocNode $node): PhpDocNode
1188+
{
1189+
$visitor = new class extends AbstractNodeVisitor {
1190+
1191+
public function enterNode(Node $node)
1192+
{
1193+
$node->setAttribute(Attribute::START_LINE, null);
1194+
$node->setAttribute(Attribute::END_LINE, null);
1195+
$node->setAttribute(Attribute::START_INDEX, null);
1196+
$node->setAttribute(Attribute::END_INDEX, null);
1197+
$node->setAttribute(Attribute::ORIGINAL_NODE, null);
1198+
1199+
return $node;
1200+
}
1201+
1202+
};
1203+
1204+
$traverser = new NodeTraverser([$visitor]);
1205+
1206+
/** @var PhpDocNode */
1207+
return $traverser->traverse([$node])[0];
11771208
}
11781209

11791210
}

0 commit comments

Comments
 (0)