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 talloc
.Object
< _test_pytalloc
.DObject
:
82 obj1
= _test_pytalloc
.new()
83 obj2
= _test_pytalloc
.DObject(dummy_func
)
85 obj2
= _test_pytalloc
.new()
86 obj1
= _test_pytalloc
.DObject(dummy_func
)
87 self
.assertFalse(obj1
== obj2
)
88 self
.assertTrue(obj1
!= obj2
)
89 self
.assertTrue(obj1
<= obj2
)
90 self
.assertTrue(obj1
< obj2
)
91 self
.assertFalse(obj1
>= obj2
)
92 self
.assertFalse(obj1
> obj2
)
95 class TallocUtilTests(unittest
.TestCase
):
97 def test_get_type(self
):
98 self
.assertTrue(talloc
.Object
is _test_pytalloc
.get_object_type())
100 def test_refrence(self
):
101 # Check correct lifetime of the talloc'd data with multiple references
103 obj
= _test_pytalloc
.DObject(lambda: lst
.append('dead'))
104 ref
= _test_pytalloc
.reference(obj
)
107 self
.assertEqual(lst
, [])
110 self
.assertEqual(lst
, ['dead'])
113 if __name__
== '__main__':
114 unittest
.TestProgram()