summaryrefslogtreecommitdiff
path: root/test/HTML5/Parser/FileInputStreamTest.php
blob: 80078ff09124f2b41bbc29bceb9e4de91275507f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
namespace HTML5\Tests\Parser;

use \HTML5\Parser\FileInputStream;

class FileInputStreamTest extends \HTML5\Tests\TestCase {

  function testConstruct() {
    $s = new FileInputStream(__DIR__ . '/FileInputStreamTest.html');

    $this->assertInstanceOf('\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());

    // Make sure we get to the second line
    $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(); // h
    $this->assertEquals('h', $canary);
    $this->assertEquals(1, $s->columnOffset());
  }

  public function testCurrentLine() {
    $s = new FileInputStream(__DIR__ . '/FileInputStreamTest.html');

    $this->assertEquals(1, $s->currentLine());

    // Make sure we get to the second line
    $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());

    // Make sure we get to the third line
    $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(); // Pop one.
    $this->assertEquals($text, $s->remainingChars());
  }

  public function testCharsUnitl() {
    $s = new FileInputStream(__DIR__ . '/FileInputStreamTest.html');

    $this->assertEquals('', $s->charsUntil('<'));
    // Pointer at '<', moves to ' '
    $this->assertEquals('<!doctype', $s->charsUntil(' ', 20));

    // Pointer at ' ', moves to '>'
    $this->assertEquals(' html', $s->charsUntil('>'));

    // Pointer at '>', moves to '\n'.
    $this->assertEquals('>', $s->charsUntil("\n"));

    // Pointer at '\n', move forward then to the next'\n'.
    $s->next();
    $this->assertEquals('<html lang="en">', $s->charsUntil("\n"));

    // Ony get one of the spaces.
    $this->assertEquals("\n ", $s->charsUntil('<', 2));

    // Get the other space.
    $this->assertEquals(" ", $s->charsUntil('<'));

    // This should scan to the end of the file.
    $text = "<head>
    <meta charset=\"utf-8\">
    <title>Test</title>
  </head>
  <body>
    <p>This is a test.</p>
  </body>
</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));
  }
}