View source
<?php
namespace Masterminds\HTML5\Tests\Parser;
use Masterminds\HTML5\Parser\StringInputStream;
class StringInputStreamTest extends \Masterminds\HTML5\Tests\TestCase {
public function testConstruct() {
$s = new StringInputStream("abc");
$this
->assertInstanceOf('\\Masterminds\\HTML5\\Parser\\StringInputStream', $s);
}
public function testNext() {
$s = new StringInputStream("abc");
$s
->next();
$this
->assertEquals('b', $s
->current());
$s
->next();
$this
->assertEquals('c', $s
->current());
}
public function testKey() {
$s = new StringInputStream("abc");
$this
->assertEquals(0, $s
->key());
$s
->next();
$this
->assertEquals(1, $s
->key());
}
public function testPeek() {
$s = new StringInputStream("abc");
$this
->assertEquals('b', $s
->peek());
$s
->next();
$this
->assertEquals('c', $s
->peek());
}
public function testCurrent() {
$s = new StringInputStream("abc");
$this
->assertEquals('a', $s
->current());
$s
->next();
$this
->assertEquals('b', $s
->current());
$s
->next();
$this
->assertEquals('c', $s
->current());
}
public function testColumnOffset() {
$s = new StringInputStream("abc\ndef\n");
$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();
$this
->assertEquals(0, $s
->columnOffset());
$s
->next();
$canary = $s
->current();
$this
->assertEquals('e', $canary);
$this
->assertEquals(1, $s
->columnOffset());
$s = new StringInputStream("abc");
$this
->assertEquals(0, $s
->columnOffset());
$s
->next();
$this
->assertEquals(1, $s
->columnOffset());
$s
->next();
$this
->assertEquals(2, $s
->columnOffset());
}
public function testCurrentLine() {
$txt = "1\n2\n\n\n\n3";
$stream = new StringInputStream($txt);
$this
->assertEquals(1, $stream
->currentLine());
$stream
->next();
$stream
->next();
$canary = $stream
->current();
$this
->assertEquals(2, $stream
->currentLine());
$this
->assertEquals('2', $canary);
$stream
->next();
$stream
->next();
$stream
->next();
$stream
->next();
$stream
->next();
$this
->assertEquals(6, $stream
->currentLine());
$this
->assertEquals('3', $stream
->current());
$this
->assertEquals(6, $stream
->currentLine());
}
public function testRemainingChars() {
$text = "abcd";
$s = new StringInputStream($text);
$this
->assertEquals($text, $s
->remainingChars());
$text = "abcd";
$s = new StringInputStream($text);
$s
->next();
$this
->assertEquals('bcd', $s
->remainingChars());
}
public function testCharsUnitl() {
$text = "abcdefffffffghi";
$s = new StringInputStream($text);
$this
->assertEquals('', $s
->charsUntil('a'));
$this
->assertEquals('ab', $s
->charsUntil('w', 2));
$this
->assertEquals('cde', $s
->charsUntil('fzxv'));
$this
->assertEquals('fffff', $s
->charsUntil('g', 5));
$this
->assertEquals('ff', $s
->charsUntil('g'));
$this
->assertEquals('ghi', $s
->charsUntil('w', 9));
}
public function testCharsWhile() {
$text = "abcdefffffffghi";
$s = new StringInputStream($text);
$this
->assertEquals('ab', $s
->charsWhile('ba'));
$this
->assertEquals('', $s
->charsWhile('a'));
$this
->assertEquals('cde', $s
->charsWhile('cdeba'));
$this
->assertEquals('ff', $s
->charsWhile('f', 2));
$this
->assertEquals('fffff', $s
->charsWhile('f'));
$this
->assertEquals('g', $s
->charsWhile('fg'));
$this
->assertEquals('hi', $s
->charsWhile('fghi', 99));
}
public function testBOM() {
$stream = new StringInputStream("a");
$this
->assertEquals("a", $stream
->remainingChars(), 'A non-leading U+FEFF (BOM/ZWNBSP) should remain');
$leading = new StringInputStream("a");
$this
->assertEquals('a', $leading
->current(), 'BOM should be stripped');
}
public function testCarriageReturn() {
$stream = new StringInputStream("\0\0\0");
$this
->assertEquals("���", $stream
->remainingChars(), 'Null character should be replaced by U+FFFD');
$this
->assertEquals(3, count($stream->errors), 'Null character should set parse error: ' . print_r($stream->errors, true));
$stream = new StringInputStream("\r\n");
$this
->assertEquals("\n", $stream
->remainingChars(), 'CRLF should be replaced by LF');
$stream = new StringInputStream("\r");
$this
->assertEquals("\n", $stream
->remainingChars(), 'CR should be replaced by LF');
}
public function invalidParseErrorTestHandler($input, $numErrors, $name) {
$stream = new StringInputStream($input, 'UTF-8');
$this
->assertEquals($input, $stream
->remainingChars(), $name . ' (stream content)');
$this
->assertEquals($numErrors, count($stream->errors), $name . ' (number of errors)');
}
public function testInvalidReplace() {
$invalidTest = array(
"" => 'Overlong representation of U+0000',
"" => 'Overlong representation of U+0000',
"" => 'Overlong representation of U+0000',
"" => 'Overlong representation of U+0000',
"" => 'Overlong representation of U+0000',
"" => 'Overlong representation of U+007F',
"" => 'Overlong representation of U+07FF',
"" => 'Overlong representation of U+FFFF',
"" => 'Incomplete two byte sequence (missing final byte)',
"" => 'Incomplete three byte sequence (missing final byte)',
"" => 'Incomplete four byte sequence (missing final byte)',
"" => 'Lone 80 continuation byte',
"" => 'Lone BF continuation byte',
"" => 'Invalid FE byte',
"" => 'Invalid FF byte',
);
foreach ($invalidTest as $test => $note) {
$stream = new StringInputStream($test);
$this
->assertEquals('a', $stream
->remainingChars(), $note);
}
}
public function testInvalidParseError() {
$this
->invalidParseErrorTestHandler("\1", 1, 'U+0001 (C0 control)');
$this
->invalidParseErrorTestHandler("\2", 1, 'U+0002 (C0 control)');
$this
->invalidParseErrorTestHandler("\3", 1, 'U+0003 (C0 control)');
$this
->invalidParseErrorTestHandler("\4", 1, 'U+0004 (C0 control)');
$this
->invalidParseErrorTestHandler("\5", 1, 'U+0005 (C0 control)');
$this
->invalidParseErrorTestHandler("\6", 1, 'U+0006 (C0 control)');
$this
->invalidParseErrorTestHandler("\7", 1, 'U+0007 (C0 control)');
$this
->invalidParseErrorTestHandler("\10", 1, 'U+0008 (C0 control)');
$this
->invalidParseErrorTestHandler("\t", 0, 'U+0009 (C0 control)');
$this
->invalidParseErrorTestHandler("\n", 0, 'U+000A (C0 control)');
$this
->invalidParseErrorTestHandler("\v", 1, 'U+000B (C0 control)');
$this
->invalidParseErrorTestHandler("\f", 0, 'U+000C (C0 control)');
$this
->invalidParseErrorTestHandler("\16", 1, 'U+000E (C0 control)');
$this
->invalidParseErrorTestHandler("\17", 1, 'U+000F (C0 control)');
$this
->invalidParseErrorTestHandler("\20", 1, 'U+0010 (C0 control)');
$this
->invalidParseErrorTestHandler("\21", 1, 'U+0011 (C0 control)');
$this
->invalidParseErrorTestHandler("\22", 1, 'U+0012 (C0 control)');
$this
->invalidParseErrorTestHandler("\23", 1, 'U+0013 (C0 control)');
$this
->invalidParseErrorTestHandler("\24", 1, 'U+0014 (C0 control)');
$this
->invalidParseErrorTestHandler("\25", 1, 'U+0015 (C0 control)');
$this
->invalidParseErrorTestHandler("\26", 1, 'U+0016 (C0 control)');
$this
->invalidParseErrorTestHandler("\27", 1, 'U+0017 (C0 control)');
$this
->invalidParseErrorTestHandler("\30", 1, 'U+0018 (C0 control)');
$this
->invalidParseErrorTestHandler("\31", 1, 'U+0019 (C0 control)');
$this
->invalidParseErrorTestHandler("\32", 1, 'U+001A (C0 control)');
$this
->invalidParseErrorTestHandler("\33", 1, 'U+001B (C0 control)');
$this
->invalidParseErrorTestHandler("\34", 1, 'U+001C (C0 control)');
$this
->invalidParseErrorTestHandler("\35", 1, 'U+001D (C0 control)');
$this
->invalidParseErrorTestHandler("\36", 1, 'U+001E (C0 control)');
$this
->invalidParseErrorTestHandler("\37", 1, 'U+001F (C0 control)');
$this
->invalidParseErrorTestHandler("", 1, 'U+007F');
$this
->invalidParseErrorTestHandler("", 1, 'U+0080 (C1 control)');
$this
->invalidParseErrorTestHandler("", 1, 'U+009F (C1 control)');
$this
->invalidParseErrorTestHandler(" ", 0, 'U+00A0 (first codepoint above highest C1 control)');
$this
->invalidParseErrorTestHandler("", 0, 'U+D7FF (one codepoint below lowest surrogate codepoint)');
$this
->invalidParseErrorTestHandler("�", 0, 'U+DE00 (one codepoint above highest surrogate codepoint)');
$this
->invalidParseErrorTestHandler("", 1, 'U+FDD0 (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+FDEF (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+FFFE (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+FFFF (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+1FFFE (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+1FFFF (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+2FFFE (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+2FFFF (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+3FFFE (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+3FFFF (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+4FFFE (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+4FFFF (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+5FFFE (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+5FFFF (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+6FFFE (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+6FFFF (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+7FFFE (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+7FFFF (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+8FFFE (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+8FFFF (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+9FFFE (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+9FFFF (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+AFFFE (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+AFFFF (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+BFFFE (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+BFFFF (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+CFFFE (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+CFFFF (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+DFFFE (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+DFFFF (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+EFFFE (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+EFFFF (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+FFFFE (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+FFFFF (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+10FFFE (permanent noncharacter)');
$this
->invalidParseErrorTestHandler("", 1, 'U+10FFFF (permanent noncharacter)');
}
}