OLD | NEW |
| (Empty) |
1 | |
2 # Copyright (c) 2001-2004 Twisted Matrix Laboratories. | |
3 # See LICENSE for details. | |
4 | |
5 | |
6 """ | |
7 Test cases for explorer | |
8 """ | |
9 | |
10 from twisted.trial import unittest | |
11 | |
12 from twisted.manhole import explorer | |
13 | |
14 import types, string | |
15 | |
16 """ | |
17 # Tests: | |
18 | |
19 Get an ObjectLink. Browse ObjectLink.identifier. Is it the same? | |
20 | |
21 Watch Object. Make sure an ObjectLink is received when: | |
22 Call a method. | |
23 Set an attribute. | |
24 | |
25 Have an Object with a setattr class. Watch it. | |
26 Do both the navite setattr and the watcher get called? | |
27 | |
28 Sequences with circular references. Does it blow up? | |
29 """ | |
30 | |
31 class SomeDohickey: | |
32 def __init__(self, *a): | |
33 self.__dict__['args'] = a | |
34 | |
35 def bip(self): | |
36 return self.args | |
37 | |
38 | |
39 class TestBrowser(unittest.TestCase): | |
40 def setUp(self): | |
41 self.pool = explorer.explorerPool | |
42 self.pool.clear() | |
43 self.testThing = ["How many stairs must a man climb down?", | |
44 SomeDohickey(42)] | |
45 | |
46 def test_chain(self): | |
47 "Following a chain of Explorers." | |
48 xplorer = self.pool.getExplorer(self.testThing, 'testThing') | |
49 self.failUnlessEqual(xplorer.id, id(self.testThing)) | |
50 self.failUnlessEqual(xplorer.identifier, 'testThing') | |
51 | |
52 dxplorer = xplorer.get_elements()[1] | |
53 self.failUnlessEqual(dxplorer.id, id(self.testThing[1])) | |
54 | |
55 class Watcher: | |
56 zero = 0 | |
57 def __init__(self): | |
58 self.links = [] | |
59 | |
60 def receiveBrowserObject(self, olink): | |
61 self.links.append(olink) | |
62 | |
63 def setZero(self): | |
64 self.zero = len(self.links) | |
65 | |
66 def len(self): | |
67 return len(self.links) - self.zero | |
68 | |
69 | |
70 class SetattrDohickey: | |
71 def __setattr__(self, k, v): | |
72 v = list(str(v)) | |
73 v.reverse() | |
74 self.__dict__[k] = string.join(v, '') | |
75 | |
76 class MiddleMan(SomeDohickey, SetattrDohickey): | |
77 pass | |
78 | |
79 # class TestWatch(unittest.TestCase): | |
80 class FIXME_Watch: | |
81 def setUp(self): | |
82 self.globalNS = globals().copy() | |
83 self.localNS = {} | |
84 self.browser = explorer.ObjectBrowser(self.globalNS, self.localNS) | |
85 self.watcher = Watcher() | |
86 | |
87 def test_setAttrPlain(self): | |
88 "Triggering a watcher response by setting an attribute." | |
89 | |
90 testThing = SomeDohickey('pencil') | |
91 self.browser.watchObject(testThing, 'testThing', | |
92 self.watcher.receiveBrowserObject) | |
93 self.watcher.setZero() | |
94 | |
95 testThing.someAttr = 'someValue' | |
96 | |
97 self.failUnlessEqual(testThing.someAttr, 'someValue') | |
98 self.failUnless(self.watcher.len()) | |
99 olink = self.watcher.links[-1] | |
100 self.failUnlessEqual(olink.id, id(testThing)) | |
101 | |
102 def test_setAttrChain(self): | |
103 "Setting an attribute on a watched object that has __setattr__" | |
104 testThing = MiddleMan('pencil') | |
105 | |
106 self.browser.watchObject(testThing, 'testThing', | |
107 self.watcher.receiveBrowserObject) | |
108 self.watcher.setZero() | |
109 | |
110 testThing.someAttr = 'ZORT' | |
111 | |
112 self.failUnlessEqual(testThing.someAttr, 'TROZ') | |
113 self.failUnless(self.watcher.len()) | |
114 olink = self.watcher.links[-1] | |
115 self.failUnlessEqual(olink.id, id(testThing)) | |
116 | |
117 | |
118 def test_method(self): | |
119 "Triggering a watcher response by invoking a method." | |
120 | |
121 for testThing in (SomeDohickey('pencil'), MiddleMan('pencil')): | |
122 self.browser.watchObject(testThing, 'testThing', | |
123 self.watcher.receiveBrowserObject) | |
124 self.watcher.setZero() | |
125 | |
126 rval = testThing.bip() | |
127 self.failUnlessEqual(rval, ('pencil',)) | |
128 | |
129 self.failUnless(self.watcher.len()) | |
130 olink = self.watcher.links[-1] | |
131 self.failUnlessEqual(olink.id, id(testThing)) | |
132 | |
133 | |
134 def function_noArgs(): | |
135 "A function which accepts no arguments at all." | |
136 return | |
137 | |
138 def function_simple(a, b, c): | |
139 "A function which accepts several arguments." | |
140 return a, b, c | |
141 | |
142 def function_variable(*a, **kw): | |
143 "A function which accepts a variable number of args and keywords." | |
144 return a, kw | |
145 | |
146 def function_crazy((alpha, beta), c, d=range(4), **kw): | |
147 "A function with a mad crazy signature." | |
148 return alpha, beta, c, d, kw | |
149 | |
150 class TestBrowseFunction(unittest.TestCase): | |
151 | |
152 def setUp(self): | |
153 self.pool = explorer.explorerPool | |
154 self.pool.clear() | |
155 | |
156 def test_sanity(self): | |
157 """Basic checks for browse_function. | |
158 | |
159 Was the proper type returned? Does it have the right name and ID? | |
160 """ | |
161 for f_name in ('function_noArgs', 'function_simple', | |
162 'function_variable', 'function_crazy'): | |
163 f = eval(f_name) | |
164 | |
165 xplorer = self.pool.getExplorer(f, f_name) | |
166 | |
167 self.failUnlessEqual(xplorer.id, id(f)) | |
168 | |
169 self.failUnless(isinstance(xplorer, explorer.ExplorerFunction)) | |
170 | |
171 self.failUnlessEqual(xplorer.name, f_name) | |
172 | |
173 def test_signature_noArgs(self): | |
174 """Testing zero-argument function signature. | |
175 """ | |
176 | |
177 xplorer = self.pool.getExplorer(function_noArgs, 'function_noArgs') | |
178 | |
179 self.failUnlessEqual(len(xplorer.signature), 0) | |
180 | |
181 def test_signature_simple(self): | |
182 """Testing simple function signature. | |
183 """ | |
184 | |
185 xplorer = self.pool.getExplorer(function_simple, 'function_simple') | |
186 | |
187 expected_signature = ('a','b','c') | |
188 | |
189 self.failUnlessEqual(xplorer.signature.name, expected_signature) | |
190 | |
191 def test_signature_variable(self): | |
192 """Testing variable-argument function signature. | |
193 """ | |
194 | |
195 xplorer = self.pool.getExplorer(function_variable, | |
196 'function_variable') | |
197 | |
198 expected_names = ('a','kw') | |
199 signature = xplorer.signature | |
200 | |
201 self.failUnlessEqual(signature.name, expected_names) | |
202 self.failUnless(signature.is_varlist(0)) | |
203 self.failUnless(signature.is_keyword(1)) | |
204 | |
205 def test_signature_crazy(self): | |
206 """Testing function with crazy signature. | |
207 """ | |
208 xplorer = self.pool.getExplorer(function_crazy, 'function_crazy') | |
209 | |
210 signature = xplorer.signature | |
211 | |
212 expected_signature = [{'name': 'c'}, | |
213 {'name': 'd', | |
214 'default': range(4)}, | |
215 {'name': 'kw', | |
216 'keywords': 1}] | |
217 | |
218 # The name of the first argument seems to be indecipherable, | |
219 # but make sure it has one (and no default). | |
220 self.failUnless(signature.get_name(0)) | |
221 self.failUnless(not signature.get_default(0)[0]) | |
222 | |
223 self.failUnlessEqual(signature.get_name(1), 'c') | |
224 | |
225 # Get a list of values from a list of ExplorerImmutables. | |
226 arg_2_default = map(lambda l: l.value, | |
227 signature.get_default(2)[1].get_elements()) | |
228 | |
229 self.failUnlessEqual(signature.get_name(2), 'd') | |
230 self.failUnlessEqual(arg_2_default, range(4)) | |
231 | |
232 self.failUnlessEqual(signature.get_name(3), 'kw') | |
233 self.failUnless(signature.is_keyword(3)) | |
234 | |
235 if __name__ == '__main__': | |
236 unittest.main() | |
OLD | NEW |