EmulativeTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace PhpParser\Lexer;
  3. use PhpParser\LexerTest;
  4. use PhpParser\Parser\Tokens;
  5. require_once __DIR__ . '/../LexerTest.php';
  6. class EmulativeTest extends LexerTest
  7. {
  8. protected function getLexer(array $options = array()) {
  9. return new Emulative($options);
  10. }
  11. /**
  12. * @dataProvider provideTestReplaceKeywords
  13. */
  14. public function testReplaceKeywords($keyword, $expectedToken) {
  15. $lexer = $this->getLexer();
  16. $lexer->startLexing('<?php ' . $keyword);
  17. $this->assertSame($expectedToken, $lexer->getNextToken());
  18. $this->assertSame(0, $lexer->getNextToken());
  19. }
  20. /**
  21. * @dataProvider provideTestReplaceKeywords
  22. */
  23. public function testNoReplaceKeywordsAfterObjectOperator($keyword) {
  24. $lexer = $this->getLexer();
  25. $lexer->startLexing('<?php ->' . $keyword);
  26. $this->assertSame(Tokens::T_OBJECT_OPERATOR, $lexer->getNextToken());
  27. $this->assertSame(Tokens::T_STRING, $lexer->getNextToken());
  28. $this->assertSame(0, $lexer->getNextToken());
  29. }
  30. public function provideTestReplaceKeywords() {
  31. return array(
  32. // PHP 5.5
  33. array('finally', Tokens::T_FINALLY),
  34. array('yield', Tokens::T_YIELD),
  35. // PHP 5.4
  36. array('callable', Tokens::T_CALLABLE),
  37. array('insteadof', Tokens::T_INSTEADOF),
  38. array('trait', Tokens::T_TRAIT),
  39. array('__TRAIT__', Tokens::T_TRAIT_C),
  40. // PHP 5.3
  41. array('__DIR__', Tokens::T_DIR),
  42. array('goto', Tokens::T_GOTO),
  43. array('namespace', Tokens::T_NAMESPACE),
  44. array('__NAMESPACE__', Tokens::T_NS_C),
  45. );
  46. }
  47. /**
  48. * @dataProvider provideTestLexNewFeatures
  49. */
  50. public function testLexNewFeatures($code, array $expectedTokens) {
  51. $lexer = $this->getLexer();
  52. $lexer->startLexing('<?php ' . $code);
  53. foreach ($expectedTokens as $expectedToken) {
  54. list($expectedTokenType, $expectedTokenText) = $expectedToken;
  55. $this->assertSame($expectedTokenType, $lexer->getNextToken($text));
  56. $this->assertSame($expectedTokenText, $text);
  57. }
  58. $this->assertSame(0, $lexer->getNextToken());
  59. }
  60. /**
  61. * @dataProvider provideTestLexNewFeatures
  62. */
  63. public function testLeaveStuffAloneInStrings($code) {
  64. $stringifiedToken = '"' . addcslashes($code, '"\\') . '"';
  65. $lexer = $this->getLexer();
  66. $lexer->startLexing('<?php ' . $stringifiedToken);
  67. $this->assertSame(Tokens::T_CONSTANT_ENCAPSED_STRING, $lexer->getNextToken($text));
  68. $this->assertSame($stringifiedToken, $text);
  69. $this->assertSame(0, $lexer->getNextToken());
  70. }
  71. public function provideTestLexNewFeatures() {
  72. return array(
  73. array('yield from', array(
  74. array(Tokens::T_YIELD_FROM, 'yield from'),
  75. )),
  76. array("yield\r\nfrom", array(
  77. array(Tokens::T_YIELD_FROM, "yield\r\nfrom"),
  78. )),
  79. array('...', array(
  80. array(Tokens::T_ELLIPSIS, '...'),
  81. )),
  82. array('**', array(
  83. array(Tokens::T_POW, '**'),
  84. )),
  85. array('**=', array(
  86. array(Tokens::T_POW_EQUAL, '**='),
  87. )),
  88. array('??', array(
  89. array(Tokens::T_COALESCE, '??'),
  90. )),
  91. array('<=>', array(
  92. array(Tokens::T_SPACESHIP, '<=>'),
  93. )),
  94. array('0b1010110', array(
  95. array(Tokens::T_LNUMBER, '0b1010110'),
  96. )),
  97. array('0b1011010101001010110101010010101011010101010101101011001110111100', array(
  98. array(Tokens::T_DNUMBER, '0b1011010101001010110101010010101011010101010101101011001110111100'),
  99. )),
  100. array('\\', array(
  101. array(Tokens::T_NS_SEPARATOR, '\\'),
  102. )),
  103. array("<<<'NOWDOC'\nNOWDOC;\n", array(
  104. array(Tokens::T_START_HEREDOC, "<<<'NOWDOC'\n"),
  105. array(Tokens::T_END_HEREDOC, 'NOWDOC'),
  106. array(ord(';'), ';'),
  107. )),
  108. array("<<<'NOWDOC'\nFoobar\nNOWDOC;\n", array(
  109. array(Tokens::T_START_HEREDOC, "<<<'NOWDOC'\n"),
  110. array(Tokens::T_ENCAPSED_AND_WHITESPACE, "Foobar\n"),
  111. array(Tokens::T_END_HEREDOC, 'NOWDOC'),
  112. array(ord(';'), ';'),
  113. )),
  114. );
  115. }
  116. }