summaryrefslogtreecommitdiff
path: root/tests/test_plugins.py
blob: a8cc27447c7c527aadc2ad46db93ccaec640e3b5 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import unittest
import logging

from slixmpp.plugins.base import PluginManager, BasePlugin, register_plugin


class A(BasePlugin):
    name = 'a'


class B(BasePlugin):
    name = 'b'


class C(BasePlugin):
    name = 'c'
    dependencies = {'b', 'd'}


class D(BasePlugin):
    name = 'd'
    dependencies = {'c'}


class E(BasePlugin):
    name = 'e'
    dependencies = {'a', 'd'}

class F(BasePlugin):
    name = 'f'
    dependencies = {'a', 'b'}


register_plugin(A)
register_plugin(B)
register_plugin(C)
register_plugin(D)
register_plugin(E)
register_plugin(F)


class TestPlugins(unittest.TestCase):


    def test_enable(self):
        """Enable a single plugin."""
        p = PluginManager(None)

        events = []

        def init(self):
            events.append('init')

        A.plugin_init = init

        p.enable('a')

        self.assertEqual(len(p), 1, "Wrong number of enabled plugins.")
        self.assertEqual(events, ['init'], "Plugin init method not called.")

    def test_disable(self):
        """Disable a single plugin."""
        p = PluginManager(None)

        events = []

        def init(self):
            events.append('init')

        def end(self):
            events.append('end')

        A.plugin_init = init
        A.plugin_end = end

        p.enable('a')
        p.disable('a')

        self.assertEqual(len(p), 0, "Wrong number of enabled plugins.")
        self.assertEqual(events, ['init', 'end'],
                "Plugin lifecycle methods not called.")

    def test_enable_dependencies(self):
        """Enable a plugin with acyclic dependencies."""
        p = PluginManager(None)

        events = []

        A.plugin_init = lambda s: events.append('init_a')
        B.plugin_init = lambda s: events.append('init_b')

        p.enable('f')

        self.assertEqual(len(p), 3, "Wrong number of enabled plugins.")
        self.assertTrue('init_a' in events, "Dependency A not enabled.")
        self.assertTrue('init_b' in events, "Dependency B not enabled.")

    def test_enable_cyclic_dependencies(self):
        """Enable a plugin with cyclic dependencies."""

        p = PluginManager(None)

        events = []

        B.plugin_init = lambda s: events.append('init_b')
        C.plugin_init = lambda s: events.append('init_c')
        D.plugin_init = lambda s: events.append('init_d')

        p.enable('c')

        self.assertEqual(len(p), 3, "Wrong number of enabled plugins.")
        self.assertTrue('init_b' in events, "Dependency B not enabled.")
        self.assertTrue('init_c' in events, "Dependency C not enabled.")
        self.assertTrue('init_d' in events, "Dependency D not enabled.")

    def test_disable_dependendents(self):
        """Disable a plugin with dependents."""

        p = PluginManager(None)

        events = []

        A.plugin_end = lambda s: events.append('end_a')
        B.plugin_end = lambda s: events.append('end_b')
        F.plugin_end = lambda s: events.append('end_f')

        p.enable('f')
        p.disable('a')

        self.assertEqual(len(p), 1, "Wrong number of enabled plugins.")
        self.assertTrue('end_f' in events, "Dependent F not disabled.")
        self.assertTrue('end_a' in events, "Plugin A not disabled.")

    def test_disable_cyclic_dependents(self):
        """Disable a plugin with cyclic dependents."""

        p = PluginManager(None)

        events = []

        B.plugin_end = lambda s: events.append('end_b')
        C.plugin_end = lambda s: events.append('end_c')
        D.plugin_end = lambda s: events.append('end_d')

        p.enable('c')
        p.disable('b')

        self.assertEqual(len(p), 0, "Wrong number of enabled plugins.")
        self.assertTrue('end_b' in events, "Plugin B not disabled.")
        self.assertTrue('end_c' in events, "Dependent C not disabled.")
        self.assertTrue('end_d' in events, "Dependent D not disabled.")



suite = unittest.TestLoader().loadTestsFromTestCase(TestPlugins)

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG,
                        format='%(levelname)-8s %(message)s')

    tests = unittest.TestSuite([suite])
    unittest.TextTestRunner(verbosity=2).run(tests)