Skip to content

Commit 90490bd

Browse files
committed
TypeParserTest - verify indexes by concatenating token values
1 parent 08ccb8d commit 90490bd

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

tests/PHPStan/Parser/TypeParserTest.php

+21-3
Original file line numberDiff line numberDiff line change
@@ -1920,6 +1920,7 @@ public function dataLinesAndIndexes(): iterable
19201920
static function (TypeNode $typeNode): TypeNode {
19211921
return $typeNode;
19221922
},
1923+
'int | object{foo: int}[]',
19231924
1,
19241925
1,
19251926
0,
@@ -1929,6 +1930,7 @@ static function (TypeNode $typeNode): TypeNode {
19291930
static function (UnionTypeNode $typeNode): TypeNode {
19301931
return $typeNode->types[0];
19311932
},
1933+
'int',
19321934
1,
19331935
1,
19341936
0,
@@ -1938,6 +1940,7 @@ static function (UnionTypeNode $typeNode): TypeNode {
19381940
static function (UnionTypeNode $typeNode): TypeNode {
19391941
return $typeNode->types[1];
19401942
},
1943+
'object{foo: int}[]',
19411944
1,
19421945
1,
19431946
4,
@@ -1953,6 +1956,7 @@ static function (UnionTypeNode $typeNode): TypeNode {
19531956
static function (TypeNode $typeNode): TypeNode {
19541957
return $typeNode;
19551958
},
1959+
'int | object{foo: int}[]',
19561960
1,
19571961
1,
19581962
0,
@@ -1962,6 +1966,7 @@ static function (TypeNode $typeNode): TypeNode {
19621966
static function (UnionTypeNode $typeNode): TypeNode {
19631967
return $typeNode->types[0];
19641968
},
1969+
'int',
19651970
1,
19661971
1,
19671972
0,
@@ -1971,6 +1976,7 @@ static function (UnionTypeNode $typeNode): TypeNode {
19711976
static function (UnionTypeNode $typeNode): TypeNode {
19721977
return $typeNode->types[1];
19731978
},
1979+
'object{foo: int}[]',
19741980
1,
19751981
1,
19761982
4,
@@ -1989,6 +1995,10 @@ static function (UnionTypeNode $typeNode): TypeNode {
19891995
static function (TypeNode $typeNode): TypeNode {
19901996
return $typeNode;
19911997
},
1998+
'array{
1999+
a: int,
2000+
b: string
2001+
}',
19922002
1,
19932003
4,
19942004
0,
@@ -2000,19 +2010,27 @@ static function (TypeNode $typeNode): TypeNode {
20002010

20012011
/**
20022012
* @dataProvider dataLinesAndIndexes
2003-
* @param list<array{callable(TypeNode): TypeNode, int, int, int, int}> $assertions
2013+
* @param list<array{callable(TypeNode): TypeNode, string, int, int, int, int}> $assertions
20042014
*/
20052015
public function testLinesAndIndexes(string $input, array $assertions): void
20062016
{
2007-
$tokens = new TokenIterator($this->lexer->tokenize($input));
2017+
$tokensArray = $this->lexer->tokenize($input);
2018+
$tokens = new TokenIterator($tokensArray);
20082019
$typeParser = new TypeParser(new ConstExprParser(true, true), true, [
20092020
'lines' => true,
20102021
'indexes' => true,
20112022
]);
20122023
$typeNode = $typeParser->parse($tokens);
20132024

2014-
foreach ($assertions as [$callable, $startLine, $endLine, $startIndex, $endIndex]) {
2025+
foreach ($assertions as [$callable, $expectedContent, $startLine, $endLine, $startIndex, $endIndex]) {
20152026
$typeToAssert = $callable($typeNode);
2027+
2028+
$content = '';
2029+
for ($i = $startIndex; $i <= $endIndex; $i++) {
2030+
$content .= $tokensArray[$i][Lexer::VALUE_OFFSET];
2031+
}
2032+
2033+
$this->assertSame($expectedContent, $content);
20162034
$this->assertSame($startLine, $typeToAssert->getAttribute(Attribute::START_LINE));
20172035
$this->assertSame($endLine, $typeToAssert->getAttribute(Attribute::END_LINE));
20182036
$this->assertSame($startIndex, $typeToAssert->getAttribute(Attribute::START_INDEX));

0 commit comments

Comments
 (0)