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

116 lines
4.0 KiB
PHP
Raw Normal View History

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