OLD | NEW |
| (Empty) |
1 | |
2 # Copyright (c) 2001-2004 Twisted Matrix Laboratories. | |
3 # See LICENSE for details. | |
4 | |
5 | |
6 """ | |
7 Test cases for twisted.hook module. | |
8 """ | |
9 | |
10 from twisted.python import hook | |
11 from twisted.trial import unittest | |
12 | |
13 class BaseClass: | |
14 """ | |
15 dummy class to help in testing. | |
16 """ | |
17 def __init__(self): | |
18 """ | |
19 dummy initializer | |
20 """ | |
21 self.calledBasePre = 0 | |
22 self.calledBasePost = 0 | |
23 self.calledBase = 0 | |
24 | |
25 def func(self, a, b): | |
26 """ | |
27 dummy method | |
28 """ | |
29 assert a == 1 | |
30 assert b == 2 | |
31 self.calledBase = self.calledBase + 1 | |
32 | |
33 | |
34 class SubClass(BaseClass): | |
35 """ | |
36 another dummy class | |
37 """ | |
38 def __init__(self): | |
39 """ | |
40 another dummy initializer | |
41 """ | |
42 BaseClass.__init__(self) | |
43 self.calledSubPre = 0 | |
44 self.calledSubPost = 0 | |
45 self.calledSub = 0 | |
46 | |
47 def func(self, a, b): | |
48 """ | |
49 another dummy function | |
50 """ | |
51 assert a == 1 | |
52 assert b == 2 | |
53 BaseClass.func(self, a, b) | |
54 self.calledSub = self.calledSub + 1 | |
55 | |
56 _clean_BaseClass = BaseClass.__dict__.copy() | |
57 _clean_SubClass = SubClass.__dict__.copy() | |
58 | |
59 def basePre(base, a, b): | |
60 """ | |
61 a pre-hook for the base class | |
62 """ | |
63 base.calledBasePre = base.calledBasePre + 1 | |
64 | |
65 def basePost(base, a, b): | |
66 """ | |
67 a post-hook for the base class | |
68 """ | |
69 base.calledBasePost = base.calledBasePost + 1 | |
70 | |
71 def subPre(sub, a, b): | |
72 """ | |
73 a pre-hook for the subclass | |
74 """ | |
75 sub.calledSubPre = sub.calledSubPre + 1 | |
76 | |
77 def subPost(sub, a, b): | |
78 """ | |
79 a post-hook for the subclass | |
80 """ | |
81 sub.calledSubPost = sub.calledSubPost + 1 | |
82 | |
83 class HookTestCase(unittest.TestCase): | |
84 """ | |
85 test case to make sure hooks are called | |
86 """ | |
87 def setUp(self): | |
88 """Make sure we have clean versions of our classes.""" | |
89 BaseClass.__dict__.clear() | |
90 BaseClass.__dict__.update(_clean_BaseClass) | |
91 SubClass.__dict__.clear() | |
92 SubClass.__dict__.update(_clean_SubClass) | |
93 | |
94 def testBaseHook(self): | |
95 """make sure that the base class's hook is called reliably | |
96 """ | |
97 base = BaseClass() | |
98 self.assertEquals(base.calledBase, 0) | |
99 self.assertEquals(base.calledBasePre, 0) | |
100 base.func(1,2) | |
101 self.assertEquals(base.calledBase, 1) | |
102 self.assertEquals(base.calledBasePre, 0) | |
103 hook.addPre(BaseClass, "func", basePre) | |
104 base.func(1, b=2) | |
105 self.assertEquals(base.calledBase, 2) | |
106 self.assertEquals(base.calledBasePre, 1) | |
107 hook.addPost(BaseClass, "func", basePost) | |
108 base.func(1, b=2) | |
109 self.assertEquals(base.calledBasePost, 1) | |
110 self.assertEquals(base.calledBase, 3) | |
111 self.assertEquals(base.calledBasePre, 2) | |
112 hook.removePre(BaseClass, "func", basePre) | |
113 hook.removePost(BaseClass, "func", basePost) | |
114 base.func(1, b=2) | |
115 self.assertEquals(base.calledBasePost, 1) | |
116 self.assertEquals(base.calledBase, 4) | |
117 self.assertEquals(base.calledBasePre, 2) | |
118 | |
119 def testSubHook(self): | |
120 """test interactions between base-class hooks and subclass hooks | |
121 """ | |
122 sub = SubClass() | |
123 self.assertEquals(sub.calledSub, 0) | |
124 self.assertEquals(sub.calledBase, 0) | |
125 sub.func(1, b=2) | |
126 self.assertEquals(sub.calledSub, 1) | |
127 self.assertEquals(sub.calledBase, 1) | |
128 hook.addPre(SubClass, 'func', subPre) | |
129 self.assertEquals(sub.calledSub, 1) | |
130 self.assertEquals(sub.calledBase, 1) | |
131 self.assertEquals(sub.calledSubPre, 0) | |
132 self.assertEquals(sub.calledBasePre, 0) | |
133 sub.func(1, b=2) | |
134 self.assertEquals(sub.calledSub, 2) | |
135 self.assertEquals(sub.calledBase, 2) | |
136 self.assertEquals(sub.calledSubPre, 1) | |
137 self.assertEquals(sub.calledBasePre, 0) | |
138 # let the pain begin | |
139 hook.addPre(BaseClass, 'func', basePre) | |
140 BaseClass.func(sub, 1, b=2) | |
141 # sub.func(1, b=2) | |
142 self.assertEquals(sub.calledBase, 3) | |
143 self.assertEquals(sub.calledBasePre, 1, str(sub.calledBasePre)) | |
144 sub.func(1, b=2) | |
145 self.assertEquals(sub.calledBasePre, 2) | |
146 self.assertEquals(sub.calledBase, 4) | |
147 self.assertEquals(sub.calledSubPre, 2) | |
148 self.assertEquals(sub.calledSub, 3) | |
149 | |
150 testCases = [HookTestCase] | |
OLD | NEW |