TokenStreamTest.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\CssSelector\Tests\Parser;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\CssSelector\Parser\Token;
  13. use Symfony\Component\CssSelector\Parser\TokenStream;
  14. class TokenStreamTest extends TestCase
  15. {
  16. public function testGetNext()
  17. {
  18. $stream = new TokenStream();
  19. $stream->push($t1 = new Token(Token::TYPE_IDENTIFIER, 'h1', 0));
  20. $stream->push($t2 = new Token(Token::TYPE_DELIMITER, '.', 2));
  21. $stream->push($t3 = new Token(Token::TYPE_IDENTIFIER, 'title', 3));
  22. $this->assertSame($t1, $stream->getNext());
  23. $this->assertSame($t2, $stream->getNext());
  24. $this->assertSame($t3, $stream->getNext());
  25. }
  26. public function testGetPeek()
  27. {
  28. $stream = new TokenStream();
  29. $stream->push($t1 = new Token(Token::TYPE_IDENTIFIER, 'h1', 0));
  30. $stream->push($t2 = new Token(Token::TYPE_DELIMITER, '.', 2));
  31. $stream->push($t3 = new Token(Token::TYPE_IDENTIFIER, 'title', 3));
  32. $this->assertSame($t1, $stream->getPeek());
  33. $this->assertSame($t1, $stream->getNext());
  34. $this->assertSame($t2, $stream->getPeek());
  35. $this->assertSame($t2, $stream->getPeek());
  36. $this->assertSame($t2, $stream->getNext());
  37. }
  38. public function testGetNextIdentifier()
  39. {
  40. $stream = new TokenStream();
  41. $stream->push(new Token(Token::TYPE_IDENTIFIER, 'h1', 0));
  42. $this->assertEquals('h1', $stream->getNextIdentifier());
  43. }
  44. public function testFailToGetNextIdentifier()
  45. {
  46. $this->expectException('Symfony\Component\CssSelector\Exception\SyntaxErrorException');
  47. $stream = new TokenStream();
  48. $stream->push(new Token(Token::TYPE_DELIMITER, '.', 2));
  49. $stream->getNextIdentifier();
  50. }
  51. public function testGetNextIdentifierOrStar()
  52. {
  53. $stream = new TokenStream();
  54. $stream->push(new Token(Token::TYPE_IDENTIFIER, 'h1', 0));
  55. $this->assertEquals('h1', $stream->getNextIdentifierOrStar());
  56. $stream->push(new Token(Token::TYPE_DELIMITER, '*', 0));
  57. $this->assertNull($stream->getNextIdentifierOrStar());
  58. }
  59. public function testFailToGetNextIdentifierOrStar()
  60. {
  61. $this->expectException('Symfony\Component\CssSelector\Exception\SyntaxErrorException');
  62. $stream = new TokenStream();
  63. $stream->push(new Token(Token::TYPE_DELIMITER, '.', 2));
  64. $stream->getNextIdentifierOrStar();
  65. }
  66. public function testSkipWhitespace()
  67. {
  68. $stream = new TokenStream();
  69. $stream->push($t1 = new Token(Token::TYPE_IDENTIFIER, 'h1', 0));
  70. $stream->push($t2 = new Token(Token::TYPE_WHITESPACE, ' ', 2));
  71. $stream->push($t3 = new Token(Token::TYPE_IDENTIFIER, 'h1', 3));
  72. $stream->skipWhitespace();
  73. $this->assertSame($t1, $stream->getNext());
  74. $stream->skipWhitespace();
  75. $this->assertSame($t3, $stream->getNext());
  76. }
  77. }