summaryrefslogtreecommitdiff
path: root/tests/glibext/hashable.py
blob: 07f22e37fcf7a5d13af82aee121167671ca22e71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190

import gi

from chrysacase import ChrysalideTestCase
from gi.repository import GObject
from pychrysalide.glibext import HashableObject


class TestStringBuilder(ChrysalideTestCase):
    """Test cases for pychrysalide.glibext.HashableObject."""


    def testHashableObjectCreation(self):
        """Create objects with HashableObject interface."""

        with self.assertRaisesRegex(NotImplementedError, 'HashableObject can not be constructed'):

            ho = HashableObject()


        class NewHashableObjectImplem(gi._gi.GObject, HashableObject):
            pass

        nho = NewHashableObjectImplem()

        self.assertIsNotNone(nho)


        class NewHashableObjectImplem2(GObject.Object, HashableObject):
            pass

        nho2 = NewHashableObjectImplem()

        self.assertIsNotNone(nho2)


    def testHashableObjectMethods(self):
        """Test the HashableObject methods."""

        class BasicHashableObjectImplem(gi._gi.GObject, HashableObject):

            def __init__(self, val):
                super().__init__()
                self._val = val

            def _hash(self):
                return self._val

        value = 1234

        ho = BasicHashableObjectImplem(value)

        self.assertEqual(hash(ho), value)


        class BasicHashableObjectImplem2(GObject.Object, HashableObject):

            def __init__(self, val):
                super().__init__()
                self._val = val

            def _hash(self):
                return self._val

        value = 5678

        ho2 = BasicHashableObjectImplem2(value)

        self.assertEqual(hash(ho2), value)


    def testCascadingHashableObjectImplementations(self):
        """Request the hash from the object parent for a full computing."""


        class CascadingHashableObjectFullImplemA(GObject.Object, HashableObject):

            def _hash(self):
                return 1

        class CascadingHashableObjectFullImplemB(CascadingHashableObjectFullImplemA, HashableObject):

            def _hash(self):
                return super()._hash() * 2

        class CascadingHashableObjectFullImplemC(CascadingHashableObjectFullImplemB, HashableObject):

            def _hash(self):
                return super()._hash() * 3


        obj_a = CascadingHashableObjectFullImplemA()

        obj_b = CascadingHashableObjectFullImplemB()

        obj_c = CascadingHashableObjectFullImplemC()


        self.assertEqual(hash(obj_a), 1)
        self.assertEqual(hash(obj_b), 2)
        self.assertEqual(hash(obj_c), 6)


        class CascadingHashableObjectPartialImplemA(GObject.Object, HashableObject):

            def _hash(self):
                return 1

        class CascadingHashableObjectPartialImplemB(CascadingHashableObjectPartialImplemA):
            pass

        class CascadingHashableObjectPartialImplemC(CascadingHashableObjectPartialImplemB, HashableObject):

            def _hash(self):
                return super()._hash() * 3


        obj_a = CascadingHashableObjectPartialImplemA()

        obj_b = CascadingHashableObjectPartialImplemB()

        obj_c = CascadingHashableObjectPartialImplemC()


        self.assertEqual(hash(obj_a), 1)
        self.assertEqual(hash(obj_b), 1)
        self.assertEqual(hash(obj_c), 3)


    def testHashableObjectExceptions(self):
        """Raise exceptions from the HashableObject interface as expected."""

        class BadHashableObjectImplem(GObject.Object, HashableObject):
            pass

        obj = BadHashableObjectImplem()


        with self.assertRaisesRegex(NotImplementedError, "method implementation is missing for '_hash'"):

            h = hash(obj)


        with self.assertRaisesRegex(TypeError, 'object parent does not implement the HashableObject interface'):

            h = obj.parent_hash()


        class BadHashableObjectImplem2(GObject.Object, HashableObject):

            def _hash(self):
                return 'AAA'

        obj2 = BadHashableObjectImplem2()


        with self.assertRaisesRegex(TypeError, 'computed hash value has to be an unsigned integer'):

            h = hash(obj2)


        class BadHashableObjectImplem3a(GObject.Object, HashableObject):

            def _hash(self):
                return 123

        class BadHashableObjectImplem3b(BadHashableObjectImplem3a, HashableObject):

            def _hash(self):
                return self.parent_hash()

        obj3 = BadHashableObjectImplem3b()


        with self.assertRaisesRegex(RuntimeError, 'object parent is not a native type'):

            h = hash(obj3)


        class BadHashableObjectImplem4(GObject.Object, HashableObject):

            def _hash(self):
                raise Exception('error')

        obj4 = BadHashableObjectImplem4()


        with self.assertRaisesRegex(Exception, 'error'):

            h = hash(obj4)