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