diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2021-05-13 08:09:00 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2021-05-13 08:09:00 (GMT) |
commit | a20cf5f374e64c6db50bf33545dc67804e90eabc (patch) | |
tree | 96ef4806bc66064b7fb9a82f5031faa3b423e8e3 /tests/glibext | |
parent | 0d52ab7d31cde2861973c2bf73156e3ba3edc4ed (diff) |
Define the first steps for singleton support.
Diffstat (limited to 'tests/glibext')
-rw-r--r-- | tests/glibext/singleton.py | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/tests/glibext/singleton.py b/tests/glibext/singleton.py new file mode 100644 index 0000000..b0608f0 --- /dev/null +++ b/tests/glibext/singleton.py @@ -0,0 +1,101 @@ + +from chrysacase import ChrysalideTestCase +from gi.repository import GObject +from pychrysalide.glibext import SingletonCandidate, SingletonFactory + + +class TestSingleton(ChrysalideTestCase): + """Test cases for pychrysalide.glibext.SingletonFactory.""" + + + def testSingletonCreation(self): + """Create singleton objects.""" + + with self.assertRaisesRegex(NotImplementedError, 'SingletonCandidate can not be constructed'): + + sc = SingletonCandidate() + + class NewSingletonImplem(GObject.Object, SingletonCandidate): + pass + + nsi = NewSingletonImplem() + + self.assertIsNotNone(nsi) + + + def testFactoryCreation(self): + """Create singleton factories.""" + + sf = SingletonFactory() + + self.assertIsNotNone(sf) + + class MyFactory(SingletonFactory): + pass + + msf = MyFactory() + + self.assertIsNotNone(msf) + + + def testSingletonMethods(self): + """Test the singleton methods.""" + + class IntegerCacheImplem(GObject.Object, SingletonCandidate): + + def __init__(self, val): + super().__init__() + self._val = val + + def __eq__(self, other): + return self._val == other._val + + def __hash__(self): + return hash('common-key') + + val_0 = IntegerCacheImplem(0) + val_0_bis = IntegerCacheImplem(0) + val_1 = IntegerCacheImplem(1) + + self.assertEqual(hash(val_0), hash(val_0_bis)) + self.assertEqual(hash(val_0), hash(val_1)) + + self.assertEqual(val_0.hash(), val_0_bis.hash()) + self.assertEqual(val_0.hash(), val_1.hash()) + + self.assertTrue(val_0 == val_0_bis) + self.assertFalse(val_0 == val_1) + + + def testSingletonFootprint(self): + """Check for singleton memory footprint.""" + + class IntegerCacheImplem(GObject.Object, SingletonCandidate): + + def __init__(self, val): + super().__init__() + self._val = val + + def __eq__(self, other): + return self._val == other._val + + def __hash__(self): + return hash('common-key') + + val_0 = IntegerCacheImplem(0) + val_0_bis = IntegerCacheImplem(0) + val_1 = IntegerCacheImplem(1) + + sf = SingletonFactory() + + obj = sf.get_instance(val_0) + + self.assertTrue(obj is val_0) + + obj = sf.get_instance(val_0_bis) + + self.assertTrue(obj is val_0) + + obj = sf.get_instance(val_1) + + self.assertTrue(obj is val_1) |