2001-01-10 06:21:11 -04:00
|
|
|
""" Python character mapping codec test
|
|
|
|
|
|
|
|
This uses the test codec in testcodec.py and thus also tests the
|
|
|
|
encodings package lookup scheme.
|
2001-01-06 11:09:57 -04:00
|
|
|
|
|
|
|
Written by Marc-Andre Lemburg (mal@lemburg.com).
|
|
|
|
|
|
|
|
(c) Copyright 2000 Guido van Rossum.
|
|
|
|
|
|
|
|
"""#"
|
|
|
|
|
2008-05-20 18:35:26 -03:00
|
|
|
import test.support, unittest
|
2001-01-06 11:09:57 -04:00
|
|
|
|
2006-02-19 11:22:22 -04:00
|
|
|
import codecs
|
|
|
|
|
|
|
|
# Register a search function which knows about our codec
|
|
|
|
def codec_search_function(encoding):
|
|
|
|
if encoding == 'testcodec':
|
|
|
|
from test import testcodec
|
|
|
|
return tuple(testcodec.getregentry())
|
|
|
|
return None
|
|
|
|
|
|
|
|
codecs.register(codec_search_function)
|
|
|
|
|
|
|
|
# test codec's name (see test/testcodec.py)
|
|
|
|
codecname = 'testcodec'
|
2001-01-17 22:22:22 -04:00
|
|
|
|
2003-02-14 07:21:53 -04:00
|
|
|
class CharmapCodecTest(unittest.TestCase):
|
|
|
|
def test_constructorx(self):
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(str(b'abc', codecname), 'abc')
|
|
|
|
self.assertEqual(str(b'xdef', codecname), 'abcdef')
|
|
|
|
self.assertEqual(str(b'defx', codecname), 'defabc')
|
|
|
|
self.assertEqual(str(b'dxf', codecname), 'dabcf')
|
|
|
|
self.assertEqual(str(b'dxfx', codecname), 'dabcfabc')
|
2003-02-14 07:21:53 -04:00
|
|
|
|
|
|
|
def test_encodex(self):
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual('abc'.encode(codecname), b'abc')
|
|
|
|
self.assertEqual('xdef'.encode(codecname), b'abcdef')
|
|
|
|
self.assertEqual('defx'.encode(codecname), b'defabc')
|
|
|
|
self.assertEqual('dxf'.encode(codecname), b'dabcf')
|
|
|
|
self.assertEqual('dxfx'.encode(codecname), b'dabcfabc')
|
2003-02-14 07:21:53 -04:00
|
|
|
|
|
|
|
def test_constructory(self):
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(str(b'ydef', codecname), 'def')
|
|
|
|
self.assertEqual(str(b'defy', codecname), 'def')
|
|
|
|
self.assertEqual(str(b'dyf', codecname), 'df')
|
|
|
|
self.assertEqual(str(b'dyfy', codecname), 'df')
|
2003-02-14 07:21:53 -04:00
|
|
|
|
|
|
|
def test_maptoundefined(self):
|
2007-05-23 16:00:48 -03:00
|
|
|
self.assertRaises(UnicodeError, str, b'abc\001', codecname)
|
2003-02-14 07:21:53 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2015-04-13 17:00:43 -03:00
|
|
|
unittest.main()
|