summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_charmapcodec.py
blob: 5e4055eec9474d18f6c48527e98c197a1f62ceae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
""" Python character mapping codec test

This uses the test codec in testcodec.py and thus also tests the
encodings package lookup scheme.

Written by Marc-Andre Lemburg (mal@lemburg.com).

(c) Copyright 2000 Guido van Rossum.

"""#"

import test.test_support, unittest

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'

class CharmapCodecTest(unittest.TestCase):
    def test_constructorx(self):
        self.assertEquals(str(b'abc', codecname), 'abc')
        self.assertEquals(str(b'xdef', codecname), 'abcdef')
        self.assertEquals(str(b'defx', codecname), 'defabc')
        self.assertEquals(str(b'dxf', codecname), 'dabcf')
        self.assertEquals(str(b'dxfx', codecname), 'dabcfabc')

    def test_encodex(self):
        self.assertEquals('abc'.encode(codecname), b'abc')
        self.assertEquals('xdef'.encode(codecname), b'abcdef')
        self.assertEquals('defx'.encode(codecname), b'defabc')
        self.assertEquals('dxf'.encode(codecname), b'dabcf')
        self.assertEquals('dxfx'.encode(codecname), b'dabcfabc')

    def test_constructory(self):
        self.assertEquals(str(b'ydef', codecname), 'def')
        self.assertEquals(str(b'defy', codecname), 'def')
        self.assertEquals(str(b'dyf', codecname), 'df')
        self.assertEquals(str(b'dyfy', codecname), 'df')

    def test_maptoundefined(self):
        self.assertRaises(UnicodeError, str, b'abc\001', codecname)

def test_main():
    test.test_support.run_unittest(CharmapCodecTest)

if __name__ == "__main__":
    test_main()