forked from robotframework/robotframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_utf8reader.py
More file actions
66 lines (48 loc) · 1.83 KB
/
test_utf8reader.py
File metadata and controls
66 lines (48 loc) · 1.83 KB
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
import os
import tempfile
import unittest
from codecs import BOM_UTF8
from io import BytesIO
from robot.utils import Utf8Reader
from robot.utils.asserts import assert_equal, assert_raises
PATH = os.path.join(tempfile.gettempdir(), 'test_utf8reader.xml')
STRING = u'Hyv\xe4\xe4\nty\xf6t\xe4\n.C\u043f\u0430\u0441\u0438\u0431\u043e'
class TestUtf8ReaderWithBom(unittest.TestCase):
BOM = BOM_UTF8
def setUp(self):
self._create()
def _create(self, content=STRING, encoding='UTF-8'):
with open(PATH, 'wb') as f:
inn = self.BOM + content.encode(encoding)
f.write(inn)
def tearDown(self):
os.remove(PATH)
def test_read(self):
with Utf8Reader(PATH) as reader:
f = reader._file
assert_equal(reader.read(), STRING)
assert_equal(f.closed, True)
def test_read_open_file(self):
with open(PATH, 'rb') as f:
with Utf8Reader(f) as reader:
assert_equal(reader.read(), STRING)
assert_equal(f.closed, False)
def test_must_open_in_binary_mode(self):
with open(PATH, 'r') as f:
assert_raises(ValueError, Utf8Reader, f)
def test_stringio_is_ok(self):
f = BytesIO(self.BOM + STRING.encode('UTF-8'))
with Utf8Reader(f) as reader:
assert_equal(reader.read(), STRING)
assert_equal(f.closed, False)
def test_readlines(self):
with Utf8Reader(PATH) as reader:
assert_equal(list(reader.readlines()), STRING.splitlines(True))
def test_invalid_encoding(self):
self._create(STRING.splitlines()[-1], 'ISO-8859-5')
with Utf8Reader(PATH) as reader:
assert_raises(UnicodeDecodeError, reader.read)
class TestUtf8ReaderWithoutBom(TestUtf8ReaderWithBom):
BOM = b''
if __name__ == '__main__':
unittest.main()