View source
<?php
namespace Masterminds\HTML5\Tests\Parser;
use Masterminds\HTML5\Parser\FileInputStream;
class FileInputStreamTest extends \Masterminds\HTML5\Tests\TestCase {
public function testConstruct() {
$s = new FileInputStream(__DIR__ . '/FileInputStreamTest.html');
$this
->assertInstanceOf('\\Masterminds\\HTML5\\Parser\\FileInputStream', $s);
}
public function testNext() {
$s = new FileInputStream(__DIR__ . '/FileInputStreamTest.html');
$s
->next();
$this
->assertEquals('!', $s
->current());
$s
->next();
$this
->assertEquals('d', $s
->current());
}
public function testKey() {
$s = new FileInputStream(__DIR__ . '/FileInputStreamTest.html');
$this
->assertEquals(0, $s
->key());
$s
->next();
$this
->assertEquals(1, $s
->key());
}
public function testPeek() {
$s = new FileInputStream(__DIR__ . '/FileInputStreamTest.html');
$this
->assertEquals('!', $s
->peek());
$s
->next();
$this
->assertEquals('d', $s
->peek());
}
public function testCurrent() {
$s = new FileInputStream(__DIR__ . '/FileInputStreamTest.html');
$this
->assertEquals('<', $s
->current());
$s
->next();
$this
->assertEquals('!', $s
->current());
$s
->next();
$this
->assertEquals('d', $s
->current());
}
public function testColumnOffset() {
$s = new FileInputStream(__DIR__ . '/FileInputStreamTest.html');
$this
->assertEquals(0, $s
->columnOffset());
$s
->next();
$this
->assertEquals(1, $s
->columnOffset());
$s
->next();
$this
->assertEquals(2, $s
->columnOffset());
$s
->next();
$this
->assertEquals(3, $s
->columnOffset());
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$this
->assertEquals(0, $s
->columnOffset());
$s
->next();
$canary = $s
->current();
$this
->assertEquals('h', $canary);
$this
->assertEquals(1, $s
->columnOffset());
}
public function testCurrentLine() {
$s = new FileInputStream(__DIR__ . '/FileInputStreamTest.html');
$this
->assertEquals(1, $s
->currentLine());
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$this
->assertEquals(2, $s
->currentLine());
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$s
->next();
$this
->assertEquals(3, $s
->currentLine());
}
public function testRemainingChars() {
$text = file_get_contents(__DIR__ . '/FileInputStreamTest.html');
$s = new FileInputStream(__DIR__ . '/FileInputStreamTest.html');
$this
->assertEquals($text, $s
->remainingChars());
$text = substr(file_get_contents(__DIR__ . '/FileInputStreamTest.html'), 1);
$s = new FileInputStream(__DIR__ . '/FileInputStreamTest.html');
$s
->next();
$this
->assertEquals($text, $s
->remainingChars());
}
public function testCharsUnitl() {
$s = new FileInputStream(__DIR__ . '/FileInputStreamTest.html');
$this
->assertEquals('', $s
->charsUntil('<'));
$this
->assertEquals('<!doctype', $s
->charsUntil(' ', 20));
$this
->assertEquals(' html', $s
->charsUntil('>'));
$this
->assertEquals('>', $s
->charsUntil("\n"));
$s
->next();
$this
->assertEquals('<html lang="en">', $s
->charsUntil("\n"));
$this
->assertEquals("\n ", $s
->charsUntil('<', 2));
$this
->assertEquals(" ", $s
->charsUntil('<'));
$text = "<head>\n <meta charset=\"utf-8\">\n <title>Test</title>\n </head>\n <body>\n <p>This is a test.</p>\n </body>\n</html>";
$this
->assertEquals($text, $s
->charsUntil("\t"));
}
public function testCharsWhile() {
$s = new FileInputStream(__DIR__ . '/FileInputStreamTest.html');
$this
->assertEquals('<!', $s
->charsWhile('!<'));
$this
->assertEquals('', $s
->charsWhile('>'));
$this
->assertEquals('doctype', $s
->charsWhile('odcyept'));
$this
->assertEquals(' htm', $s
->charsWhile('html ', 4));
}
}