-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_iconvcodec.py
More file actions
54 lines (43 loc) · 1.96 KB
/
test_iconvcodec.py
File metadata and controls
54 lines (43 loc) · 1.96 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
import unittest
import iconvcodec
import codecs
import sys
class TestIconvcodecModule(unittest.TestCase):
@unittest.skipUnless(sys.platform.startswith("linux"), "Linux only test")
def test_encode(self):
bytestring = "Hallo".encode("T.61")
self.assertEqual(bytestring, b"Hallo")
def test_encode_with_long_out(self):
"""Edge case where output has more bytes than input as utf-8"""
bytestring = "™".encode("ASCII//TRANSLIT")
if sys.platform.startswith("linux"):
self.assertEqual(bytestring, b"(TM)")
else:
self.assertEqual(bytestring, b"TM")
@unittest.skipUnless(sys.platform.startswith("linux"), "Linux only test")
def test_decode(self):
string = b"Hallo".decode("T.61")
self.assertEqual(string, "Hallo")
@unittest.skipUnless(sys.platform.startswith("linux"), "Linux only test")
def test_transliterate(self):
string = "abc ß α € àḃç"
bytestring = string.encode("ASCII//TRANSLIT")
self.assertEqual(bytestring, b"abc ss ? EUR abc")
def test_incremental_encode(self):
encoder = codecs.getincrementalencoder("ASCII//TRANSLIT")()
first = encoder.encode("Foo")
second = encoder.encode("bar", final=True)
self.assertEqual(first, b"Foo")
self.assertEqual(second, b"bar")
@unittest.skipUnless(sys.platform.startswith("linux"), "Linux only test")
def test_incremental_decode(self):
decoder = codecs.getincrementaldecoder("UCS2")()
first = decoder.decode(b"\x41")
second = decoder.decode(b"\x01", final=True)
self.assertEqual(first, "")
self.assertEqual(second, "\u0141")
def test_invalid_errors(self):
with self.assertRaisesRegex(ValueError, "unsupported error handling"):
"™".encode("ASCII//TRANSLIT", "WRONG")
with self.assertRaisesRegex(ValueError, "unsupported error handling"):
b"a".decode("ASCII//TRANSLIT", "WRONG")