summaryrefslogtreecommitdiff
path: root/tests/glibext/comparable.py
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2025-01-26 14:25:51 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2025-01-26 14:25:51 (GMT)
commit56c148de74ed8c78ce54ed24daa83ec2f641e054 (patch)
treed4e43da9d6c729146c77fb30de8fa3767b257afb /tests/glibext/comparable.py
parentb1227a2779c9a72cab1295a1419a9c990df6488e (diff)
Define new interfaces for arch operands.
Diffstat (limited to 'tests/glibext/comparable.py')
-rw-r--r--tests/glibext/comparable.py132
1 files changed, 132 insertions, 0 deletions
diff --git a/tests/glibext/comparable.py b/tests/glibext/comparable.py
new file mode 100644
index 0000000..48291ca
--- /dev/null
+++ b/tests/glibext/comparable.py
@@ -0,0 +1,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