2 # Simple tests for the talloc python bindings.
3 # Copyright (C) 2015 Petr Viktorin <pviktori@redhat.com>
18 class TallocTests(unittest.TestCase):
20 def test_report_full(self):
21 # report_full is hardcoded to print to stdout, so use a subprocess
22 process = subprocess.Popen([
25 import talloc, _test_pytalloc
26 obj = _test_pytalloc.new()
27 talloc.report_full(obj)
29 ], stdout=subprocess.PIPE)
30 output, stderr = process.communicate()
32 self.assertTrue("full talloc report on 'talloc.Object" in output)
33 self.assertTrue("This is a test string" in output)
35 def test_totalblocks(self):
36 obj = _test_pytalloc.new()
37 # Two blocks: the string, and the name
38 self.assertEqual(talloc.total_blocks(obj), 2)
41 obj = _test_pytalloc.new()
42 prefix = '<talloc.Object talloc object at'
43 self.assertTrue(repr(obj).startswith(prefix))
44 self.assertEqual(repr(obj), str(obj))
46 def test_destructor(self):
47 # Check correct lifetime of the talloc'd data
49 obj = _test_pytalloc.DObject(lambda: lst.append('dead'))
50 self.assertEqual(lst, [])
53 self.assertEqual(lst, ['dead'])
56 class TallocComparisonTests(unittest.TestCase):
58 def test_compare_same(self):
59 obj1 = _test_pytalloc.new()
60 self.assertTrue(obj1 == obj1)
61 self.assertFalse(obj1 != obj1)
62 self.assertTrue(obj1 <= obj1)
63 self.assertFalse(obj1 < obj1)
64 self.assertTrue(obj1 >= obj1)
65 self.assertFalse(obj1 > obj1)
67 def test_compare_different(self):
68 # object comparison is consistent
71 _test_pytalloc.new()])
72 self.assertFalse(obj1 == obj2)
73 self.assertTrue(obj1 != obj2)
74 self.assertTrue(obj1 <= obj2)
75 self.assertTrue(obj1 < obj2)
76 self.assertFalse(obj1 >= obj2)
77 self.assertFalse(obj1 > obj2)
79 def test_compare_different_types(self):
80 # object comparison falls back to comparing types
81 if sys.version_info >= (3, 0):
82 # In Python 3, types are unorderable -- nothing to test
84 if talloc.Object < _test_pytalloc.DObject:
85 obj1 = _test_pytalloc.new()
86 obj2 = _test_pytalloc.DObject(dummy_func)
88 obj2 = _test_pytalloc.new()
89 obj1 = _test_pytalloc.DObject(dummy_func)
90 self.assertFalse(obj1 == obj2)
91 self.assertTrue(obj1 != obj2)
92 self.assertTrue(obj1 <= obj2)
93 self.assertTrue(obj1 < obj2)
94 self.assertFalse(obj1 >= obj2)
95 self.assertFalse(obj1 > obj2)
98 class TallocUtilTests(unittest.TestCase):
100 def test_get_type(self):
101 self.assertTrue(talloc.Object is _test_pytalloc.get_object_type())
103 def test_refrence(self):
104 # Check correct lifetime of the talloc'd data with multiple references
106 obj = _test_pytalloc.DObject(lambda: lst.append('dead'))
107 ref = _test_pytalloc.reference(obj)
110 self.assertEqual(lst, [])
113 self.assertEqual(lst, ['dead'])
116 if __name__ == '__main__':
117 unittest.TestProgram()