-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest_codec.py
232 lines (184 loc) · 8.98 KB
/
test_codec.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# The code in this file is copied from https://github.com/praekelt/vumi/blob/master/vumi/codecs/tests/test_vumi_codecs.py
# Vumi's license is included below:
# Copyright (c) Praekelt Foundation and individual contributors.
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of the Praekelt Foundation nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import codecs
from unittest import TestCase, skip
import naz
class TestCodec(TestCase):
"""
run tests as:
python -m unittest discover -v -s .
run one testcase as:
python -m unittest -v tests.test_codec.TestCodec.test_something
"""
def test_byte_encode_guard(self):
codec = codecs.lookup("utf-8")
self.assertRaises(TypeError, codec.encode, b"some bytes")
def test_string_decode_guard(self):
codec = codecs.lookup("utf-8")
self.assertRaises(TypeError, codec.decode, "unicode")
def test_default_encoding(self):
codec = codecs.lookup("utf-8")
self.assertEqual(codec.encode("a")[0], b"a")
def test_default_decoding(self):
codec = codecs.lookup("utf-8")
self.assertEqual(codec.decode(b"a")[0], "a")
def test_encode_utf8(self):
codec = codecs.lookup("utf-8")
self.assertEqual(codec.encode("Zoë")[0], b"Zo\xc3\xab")
def test_decode_utf8(self):
codec = codecs.lookup("utf-8")
self.assertEqual(codec.decode(b"Zo\xc3\xab")[0], "Zoë")
def test_encode_utf16be(self):
codec = codecs.lookup("utf-16be")
self.assertEqual(codec.encode("Zoë")[0], b"\x00Z\x00o\x00\xeb")
def test_decode_utf16be(self):
codec = codecs.lookup("utf-16be")
self.assertEqual(codec.decode(b"\x00Z\x00o\x00\xeb")[0], "Zoë")
def test_encode_ucs2(self):
codec = naz.codec.UCS2Codec()
self.assertEqual(codec.encode("Zoë")[0], b"\x00Z\x00o\x00\xeb")
def test_decode_ucs2(self):
codec = naz.codec.UCS2Codec()
self.assertEqual(codec.decode(b"\x00Z\x00o\x00\xeb")[0], "Zoë")
def test_encode_gsm0338(self):
codec = naz.codec.GSM7BitCodec()
self.assertEqual(
codec.encode("HÜLK")[0], "".join([chr(code) for code in [72, 94, 76, 75]]).encode()
)
def test_encode_gsm0338_extended(self):
codec = naz.codec.GSM7BitCodec()
self.assertEqual(
codec.encode("foo €")[0],
"".join([chr(code) for code in [102, 111, 111, 32, 27, 101]]).encode(),
)
def test_decode_gsm0338_extended(self):
codec = naz.codec.GSM7BitCodec()
self.assertEqual(
codec.decode("".join([chr(code) for code in [102, 111, 111, 32, 27, 101]]).encode())[0],
"foo €",
)
def test_encode_gsm0338_strict(self):
codec = naz.codec.GSM7BitCodec()
self.assertRaises(UnicodeEncodeError, codec.encode, "Zoë", "strict")
def test_encode_gsm0338_ignore(self):
codec = naz.codec.GSM7BitCodec()
self.assertEqual(codec.encode("Zoë", "ignore")[0], b"Zo")
def test_encode_gsm0338_replace(self):
codec = naz.codec.GSM7BitCodec()
self.assertEqual(codec.encode("Zoë", "replace")[0], b"Zo?")
def test_decode_gsm0338_strict(self):
codec = naz.codec.GSM7BitCodec()
self.assertRaises(UnicodeDecodeError, codec.decode, "Zoë".encode("utf-8"), "strict")
def test_decode_gsm0338_ignore(self):
codec = naz.codec.GSM7BitCodec()
self.assertEqual(codec.decode("Zoë".encode("utf-8"), "ignore")[0], "Zo")
def test_decode_gsm0338_replace(self):
codec = naz.codec.GSM7BitCodec()
self.assertEqual(codec.decode("Zoë".encode("utf-8"), "replace")[0], "Zo??")
class TestCodecRegistration(TestCase):
"""
run tests as:
python -m unittest discover -v -s .
run one testcase as:
python -m unittest -v tests.test_codec.TestCodecRegistration.test_something
"""
def test_registration_of_inbuilt_codecs(self):
# register
naz.codec.register_codecs()
for k, _ in naz.codec._INBUILT_CODECS.items():
codec = codecs.lookup(k)
self.assertEqual(codec.name, k)
def test_registration_of_custom_codecs(self):
_sheng_encoding = "kenyan_sheng"
with self.assertRaises(LookupError):
codecs.lookup(_sheng_encoding)
class KenyanShengCodec(codecs.Codec):
# All the methods have to be staticmethods because they are passed to `codecs.CodecInfo`
@staticmethod
def encode(input, errors="strict"):
return codecs.utf_8_encode(input, errors)
@staticmethod
def decode(input, errors="strict"):
return codecs.utf_8_decode(input, errors)
custom_codecs = {
_sheng_encoding: codecs.CodecInfo(
name=_sheng_encoding,
encode=KenyanShengCodec.encode,
decode=KenyanShengCodec.decode,
),
}
# register
naz.codec.register_codecs(custom_codecs)
codec = codecs.lookup(_sheng_encoding)
self.assertEqual(codec.name, _sheng_encoding)
@skip(
"""
TODO:fix this. It does not work.
Note: Encodings are first looked up in the registry's cache.
thus if you call `register_codecs` and then call it again with different
codecs, the second codecs may not take effect.
ie; codecs.lookup(encoding) will return the first codecs since they were stored
in the cache.
There doesn't appear to be away to clear codec cache at runtime.
see: https://docs.python.org/3/library/codecs.html#codecs.lookup
This test passes when called in isolation but it fails when all tests are ran together.
This is because, when all tests are ran together; `naz.codec.register_codecs` will already have
been called and registered the inbuilt codecs. So when this test runs,it tries to override
an already registered codec. And then it calls `codecs.lookup()`. However,
lookup will return the codec that is in the cache which is the inbuilt codecs instead of the ones
we just tried to register in this test.
We should look if to find a way to clear the codec cache at runtime.
There's a C api for that `_PyCodec_Forget`
https://sourcegraph.com/github.com/python/[email protected]/-/blob/Python/codecs.c#L193
We need to figure out how to call it or find other alternatives.
I have sent an email to Marc-Andre Lemburg asking for advice.
"""
)
def test_codec_overriding(self):
"""
tests that users can be able to override an inbuilt codec
with their own implementation.
"""
class OverridingCodec(codecs.Codec):
# All the methods have to be staticmethods because they are passed to `codecs.CodecInfo`
@staticmethod
def encode(input, errors="strict"):
return codecs.utf_8_encode(input, errors)
@staticmethod
def decode(input, errors="strict"):
return codecs.utf_8_decode(input, errors)
custom_codecs = {
"gsm0338": codecs.CodecInfo(
name="gsm0338", encode=OverridingCodec.encode, decode=OverridingCodec.decode,
),
}
# register, this will override inbuilt `gsm0338` codec with a custom one.
naz.codec.register_codecs(custom_codecs)
new_codec = codecs.lookup("gsm0338")
self.assertNotEqual(new_codec.encode, naz.codec.GSM7BitCodec.encode)
self.assertNotEqual(new_codec.decode, naz.codec.GSM7BitCodec.decode)
self.assertEqual(new_codec.encode, OverridingCodec.encode)
self.assertEqual(new_codec.decode, OverridingCodec.decode)