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
|
import gi
from chrysacase import ChrysalideTestCase
from gi.repository import GObject
from pychrysalide.glibext import ComparableObject
class TestStringBuilder(ChrysalideTestCase):
"""Test cases for pychrysalide.glibext.ComparableObject."""
def testComparableObjectCreation(self):
"""Create objects with ComparableObject interface."""
with self.assertRaisesRegex(NotImplementedError, 'ComparableObject can not be constructed'):
co = ComparableObject()
class NewComparableObjectImplem(gi._gi.GObject, ComparableObject):
pass
nco = NewComparableObjectImplem()
self.assertIsNotNone(nco)
class NewComparableObjectImplem2(GObject.Object, ComparableObject):
pass
nco2 = NewComparableObjectImplem()
self.assertIsNotNone(nco2)
def testComparableObjectMethods(self):
"""Test the ComparableObject methods."""
class BasicComparableObjectImplem(GObject.Object, ComparableObject):
def __init__(self, val):
super().__init__()
self._val = val
def _compare(self, other):
if self._val < other._val:
status = -1
elif self._val > other._val:
status = 1
else:
status = 0
return status
a = BasicComparableObjectImplem(123)
b = BasicComparableObjectImplem(456)
self.assertTrue(a <= b)
# Sans l'action de inherit_interface_slots(), c'est pygobject_richcompare() qui est appelée,
# laquelle compare simplement les adresses des pointeurs
c = BasicComparableObjectImplem(789)
d = BasicComparableObjectImplem(234)
self.assertTrue(c > d)
def testComparableObjectExceptions(self):
"""Raise exceptions from the ComparableObject interface as expected."""
class OtherComparableObject(GObject.Object, ComparableObject):
pass
other = OtherComparableObject()
class BadComparableObjectImplem(GObject.Object, ComparableObject):
pass
obj = BadComparableObjectImplem()
with self.assertRaisesRegex(NotImplementedError, "method implementation is missing for '_compare'"):
s = obj < other
class BadComparableObjectImplem2(GObject.Object, ComparableObject):
def _compare(self, other):
return 'AAA'
obj2 = BadComparableObjectImplem2()
with self.assertRaisesRegex(TypeError, 'comparison status has to be a signed integer'):
s = obj2 < other
class BadComparableObjectImplem3a(GObject.Object, ComparableObject):
def _compare(self, other):
return 123
class BadComparableObjectImplem3b(BadComparableObjectImplem3a, ComparableObject):
def _compare(self, other):
return self.parent_compare()
obj3 = BadComparableObjectImplem3b()
with self.assertRaisesRegex(RuntimeError, 'object parent is not a native type'):
s = obj3 < other
class BadComparableObjectImplem4(GObject.Object, ComparableObject):
def _compare(self, other):
raise Exception('error')
obj4 = BadComparableObjectImplem4()
with self.assertRaisesRegex(Exception, 'error'):
s = obj4 < other
|