Debian Codegraph @

tests/test-encoding-func.py

raw
1 import unittest
2
3 from mercurial import encoding
4
5
6 class IsasciistrTest(unittest.TestCase):
7 asciistrs = [
8 b'a',
9 b'ab',
10 b'abc',
11 b'abcd',
12 b'abcde',
13 b'abcdefghi',
14 b'abcd\0fghi',
15 ]
16
17 def testascii(self):
18 for s in self.asciistrs:
19 self.assertTrue(encoding.isasciistr(s))
20
21 def testnonasciichar(self):
22 for s in self.asciistrs:
23 for i in range(len(s)):
24 t = bytearray(s)
25 t[i] |= 0x80
26 self.assertFalse(encoding.isasciistr(bytes(t)))
27
28
29 class LocalEncodingTest(unittest.TestCase):
30 def testasciifastpath(self):
31 s = b'\0' * 100
32 self.assertTrue(s is encoding.tolocal(s))
33 self.assertTrue(s is encoding.fromlocal(s))
34
35
36 class Utf8bEncodingTest(unittest.TestCase):
37 def setUp(self):
38 self.origencoding = encoding.encoding
39
40 def tearDown(self):
41 encoding.encoding = self.origencoding
42
43 def testasciifastpath(self):
44 s = b'\0' * 100
45 self.assertTrue(s is encoding.toutf8b(s))
46 self.assertTrue(s is encoding.fromutf8b(s))
47
48 def testlossylatin(self):
49 encoding.encoding = b'ascii'
50 s = '\xc0'.encode()
51 l = encoding.tolocal(s)
52 self.assertEqual(l, b'?') # lossy
53 self.assertEqual(s, encoding.toutf8b(l)) # utf8 sequence preserved
54
55 def testlosslesslatin(self):
56 encoding.encoding = b'latin-1'
57 s = '\xc0'.encode()
58 l = encoding.tolocal(s)
59 self.assertEqual(l, b'\xc0') # lossless
60 self.assertEqual(s, encoding.toutf8b(l)) # convert back to utf-8
61
62 def testlossy0xed(self):
63 encoding.encoding = b'euc-kr' # U+Dxxx Hangul
64 s = '\ud1bc\xc0'.encode()
65 l = encoding.tolocal(s)
66 self.assertIn(b'\xed', l)
67 self.assertTrue(l.endswith(b'?')) # lossy
68 self.assertEqual(s, encoding.toutf8b(l)) # utf8 sequence preserved
69
70 def testlossless0xed(self):
71 encoding.encoding = b'euc-kr' # U+Dxxx Hangul
72 s = '\ud1bc'.encode()
73 l = encoding.tolocal(s)
74 self.assertEqual(l, b'\xc5\xed') # lossless
75 self.assertEqual(s, encoding.toutf8b(l)) # convert back to utf-8
76
77
78 if __name__ == '__main__':
79 import silenttestrunner
80
81 silenttestrunner.main(__name__)