1 | n/a | """ Python character mapping codec test |
---|
2 | n/a | |
---|
3 | n/a | This uses the test codec in testcodec.py and thus also tests the |
---|
4 | n/a | encodings package lookup scheme. |
---|
5 | n/a | |
---|
6 | n/a | Written by Marc-Andre Lemburg (mal@lemburg.com). |
---|
7 | n/a | |
---|
8 | n/a | (c) Copyright 2000 Guido van Rossum. |
---|
9 | n/a | |
---|
10 | n/a | """#" |
---|
11 | n/a | |
---|
12 | n/a | import unittest |
---|
13 | n/a | |
---|
14 | n/a | import codecs |
---|
15 | n/a | |
---|
16 | n/a | # Register a search function which knows about our codec |
---|
17 | n/a | def codec_search_function(encoding): |
---|
18 | n/a | if encoding == 'testcodec': |
---|
19 | n/a | from test import testcodec |
---|
20 | n/a | return tuple(testcodec.getregentry()) |
---|
21 | n/a | return None |
---|
22 | n/a | |
---|
23 | n/a | codecs.register(codec_search_function) |
---|
24 | n/a | |
---|
25 | n/a | # test codec's name (see test/testcodec.py) |
---|
26 | n/a | codecname = 'testcodec' |
---|
27 | n/a | |
---|
28 | n/a | class CharmapCodecTest(unittest.TestCase): |
---|
29 | n/a | def test_constructorx(self): |
---|
30 | n/a | self.assertEqual(str(b'abc', codecname), 'abc') |
---|
31 | n/a | self.assertEqual(str(b'xdef', codecname), 'abcdef') |
---|
32 | n/a | self.assertEqual(str(b'defx', codecname), 'defabc') |
---|
33 | n/a | self.assertEqual(str(b'dxf', codecname), 'dabcf') |
---|
34 | n/a | self.assertEqual(str(b'dxfx', codecname), 'dabcfabc') |
---|
35 | n/a | |
---|
36 | n/a | def test_encodex(self): |
---|
37 | n/a | self.assertEqual('abc'.encode(codecname), b'abc') |
---|
38 | n/a | self.assertEqual('xdef'.encode(codecname), b'abcdef') |
---|
39 | n/a | self.assertEqual('defx'.encode(codecname), b'defabc') |
---|
40 | n/a | self.assertEqual('dxf'.encode(codecname), b'dabcf') |
---|
41 | n/a | self.assertEqual('dxfx'.encode(codecname), b'dabcfabc') |
---|
42 | n/a | |
---|
43 | n/a | def test_constructory(self): |
---|
44 | n/a | self.assertEqual(str(b'ydef', codecname), 'def') |
---|
45 | n/a | self.assertEqual(str(b'defy', codecname), 'def') |
---|
46 | n/a | self.assertEqual(str(b'dyf', codecname), 'df') |
---|
47 | n/a | self.assertEqual(str(b'dyfy', codecname), 'df') |
---|
48 | n/a | |
---|
49 | n/a | def test_maptoundefined(self): |
---|
50 | n/a | self.assertRaises(UnicodeError, str, b'abc\001', codecname) |
---|
51 | n/a | |
---|
52 | n/a | if __name__ == "__main__": |
---|
53 | n/a | unittest.main() |
---|