php-parser/test/PhpParser/Lexer/EmulativeTest.php

134 lines
4.2 KiB
PHP
Raw Normal View History

2012-02-21 18:45:07 +01:00
<?php
namespace PhpParser\Lexer;
use PhpParser\LexerTest;
use PhpParser\Parser\Tokens;
require_once __DIR__ . '/../LexerTest.php';
class EmulativeTest extends LexerTest
2012-02-21 18:45:07 +01:00
{
2017-08-13 14:35:03 +02:00
protected function getLexer(array $options = []) {
return new Emulative($options);
}
2012-02-21 18:45:07 +01:00
/**
* @dataProvider provideTestReplaceKeywords
*/
public function testReplaceKeywords($keyword, $expectedToken) {
$lexer = $this->getLexer();
$lexer->startLexing('<?php ' . $keyword);
2012-02-21 18:45:07 +01:00
$this->assertSame($expectedToken, $lexer->getNextToken());
$this->assertSame(0, $lexer->getNextToken());
2012-02-21 18:45:07 +01:00
}
/**
* @dataProvider provideTestReplaceKeywords
*/
public function testNoReplaceKeywordsAfterObjectOperator($keyword) {
$lexer = $this->getLexer();
$lexer->startLexing('<?php ->' . $keyword);
2012-02-21 18:45:07 +01:00
$this->assertSame(Tokens::T_OBJECT_OPERATOR, $lexer->getNextToken());
$this->assertSame(Tokens::T_STRING, $lexer->getNextToken());
$this->assertSame(0, $lexer->getNextToken());
2012-02-21 18:45:07 +01:00
}
public function provideTestReplaceKeywords() {
2017-08-13 14:35:03 +02:00
return [
// PHP 5.5
2017-08-13 14:35:03 +02:00
['finally', Tokens::T_FINALLY],
['yield', Tokens::T_YIELD],
// PHP 5.4
2017-08-13 14:35:03 +02:00
['callable', Tokens::T_CALLABLE],
['insteadof', Tokens::T_INSTEADOF],
['trait', Tokens::T_TRAIT],
['__TRAIT__', Tokens::T_TRAIT_C],
// PHP 5.3
2017-08-13 14:35:03 +02:00
['__DIR__', Tokens::T_DIR],
['goto', Tokens::T_GOTO],
['namespace', Tokens::T_NAMESPACE],
['__NAMESPACE__', Tokens::T_NS_C],
];
2012-02-21 18:45:07 +01:00
}
/**
* @dataProvider provideTestLexNewFeatures
*/
public function testLexNewFeatures($code, array $expectedTokens) {
$lexer = $this->getLexer();
$lexer->startLexing('<?php ' . $code);
2012-02-21 18:45:07 +01:00
foreach ($expectedTokens as $expectedToken) {
list($expectedTokenType, $expectedTokenText) = $expectedToken;
$this->assertSame($expectedTokenType, $lexer->getNextToken($text));
2014-09-30 20:38:09 +02:00
$this->assertSame($expectedTokenText, $text);
2012-02-21 18:45:07 +01:00
}
$this->assertSame(0, $lexer->getNextToken());
2012-02-21 18:45:07 +01:00
}
/**
* @dataProvider provideTestLexNewFeatures
*/
public function testLeaveStuffAloneInStrings($code) {
$stringifiedToken = '"' . addcslashes($code, '"\\') . '"';
$lexer = $this->getLexer();
$lexer->startLexing('<?php ' . $stringifiedToken);
$this->assertSame(Tokens::T_CONSTANT_ENCAPSED_STRING, $lexer->getNextToken($text));
2014-09-30 20:38:09 +02:00
$this->assertSame($stringifiedToken, $text);
$this->assertSame(0, $lexer->getNextToken());
2012-02-21 18:45:07 +01:00
}
public function provideTestLexNewFeatures() {
2017-08-13 14:35:03 +02:00
return [
['yield from', [
[Tokens::T_YIELD_FROM, 'yield from'],
]],
["yield\r\nfrom", [
[Tokens::T_YIELD_FROM, "yield\r\nfrom"],
]],
['...', [
[Tokens::T_ELLIPSIS, '...'],
]],
['**', [
[Tokens::T_POW, '**'],
]],
['**=', [
[Tokens::T_POW_EQUAL, '**='],
]],
['??', [
[Tokens::T_COALESCE, '??'],
]],
['<=>', [
[Tokens::T_SPACESHIP, '<=>'],
]],
['0b1010110', [
[Tokens::T_LNUMBER, '0b1010110'],
]],
['0b1011010101001010110101010010101011010101010101101011001110111100', [
[Tokens::T_DNUMBER, '0b1011010101001010110101010010101011010101010101101011001110111100'],
]],
['\\', [
[Tokens::T_NS_SEPARATOR, '\\'],
]],
["<<<'NOWDOC'\nNOWDOC;\n", [
[Tokens::T_START_HEREDOC, "<<<'NOWDOC'\n"],
[Tokens::T_END_HEREDOC, 'NOWDOC'],
[ord(';'), ';'],
]],
["<<<'NOWDOC'\nFoobar\nNOWDOC;\n", [
[Tokens::T_START_HEREDOC, "<<<'NOWDOC'\n"],
[Tokens::T_ENCAPSED_AND_WHITESPACE, "Foobar\n"],
[Tokens::T_END_HEREDOC, 'NOWDOC'],
[ord(';'), ';'],
]],
];
2012-02-21 18:45:07 +01:00
}
}