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 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(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') 35 36 def test_encodex(self): 37 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') 42 43 def test_constructory(self): 44 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') 48 49 def test_maptoundefined(self): 50 self.assertRaises(UnicodeError, str, b'abc\001', codecname) 51 52if __name__ == "__main__": 53 unittest.main() 54