11from __future__import absolute_import ,division ,unicode_literals
22
3- from .import support # flake8: noqa
3+ from .import support # noqa
4+
45import codecs
56from io import BytesIO
6- import socket
77
88import six
99from six .moves import http_client ,urllib
1010
1111from html5lib .inputstream import (BufferedStream ,HTMLInputStream ,
1212HTMLUnicodeInputStream ,HTMLBinaryInputStream )
1313
14+
1415def test_basic ():
1516s = b"abc"
1617fp = BufferedStream (BytesIO (s ))
1718read = fp .read (10 )
1819assert read == s
1920
21+
2022def test_read_length ():
2123fp = BufferedStream (BytesIO (b"abcdef" ))
2224read1 = fp .read (1 )
@@ -28,17 +30,23 @@ def test_read_length():
2830read4 = fp .read (4 )
2931assert read4 == b""
3032
33+
3134def test_tell ():
3235fp = BufferedStream (BytesIO (b"abcdef" ))
3336read1 = fp .read (1 )
37+ assert read1 == b"a"
3438assert fp .tell ()== 1
3539read2 = fp .read (2 )
40+ assert read2 == b"bc"
3641assert fp .tell ()== 3
3742read3 = fp .read (3 )
43+ assert read3 == b"def"
3844assert fp .tell ()== 6
3945read4 = fp .read (4 )
46+ assert read4 == b""
4047assert fp .tell ()== 6
4148
49+
4250def test_seek ():
4351fp = BufferedStream (BytesIO (b"abcdef" ))
4452read1 = fp .read (1 )
@@ -55,20 +63,26 @@ def test_seek():
5563read5 = fp .read (2 )
5664assert read5 == b"ef"
5765
66+
5867def test_seek_tell ():
5968fp = BufferedStream (BytesIO (b"abcdef" ))
6069read1 = fp .read (1 )
70+ assert read1 == b"a"
6171assert fp .tell ()== 1
6272fp .seek (0 )
6373read2 = fp .read (1 )
74+ assert read2 == b"a"
6475assert fp .tell ()== 1
6576read3 = fp .read (2 )
77+ assert read3 == b"bc"
6678assert fp .tell ()== 3
6779fp .seek (2 )
6880read4 = fp .read (2 )
81+ assert read4 == b"cd"
6982assert fp .tell ()== 4
7083fp .seek (4 )
7184read5 = fp .read (2 )
85+ assert read5 == b"ef"
7286assert fp .tell ()== 6
7387
7488
@@ -85,28 +99,33 @@ def test_char_ascii():
8599assert stream .charEncoding [0 ].name == 'windows-1252'
86100assert stream .char ()== "'"
87101
102+
88103def test_char_utf8 ():
89104stream = HTMLInputStream ('\u2018 ' .encode ('utf-8' ),encoding = 'utf-8' )
90105assert stream .charEncoding [0 ].name == 'utf-8'
91106assert stream .char ()== '\u2018 '
92107
108+
93109def test_char_win1252 ():
94110stream = HTMLInputStream ("\xa9 \xf1 \u2019 " .encode ('windows-1252' ))
95111assert stream .charEncoding [0 ].name == 'windows-1252'
96112assert stream .char ()== "\xa9 "
97113assert stream .char ()== "\xf1 "
98114assert stream .char ()== "\u2019 "
99115
116+
100117def test_bom ():
101118stream = HTMLInputStream (codecs .BOM_UTF8 + b"'" )
102119assert stream .charEncoding [0 ].name == 'utf-8'
103120assert stream .char ()== "'"
104121
122+
105123def test_utf_16 ():
106124stream = HTMLInputStream ((' ' * 1025 ).encode ('utf-16' ))
107125assert stream .charEncoding [0 ].name in ['utf-16le' ,'utf-16be' ]
108126assert len (stream .charsUntil (' ' ,True ))== 1025
109127
128+
110129def test_newlines ():
111130stream = HTMLBinaryInputStreamShortChunk (codecs .BOM_UTF8 + b"a\n bb\r \n ccc\r ddddxe" )
112131assert stream .position ()== (1 ,0 )
@@ -117,11 +136,13 @@ def test_newlines():
117136assert stream .charsUntil ('e' )== "x"
118137assert stream .position ()== (4 ,5 )
119138
139+
120140def test_newlines2 ():
121141size = HTMLUnicodeInputStream ._defaultChunkSize
122142stream = HTMLInputStream ("\r " * size + "\n " )
123143assert stream .charsUntil ('x' )== "\n " * size
124144
145+
125146def test_position ():
126147stream = HTMLBinaryInputStreamShortChunk (codecs .BOM_UTF8 + b"a\n bb\n ccc\n ddde\n f\n gh" )
127148assert stream .position ()== (1 ,0 )
@@ -140,6 +161,7 @@ def test_position():
140161assert stream .charsUntil ('h' )== "e\n f\n g"
141162assert stream .position ()== (6 ,1 )
142163
164+
143165def test_position2 ():
144166stream = HTMLUnicodeInputStreamShortChunk ("abc\n d" )
145167assert stream .position ()== (1 ,0 )
@@ -154,6 +176,7 @@ def test_position2():
154176assert stream .char ()== "d"
155177assert stream .position ()== (2 ,1 )
156178
179+
157180def test_python_issue_20007 ():
158181"""
159182 Make sure we have a work-around for Python bug #20007
@@ -168,6 +191,7 @@ def makefile(self, _mode, _bufsize=None):
168191stream = HTMLInputStream (source )
169192assert stream .charsUntil (" " )== "Text"
170193
194+
171195def test_python_issue_20007_b ():
172196"""
173197 Make sure we have a work-around for Python bug #20007