phpdoc-parser/tests/PHPStan/Parser/ConstExprParserTest.php

362 lines
6.3 KiB
PHP
Raw Normal View History

2017-11-18 21:58:26 +01:00
<?php declare(strict_types = 1);
namespace PHPStan\PhpDocParser\Parser;
2018-04-12 16:16:37 +02:00
use Iterator;
2017-11-18 21:58:26 +01:00
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprArrayItemNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprArrayNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprFalseNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprFloatNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprNullNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprStringNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprTrueNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPUnit\Framework\TestCase;
2017-11-18 21:58:26 +01:00
class ConstExprParserTest extends TestCase
2017-11-18 21:58:26 +01:00
{
/** @var Lexer */
private $lexer;
/** @var ConstExprParser */
private $constExprParser;
2019-04-23 21:27:03 +02:00
protected function setUp(): void
2017-11-18 21:58:26 +01:00
{
parent::setUp();
$this->lexer = new Lexer();
$this->constExprParser = new ConstExprParser();
}
/**
* @dataProvider provideTrueNodeParseData
* @dataProvider provideFalseNodeParseData
* @dataProvider provideNullNodeParseData
* @dataProvider provideIntegerNodeParseData
* @dataProvider provideFloatNodeParseData
* @dataProvider provideStringNodeParseData
* @dataProvider provideArrayNodeParseData
* @dataProvider provideFetchNodeParseData
2017-11-18 21:58:26 +01:00
*/
2019-04-23 21:27:03 +02:00
public function testParse(string $input, ConstExprNode $expectedExpr, int $nextTokenType = Lexer::TOKEN_END): void
2017-11-18 21:58:26 +01:00
{
$tokens = new TokenIterator($this->lexer->tokenize($input));
$exprNode = $this->constExprParser->parse($tokens);
$this->assertSame((string) $expectedExpr, (string) $exprNode);
$this->assertEquals($expectedExpr, $exprNode);
$this->assertSame($nextTokenType, $tokens->currentTokenType());
}
2018-04-12 16:16:37 +02:00
public function provideTrueNodeParseData(): Iterator
2017-11-18 21:58:26 +01:00
{
yield [
'true',
new ConstExprTrueNode(),
];
yield [
'TRUE',
new ConstExprTrueNode(),
];
yield [
'tRUe',
new ConstExprTrueNode(),
];
}
2018-04-12 16:16:37 +02:00
public function provideFalseNodeParseData(): Iterator
{
yield [
'false',
new ConstExprFalseNode(),
];
yield [
'FALSE',
new ConstExprFalseNode(),
];
yield [
'fALse',
new ConstExprFalseNode(),
];
}
2018-04-12 16:16:37 +02:00
public function provideNullNodeParseData(): Iterator
{
yield [
'null',
new ConstExprNullNode(),
];
yield [
'NULL',
new ConstExprNullNode(),
];
yield [
'nULl',
new ConstExprNullNode(),
];
}
2018-04-12 16:16:37 +02:00
public function provideIntegerNodeParseData(): Iterator
{
yield [
'123',
new ConstExprIntegerNode('123'),
];
yield [
'0b0110101',
new ConstExprIntegerNode('0b0110101'),
];
yield [
'0o777',
new ConstExprIntegerNode('0o777'),
];
yield [
'0x7Fb4',
new ConstExprIntegerNode('0x7Fb4'),
];
yield [
'-0O777',
new ConstExprIntegerNode('-0O777'),
];
yield [
'-0X7Fb4',
new ConstExprIntegerNode('-0X7Fb4'),
];
}
2018-04-12 16:16:37 +02:00
public function provideFloatNodeParseData(): Iterator
{
yield [
'123.4',
new ConstExprFloatNode('123.4'),
];
yield [
'.123',
new ConstExprFloatNode('.123'),
];
yield [
'123.',
new ConstExprFloatNode('123.'),
];
yield [
'123e4',
new ConstExprFloatNode('123e4'),
];
yield [
'123E4',
new ConstExprFloatNode('123E4'),
];
yield [
'12.3e4',
new ConstExprFloatNode('12.3e4'),
];
yield [
'-123',
new ConstExprIntegerNode('-123'),
];
yield [
'-123.4',
new ConstExprFloatNode('-123.4'),
];
yield [
'-.123',
new ConstExprFloatNode('-.123'),
];
yield [
'-123.',
new ConstExprFloatNode('-123.'),
];
yield [
'-123e-4',
new ConstExprFloatNode('-123e-4'),
];
yield [
'-12.3e-4',
new ConstExprFloatNode('-12.3e-4'),
];
}
2018-04-12 16:16:37 +02:00
public function provideStringNodeParseData(): Iterator
{
yield [
'"foo"',
new ConstExprStringNode('"foo"'),
];
yield [
'"Foo \\n\\"\\r Bar"',
new ConstExprStringNode('"Foo \\n\\"\\r Bar"'),
];
yield [
'\'bar\'',
new ConstExprStringNode('\'bar\''),
];
yield [
'\'Foo \\\' Bar\'',
new ConstExprStringNode('\'Foo \\\' Bar\''),
];
}
2018-04-12 16:16:37 +02:00
public function provideArrayNodeParseData(): Iterator
{
yield [
'[]',
new ConstExprArrayNode([]),
];
yield [
'[123]',
new ConstExprArrayNode([
new ConstExprArrayItemNode(
null,
new ConstExprIntegerNode('123')
),
]),
];
yield [
'[1, 2, 3]',
new ConstExprArrayNode([
new ConstExprArrayItemNode(
null,
new ConstExprIntegerNode('1')
),
new ConstExprArrayItemNode(
null,
new ConstExprIntegerNode('2')
),
new ConstExprArrayItemNode(
null,
new ConstExprIntegerNode('3')
),
]),
];
yield [
'[1, 2, 3, ]',
new ConstExprArrayNode([
new ConstExprArrayItemNode(
null,
new ConstExprIntegerNode('1')
),
new ConstExprArrayItemNode(
null,
new ConstExprIntegerNode('2')
),
new ConstExprArrayItemNode(
null,
new ConstExprIntegerNode('3')
),
]),
];
yield [
'[1 => 2]',
new ConstExprArrayNode([
new ConstExprArrayItemNode(
new ConstExprIntegerNode('1'),
new ConstExprIntegerNode('2')
),
]),
];
yield [
'[1 => 2, 3]',
new ConstExprArrayNode([
new ConstExprArrayItemNode(
new ConstExprIntegerNode('1'),
new ConstExprIntegerNode('2')
),
new ConstExprArrayItemNode(
null,
new ConstExprIntegerNode('3')
),
]),
];
yield [
'[1, [2, 3]]',
new ConstExprArrayNode([
new ConstExprArrayItemNode(
null,
new ConstExprIntegerNode('1')
),
new ConstExprArrayItemNode(
null,
new ConstExprArrayNode([
new ConstExprArrayItemNode(
null,
new ConstExprIntegerNode('2')
),
new ConstExprArrayItemNode(
null,
new ConstExprIntegerNode('3')
),
])
),
]),
];
}
2018-04-12 16:16:37 +02:00
public function provideFetchNodeParseData(): Iterator
{
yield [
'GLOBAL_CONSTANT',
new ConstFetchNode('', 'GLOBAL_CONSTANT'),
];
yield [
'Foo\\Bar\\GLOBAL_CONSTANT',
new ConstFetchNode('', 'Foo\\Bar\\GLOBAL_CONSTANT'),
];
yield [
'Foo\\Bar::CLASS_CONSTANT',
new ConstFetchNode('Foo\\Bar', 'CLASS_CONSTANT'),
];
yield [
'self::CLASS_CONSTANT',
new ConstFetchNode('self', 'CLASS_CONSTANT'),
2017-11-18 21:58:26 +01:00
];
}
}