/
tests.py
201 lines (150 loc) · 5.82 KB
/
tests.py
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from twisted.trial import unittest
import zope.interface
import bravo_plugin
class EdgeHolder(object):
def __init__(self, name, before, after):
self.name = name
self.before = before
self.after = after
class TestDependencyHelpers(unittest.TestCase):
def test_add_plugin_edges_after(self):
d = {
"first": EdgeHolder("first", tuple(), tuple()),
"second": EdgeHolder("second", tuple(), ("first",)),
}
bravo_plugin.add_plugin_edges(d)
self.assertEqual(d["first"].before, set(["second"]))
def test_add_plugin_edges_before(self):
d = {
"first": EdgeHolder("first", ("second",), tuple()),
"second": EdgeHolder("second", tuple(), tuple()),
}
bravo_plugin.add_plugin_edges(d)
self.assertEqual(d["second"].after, set(["first"]))
def test_add_plugin_edges_bogus(self):
d = {
"first": EdgeHolder("first", ("second",), tuple()),
}
bravo_plugin.add_plugin_edges(d)
self.assertEqual(d["first"].before, set())
def test_sort_plugins(self):
l = [
EdgeHolder("first", ("second",), tuple()),
EdgeHolder("second", tuple(), ("first",)),
]
sorted = bravo_plugin.sort_plugins(l)
l.reverse()
self.assertEqual(l, sorted)
def test_sort_plugins_no_dependency(self):
"""
Test whether a plugin with no dependencies is excluded.
"""
l = [
EdgeHolder("first", ("second",), tuple()),
EdgeHolder("second", tuple(), ("first",)),
EdgeHolder("third", tuple(), tuple()),
]
sorted = bravo_plugin.sort_plugins(l)
self.assertEqual(set(l), set(sorted))
def test_sort_plugins_missing_dependency(self):
"""
Test whether a missing "after" dependency causes a plugin to be
excluded.
"""
l = [
EdgeHolder("first", ("second",), tuple()),
EdgeHolder("second", tuple(), ("first",)),
EdgeHolder("third", tuple(), ("missing",)),
]
sorted = bravo_plugin.sort_plugins(l)
self.assertEqual(set(l), set(sorted))
class TestOptions(unittest.TestCase):
def test_identity(self):
names = ["first", "second"]
d = {"first": None, "second": None}
self.assertEqual(sorted(["first", "second"]),
sorted(bravo_plugin.expand_names(d, names)))
def test_doubled(self):
names = ["first", "first", "second"]
d = {"first": None, "second": None}
self.assertEqual(sorted(["first", "second"]),
sorted(bravo_plugin.expand_names(d, names)))
def test_wildcard(self):
names = ["*"]
d = {"first": None, "second": None}
self.assertEqual(set(["first", "second"]),
set(bravo_plugin.expand_names(d, names)))
def test_wildcard_removed(self):
names = ["*", "-first"]
d = {"first": None, "second": None}
self.assertEqual(["second"], bravo_plugin.expand_names(d, names))
def test_wildcard_after_removed(self):
names = ["-first", "*"]
d = {"first": None, "second": None}
self.assertEqual(["second"], bravo_plugin.expand_names(d, names))
def test_removed_conflict(self):
"""
If a name is both included and excluded, the exclusion takes
precedence.
"""
names = ["first", "-first", "second"]
d = {"first": None, "second": None}
self.assertEqual(["second"], bravo_plugin.expand_names(d, names))
def test_removed_conflict_after(self):
names = ["-first", "first", "second"]
d = {"first": None, "second": None}
self.assertEqual(["second"], bravo_plugin.expand_names(d, names))
class ITestInterface(zope.interface.Interface):
name = zope.interface.Attribute("")
attr = zope.interface.Attribute("")
def meth(arg):
pass
class TestVerifyPlugin(unittest.TestCase):
def test_no_name(self):
class NoName(object):
zope.interface.implements(ITestInterface)
self.assertRaises(bravo_plugin.PluginException,
bravo_plugin.verify_plugin,
ITestInterface,
NoName())
def test_no_attribute(self):
class NoAttr(object):
zope.interface.implements(ITestInterface)
name = "test"
self.assertRaises(bravo_plugin.PluginException,
bravo_plugin.verify_plugin,
ITestInterface,
NoAttr())
def test_no_method(self):
class NoMeth(object):
zope.interface.implements(ITestInterface)
name = "test"
attr = "unit"
self.assertRaises(bravo_plugin.PluginException,
bravo_plugin.verify_plugin,
ITestInterface,
NoMeth())
def test_broken_method(self):
class BrokenMeth(object):
zope.interface.implements(ITestInterface)
name = "test"
attr = "unit"
def meth(self, arg, extra):
pass
self.assertRaises(bravo_plugin.PluginException,
bravo_plugin.verify_plugin,
ITestInterface,
BrokenMeth())
# BMI (and only BMI!) writes an error to the log, so let's flush it
# out and pass the test.
self.flushLoggedErrors()
def test_success(self):
class Valid(object):
zope.interface.implements(ITestInterface)
name = "test"
attr = "unit"
def meth(self, arg):
pass
valid = Valid()
self.assertEqual(bravo_plugin.verify_plugin(ITestInterface, valid),
valid)