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