From 90490bd8fd8530a272043c4950c180b6d0cf5f81 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Sat, 22 Apr 2023 14:54:28 +0200 Subject: [PATCH] TypeParserTest - verify indexes by concatenating token values --- tests/PHPStan/Parser/TypeParserTest.php | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/tests/PHPStan/Parser/TypeParserTest.php b/tests/PHPStan/Parser/TypeParserTest.php index b680f4d8..81cf75ae 100644 --- a/tests/PHPStan/Parser/TypeParserTest.php +++ b/tests/PHPStan/Parser/TypeParserTest.php @@ -1920,6 +1920,7 @@ public function dataLinesAndIndexes(): iterable static function (TypeNode $typeNode): TypeNode { return $typeNode; }, + 'int | object{foo: int}[]', 1, 1, 0, @@ -1929,6 +1930,7 @@ static function (TypeNode $typeNode): TypeNode { static function (UnionTypeNode $typeNode): TypeNode { return $typeNode->types[0]; }, + 'int', 1, 1, 0, @@ -1938,6 +1940,7 @@ static function (UnionTypeNode $typeNode): TypeNode { static function (UnionTypeNode $typeNode): TypeNode { return $typeNode->types[1]; }, + 'object{foo: int}[]', 1, 1, 4, @@ -1953,6 +1956,7 @@ static function (UnionTypeNode $typeNode): TypeNode { static function (TypeNode $typeNode): TypeNode { return $typeNode; }, + 'int | object{foo: int}[]', 1, 1, 0, @@ -1962,6 +1966,7 @@ static function (TypeNode $typeNode): TypeNode { static function (UnionTypeNode $typeNode): TypeNode { return $typeNode->types[0]; }, + 'int', 1, 1, 0, @@ -1971,6 +1976,7 @@ static function (UnionTypeNode $typeNode): TypeNode { static function (UnionTypeNode $typeNode): TypeNode { return $typeNode->types[1]; }, + 'object{foo: int}[]', 1, 1, 4, @@ -1989,6 +1995,10 @@ static function (UnionTypeNode $typeNode): TypeNode { static function (TypeNode $typeNode): TypeNode { return $typeNode; }, + 'array{ + a: int, + b: string + }', 1, 4, 0, @@ -2000,19 +2010,27 @@ static function (TypeNode $typeNode): TypeNode { /** * @dataProvider dataLinesAndIndexes - * @param list $assertions + * @param list $assertions */ public function testLinesAndIndexes(string $input, array $assertions): void { - $tokens = new TokenIterator($this->lexer->tokenize($input)); + $tokensArray = $this->lexer->tokenize($input); + $tokens = new TokenIterator($tokensArray); $typeParser = new TypeParser(new ConstExprParser(true, true), true, [ 'lines' => true, 'indexes' => true, ]); $typeNode = $typeParser->parse($tokens); - foreach ($assertions as [$callable, $startLine, $endLine, $startIndex, $endIndex]) { + foreach ($assertions as [$callable, $expectedContent, $startLine, $endLine, $startIndex, $endIndex]) { $typeToAssert = $callable($typeNode); + + $content = ''; + for ($i = $startIndex; $i <= $endIndex; $i++) { + $content .= $tokensArray[$i][Lexer::VALUE_OFFSET]; + } + + $this->assertSame($expectedContent, $content); $this->assertSame($startLine, $typeToAssert->getAttribute(Attribute::START_LINE)); $this->assertSame($endLine, $typeToAssert->getAttribute(Attribute::END_LINE)); $this->assertSame($startIndex, $typeToAssert->getAttribute(Attribute::START_INDEX));