TokenTest.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php declare(strict_types=1);
  2. namespace PhpParser;
  3. class TokenTest extends \PHPUnit\Framework\TestCase {
  4. public function testGetTokenName(): void {
  5. $token = new Token(\ord(','), ',');
  6. $this->assertSame(',', $token->getTokenName());
  7. $token = new Token(\T_WHITESPACE, ' ');
  8. $this->assertSame('T_WHITESPACE', $token->getTokenName());
  9. }
  10. public function testIs(): void {
  11. $token = new Token(\ord(','), ',');
  12. $this->assertTrue($token->is(\ord(',')));
  13. $this->assertFalse($token->is(\ord(';')));
  14. $this->assertTrue($token->is(','));
  15. $this->assertFalse($token->is(';'));
  16. $this->assertTrue($token->is([\ord(','), \ord(';')]));
  17. $this->assertFalse($token->is([\ord('!'), \ord(';')]));
  18. $this->assertTrue($token->is([',', ';']));
  19. $this->assertFalse($token->is(['!', ';']));
  20. }
  21. /** @dataProvider provideTestIsIgnorable */
  22. public function testIsIgnorable(int $id, string $text, bool $isIgnorable): void {
  23. $token = new Token($id, $text);
  24. $this->assertSame($isIgnorable, $token->isIgnorable());
  25. }
  26. public static function provideTestIsIgnorable() {
  27. return [
  28. [\T_STRING, 'foo', false],
  29. [\T_WHITESPACE, ' ', true],
  30. [\T_COMMENT, '// foo', true],
  31. [\T_DOC_COMMENT, '/** foo */', true],
  32. [\T_OPEN_TAG, '<?php ', true],
  33. ];
  34. }
  35. public function testToString(): void {
  36. $token = new Token(\ord(','), ',');
  37. $this->assertSame(',', (string) $token);
  38. $token = new Token(\T_STRING, 'foo');
  39. $this->assertSame('foo', (string) $token);
  40. }
  41. }