ReaderTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\Reader;
  13. class ReaderTest extends TestCase
  14. {
  15. public function testIsEOF()
  16. {
  17. $reader = new Reader('');
  18. $this->assertTrue($reader->isEOF());
  19. $reader = new Reader('hello');
  20. $this->assertFalse($reader->isEOF());
  21. $this->assignPosition($reader, 2);
  22. $this->assertFalse($reader->isEOF());
  23. $this->assignPosition($reader, 5);
  24. $this->assertTrue($reader->isEOF());
  25. }
  26. public function testGetRemainingLength()
  27. {
  28. $reader = new Reader('hello');
  29. $this->assertEquals(5, $reader->getRemainingLength());
  30. $this->assignPosition($reader, 2);
  31. $this->assertEquals(3, $reader->getRemainingLength());
  32. $this->assignPosition($reader, 5);
  33. $this->assertEquals(0, $reader->getRemainingLength());
  34. }
  35. public function testGetSubstring()
  36. {
  37. $reader = new Reader('hello');
  38. $this->assertEquals('he', $reader->getSubstring(2));
  39. $this->assertEquals('el', $reader->getSubstring(2, 1));
  40. $this->assignPosition($reader, 2);
  41. $this->assertEquals('ll', $reader->getSubstring(2));
  42. $this->assertEquals('lo', $reader->getSubstring(2, 1));
  43. }
  44. public function testGetOffset()
  45. {
  46. $reader = new Reader('hello');
  47. $this->assertEquals(2, $reader->getOffset('ll'));
  48. $this->assertFalse($reader->getOffset('w'));
  49. $this->assignPosition($reader, 2);
  50. $this->assertEquals(0, $reader->getOffset('ll'));
  51. $this->assertFalse($reader->getOffset('he'));
  52. }
  53. public function testFindPattern()
  54. {
  55. $reader = new Reader('hello');
  56. $this->assertFalse($reader->findPattern('/world/'));
  57. $this->assertEquals(['hello', 'h'], $reader->findPattern('/^([a-z]).*/'));
  58. $this->assignPosition($reader, 2);
  59. $this->assertFalse($reader->findPattern('/^h.*/'));
  60. $this->assertEquals(['llo'], $reader->findPattern('/^llo$/'));
  61. }
  62. public function testMoveForward()
  63. {
  64. $reader = new Reader('hello');
  65. $this->assertEquals(0, $reader->getPosition());
  66. $reader->moveForward(2);
  67. $this->assertEquals(2, $reader->getPosition());
  68. }
  69. public function testToEnd()
  70. {
  71. $reader = new Reader('hello');
  72. $reader->moveToEnd();
  73. $this->assertTrue($reader->isEOF());
  74. }
  75. private function assignPosition(Reader $reader, $value)
  76. {
  77. $position = new \ReflectionProperty($reader, 'position');
  78. $position->setAccessible(true);
  79. $position->setValue($reader, $value);
  80. }
  81. }