diff options
-rw-r--r-- | src/HTML5/Parser/Scanner.php | 5 | ||||
-rw-r--r-- | test/HTML5/ScannerTest.php | 65 |
2 files changed, 69 insertions, 1 deletions
diff --git a/src/HTML5/Parser/Scanner.php b/src/HTML5/Parser/Scanner.php index 33621fd..0260391 100644 --- a/src/HTML5/Parser/Scanner.php +++ b/src/HTML5/Parser/Scanner.php @@ -36,7 +36,10 @@ class Scanner { } /** - * Take a peek at the next character in the data. + * Take a peek at the character after the next character in the data. + * + * For example, you start scanning the string abc. The pointer is before a. + * When you start peek() will return b while next() will return a. * * @return string * The next character. diff --git a/test/HTML5/ScannerTest.php b/test/HTML5/ScannerTest.php new file mode 100644 index 0000000..919f8fc --- /dev/null +++ b/test/HTML5/ScannerTest.php @@ -0,0 +1,65 @@ +<?php +/** + * @file + * Test the Scanner. This requires the InputStream tests are all good. + */ +namespace HTML5\Tests; + +use \HTML5\InputStream; +use \HTML5\Parser\Scanner; + +require_once 'TestCase.php'; + +class ScannerTest extends TestCase { + + /** + * A canary test to make sure the basics are setup and working. + */ + public function testConstruct() { + $is = new InputStream("abc"); + $s = new Scanner($is); + + $this->assertInstanceOf('\HTML5\Parser\Scanner', $s); + } + + public function testNext() { + $s = new Scanner(new InputStream("abc")); + + $this->assertEquals('a', $s->next()); + $this->assertEquals('b', $s->next()); + $this->assertEquals('c', $s->next()); + } + + public function testPosition() { + $s = new Scanner(new InputStream("abc")); + + $this->assertEquals(0, $s->position()); + + $s->next(); + $this->assertEquals(1, $s->position()); + } + + public function testPeek() { + $s = new Scanner(new InputStream("abc")); + + // The scanner is currently pointed before a. + $this->assertEquals('b', $s->peek()); + + $s->next(); + $this->assertEquals('c', $s->peek()); + } + + public function testCurrent() { + $s = new Scanner(new InputStream("abc")); + + // Before scanning the string begins the current is empty. + $this->assertEquals('', $s->current()); + + $c = $s->next(); + $this->assertEquals($c, $s->current()); + + // Test movement through the string. + $c = $s->next(); + $this->assertEquals($c, $s->current()); + } +}
\ No newline at end of file |