Skip to content

Commit

Permalink
TypeParserTest - verify indexes by concatenating token values
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 22, 2023
1 parent 08ccb8d commit 90490bd
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions tests/PHPStan/Parser/TypeParserTest.php
Expand Up @@ -1920,6 +1920,7 @@ public function dataLinesAndIndexes(): iterable
static function (TypeNode $typeNode): TypeNode {
return $typeNode;
},
'int | object{foo: int}[]',
1,
1,
0,
Expand All @@ -1929,6 +1930,7 @@ static function (TypeNode $typeNode): TypeNode {
static function (UnionTypeNode $typeNode): TypeNode {
return $typeNode->types[0];
},
'int',
1,
1,
0,
Expand All @@ -1938,6 +1940,7 @@ static function (UnionTypeNode $typeNode): TypeNode {
static function (UnionTypeNode $typeNode): TypeNode {
return $typeNode->types[1];
},
'object{foo: int}[]',
1,
1,
4,
Expand All @@ -1953,6 +1956,7 @@ static function (UnionTypeNode $typeNode): TypeNode {
static function (TypeNode $typeNode): TypeNode {
return $typeNode;
},
'int | object{foo: int}[]',
1,
1,
0,
Expand All @@ -1962,6 +1966,7 @@ static function (TypeNode $typeNode): TypeNode {
static function (UnionTypeNode $typeNode): TypeNode {
return $typeNode->types[0];
},
'int',
1,
1,
0,
Expand All @@ -1971,6 +1976,7 @@ static function (UnionTypeNode $typeNode): TypeNode {
static function (UnionTypeNode $typeNode): TypeNode {
return $typeNode->types[1];
},
'object{foo: int}[]',
1,
1,
4,
Expand All @@ -1989,6 +1995,10 @@ static function (UnionTypeNode $typeNode): TypeNode {
static function (TypeNode $typeNode): TypeNode {
return $typeNode;
},
'array{
a: int,
b: string
}',
1,
4,
0,
Expand All @@ -2000,19 +2010,27 @@ static function (TypeNode $typeNode): TypeNode {

/**
* @dataProvider dataLinesAndIndexes
* @param list<array{callable(TypeNode): TypeNode, int, int, int, int}> $assertions
* @param list<array{callable(TypeNode): TypeNode, string, int, int, int, int}> $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));
Expand Down

0 comments on commit 90490bd

Please sign in to comment.