Move xdrlib tests from the module into a separate test script,

port the tests to unittest and add a few new tests.
This commit is contained in:
Walter Dörwald 2006-12-01 16:59:47 +00:00
parent c764340ba2
commit f008203cb4
3 changed files with 54 additions and 78 deletions

View File

@ -1,19 +0,0 @@
test_xdrlib
pack test 0 succeeded
pack test 1 succeeded
pack test 2 succeeded
pack test 3 succeeded
pack test 4 succeeded
pack test 5 succeeded
pack test 6 succeeded
pack test 7 succeeded
pack test 8 succeeded
unpack test 0 succeeded : 9
unpack test 1 succeeded : True
unpack test 2 succeeded : False
unpack test 3 succeeded : 45
unpack test 4 succeeded : 1.89999997616
unpack test 5 succeeded : 1.9
unpack test 6 succeeded : hello world
unpack test 7 succeeded : [0, 1, 2, 3, 4]
unpack test 8 succeeded : ['what', 'is', 'hapnin', 'doctor']

View File

@ -1,3 +1,56 @@
from test import test_support
import unittest
import xdrlib import xdrlib
xdrlib._test() class XDRTest(unittest.TestCase):
def test_xdr(self):
p = xdrlib.Packer()
s = 'hello world'
a = ['what', 'is', 'hapnin', 'doctor']
p.pack_int(42)
p.pack_uint(9)
p.pack_bool(True)
p.pack_bool(False)
p.pack_uhyper(45L)
p.pack_float(1.9)
p.pack_double(1.9)
p.pack_string(s)
p.pack_list(range(5), p.pack_uint)
p.pack_array(a, p.pack_string)
# now verify
data = p.get_buffer()
up = xdrlib.Unpacker(data)
self.assertEqual(up.get_position(), 0)
self.assertEqual(up.unpack_int(), 42)
self.assertEqual(up.unpack_uint(), 9)
self.assert_(up.unpack_bool() is True)
# remember position
pos = up.get_position()
self.assert_(up.unpack_bool() is False)
# rewind and unpack again
up.set_position(pos)
self.assert_(up.unpack_bool() is False)
self.assertEqual(up.unpack_uhyper(), 45L)
self.assertAlmostEqual(up.unpack_float(), 1.9)
self.assertAlmostEqual(up.unpack_double(), 1.9)
self.assertEqual(up.unpack_string(), s)
self.assertEqual(up.unpack_list(up.unpack_uint), range(5))
self.assertEqual(up.unpack_array(up.unpack_string), a)
up.done()
self.assertRaises(EOFError, up.unpack_uint)
def test_main():
test_support.run_unittest(XDRTest)
if __name__ == "__main__":
test_main()

View File

@ -227,61 +227,3 @@ class Unpacker:
def unpack_array(self, unpack_item): def unpack_array(self, unpack_item):
n = self.unpack_uint() n = self.unpack_uint()
return self.unpack_farray(n, unpack_item) return self.unpack_farray(n, unpack_item)
# test suite
def _test():
p = Packer()
packtest = [
(p.pack_uint, (9,)),
(p.pack_bool, (True,)),
(p.pack_bool, (False,)),
(p.pack_uhyper, (45L,)),
(p.pack_float, (1.9,)),
(p.pack_double, (1.9,)),
(p.pack_string, ('hello world',)),
(p.pack_list, (range(5), p.pack_uint)),
(p.pack_array, (['what', 'is', 'hapnin', 'doctor'], p.pack_string)),
]
succeedlist = [1] * len(packtest)
count = 0
for method, args in packtest:
print 'pack test', count,
try:
method(*args)
print 'succeeded'
except ConversionError, var:
print 'ConversionError:', var.msg
succeedlist[count] = 0
count = count + 1
data = p.get_buffer()
# now verify
up = Unpacker(data)
unpacktest = [
(up.unpack_uint, (), lambda x: x == 9),
(up.unpack_bool, (), lambda x: x is True),
(up.unpack_bool, (), lambda x: x is False),
(up.unpack_uhyper, (), lambda x: x == 45L),
(up.unpack_float, (), lambda x: 1.89 < x < 1.91),
(up.unpack_double, (), lambda x: 1.89 < x < 1.91),
(up.unpack_string, (), lambda x: x == 'hello world'),
(up.unpack_list, (up.unpack_uint,), lambda x: x == range(5)),
(up.unpack_array, (up.unpack_string,),
lambda x: x == ['what', 'is', 'hapnin', 'doctor']),
]
count = 0
for method, args, pred in unpacktest:
print 'unpack test', count,
try:
if succeedlist[count]:
x = method(*args)
print pred(x) and 'succeeded' or 'failed', ':', x
else:
print 'skipping'
except ConversionError, var:
print 'ConversionError:', var.msg
count = count + 1
if __name__ == '__main__':
_test()