ยปCore Development>Code coverage>Lib/test/test_xdrlib.py

Python code coverage for Lib/test/test_xdrlib.py

#countcontent
1n/aimport unittest
2n/a
3n/aimport xdrlib
4n/a
5n/aclass XDRTest(unittest.TestCase):
6n/a
7n/a def test_xdr(self):
8n/a p = xdrlib.Packer()
9n/a
10n/a s = b'hello world'
11n/a a = [b'what', b'is', b'hapnin', b'doctor']
12n/a
13n/a p.pack_int(42)
14n/a p.pack_int(-17)
15n/a p.pack_uint(9)
16n/a p.pack_bool(True)
17n/a p.pack_bool(False)
18n/a p.pack_uhyper(45)
19n/a p.pack_float(1.9)
20n/a p.pack_double(1.9)
21n/a p.pack_string(s)
22n/a p.pack_list(range(5), p.pack_uint)
23n/a p.pack_array(a, p.pack_string)
24n/a
25n/a # now verify
26n/a data = p.get_buffer()
27n/a up = xdrlib.Unpacker(data)
28n/a
29n/a self.assertEqual(up.get_position(), 0)
30n/a
31n/a self.assertEqual(up.unpack_int(), 42)
32n/a self.assertEqual(up.unpack_int(), -17)
33n/a self.assertEqual(up.unpack_uint(), 9)
34n/a self.assertTrue(up.unpack_bool() is True)
35n/a
36n/a # remember position
37n/a pos = up.get_position()
38n/a self.assertTrue(up.unpack_bool() is False)
39n/a
40n/a # rewind and unpack again
41n/a up.set_position(pos)
42n/a self.assertTrue(up.unpack_bool() is False)
43n/a
44n/a self.assertEqual(up.unpack_uhyper(), 45)
45n/a self.assertAlmostEqual(up.unpack_float(), 1.9)
46n/a self.assertAlmostEqual(up.unpack_double(), 1.9)
47n/a self.assertEqual(up.unpack_string(), s)
48n/a self.assertEqual(up.unpack_list(up.unpack_uint), list(range(5)))
49n/a self.assertEqual(up.unpack_array(up.unpack_string), a)
50n/a up.done()
51n/a self.assertRaises(EOFError, up.unpack_uint)
52n/a
53n/aclass ConversionErrorTest(unittest.TestCase):
54n/a
55n/a def setUp(self):
56n/a self.packer = xdrlib.Packer()
57n/a
58n/a def assertRaisesConversion(self, *args):
59n/a self.assertRaises(xdrlib.ConversionError, *args)
60n/a
61n/a def test_pack_int(self):
62n/a self.assertRaisesConversion(self.packer.pack_int, 'string')
63n/a
64n/a def test_pack_uint(self):
65n/a self.assertRaisesConversion(self.packer.pack_uint, 'string')
66n/a
67n/a def test_float(self):
68n/a self.assertRaisesConversion(self.packer.pack_float, 'string')
69n/a
70n/a def test_double(self):
71n/a self.assertRaisesConversion(self.packer.pack_double, 'string')
72n/a
73n/a def test_uhyper(self):
74n/a self.assertRaisesConversion(self.packer.pack_uhyper, 'string')
75n/a
76n/aif __name__ == "__main__":
77n/a unittest.main()