2 namespace Masterminds\HTML5\Tests\Parser;
4 use Masterminds\HTML5\Parser\FileInputStream;
6 class FileInputStreamTest extends \Masterminds\HTML5\Tests\TestCase
9 public function testConstruct()
11 $s = new FileInputStream(__DIR__ . '/FileInputStreamTest.html');
13 $this->assertInstanceOf('\Masterminds\HTML5\Parser\FileInputStream', $s);
16 public function testNext()
18 $s = new FileInputStream(__DIR__ . '/FileInputStreamTest.html');
21 $this->assertEquals('!', $s->current());
23 $this->assertEquals('d', $s->current());
26 public function testKey()
28 $s = new FileInputStream(__DIR__ . '/FileInputStreamTest.html');
30 $this->assertEquals(0, $s->key());
33 $this->assertEquals(1, $s->key());
36 public function testPeek()
38 $s = new FileInputStream(__DIR__ . '/FileInputStreamTest.html');
40 $this->assertEquals('!', $s->peek());
43 $this->assertEquals('d', $s->peek());
46 public function testCurrent()
48 $s = new FileInputStream(__DIR__ . '/FileInputStreamTest.html');
50 $this->assertEquals('<', $s->current());
53 $this->assertEquals('!', $s->current());
56 $this->assertEquals('d', $s->current());
59 public function testColumnOffset()
61 $s = new FileInputStream(__DIR__ . '/FileInputStreamTest.html');
62 $this->assertEquals(0, $s->columnOffset());
64 $this->assertEquals(1, $s->columnOffset());
66 $this->assertEquals(2, $s->columnOffset());
68 $this->assertEquals(3, $s->columnOffset());
70 // Make sure we get to the second line
84 $this->assertEquals(0, $s->columnOffset());
87 $canary = $s->current(); // h
88 $this->assertEquals('h', $canary);
89 $this->assertEquals(1, $s->columnOffset());
92 public function testCurrentLine()
94 $s = new FileInputStream(__DIR__ . '/FileInputStreamTest.html');
96 $this->assertEquals(1, $s->currentLine());
98 // Make sure we get to the second line
115 $this->assertEquals(2, $s->currentLine());
117 // Make sure we get to the third line
135 $this->assertEquals(3, $s->currentLine());
138 public function testRemainingChars()
140 $text = file_get_contents(__DIR__ . '/FileInputStreamTest.html');
141 $s = new FileInputStream(__DIR__ . '/FileInputStreamTest.html');
142 $this->assertEquals($text, $s->remainingChars());
144 $text = substr(file_get_contents(__DIR__ . '/FileInputStreamTest.html'), 1);
145 $s = new FileInputStream(__DIR__ . '/FileInputStreamTest.html');
146 $s->next(); // Pop one.
147 $this->assertEquals($text, $s->remainingChars());
150 public function testCharsUnitl()
152 $s = new FileInputStream(__DIR__ . '/FileInputStreamTest.html');
154 $this->assertEquals('', $s->charsUntil('<'));
155 // Pointer at '<', moves to ' '
156 $this->assertEquals('<!doctype', $s->charsUntil(' ', 20));
158 // Pointer at ' ', moves to '>'
159 $this->assertEquals(' html', $s->charsUntil('>'));
161 // Pointer at '>', moves to '\n'.
162 $this->assertEquals('>', $s->charsUntil("\n"));
164 // Pointer at '\n', move forward then to the next'\n'.
166 $this->assertEquals('<html lang="en">', $s->charsUntil("\n"));
168 // Ony get one of the spaces.
169 $this->assertEquals("\n ", $s->charsUntil('<', 2));
171 // Get the other space.
172 $this->assertEquals(" ", $s->charsUntil('<'));
174 // This should scan to the end of the file.
176 <meta charset=\"utf-8\">
180 <p>This is a test.</p>
183 $this->assertEquals($text, $s->charsUntil("\t"));
186 public function testCharsWhile()
188 $s = new FileInputStream(__DIR__ . '/FileInputStreamTest.html');
190 $this->assertEquals('<!', $s->charsWhile('!<'));
191 $this->assertEquals('', $s->charsWhile('>'));
192 $this->assertEquals('doctype', $s->charsWhile('odcyept'));
193 $this->assertEquals(' htm', $s->charsWhile('html ', 4));