1""" Python character mapping codec test
2
3This uses the test codec in testcodec.py and thus also tests the
4encodings package lookup scheme.
5
6Written by Marc-Andre Lemburg (mal@lemburg.com).
7
8(c) Copyright 2000 Guido van Rossum.
9
10"""#"
11
12import test.test_support, unittest
13
14import 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'
27
28class CharmapCodecTest(unittest.TestCase):
29    def test_constructorx(self):
30        self.assertEqual(unicode('abc', codecname), u'abc')
31        self.assertEqual(unicode('xdef', codecname), u'abcdef')
32        self.assertEqual(unicode('defx', codecname), u'defabc')
33        self.assertEqual(unicode('dxf', codecname), u'dabcf')
34        self.assertEqual(unicode('dxfx', codecname), u'dabcfabc')
35
36    def test_encodex(self):
37        self.assertEqual(u'abc'.encode(codecname), 'abc')
38        self.assertEqual(u'xdef'.encode(codecname), 'abcdef')
39        self.assertEqual(u'defx'.encode(codecname), 'defabc')
40        self.assertEqual(u'dxf'.encode(codecname), 'dabcf')
41        self.assertEqual(u'dxfx'.encode(codecname), 'dabcfabc')
42
43    def test_constructory(self):
44        self.assertEqual(unicode('ydef', codecname), u'def')
45        self.assertEqual(unicode('defy', codecname), u'def')
46        self.assertEqual(unicode('dyf', codecname), u'df')
47        self.assertEqual(unicode('dyfy', codecname), u'df')
48
49    def test_maptoundefined(self):
50        self.assertRaises(UnicodeError, unicode, 'abc\001', codecname)
51
52def test_main():
53    test.test_support.run_unittest(CharmapCodecTest)
54
55if __name__ == "__main__":
56    test_main()
57