forked from mopidy/mopidy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_encoding.py
More file actions
52 lines (32 loc) · 1.54 KB
/
test_encoding.py
File metadata and controls
52 lines (32 loc) · 1.54 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
from __future__ import absolute_import, unicode_literals
import unittest
import mock
from mopidy.internal import encoding
@mock.patch('mopidy.internal.encoding.locale.getpreferredencoding')
class LocaleDecodeTest(unittest.TestCase):
def test_can_decode_utf8_strings_with_french_content(self, mock):
mock.return_value = 'UTF-8'
result = encoding.locale_decode(
b'[Errno 98] Adresse d\xc3\xa9j\xc3\xa0 utilis\xc3\xa9e')
self.assertEqual('[Errno 98] Adresse d\xe9j\xe0 utilis\xe9e', result)
def test_can_decode_an_ioerror_with_french_content(self, mock):
mock.return_value = 'UTF-8'
error = IOError(98, b'Adresse d\xc3\xa9j\xc3\xa0 utilis\xc3\xa9e')
result = encoding.locale_decode(error)
expected = '[Errno 98] Adresse d\xe9j\xe0 utilis\xe9e'
self.assertEqual(
expected, result,
'{!r} decoded to {!r} does not match expected {!r}'.format(
error, result, expected))
def test_does_not_use_locale_to_decode_unicode_strings(self, mock):
mock.return_value = 'UTF-8'
encoding.locale_decode('abc')
self.assertFalse(mock.called)
def test_does_not_use_locale_to_decode_ascii_bytestrings(self, mock):
mock.return_value = 'UTF-8'
encoding.locale_decode(b'abc')
self.assertFalse(mock.called)
def test_replaces_unknown_bytes_instead_of_crashing(self, mock):
mock.return_value = 'US-ASCII'
result = encoding.locale_decode(b'abc\xc3def')
assert result == 'abc\ufffddef'