TokenStreamTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('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->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('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. }