2012-02-21 18:45:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class PHPParser_Tests_Lexer_EmulativeTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
2012-04-25 20:04:46 +02:00
|
|
|
/** @var PHPParser_Lexer_Emulative */
|
|
|
|
protected $lexer;
|
|
|
|
|
|
|
|
protected function setUp() {
|
|
|
|
$this->lexer = new PHPParser_Lexer_Emulative;
|
|
|
|
}
|
|
|
|
|
2012-02-21 18:45:07 +01:00
|
|
|
/**
|
|
|
|
* @dataProvider provideTestReplaceKeywords
|
|
|
|
*/
|
|
|
|
public function testReplaceKeywords($keyword, $expectedToken) {
|
2012-04-25 20:04:46 +02:00
|
|
|
$this->lexer->startLexing('<?php ' . $keyword);
|
2012-02-21 18:45:07 +01:00
|
|
|
|
2012-04-25 20:04:46 +02:00
|
|
|
$this->assertEquals($expectedToken, $this->lexer->getNextToken());
|
|
|
|
$this->assertEquals(0, $this->lexer->getNextToken());
|
2012-02-21 18:45:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideTestReplaceKeywords
|
|
|
|
*/
|
|
|
|
public function testNoReplaceKeywordsAfterObjectOperator($keyword) {
|
2012-04-25 20:04:46 +02:00
|
|
|
$this->lexer->startLexing('<?php ->' . $keyword);
|
2012-02-21 18:45:07 +01:00
|
|
|
|
2012-04-25 20:04:46 +02:00
|
|
|
$this->assertEquals(PHPParser_Parser::T_OBJECT_OPERATOR, $this->lexer->getNextToken());
|
|
|
|
$this->assertEquals(PHPParser_Parser::T_STRING, $this->lexer->getNextToken());
|
|
|
|
$this->assertEquals(0, $this->lexer->getNextToken());
|
2012-02-21 18:45:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideTestReplaceKeywords() {
|
|
|
|
return array(
|
|
|
|
array('callable', PHPParser_Parser::T_CALLABLE),
|
|
|
|
array('insteadof', PHPParser_Parser::T_INSTEADOF),
|
|
|
|
array('trait', PHPParser_Parser::T_TRAIT),
|
|
|
|
array('__TRAIT__', PHPParser_Parser::T_TRAIT_C),
|
|
|
|
array('__DIR__', PHPParser_Parser::T_DIR),
|
|
|
|
array('goto', PHPParser_Parser::T_GOTO),
|
|
|
|
array('namespace', PHPParser_Parser::T_NAMESPACE),
|
|
|
|
array('__NAMESPACE__', PHPParser_Parser::T_NS_C),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideTestLexNewFeatures
|
|
|
|
*/
|
|
|
|
public function testLexNewFeatures($code, array $expectedTokens) {
|
2012-04-25 20:04:46 +02:00
|
|
|
$this->lexer->startLexing('<?php ' . $code);
|
2012-02-21 18:45:07 +01:00
|
|
|
|
|
|
|
foreach ($expectedTokens as $expectedToken) {
|
|
|
|
list($expectedTokenType, $expectedTokenText) = $expectedToken;
|
2012-04-25 20:04:46 +02:00
|
|
|
$this->assertEquals($expectedTokenType, $this->lexer->getNextToken($text));
|
2012-02-21 18:45:07 +01:00
|
|
|
$this->assertEquals($expectedTokenText, $text);
|
|
|
|
}
|
2012-04-25 20:04:46 +02:00
|
|
|
$this->assertEquals(0, $this->lexer->getNextToken());
|
2012-02-21 18:45:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideTestLexNewFeatures
|
|
|
|
*/
|
|
|
|
public function testLeaveStuffAloneInStrings($code) {
|
|
|
|
$stringifiedToken = '"' . addcslashes($code, '"\\') . '"';
|
2012-04-25 20:04:46 +02:00
|
|
|
$this->lexer->startLexing('<?php ' . $stringifiedToken);
|
2012-02-21 18:45:07 +01:00
|
|
|
|
2012-04-25 20:04:46 +02:00
|
|
|
$this->assertEquals(PHPParser_Parser::T_CONSTANT_ENCAPSED_STRING, $this->lexer->getNextToken($text));
|
2012-02-21 18:45:07 +01:00
|
|
|
$this->assertEquals($stringifiedToken, $text);
|
2012-04-25 20:04:46 +02:00
|
|
|
$this->assertEquals(0, $this->lexer->getNextToken());
|
2012-02-21 18:45:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideTestLexNewFeatures() {
|
|
|
|
return array(
|
|
|
|
array('0b1010110', array(
|
|
|
|
array(PHPParser_Parser::T_LNUMBER, '0b1010110'),
|
|
|
|
)),
|
2012-06-08 18:09:42 +02:00
|
|
|
array('0b1011010101001010110101010010101011010101010101101011001110111100', array(
|
|
|
|
array(PHPParser_Parser::T_DNUMBER, '0b1011010101001010110101010010101011010101010101101011001110111100'),
|
2012-02-21 18:45:07 +01:00
|
|
|
)),
|
|
|
|
array('\\', array(
|
|
|
|
array(PHPParser_Parser::T_NS_SEPARATOR, '\\'),
|
|
|
|
)),
|
|
|
|
array("<<<'NOWDOC'\nNOWDOC;\n", array(
|
|
|
|
array(PHPParser_Parser::T_START_HEREDOC, "<<<'NOWDOC'\n"),
|
|
|
|
array(PHPParser_Parser::T_END_HEREDOC, 'NOWDOC'),
|
|
|
|
array(ord(';'), ';'),
|
|
|
|
)),
|
|
|
|
array("<<<'NOWDOC'\nFoobar\nNOWDOC;\n", array(
|
|
|
|
array(PHPParser_Parser::T_START_HEREDOC, "<<<'NOWDOC'\n"),
|
|
|
|
array(PHPParser_Parser::T_ENCAPSED_AND_WHITESPACE, "Foobar\n"),
|
|
|
|
array(PHPParser_Parser::T_END_HEREDOC, 'NOWDOC'),
|
|
|
|
array(ord(';'), ';'),
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|