Skip to content

Commit 64e6f13

Browse files
committed
Add tests for BufferedStream.
1 parent c09a92b commit 64e6f13

File tree

1 file changed

+65
-2
lines changed

1 file changed

+65
-2
lines changed

html5lib/tests/test_stream.py

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,71 @@
33
from . import support # flake8: noqa
44
import unittest
55
import codecs
6-
7-
from html5lib.inputstream import HTMLInputStream, HTMLUnicodeInputStream, HTMLBinaryInputStream
6+
from io import BytesIO
7+
8+
from html5lib.inputstream import (BufferedStream, HTMLInputStream,
9+
HTMLUnicodeInputStream, HTMLBinaryInputStream)
10+
11+
class BufferedStreamTest(unittest.TestCase):
12+
def test_basic(self):
13+
s = b"abc"
14+
fp = BufferedStream(BytesIO(s))
15+
read = fp.read(10)
16+
assert read == s
17+
18+
def test_read_length(self):
19+
fp = BufferedStream(BytesIO(b"abcdef"))
20+
read1 = fp.read(1)
21+
assert read1 == b"a"
22+
read2 = fp.read(2)
23+
assert read2 == b"bc"
24+
read3 = fp.read(3)
25+
assert read3 == b"def"
26+
read4 = fp.read(4)
27+
assert read4 == b""
28+
29+
def test_tell(self):
30+
fp = BufferedStream(BytesIO(b"abcdef"))
31+
read1 = fp.read(1)
32+
assert fp.tell() == 1
33+
read2 = fp.read(2)
34+
assert fp.tell() == 3
35+
read3 = fp.read(3)
36+
assert fp.tell() == 6
37+
read4 = fp.read(4)
38+
assert fp.tell() == 6
39+
40+
def test_seek(self):
41+
fp = BufferedStream(BytesIO(b"abcdef"))
42+
read1 = fp.read(1)
43+
assert read1 == b"a"
44+
fp.seek(0)
45+
read2 = fp.read(1)
46+
assert read2 == b"a"
47+
read3 = fp.read(2)
48+
assert read3 == b"bc"
49+
fp.seek(2)
50+
read4 = fp.read(2)
51+
assert read4 == b"cd"
52+
fp.seek(4)
53+
read5 = fp.read(2)
54+
assert read5 == b"ef"
55+
56+
def test_seek_tell(self):
57+
fp = BufferedStream(BytesIO(b"abcdef"))
58+
read1 = fp.read(1)
59+
assert fp.tell() == 1
60+
fp.seek(0)
61+
read2 = fp.read(1)
62+
assert fp.tell() == 1
63+
read3 = fp.read(2)
64+
assert fp.tell() == 3
65+
fp.seek(2)
66+
read4 = fp.read(2)
67+
assert fp.tell() == 4
68+
fp.seek(4)
69+
read5 = fp.read(2)
70+
assert fp.tell() == 6
871

972

1073
class HTMLUnicodeInputStreamShortChunk(HTMLUnicodeInputStream):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy