-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
299 lines (258 loc) · 8.59 KB
/
test.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
from unittest import TestCase
from aspect import Advisable, Event, Pointcut, OrPointcut, AndPointcut, \
NotPointcut, Call, CFlow, CFlowBelow, around, before, after
class AdvisableTests(TestCase):
def testUnadvisedCall(self):
class Foo(object):
__metaclass__ = Advisable
def a(self, i):
return i + 1
def b(self, *args):
return args
def c(self, i, **kw):
return kw[i]
f = Foo()
self.assertEqual(f.a(4), 5)
self.assertEqual(f.b('a', 'b'), ('a', 'b'))
self.assertEqual(f.c('c', c=4, d=5), 4)
## from aspect import Pointcut, OrPointcut, AndPointcut, NotPointcut, \
## Call, CFlow, CFlowBelow, before, after
class EventTests(TestCase):
def testMethods(self):
class Foo:
def bar(self, i):
pass
args, kw = (1,), {}
f = Foo()
e = Event(Event.around, f, Foo.bar, args, kw)
self.assertEqual(e.getMethod(), Foo.bar)
self.assertEqual(e.getObjectClass(), Foo)
self.assertEqual(e.getArgs(), args)
self.assertEqual(e.getObject(), f)
## ex = TypeError('not enough poo')
## e = Event(f, Foo.bar, args, kw,
## exception=ex)
## self.assertEqual(e.getException(), ex)
def testGetArg(self):
class Foo:
__metaclass__ = Advisable
def bar(self, i, j):
pass
i, j = 11, 72
e = Event(Event.around, Foo(), Foo.bar, (i, j), {})
self.assertEqual(e.getArg('i'), i)
self.assertEqual(e.getArg('j'), j)
class PointcutTests(TestCase):
class Advice:
event = None
def __call__(self, event):
self.event = event
class P(Pointcut):
def __init__(self, advisep=False):
super(PointcutTests.P, self).__init__()
self.advisep = advisep
def isAdvisable(self, event):
return self.advisep
def notifyBefore(self, event):
self.before = 1
def notifyAfter(self, event):
self.after = 1
def testComposites(self):
e = 'hey'
pc = self.P() | self.P()
self.assertEqual(pc.__class__, OrPointcut)
self.assertEqual(pc.isAdvisable(e), False)
pc = self.P() | self.P(True)
self.assertEqual(pc.isAdvisable(e), True)
pc = self.P() & self.P()
self.assertEqual(pc.__class__, AndPointcut)
self.assertEqual(pc.isAdvisable(e), False)
pc = self.P(True) & self.P()
self.assertEqual(pc.isAdvisable(e), False)
pc = self.P(True) & self.P(True)
self.assertEqual(pc.isAdvisable(e), True)
def testNegation(self):
pc = ~self.P(True)
e = 'hey'
self.assertEqual(pc.__class__, NotPointcut)
self.assertEqual(pc.isAdvisable(e), False)
pc = ~self.P()
self.assertEqual(pc.isAdvisable(e), True)
class CallCFlowTests(TestCase):
class Boo:
__metaclass__ = Advisable
def bar(self):
pass
class Foo:
__metaclass__ = Advisable
def bar(self):
self.baz()
def baz(self):
pass
def testCall(self):
Foo = self.Foo
Boo = self.Boo
pc = Call(Foo.bar)
pc.setAdvice(lambda x: x)
e = Event(Event.around, Foo(), Foo.baz, (), {})
self.assertEqual(pc.isAdvisable(e), False)
e = Event(Event.around, Foo(), Foo.bar, (), {})
self.assertEqual(pc.isAdvisable(e), True)
e = Event(Event.around, Boo(), Boo.bar, (), {})
self.assertEqual(pc.isAdvisable(e), False)
def testCFlow(self):
Foo, Boo = self.Foo, self.Boo
p = CFlow(Call(Foo.bar))
p.setAdvice(lambda x: x)
e_uninteresting = Event(Event.around, Boo(), Boo.bar, (), {})
e = Event(Event.around, Foo(), Foo.bar, (), {})
self.assertEqual(p.isAdvisable(e), False)
p.notifyBefore(e_uninteresting)
self.assertEqual(p.isAdvisable(e), False)
p.notifyBefore(e)
self.assertEqual(p.isAdvisable(e), True)
p.notifyAfter(e)
self.assertEqual(p.isAdvisable(e), False)
class FunctionalTests(TestCase):
def setUp(self):
class Foo:
__metaclass__ = Advisable
def recursive(self, i):
if i <= 0:
return 1
else:
return i*self.recursive(i - 1)
def bar(self):
self.baz()
def baz(self):
self.zab()
def zab(self):
pass
class Advice:
def __init__(self):
self.events = []
def notify(self, event):
self.events.append(event)
r = apply(event.callNextFunction, event.getArgs())
return r
def append1(self, event):
self.events.append(1)
def append2(self, event):
self.events.append(2)
self.Foo = Foo
self.Advice = Advice
def tearDown(self):
del self.Foo
del self.Advice
def testRecursive(self):
Foo = self.Foo
f = Foo()
pc = Call(Foo.recursive) & ~CFlowBelow(Call(Foo.recursive))
advice = self.Advice()
around(pc, advice.notify)
self.assertEqual(f.recursive(5), 120)
self.assertEqual(len(advice.events), 1)
def testOrder(self):
Foo = self.Foo
a = self.Advice()
pc = Call(Foo.baz)
before(pc, a.append1)
before(pc.copy(), a.append2)
f = Foo()
f.baz()
self.assertEqual(a.events, [1, 2])
def testCFlow(self):
Foo = self.Foo
advice = self.Advice()
f = Foo()
bazInBoo = Call(Foo.baz) & CFlow(Call(Foo.bar))
around(bazInBoo, advice.notify)
f.baz()
self.assertEqual(len(advice.events), 0)
f.bar()
self.assertEqual(len(advice.events), 1)
def testCflowNesting(self):
Foo = self.Foo
advice = self.Advice()
f = Foo()
pc = CFlow(Call(Foo.bar))
around(pc, advice.notify)
f.bar()
self.assertEqual(len(advice.events), 3)
def testCflow(self):
class Foo:
__metaclass__ = Advisable
def foo(self):
self.bar()
def bar(self):
pass
a = self.Advice()
f = Foo()
pc = CFlow(Call(Foo.foo))
around(pc, a.notify)
f.foo()
self.assertEqual(len(a.events), 2)
def testTwoClasses(self):
class Foo:
__metaclass__ = Advisable
def foo(self, b):
b.bar()
class Bar:
__metaclass__ = Advisable
def bar(self):
pass
pc = Call(Bar.bar) & CFlow(Call(Foo.foo))
a = self.Advice()
around(pc, a.notify)
f = Foo()
b = Bar()
f.foo(b)
self.assertEqual(len(a.events), 1)
self.assertEqual(a.events[0].getMethod().__name__, 'bar')
def testOrPc(self):
class Foo:
__metaclass__ = Advisable
def foo(self):
self.bar()
def bar(self):
self.baz()
def baz(self):
pass
pc = CFlow(Call(Foo.bar) & CFlow(Call(Foo.foo)))
f = Foo()
a = self.Advice()
around(pc, a.notify)
f.bar()
f.baz()
self.assertEqual(len(a.events), 0)
f.foo()
self.assertEqual(len(a.events), 2)
self.assertEqual(a.events[0].getMethod().__name__, 'bar')
self.assertEqual(a.events[1].getMethod().__name__, 'baz')
def testSubclass(self):
class Foo:
__metaclass__ = Advisable
def foo(self):
pass
class Bar(Foo):
def foo(self):
pass
b = Bar()
a = self.Advice()
pc = Call(Foo.foo)
around(pc, a.notify)
b.foo()
self.assertEqual(len(a.events), 1)
def suite(allTests=(AdvisableTests, EventTests,
PointcutTests, CallCFlowTests,
FunctionalTests)):
from unittest import makeSuite, TestSuite
suites = []
for test in allTests:
if not isinstance(test,TestSuite):
test = makeSuite(test, 'test')
suites.append(test)
return TestSuite(suites)
if __name__ == '__main__':
from unittest import main
main(defaultTest='suite')