blob: 6226587267e1c08de1a1c856fc8c6881e24e699b [file] [log] [blame]
Marc-André Lemburgef342742001-01-10 10:21:11 +00001""" Python character mapping codec test
2
3This uses the test codec in testcodec.py and thus also tests the
4encodings package lookup scheme.
Marc-André Lemburg1109db42001-01-06 15:09:57 +00005
6Written by Marc-Andre Lemburg (mal@lemburg.com).
7
8(c) Copyright 2000 Guido van Rossum.
9
10"""#"
Marc-André Lemburg1109db42001-01-06 15:09:57 +000011
Benjamin Petersonee8712c2008-05-20 21:35:26 +000012import test.support, unittest
Marc-André Lemburg1109db42001-01-06 15:09:57 +000013
Marc-André Lemburgfe4b34c2006-02-19 15:22:22 +000014import codecs
15
16# Register a search function which knows about our codec
17def codec_search_function(encoding):
18 if encoding == 'testcodec':
19 from test import testcodec
20 return tuple(testcodec.getregentry())
21 return None
22
23codecs.register(codec_search_function)
24
25# test codec's name (see test/testcodec.py)
26codecname = 'testcodec'
Tim Petersd2bf3b72001-01-18 02:22:22 +000027
Walter Dörwaldbb2734a2003-02-14 11:21:53 +000028class CharmapCodecTest(unittest.TestCase):
29 def test_constructorx(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +000030 self.assertEqual(str(b'abc', codecname), 'abc')
31 self.assertEqual(str(b'xdef', codecname), 'abcdef')
32 self.assertEqual(str(b'defx', codecname), 'defabc')
33 self.assertEqual(str(b'dxf', codecname), 'dabcf')
34 self.assertEqual(str(b'dxfx', codecname), 'dabcfabc')
Marc-André Lemburgef342742001-01-10 10:21:11 +000035
Walter Dörwaldbb2734a2003-02-14 11:21:53 +000036 def test_encodex(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +000037 self.assertEqual('abc'.encode(codecname), b'abc')
38 self.assertEqual('xdef'.encode(codecname), b'abcdef')
39 self.assertEqual('defx'.encode(codecname), b'defabc')
40 self.assertEqual('dxf'.encode(codecname), b'dabcf')
41 self.assertEqual('dxfx'.encode(codecname), b'dabcfabc')
Marc-André Lemburgef342742001-01-10 10:21:11 +000042
Walter Dörwaldbb2734a2003-02-14 11:21:53 +000043 def test_constructory(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +000044 self.assertEqual(str(b'ydef', codecname), 'def')
45 self.assertEqual(str(b'defy', codecname), 'def')
46 self.assertEqual(str(b'dyf', codecname), 'df')
47 self.assertEqual(str(b'dyfy', codecname), 'df')
Marc-André Lemburgef342742001-01-10 10:21:11 +000048
Walter Dörwaldbb2734a2003-02-14 11:21:53 +000049 def test_maptoundefined(self):
Walter Dörwaldeab198d2007-05-23 19:00:48 +000050 self.assertRaises(UnicodeError, str, b'abc\001', codecname)
Walter Dörwaldbb2734a2003-02-14 11:21:53 +000051
52def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +000053 test.support.run_unittest(CharmapCodecTest)
Walter Dörwaldbb2734a2003-02-14 11:21:53 +000054
55if __name__ == "__main__":
56 test_main()