forked from chardet/chardet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
56 lines (48 loc) · 1.73 KB
/
test.py
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
import os
import sys
import unittest
from chardet.universaldetector import UniversalDetector
class TestCase(unittest.TestCase):
def __init__(self, file_name, encoding):
unittest.TestCase.__init__(self)
self.file_name = file_name
encoding = encoding.lower()
for postfix in ['-arabic',
'-bulgarian',
'-cyrillic',
'-greek',
'-hebrew',
'-hungarian',
'-turkish']:
if encoding.endswith(postfix):
encoding, _, _ = encoding.rpartition(postfix)
self.encoding = encoding
def runTest(self):
u = UniversalDetector()
for line in open(self.file_name, 'rb'):
u.feed(line)
if u.done:
break
u.close()
self.assertEqual(u.result['encoding'].lower(), self.encoding,
"Expected %s, but got %r in %s" %
(self.encoding, u.result['encoding'],
self.file_name))
def main():
suite = unittest.TestSuite()
if len(sys.argv) > 1:
base_path = sys.argv[1]
else:
base_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)), 'tests')
for encoding in os.listdir(base_path):
path = os.path.join(base_path, encoding)
if not os.path.isdir(path):
continue
for file_name in os.listdir(path):
_, ext = os.path.splitext(file_name)
if ext not in ['.html', '.txt', '.xml', '.srt']:
continue
suite.addTest(TestCase(os.path.join(path, file_name), encoding))
unittest.TextTestRunner().run(suite)
main()