FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
html5lib-python/html5lib/tests/test_encoding.py at gcode-157 · andersk/html5lib-python · GitHub
andersk
/
html5lib-python
Public
forked from
html5lib/html5lib-python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
html5lib-python
/
html5lib
/
tests
/
test_encoding.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
63 lines (45 loc) · 2.09 KB
Breadcrumbs
html5lib-python
/
html5lib
/
tests
/
test_encoding.py
Copy path
File metadata and controls
63 lines (45 loc) · 2.09 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
from
__future__
import
absolute_import
,
division
,
unicode_literals
import
os
import
unittest
try
:
unittest
.
TestCase
.
assertEqual
except
AttributeError
:
unittest
.
TestCase
.
assertEqual
=
unittest
.
TestCase
.
assertEquals
from
.
support
import
get_data_files
,
TestData
,
test_dir
,
errorMessage
from
html5lib
import
HTMLParser
,
inputstream
class
Html5EncodingTestCase
(
unittest
.
TestCase
):
def
test_codec_name_a
(
self
):
self
.
assertEqual
(
inputstream
.
codecName
(
"utf-8"
),
"utf-8"
)
def
test_codec_name_b
(
self
):
self
.
assertEqual
(
inputstream
.
codecName
(
"utf8"
),
"utf-8"
)
def
test_codec_name_c
(
self
):
self
.
assertEqual
(
inputstream
.
codecName
(
" utf8 "
),
"utf-8"
)
def
test_codec_name_d
(
self
):
self
.
assertEqual
(
inputstream
.
codecName
(
"ISO_8859--1"
),
"windows-1252"
)
def
runParserEncodingTest
(
data
,
encoding
):
p
=
HTMLParser
()
p
.
parse
(
data
,
useChardet
=
False
)
encoding
=
encoding
.
lower
().
decode
(
"ascii"
)
assert
encoding
==
p
.
tokenizer
.
stream
.
charEncoding
[
0
],
errorMessage
(
data
,
encoding
,
p
.
tokenizer
.
stream
.
charEncoding
[
0
])
def
runPreScanEncodingTest
(
data
,
encoding
):
stream
=
inputstream
.
HTMLBinaryInputStream
(
data
,
chardet
=
False
)
encoding
=
encoding
.
lower
().
decode
(
"ascii"
)
# Very crude way to ignore irrelevant tests
if
len
(
data
)
>
stream
.
numBytesMeta
:
return
assert
encoding
==
stream
.
charEncoding
[
0
],
errorMessage
(
data
,
encoding
,
stream
.
charEncoding
[
0
])
def
test_encoding
():
for
filename
in
get_data_files
(
"encoding"
):
tests
=
TestData
(
filename
,
b"data"
,
encoding
=
None
)
for
idx
,
test
in
enumerate
(
tests
):
yield
(
runParserEncodingTest
,
test
[
b'data'
],
test
[
b'encoding'
])
yield
(
runPreScanEncodingTest
,
test
[
b'data'
],
test
[
b'encoding'
])
try
:
import
chardet
# flake8: noqa
except
ImportError
:
print
(
"chardet not found, skipping chardet tests"
)
else
:
def
test_chardet
():
with
open
(
os
.
path
.
join
(
test_dir
,
"encoding"
,
"chardet"
,
"test_big5.txt"
),
"rb"
)
as
fp
:
encoding
=
inputstream
.
HTMLInputStream
(
fp
.
read
()).
charEncoding
assert
encoding
[
0
].
lower
()
==
"big5"
Back
|
FazBrowse Home
|
New Git URL