summaryrefslogtreecommitdiff
path: root/tests/glibext/singleton.py
blob: 4588ae589d48719101b020ea39cf7e1ac05eece8 (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

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 _list_inner_instances(self):
                return ()

            def __hash__(self):
                return hash('common-key')

            def __eq__(self, other):
                return self._val == other._val

        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."""

        sf = SingletonFactory()


        class IntegerCacheImplem(GObject.Object, SingletonCandidate):

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

            def _list_inner_instances(self):
                return ()

            def __hash__(self):
                return hash('common-key')

            def __eq__(self, other):
                return self._val == other._val

            def _set_read_only(self):
                pass

        val_0 = IntegerCacheImplem(0)
        val_0_bis = IntegerCacheImplem(0)
        val_1 = IntegerCacheImplem(1)

        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)

        self.assertEqual(len(obj.inner_instances), 0)


        class MasterCacheImplem(GObject.Object, SingletonCandidate):

            def __init__(self, children):
                super().__init__()
                self._children = children

            def _list_inner_instances(self):
                return self._children

            def _update_inner_instances(self, instances):
                self._children = instances

            def __hash__(self):
                return hash('master-key')

            def __eq__(self, other):
                return False

            def _set_read_only(self):
                pass

        master = MasterCacheImplem(( val_0_bis, val_1 ))

        obj = sf.get_instance(master)

        self.assertTrue(obj.inner_instances[0] is val_0)

        self.assertTrue(obj.inner_instances[1] is val_1)