Marc-André Lemburg | ef34274 | 2001-01-10 10:21:11 +0000 | [diff] [blame] | 1 | """ Python character mapping codec test |
| 2 | |
| 3 | This uses the test codec in testcodec.py and thus also tests the |
| 4 | encodings package lookup scheme. |
Marc-André Lemburg | 1109db4 | 2001-01-06 15:09:57 +0000 | [diff] [blame] | 5 | |
| 6 | Written by Marc-Andre Lemburg (mal@lemburg.com). |
| 7 | |
| 8 | (c) Copyright 2000 Guido van Rossum. |
| 9 | |
| 10 | """#" |
Marc-André Lemburg | 1109db4 | 2001-01-06 15:09:57 +0000 | [diff] [blame] | 11 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 12 | import test.support, unittest |
Marc-André Lemburg | 1109db4 | 2001-01-06 15:09:57 +0000 | [diff] [blame] | 13 | |
Marc-André Lemburg | fe4b34c | 2006-02-19 15:22:22 +0000 | [diff] [blame] | 14 | import codecs |
| 15 | |
| 16 | # Register a search function which knows about our codec |
| 17 | def codec_search_function(encoding): |
| 18 | if encoding == 'testcodec': |
| 19 | from test import testcodec |
| 20 | return tuple(testcodec.getregentry()) |
| 21 | return None |
| 22 | |
| 23 | codecs.register(codec_search_function) |
| 24 | |
| 25 | # test codec's name (see test/testcodec.py) |
| 26 | codecname = 'testcodec' |
Tim Peters | d2bf3b7 | 2001-01-18 02:22:22 +0000 | [diff] [blame] | 27 | |
Walter Dörwald | bb2734a | 2003-02-14 11:21:53 +0000 | [diff] [blame] | 28 | class CharmapCodecTest(unittest.TestCase): |
| 29 | def test_constructorx(self): |
Walter Dörwald | eab198d | 2007-05-23 19:00:48 +0000 | [diff] [blame] | 30 | self.assertEquals(str(b'abc', codecname), 'abc') |
| 31 | self.assertEquals(str(b'xdef', codecname), 'abcdef') |
| 32 | self.assertEquals(str(b'defx', codecname), 'defabc') |
| 33 | self.assertEquals(str(b'dxf', codecname), 'dabcf') |
| 34 | self.assertEquals(str(b'dxfx', codecname), 'dabcfabc') |
Marc-André Lemburg | ef34274 | 2001-01-10 10:21:11 +0000 | [diff] [blame] | 35 | |
Walter Dörwald | bb2734a | 2003-02-14 11:21:53 +0000 | [diff] [blame] | 36 | def test_encodex(self): |
Walter Dörwald | eab198d | 2007-05-23 19:00:48 +0000 | [diff] [blame] | 37 | self.assertEquals('abc'.encode(codecname), b'abc') |
| 38 | self.assertEquals('xdef'.encode(codecname), b'abcdef') |
| 39 | self.assertEquals('defx'.encode(codecname), b'defabc') |
| 40 | self.assertEquals('dxf'.encode(codecname), b'dabcf') |
| 41 | self.assertEquals('dxfx'.encode(codecname), b'dabcfabc') |
Marc-André Lemburg | ef34274 | 2001-01-10 10:21:11 +0000 | [diff] [blame] | 42 | |
Walter Dörwald | bb2734a | 2003-02-14 11:21:53 +0000 | [diff] [blame] | 43 | def test_constructory(self): |
Walter Dörwald | eab198d | 2007-05-23 19:00:48 +0000 | [diff] [blame] | 44 | self.assertEquals(str(b'ydef', codecname), 'def') |
| 45 | self.assertEquals(str(b'defy', codecname), 'def') |
| 46 | self.assertEquals(str(b'dyf', codecname), 'df') |
| 47 | self.assertEquals(str(b'dyfy', codecname), 'df') |
Marc-André Lemburg | ef34274 | 2001-01-10 10:21:11 +0000 | [diff] [blame] | 48 | |
Walter Dörwald | bb2734a | 2003-02-14 11:21:53 +0000 | [diff] [blame] | 49 | def test_maptoundefined(self): |
Walter Dörwald | eab198d | 2007-05-23 19:00:48 +0000 | [diff] [blame] | 50 | self.assertRaises(UnicodeError, str, b'abc\001', codecname) |
Walter Dörwald | bb2734a | 2003-02-14 11:21:53 +0000 | [diff] [blame] | 51 | |
| 52 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 53 | test.support.run_unittest(CharmapCodecTest) |
Walter Dörwald | bb2734a | 2003-02-14 11:21:53 +0000 | [diff] [blame] | 54 | |
| 55 | if __name__ == "__main__": |
| 56 | test_main() |