Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ef34274

Browse files
committed
Moved the test codec definition to a new module and updated the test and
codec to test all charmap codec features. As side-effect of moving the test codec into a new module, the encodings package codec import mechanism is checked as well.
1 parent 0d9f9dc commit ef34274

3 files changed

Lines changed: 97 additions & 54 deletions

File tree

Lib/test/output/test_charmapcodec

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
11
test_charmapcodec
2+
u'abc' == u'abc': OK
3+
u'abcdef' == u'abcdef': OK
4+
u'defabc' == u'defabc': OK
5+
u'dabcf' == u'dabcf': OK
6+
u'dabcfabc' == u'dabcfabc': OK
7+
'abc' == 'abc': OK
8+
'abcdef' == 'abcdef': OK
9+
'defabc' == 'defabc': OK
10+
'dabcf' == 'dabcf': OK
11+
'dabcfabc' == 'dabcfabc': OK
12+
u'def' == u'def': OK
13+
u'def' == u'def': OK
14+
u'df' == u'df': OK
15+
u'df' == u'df': OK
16+
\001 maps to undefined: OK

Lib/test/test_charmapcodec.py

Lines changed: 33 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,44 @@
1-
""" Python Character Mapping Codec test
1+
""" Python character mapping codec test
2+
3+
This uses the test codec in testcodec.py and thus also tests the
4+
encodings package lookup scheme.
25
36
Written by Marc-Andre Lemburg ([email protected]).
47
58
(c) Copyright 2000 Guido van Rossum.
69
710
"""#"
8-
import codecs
9-
10-
### Codec APIs
11-
12-
class Codec(codecs.Codec):
13-
14-
def encode(self,input,errors='strict'):
15-
16-
return codecs.charmap_encode(input,errors,encoding_map)
17-
18-
def decode(self,input,errors='strict'):
19-
20-
return codecs.charmap_decode(input,errors,decoding_map)
21-
22-
class StreamWriter(Codec,codecs.StreamWriter):
23-
pass
24-
25-
class StreamReader(Codec,codecs.StreamReader):
26-
pass
27-
28-
### encodings module API
29-
30-
def getregentry():
31-
32-
return (Codec().encode,Codec().decode,StreamReader,StreamWriter)
33-
34-
### Decoding Map
35-
36-
decoding_map = codecs.make_identity_dict(range(256))
37-
decoding_map.update({
38-
0x0078: u"abc",
39-
"abc": 0x0078,
40-
})
41-
42-
### Encoding Map
43-
44-
encoding_map = {}
45-
for k,v in decoding_map.items():
46-
encoding_map[v] = k
47-
48-
49-
### Tests
5011

5112
def check(a, b):
5213
if a != b:
5314
print '*** check failed: %s != %s' % (repr(a), repr(b))
54-
55-
check(unicode('abc', 'mycp'), u'abc')
56-
check(unicode('xdef', 'mycp'), u'abcdef')
57-
check(unicode('defx', 'mycp'), u'defabc')
58-
check(unicode('dxf', 'mycp'), u'dabcf')
59-
check(unicode('dxfx', 'mycp'), u'dabcfabc')
15+
else:
16+
print '%s == %s: OK' % (repr(a), repr(b))
6017

61-
check(u'abc'.encode('mycp'), 'abc')
62-
check(u'xdef'.encode('mycp'), 'abcdef')
63-
check(u'defx'.encode('mycp'), 'defabc')
64-
check(u'dxf'.encode('mycp'), 'dabcf')
65-
check(u'dxfx'.encode('mycp'), 'dabcfabc')
18+
# test codec's full path name (see test/testcodec.py)
19+
codecname = 'testcodec'
20+
21+
check(unicode('abc', codecname), u'abc')
22+
check(unicode('xdef', codecname), u'abcdef')
23+
check(unicode('defx', codecname), u'defabc')
24+
check(unicode('dxf', codecname), u'dabcf')
25+
check(unicode('dxfx', codecname), u'dabcfabc')
26+
27+
check(u'abc'.encode(codecname), 'abc')
28+
check(u'xdef'.encode(codecname), 'abcdef')
29+
check(u'defx'.encode(codecname), 'defabc')
30+
check(u'dxf'.encode(codecname), 'dabcf')
31+
check(u'dxfx'.encode(codecname), 'dabcfabc')
32+
33+
check(unicode('ydef', codecname), u'def')
34+
check(unicode('defy', codecname), u'def')
35+
check(unicode('dyf', codecname), u'df')
36+
check(unicode('dyfy', codecname), u'df')
37+
38+
try:
39+
unicode('abc\001', codecname)
40+
except UnicodeError:
41+
print '\\001 maps to undefined: OK'
42+
else:
43+
print '*** check failed: \\001 does not map to undefined'
44+

Lib/test/testcodec.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
""" Test Codecs (used by test_charmapcodec)
2+
3+
Written by Marc-Andre Lemburg ([email protected]).
4+
5+
(c) Copyright 2000 Guido van Rossum.
6+
7+
"""#"
8+
import codecs
9+
10+
### Codec APIs
11+
12+
class Codec(codecs.Codec):
13+
14+
def encode(self,input,errors='strict'):
15+
16+
return codecs.charmap_encode(input,errors,encoding_map)
17+
18+
def decode(self,input,errors='strict'):
19+
20+
return codecs.charmap_decode(input,errors,decoding_map)
21+
22+
class StreamWriter(Codec,codecs.StreamWriter):
23+
pass
24+
25+
class StreamReader(Codec,codecs.StreamReader):
26+
pass
27+
28+
### encodings module API
29+
30+
def getregentry():
31+
32+
return (Codec().encode,Codec().decode,StreamReader,StreamWriter)
33+
34+
### Decoding Map
35+
36+
decoding_map = codecs.make_identity_dict(range(256))
37+
decoding_map.update({
38+
0x78: u"abc", # 1-n decoding mapping
39+
"abc": 0x0078,# 1-n encoding mapping
40+
0x01: None, # decoding mapping to <undefined>
41+
0x79: u"", # decoding mapping to <remove character>
42+
})
43+
44+
### Encoding Map
45+
46+
encoding_map = {}
47+
for k,v in decoding_map.items():
48+
encoding_map[v] = k
49+

0 commit comments

Comments
 (0)