8fa5110686f18973d09e47085e31b112d3004e62
[yaffs-website] / vendor / masterminds / html5 / test / HTML5 / Parser / ScannerTest.php
1 <?php
2 /**
3  * @file
4  * Test the Scanner. This requires the InputStream tests are all good.
5  */
6 namespace Masterminds\HTML5\Tests\Parser;
7
8 use Masterminds\HTML5\Parser\StringInputStream;
9 use Masterminds\HTML5\Parser\Scanner;
10
11 class ScannerTest extends \Masterminds\HTML5\Tests\TestCase
12 {
13
14     /**
15      * A canary test to make sure the basics are setup and working.
16      */
17     public function testConstruct()
18     {
19         $is = new StringInputStream("abc");
20         $s = new Scanner($is);
21
22         $this->assertInstanceOf('\Masterminds\HTML5\Parser\Scanner', $s);
23     }
24
25     public function testNext()
26     {
27         $s = new Scanner(new StringInputStream("abc"));
28
29         $this->assertEquals('b', $s->next());
30         $this->assertEquals('c', $s->next());
31     }
32
33     public function testPosition()
34     {
35         $s = new Scanner(new StringInputStream("abc"));
36
37         $this->assertEquals(0, $s->position());
38
39         $s->next();
40         $this->assertEquals(1, $s->position());
41     }
42
43     public function testPeek()
44     {
45         $s = new Scanner(new StringInputStream("abc"));
46
47         $this->assertEquals('b', $s->peek());
48
49         $s->next();
50         $this->assertEquals('c', $s->peek());
51     }
52
53     public function testCurrent()
54     {
55         $s = new Scanner(new StringInputStream("abc"));
56
57         // Before scanning the string begins the current is empty.
58         $this->assertEquals('a', $s->current());
59
60         $c = $s->next();
61         $this->assertEquals('b', $s->current());
62
63         // Test movement through the string.
64         $c = $s->next();
65         $this->assertEquals('c', $s->current());
66     }
67
68     public function testUnconsume()
69     {
70         $s = new Scanner(new StringInputStream("abcdefghijklmnopqrst"));
71
72         // Get initial position.
73         $s->next();
74         $start = $s->position();
75
76         // Move forward a bunch of positions.
77         $amount = 7;
78         for ($i = 0; $i < $amount; $i ++) {
79             $s->next();
80         }
81
82         // Roll back the amount we moved forward.
83         $s->unconsume($amount);
84
85         $this->assertEquals($start, $s->position());
86     }
87
88     public function testGetHex()
89     {
90         $s = new Scanner(new StringInputStream("ab13ck45DE*"));
91
92         $this->assertEquals('ab13c', $s->getHex());
93
94         $s->next();
95         $this->assertEquals('45DE', $s->getHex());
96     }
97
98     public function testGetAsciiAlpha()
99     {
100         $s = new Scanner(new StringInputStream("abcdef1%mnop*"));
101
102         $this->assertEquals('abcdef', $s->getAsciiAlpha());
103
104         // Move past the 1% to scan the next group of text.
105         $s->next();
106         $s->next();
107         $this->assertEquals('mnop', $s->getAsciiAlpha());
108     }
109
110     public function testGetAsciiAlphaNum()
111     {
112         $s = new Scanner(new StringInputStream("abcdef1ghpo#mn94op"));
113
114         $this->assertEquals('abcdef1ghpo', $s->getAsciiAlphaNum());
115
116         // Move past the # to scan the next group of text.
117         $s->next();
118         $this->assertEquals('mn94op', $s->getAsciiAlphaNum());
119     }
120
121     public function testGetNumeric()
122     {
123         $s = new Scanner(new StringInputStream("1784a 45 9867 #"));
124
125         $this->assertEquals('1784', $s->getNumeric());
126
127         // Move past the 'a ' to scan the next group of text.
128         $s->next();
129         $s->next();
130         $this->assertEquals('45', $s->getNumeric());
131     }
132
133     public function testCurrentLine()
134     {
135         $s = new Scanner(new StringInputStream("1784a\n45\n9867 #\nThis is a test."));
136
137         $this->assertEquals(1, $s->currentLine());
138
139         // Move to the next line.
140         $s->getAsciiAlphaNum();
141         $s->next();
142         $this->assertEquals(2, $s->currentLine());
143     }
144
145     public function testColumnOffset()
146     {
147         $s = new Scanner(new StringInputStream("1784a a\n45 9867 #\nThis is a test."));
148
149         // Move the pointer to the space.
150         $s->getAsciiAlphaNum();
151         $this->assertEquals(5, $s->columnOffset());
152
153         // We move the pointer ahead. There must be a better way to do this.
154         $s->next();
155         $s->next();
156         $s->next();
157         $s->next();
158         $s->next();
159         $s->next();
160         $this->assertEquals(3, $s->columnOffset());
161     }
162
163     public function testRemainingChars()
164     {
165         $string = "\n45\n9867 #\nThis is a test.";
166         $s = new Scanner(new StringInputStream("1784a\n45\n9867 #\nThis is a test."));
167
168         $s->getAsciiAlphaNum();
169         $this->assertEquals($string, $s->remainingChars());
170     }
171 }