4 * Test the Scanner. This requires the InputStream tests are all good.
6 namespace Masterminds\HTML5\Tests\Parser;
8 use Masterminds\HTML5\Parser\StringInputStream;
9 use Masterminds\HTML5\Parser\Scanner;
11 class ScannerTest extends \Masterminds\HTML5\Tests\TestCase
15 * A canary test to make sure the basics are setup and working.
17 public function testConstruct()
19 $is = new StringInputStream("abc");
20 $s = new Scanner($is);
22 $this->assertInstanceOf('\Masterminds\HTML5\Parser\Scanner', $s);
25 public function testNext()
27 $s = new Scanner(new StringInputStream("abc"));
29 $this->assertEquals('b', $s->next());
30 $this->assertEquals('c', $s->next());
33 public function testPosition()
35 $s = new Scanner(new StringInputStream("abc"));
37 $this->assertEquals(0, $s->position());
40 $this->assertEquals(1, $s->position());
43 public function testPeek()
45 $s = new Scanner(new StringInputStream("abc"));
47 $this->assertEquals('b', $s->peek());
50 $this->assertEquals('c', $s->peek());
53 public function testCurrent()
55 $s = new Scanner(new StringInputStream("abc"));
57 // Before scanning the string begins the current is empty.
58 $this->assertEquals('a', $s->current());
61 $this->assertEquals('b', $s->current());
63 // Test movement through the string.
65 $this->assertEquals('c', $s->current());
68 public function testUnconsume()
70 $s = new Scanner(new StringInputStream("abcdefghijklmnopqrst"));
72 // Get initial position.
74 $start = $s->position();
76 // Move forward a bunch of positions.
78 for ($i = 0; $i < $amount; $i ++) {
82 // Roll back the amount we moved forward.
83 $s->unconsume($amount);
85 $this->assertEquals($start, $s->position());
88 public function testGetHex()
90 $s = new Scanner(new StringInputStream("ab13ck45DE*"));
92 $this->assertEquals('ab13c', $s->getHex());
95 $this->assertEquals('45DE', $s->getHex());
98 public function testGetAsciiAlpha()
100 $s = new Scanner(new StringInputStream("abcdef1%mnop*"));
102 $this->assertEquals('abcdef', $s->getAsciiAlpha());
104 // Move past the 1% to scan the next group of text.
107 $this->assertEquals('mnop', $s->getAsciiAlpha());
110 public function testGetAsciiAlphaNum()
112 $s = new Scanner(new StringInputStream("abcdef1ghpo#mn94op"));
114 $this->assertEquals('abcdef1ghpo', $s->getAsciiAlphaNum());
116 // Move past the # to scan the next group of text.
118 $this->assertEquals('mn94op', $s->getAsciiAlphaNum());
121 public function testGetNumeric()
123 $s = new Scanner(new StringInputStream("1784a 45 9867 #"));
125 $this->assertEquals('1784', $s->getNumeric());
127 // Move past the 'a ' to scan the next group of text.
130 $this->assertEquals('45', $s->getNumeric());
133 public function testCurrentLine()
135 $s = new Scanner(new StringInputStream("1784a\n45\n9867 #\nThis is a test."));
137 $this->assertEquals(1, $s->currentLine());
139 // Move to the next line.
140 $s->getAsciiAlphaNum();
142 $this->assertEquals(2, $s->currentLine());
145 public function testColumnOffset()
147 $s = new Scanner(new StringInputStream("1784a a\n45 9867 #\nThis is a test."));
149 // Move the pointer to the space.
150 $s->getAsciiAlphaNum();
151 $this->assertEquals(5, $s->columnOffset());
153 // We move the pointer ahead. There must be a better way to do this.
160 $this->assertEquals(3, $s->columnOffset());
163 public function testRemainingChars()
165 $string = "\n45\n9867 #\nThis is a test.";
166 $s = new Scanner(new StringInputStream("1784a\n45\n9867 #\nThis is a test."));
168 $s->getAsciiAlphaNum();
169 $this->assertEquals($string, $s->remainingChars());