ScannerTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * @file
  4. * Test the Scanner. This requires the InputStream tests are all good.
  5. */
  6. namespace Masterminds\HTML5\Tests\Parser;
  7. use Masterminds\HTML5\Parser\StringInputStream;
  8. use Masterminds\HTML5\Parser\Scanner;
  9. class ScannerTest extends \Masterminds\HTML5\Tests\TestCase
  10. {
  11. /**
  12. * A canary test to make sure the basics are setup and working.
  13. */
  14. public function testConstructDeprecated()
  15. {
  16. $is = new StringInputStream('abc');
  17. $s = new Scanner($is);
  18. $this->assertInstanceOf('\Masterminds\HTML5\Parser\Scanner', $s);
  19. }
  20. public function testConstruct()
  21. {
  22. $this->assertInstanceOf('\Masterminds\HTML5\Parser\Scanner', new Scanner('abc'));
  23. }
  24. public function testNextDeprecated()
  25. {
  26. $s = new Scanner(new StringInputStream('abc'));
  27. $this->assertEquals('b', $s->next());
  28. $this->assertEquals('c', $s->next());
  29. }
  30. public function testNext()
  31. {
  32. $s = new Scanner('abc');
  33. $this->assertEquals('b', $s->next());
  34. $this->assertEquals('c', $s->next());
  35. }
  36. public function testPosition()
  37. {
  38. $s = new Scanner('abc');
  39. $this->assertEquals(0, $s->position());
  40. $s->next();
  41. $this->assertEquals(1, $s->position());
  42. }
  43. public function testPeek()
  44. {
  45. $s = new Scanner('abc');
  46. $this->assertEquals('b', $s->peek());
  47. $s->next();
  48. $this->assertEquals('c', $s->peek());
  49. }
  50. public function testCurrent()
  51. {
  52. $s = new Scanner('abc');
  53. // Before scanning the string begins the current is empty.
  54. $this->assertEquals('a', $s->current());
  55. $c = $s->next();
  56. $this->assertEquals('b', $s->current());
  57. // Test movement through the string.
  58. $c = $s->next();
  59. $this->assertEquals('c', $s->current());
  60. }
  61. public function testUnconsume()
  62. {
  63. $s = new Scanner('abcdefghijklmnopqrst');
  64. // Get initial position.
  65. $s->next();
  66. $start = $s->position();
  67. // Move forward a bunch of positions.
  68. $amount = 7;
  69. for ($i = 0; $i < $amount; ++$i) {
  70. $s->next();
  71. }
  72. // Roll back the amount we moved forward.
  73. $s->unconsume($amount);
  74. $this->assertEquals($start, $s->position());
  75. }
  76. public function testGetHex()
  77. {
  78. $s = new Scanner('ab13ck45DE*');
  79. $this->assertEquals('ab13c', $s->getHex());
  80. $s->next();
  81. $this->assertEquals('45DE', $s->getHex());
  82. }
  83. public function testGetAsciiAlpha()
  84. {
  85. $s = new Scanner('abcdef1%mnop*');
  86. $this->assertEquals('abcdef', $s->getAsciiAlpha());
  87. // Move past the 1% to scan the next group of text.
  88. $s->next();
  89. $s->next();
  90. $this->assertEquals('mnop', $s->getAsciiAlpha());
  91. }
  92. public function testGetAsciiAlphaNum()
  93. {
  94. $s = new Scanner('abcdef1ghpo#mn94op');
  95. $this->assertEquals('abcdef1ghpo', $s->getAsciiAlphaNum());
  96. // Move past the # to scan the next group of text.
  97. $s->next();
  98. $this->assertEquals('mn94op', $s->getAsciiAlphaNum());
  99. }
  100. public function testGetNumeric()
  101. {
  102. $s = new Scanner('1784a 45 9867 #');
  103. $this->assertEquals('1784', $s->getNumeric());
  104. // Move past the 'a ' to scan the next group of text.
  105. $s->next();
  106. $s->next();
  107. $this->assertEquals('45', $s->getNumeric());
  108. }
  109. public function testCurrentLine()
  110. {
  111. $s = new Scanner("1784a\n45\n9867 #\nThis is a test.");
  112. $this->assertEquals(1, $s->currentLine());
  113. // Move to the next line.
  114. $s->getAsciiAlphaNum();
  115. $s->next();
  116. $this->assertEquals(2, $s->currentLine());
  117. }
  118. public function testColumnOffset()
  119. {
  120. $s = new Scanner("1784a a\n45 9867 #\nThis is a test.");
  121. // Move the pointer to the space.
  122. $s->getAsciiAlphaNum();
  123. $this->assertEquals(5, $s->columnOffset());
  124. // We move the pointer ahead. There must be a better way to do this.
  125. $s->next();
  126. $s->next();
  127. $s->next();
  128. $s->next();
  129. $s->next();
  130. $s->next();
  131. $this->assertEquals(3, $s->columnOffset());
  132. }
  133. public function testRemainingChars()
  134. {
  135. $string = "\n45\n9867 #\nThis is a test.";
  136. $s = new Scanner("1784a\n45\n9867 #\nThis is a test.");
  137. $s->getAsciiAlphaNum();
  138. $this->assertEquals($string, $s->remainingChars());
  139. }
  140. }