Show More
|
1 | NO CONTENT: modified file chmod 100755 => 100644 |
|
1 | NO CONTENT: modified file chmod 100755 => 100644 |
|
1 | NO CONTENT: modified file chmod 100755 => 100644 |
|
1 | NO CONTENT: modified file chmod 100755 => 100644 |
|
1 | NO CONTENT: modified file chmod 100755 => 100644 |
@@ -1,92 +1,94 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """This file contains unittests for the |
|
3 | 3 | IPython.frontend.cocoa.cocoa_frontend module. |
|
4 | 4 | """ |
|
5 | 5 | __docformat__ = "restructuredtext en" |
|
6 | 6 | |
|
7 | 7 | #--------------------------------------------------------------------------- |
|
8 | 8 | # Copyright (C) 2005 The IPython Development Team |
|
9 | 9 | # |
|
10 | 10 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | 11 | # the file COPYING, distributed as part of this software. |
|
12 | 12 | #--------------------------------------------------------------------------- |
|
13 | 13 | |
|
14 | 14 | #--------------------------------------------------------------------------- |
|
15 | 15 | # Imports |
|
16 | 16 | #--------------------------------------------------------------------------- |
|
17 | from IPython.kernel.core.interpreter import Interpreter | |
|
18 | import IPython.kernel.engineservice as es | |
|
19 | from IPython.testing.util import DeferredTestCase | |
|
20 | from twisted.internet.defer import succeed | |
|
21 | 17 | |
|
22 | 18 | try: |
|
19 | from IPython.kernel.core.interpreter import Interpreter | |
|
20 | import IPython.kernel.engineservice as es | |
|
21 | from IPython.testing.util import DeferredTestCase | |
|
22 | from twisted.internet.defer import succeed | |
|
23 | 23 | from IPython.frontend.cocoa.cocoa_frontend import IPythonCocoaController |
|
24 | 24 | from Foundation import NSMakeRect |
|
25 | 25 | from AppKit import NSTextView, NSScrollView |
|
26 | 26 | except ImportError: |
|
27 | pass | |
|
28 | else: | |
|
27 | 29 | class TestIPythonCocoaControler(DeferredTestCase): |
|
28 | 30 | """Tests for IPythonCocoaController""" |
|
29 | 31 | |
|
30 | 32 | def setUp(self): |
|
31 | 33 | self.controller = IPythonCocoaController.alloc().init() |
|
32 | 34 | self.engine = es.EngineService() |
|
33 | 35 | self.engine.startService() |
|
34 | 36 | |
|
35 | 37 | |
|
36 | 38 | def tearDown(self): |
|
37 | 39 | self.controller = None |
|
38 | 40 | self.engine.stopService() |
|
39 | 41 | |
|
40 | 42 | def testControllerExecutesCode(self): |
|
41 | 43 | code ="""5+5""" |
|
42 | 44 | expected = Interpreter().execute(code) |
|
43 | 45 | del expected['number'] |
|
44 | 46 | def removeNumberAndID(result): |
|
45 | 47 | del result['number'] |
|
46 | 48 | del result['id'] |
|
47 | 49 | return result |
|
48 | 50 | self.assertDeferredEquals( |
|
49 | 51 | self.controller.execute(code).addCallback(removeNumberAndID), |
|
50 | 52 | expected) |
|
51 | 53 | |
|
52 | 54 | def testControllerMirrorsUserNSWithValuesAsStrings(self): |
|
53 | 55 | code = """userns1=1;userns2=2""" |
|
54 | 56 | def testControllerUserNS(result): |
|
55 | 57 | self.assertEquals(self.controller.userNS['userns1'], 1) |
|
56 | 58 | self.assertEquals(self.controller.userNS['userns2'], 2) |
|
57 | 59 | |
|
58 | 60 | self.controller.execute(code).addCallback(testControllerUserNS) |
|
59 | 61 | |
|
60 | 62 | |
|
61 | 63 | def testControllerInstantiatesIEngine(self): |
|
62 | 64 | self.assert_(es.IEngineBase.providedBy(self.controller.engine)) |
|
63 | 65 | |
|
64 | 66 | def testControllerCompletesToken(self): |
|
65 | 67 | code = """longNameVariable=10""" |
|
66 | 68 | def testCompletes(result): |
|
67 | 69 | self.assert_("longNameVariable" in result) |
|
68 | 70 | |
|
69 | 71 | def testCompleteToken(result): |
|
70 | 72 | self.controller.complete("longNa").addCallback(testCompletes) |
|
71 | 73 | |
|
72 | 74 | self.controller.execute(code).addCallback(testCompletes) |
|
73 | 75 | |
|
74 | 76 | |
|
75 | 77 | def testCurrentIndent(self): |
|
76 | 78 | """test that current_indent_string returns current indent or None. |
|
77 | 79 | Uses _indent_for_block for direct unit testing. |
|
78 | 80 | """ |
|
79 | 81 | |
|
80 | 82 | self.controller.tabUsesSpaces = True |
|
81 | 83 | self.assert_(self.controller._indent_for_block("""a=3""") == None) |
|
82 | 84 | self.assert_(self.controller._indent_for_block("") == None) |
|
83 | 85 | block = """def test():\n a=3""" |
|
84 | 86 | self.assert_(self.controller._indent_for_block(block) == \ |
|
85 | 87 | ' ' * self.controller.tabSpaces) |
|
86 | 88 | |
|
87 | 89 | block = """if(True):\n%sif(False):\n%spass""" % \ |
|
88 | 90 | (' '*self.controller.tabSpaces, |
|
89 | 91 | 2*' '*self.controller.tabSpaces) |
|
90 | 92 | self.assert_(self.controller._indent_for_block(block) == \ |
|
91 | 93 | 2*(' '*self.controller.tabSpaces)) |
|
92 | 94 |
@@ -1,359 +1,358 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | # -*- test-case-name: IPython.frontend.tests.test_frontendbase -*- |
|
3 | 3 | """ |
|
4 | 4 | frontendbase provides an interface and base class for GUI frontends for |
|
5 | 5 | IPython.kernel/IPython.kernel.core. |
|
6 | 6 | |
|
7 | 7 | Frontend implementations will likely want to subclass FrontEndBase. |
|
8 | 8 | |
|
9 | 9 | Author: Barry Wark |
|
10 | 10 | """ |
|
11 | 11 | __docformat__ = "restructuredtext en" |
|
12 | 12 | |
|
13 | 13 | #------------------------------------------------------------------------------- |
|
14 | 14 | # Copyright (C) 2008 The IPython Development Team |
|
15 | 15 | # |
|
16 | 16 | # Distributed under the terms of the BSD License. The full license is in |
|
17 | 17 | # the file COPYING, distributed as part of this software. |
|
18 | 18 | #------------------------------------------------------------------------------- |
|
19 | 19 | |
|
20 | 20 | #------------------------------------------------------------------------------- |
|
21 | 21 | # Imports |
|
22 | 22 | #------------------------------------------------------------------------------- |
|
23 | 23 | import string |
|
24 | 24 | import uuid |
|
25 | 25 | import _ast |
|
26 | 26 | |
|
27 | 27 | from zopeinterface import Interface, Attribute, implements, classProvides |
|
28 | 28 | |
|
29 | 29 | from IPython.kernel.core.history import FrontEndHistory |
|
30 | 30 | from IPython.kernel.core.util import Bunch |
|
31 | from IPython.kernel.engineservice import IEngineCore | |
|
32 | 31 | |
|
33 | 32 | ############################################################################## |
|
34 | 33 | # TEMPORARY!!! fake configuration, while we decide whether to use tconfig or |
|
35 | 34 | # not |
|
36 | 35 | |
|
37 | 36 | rc = Bunch() |
|
38 | 37 | rc.prompt_in1 = r'In [$number]: ' |
|
39 | 38 | rc.prompt_in2 = r'...' |
|
40 | 39 | rc.prompt_out = r'Out [$number]: ' |
|
41 | 40 | |
|
42 | 41 | ############################################################################## |
|
43 | 42 | # Interface definitions |
|
44 | 43 | ############################################################################## |
|
45 | 44 | |
|
46 | 45 | class IFrontEndFactory(Interface): |
|
47 | 46 | """Factory interface for frontends.""" |
|
48 | 47 | |
|
49 | 48 | def __call__(engine=None, history=None): |
|
50 | 49 | """ |
|
51 | 50 | Parameters: |
|
52 | 51 | interpreter : IPython.kernel.engineservice.IEngineCore |
|
53 | 52 | """ |
|
54 | 53 | |
|
55 | 54 | pass |
|
56 | 55 | |
|
57 | 56 | |
|
58 | 57 | class IFrontEnd(Interface): |
|
59 | 58 | """Interface for frontends. All methods return t.i.d.Deferred""" |
|
60 | 59 | |
|
61 | 60 | Attribute("input_prompt_template", "string.Template instance\ |
|
62 | 61 | substituteable with execute result.") |
|
63 | 62 | Attribute("output_prompt_template", "string.Template instance\ |
|
64 | 63 | substituteable with execute result.") |
|
65 | 64 | Attribute("continuation_prompt_template", "string.Template instance\ |
|
66 | 65 | substituteable with execute result.") |
|
67 | 66 | |
|
68 | 67 | def update_cell_prompt(result, blockID=None): |
|
69 | 68 | """Subclass may override to update the input prompt for a block. |
|
70 | 69 | |
|
71 | 70 | In asynchronous frontends, this method will be called as a |
|
72 | 71 | twisted.internet.defer.Deferred's callback/errback. |
|
73 | 72 | Implementations should thus return result when finished. |
|
74 | 73 | |
|
75 | 74 | Result is a result dict in case of success, and a |
|
76 | 75 | twisted.python.util.failure.Failure in case of an error |
|
77 | 76 | """ |
|
78 | 77 | |
|
79 | 78 | pass |
|
80 | 79 | |
|
81 | 80 | def render_result(result): |
|
82 | 81 | """Render the result of an execute call. Implementors may choose the |
|
83 | 82 | method of rendering. |
|
84 | 83 | For example, a notebook-style frontend might render a Chaco plot |
|
85 | 84 | inline. |
|
86 | 85 | |
|
87 | 86 | Parameters: |
|
88 | 87 | result : dict (result of IEngineBase.execute ) |
|
89 | 88 | blockID = result['blockID'] |
|
90 | 89 | |
|
91 | 90 | Result: |
|
92 | 91 | Output of frontend rendering |
|
93 | 92 | """ |
|
94 | 93 | |
|
95 | 94 | pass |
|
96 | 95 | |
|
97 | 96 | def render_error(failure): |
|
98 | 97 | """Subclasses must override to render the failure. |
|
99 | 98 | |
|
100 | 99 | In asynchronous frontend, since this method will be called as a |
|
101 | 100 | twisted.internet.defer.Deferred's callback. Implementations |
|
102 | 101 | should thus return result when finished. |
|
103 | 102 | |
|
104 | 103 | blockID = failure.blockID |
|
105 | 104 | """ |
|
106 | 105 | |
|
107 | 106 | pass |
|
108 | 107 | |
|
109 | 108 | def input_prompt(number=''): |
|
110 | 109 | """Returns the input prompt by subsituting into |
|
111 | 110 | self.input_prompt_template |
|
112 | 111 | """ |
|
113 | 112 | pass |
|
114 | 113 | |
|
115 | 114 | def output_prompt(number=''): |
|
116 | 115 | """Returns the output prompt by subsituting into |
|
117 | 116 | self.output_prompt_template |
|
118 | 117 | """ |
|
119 | 118 | |
|
120 | 119 | pass |
|
121 | 120 | |
|
122 | 121 | def continuation_prompt(): |
|
123 | 122 | """Returns the continuation prompt by subsituting into |
|
124 | 123 | self.continuation_prompt_template |
|
125 | 124 | """ |
|
126 | 125 | |
|
127 | 126 | pass |
|
128 | 127 | |
|
129 | 128 | def is_complete(block): |
|
130 | 129 | """Returns True if block is complete, False otherwise.""" |
|
131 | 130 | |
|
132 | 131 | pass |
|
133 | 132 | |
|
134 | 133 | def compile_ast(block): |
|
135 | 134 | """Compiles block to an _ast.AST""" |
|
136 | 135 | |
|
137 | 136 | pass |
|
138 | 137 | |
|
139 | 138 | def get_history_previous(current_block): |
|
140 | 139 | """Returns the block previous in the history. Saves currentBlock if |
|
141 | 140 | the history_cursor is currently at the end of the input history""" |
|
142 | 141 | pass |
|
143 | 142 | |
|
144 | 143 | def get_history_next(): |
|
145 | 144 | """Returns the next block in the history.""" |
|
146 | 145 | |
|
147 | 146 | pass |
|
148 | 147 | |
|
149 | 148 | def complete(self, line): |
|
150 | 149 | """Returns the list of possible completions, and the completed |
|
151 | 150 | line. |
|
152 | 151 | |
|
153 | 152 | The input argument is the full line to be completed. This method |
|
154 | 153 | returns both the line completed as much as possible, and the list |
|
155 | 154 | of further possible completions (full words). |
|
156 | 155 | """ |
|
157 | 156 | pass |
|
158 | 157 | |
|
159 | 158 | |
|
160 | 159 | ############################################################################## |
|
161 | 160 | # Base class for all the frontends. |
|
162 | 161 | ############################################################################## |
|
163 | 162 | |
|
164 | 163 | class FrontEndBase(object): |
|
165 | 164 | """ |
|
166 | 165 | FrontEndBase manages the state tasks for a CLI frontend: |
|
167 | 166 | - Input and output history management |
|
168 | 167 | - Input/continuation and output prompt generation |
|
169 | 168 | |
|
170 | 169 | Some issues (due to possibly unavailable engine): |
|
171 | 170 | - How do we get the current cell number for the engine? |
|
172 | 171 | - How do we handle completions? |
|
173 | 172 | """ |
|
174 | 173 | |
|
175 | 174 | history_cursor = 0 |
|
176 | 175 | |
|
177 | 176 | input_prompt_template = string.Template(rc.prompt_in1) |
|
178 | 177 | output_prompt_template = string.Template(rc.prompt_out) |
|
179 | 178 | continuation_prompt_template = string.Template(rc.prompt_in2) |
|
180 | 179 | |
|
181 | 180 | def __init__(self, shell=None, history=None): |
|
182 | 181 | self.shell = shell |
|
183 | 182 | if history is None: |
|
184 | 183 | self.history = FrontEndHistory(input_cache=['']) |
|
185 | 184 | else: |
|
186 | 185 | self.history = history |
|
187 | 186 | |
|
188 | 187 | |
|
189 | 188 | def input_prompt(self, number=''): |
|
190 | 189 | """Returns the current input prompt |
|
191 | 190 | |
|
192 | 191 | It would be great to use ipython1.core.prompts.Prompt1 here |
|
193 | 192 | """ |
|
194 | 193 | return self.input_prompt_template.safe_substitute({'number':number}) |
|
195 | 194 | |
|
196 | 195 | |
|
197 | 196 | def continuation_prompt(self): |
|
198 | 197 | """Returns the current continuation prompt""" |
|
199 | 198 | |
|
200 | 199 | return self.continuation_prompt_template.safe_substitute() |
|
201 | 200 | |
|
202 | 201 | def output_prompt(self, number=''): |
|
203 | 202 | """Returns the output prompt for result""" |
|
204 | 203 | |
|
205 | 204 | return self.output_prompt_template.safe_substitute({'number':number}) |
|
206 | 205 | |
|
207 | 206 | |
|
208 | 207 | def is_complete(self, block): |
|
209 | 208 | """Determine if block is complete. |
|
210 | 209 | |
|
211 | 210 | Parameters |
|
212 | 211 | block : string |
|
213 | 212 | |
|
214 | 213 | Result |
|
215 | 214 | True if block can be sent to the engine without compile errors. |
|
216 | 215 | False otherwise. |
|
217 | 216 | """ |
|
218 | 217 | |
|
219 | 218 | try: |
|
220 | 219 | ast = self.compile_ast(block) |
|
221 | 220 | except: |
|
222 | 221 | return False |
|
223 | 222 | |
|
224 | 223 | lines = block.split('\n') |
|
225 | 224 | return (len(lines)==1 or str(lines[-1])=='') |
|
226 | 225 | |
|
227 | 226 | |
|
228 | 227 | def compile_ast(self, block): |
|
229 | 228 | """Compile block to an AST |
|
230 | 229 | |
|
231 | 230 | Parameters: |
|
232 | 231 | block : str |
|
233 | 232 | |
|
234 | 233 | Result: |
|
235 | 234 | AST |
|
236 | 235 | |
|
237 | 236 | Throws: |
|
238 | 237 | Exception if block cannot be compiled |
|
239 | 238 | """ |
|
240 | 239 | |
|
241 | 240 | return compile(block, "<string>", "exec", _ast.PyCF_ONLY_AST) |
|
242 | 241 | |
|
243 | 242 | |
|
244 | 243 | def execute(self, block, blockID=None): |
|
245 | 244 | """Execute the block and return the result. |
|
246 | 245 | |
|
247 | 246 | Parameters: |
|
248 | 247 | block : {str, AST} |
|
249 | 248 | blockID : any |
|
250 | 249 | Caller may provide an ID to identify this block. |
|
251 | 250 | result['blockID'] := blockID |
|
252 | 251 | |
|
253 | 252 | Result: |
|
254 | 253 | Deferred result of self.interpreter.execute |
|
255 | 254 | """ |
|
256 | 255 | |
|
257 | 256 | if(not self.is_complete(block)): |
|
258 | 257 | raise Exception("Block is not compilable") |
|
259 | 258 | |
|
260 | 259 | if(blockID == None): |
|
261 | 260 | blockID = uuid.uuid4() #random UUID |
|
262 | 261 | |
|
263 | 262 | try: |
|
264 | 263 | result = self.shell.execute(block) |
|
265 | 264 | except Exception,e: |
|
266 | 265 | e = self._add_block_id_for_failure(e, blockID=blockID) |
|
267 | 266 | e = self.update_cell_prompt(e, blockID=blockID) |
|
268 | 267 | e = self.render_error(e) |
|
269 | 268 | else: |
|
270 | 269 | result = self._add_block_id_for_result(result, blockID=blockID) |
|
271 | 270 | result = self.update_cell_prompt(result, blockID=blockID) |
|
272 | 271 | result = self.render_result(result) |
|
273 | 272 | |
|
274 | 273 | return result |
|
275 | 274 | |
|
276 | 275 | |
|
277 | 276 | def _add_block_id_for_result(self, result, blockID): |
|
278 | 277 | """Add the blockID to result or failure. Unfortunatley, we have to |
|
279 | 278 | treat failures differently than result dicts. |
|
280 | 279 | """ |
|
281 | 280 | |
|
282 | 281 | result['blockID'] = blockID |
|
283 | 282 | |
|
284 | 283 | return result |
|
285 | 284 | |
|
286 | 285 | def _add_block_id_for_failure(self, failure, blockID): |
|
287 | 286 | """_add_block_id_for_failure""" |
|
288 | 287 | failure.blockID = blockID |
|
289 | 288 | return failure |
|
290 | 289 | |
|
291 | 290 | |
|
292 | 291 | def _add_history(self, result, block=None): |
|
293 | 292 | """Add block to the history""" |
|
294 | 293 | |
|
295 | 294 | assert(block != None) |
|
296 | 295 | self.history.add_items([block]) |
|
297 | 296 | self.history_cursor += 1 |
|
298 | 297 | |
|
299 | 298 | return result |
|
300 | 299 | |
|
301 | 300 | |
|
302 | 301 | def get_history_previous(self, current_block): |
|
303 | 302 | """ Returns previous history string and decrement history cursor. |
|
304 | 303 | """ |
|
305 | 304 | command = self.history.get_history_item(self.history_cursor - 1) |
|
306 | 305 | |
|
307 | 306 | if command is not None: |
|
308 | 307 | if(self.history_cursor+1 == len(self.history.input_cache)): |
|
309 | 308 | self.history.input_cache[self.history_cursor] = current_block |
|
310 | 309 | self.history_cursor -= 1 |
|
311 | 310 | return command |
|
312 | 311 | |
|
313 | 312 | |
|
314 | 313 | def get_history_next(self): |
|
315 | 314 | """ Returns next history string and increment history cursor. |
|
316 | 315 | """ |
|
317 | 316 | command = self.history.get_history_item(self.history_cursor+1) |
|
318 | 317 | |
|
319 | 318 | if command is not None: |
|
320 | 319 | self.history_cursor += 1 |
|
321 | 320 | return command |
|
322 | 321 | |
|
323 | 322 | ### |
|
324 | 323 | # Subclasses probably want to override these methods... |
|
325 | 324 | ### |
|
326 | 325 | |
|
327 | 326 | def update_cell_prompt(self, result, blockID=None): |
|
328 | 327 | """Subclass may override to update the input prompt for a block. |
|
329 | 328 | |
|
330 | 329 | This method only really makes sens in asyncrhonous frontend. |
|
331 | 330 | Since this method will be called as a |
|
332 | 331 | twisted.internet.defer.Deferred's callback, implementations should |
|
333 | 332 | return result when finished. |
|
334 | 333 | """ |
|
335 | 334 | |
|
336 | 335 | raise NotImplementedError |
|
337 | 336 | |
|
338 | 337 | |
|
339 | 338 | def render_result(self, result): |
|
340 | 339 | """Subclasses must override to render result. |
|
341 | 340 | |
|
342 | 341 | In asynchronous frontends, this method will be called as a |
|
343 | 342 | twisted.internet.defer.Deferred's callback. Implementations |
|
344 | 343 | should thus return result when finished. |
|
345 | 344 | """ |
|
346 | 345 | |
|
347 | 346 | raise NotImplementedError |
|
348 | 347 | |
|
349 | 348 | |
|
350 | 349 | def render_error(self, failure): |
|
351 | 350 | """Subclasses must override to render the failure. |
|
352 | 351 | |
|
353 | 352 | In asynchronous frontends, this method will be called as a |
|
354 | 353 | twisted.internet.defer.Deferred's callback. Implementations |
|
355 | 354 | should thus return result when finished. |
|
356 | 355 | """ |
|
357 | 356 | |
|
358 | 357 | raise NotImplementedError |
|
359 | 358 |
@@ -1,152 +1,157 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | |
|
3 | 3 | """This file contains unittests for the frontendbase module.""" |
|
4 | 4 | |
|
5 | 5 | __docformat__ = "restructuredtext en" |
|
6 | 6 | |
|
7 | 7 | #--------------------------------------------------------------------------- |
|
8 | 8 | # Copyright (C) 2008 The IPython Development Team |
|
9 | 9 | # |
|
10 | 10 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | 11 | # the file COPYING, distributed as part of this software. |
|
12 | 12 | #--------------------------------------------------------------------------- |
|
13 | 13 | |
|
14 | 14 | #--------------------------------------------------------------------------- |
|
15 | 15 | # Imports |
|
16 | 16 | #--------------------------------------------------------------------------- |
|
17 | 17 | |
|
18 | 18 | import unittest |
|
19 | from IPython.frontend.asyncfrontendbase import AsyncFrontEndBase | |
|
20 | from IPython.frontend import frontendbase | |
|
21 | from IPython.kernel.engineservice import EngineService | |
|
19 | ||
|
20 | try: | |
|
21 | from IPython.frontend.asyncfrontendbase import AsyncFrontEndBase | |
|
22 | from IPython.frontend import frontendbase | |
|
23 | from IPython.kernel.engineservice import EngineService | |
|
24 | except ImportError: | |
|
25 | import nose | |
|
26 | raise nose.SkipTest("This test requires zope.interface and Twisted") | |
|
22 | 27 | |
|
23 | 28 | class FrontEndCallbackChecker(AsyncFrontEndBase): |
|
24 | 29 | """FrontEndBase subclass for checking callbacks""" |
|
25 | 30 | def __init__(self, engine=None, history=None): |
|
26 | 31 | super(FrontEndCallbackChecker, self).__init__(engine=engine, |
|
27 | 32 | history=history) |
|
28 | 33 | self.updateCalled = False |
|
29 | 34 | self.renderResultCalled = False |
|
30 | 35 | self.renderErrorCalled = False |
|
31 |
|
|
|
36 | ||
|
32 | 37 | def update_cell_prompt(self, result, blockID=None): |
|
33 | 38 | self.updateCalled = True |
|
34 | 39 | return result |
|
35 |
|
|
|
40 | ||
|
36 | 41 | def render_result(self, result): |
|
37 | 42 | self.renderResultCalled = True |
|
38 | 43 | return result |
|
39 | 44 | |
|
40 | 45 | |
|
41 | 46 | def render_error(self, failure): |
|
42 | 47 | self.renderErrorCalled = True |
|
43 | 48 | return failure |
|
44 | 49 | |
|
45 | 50 | |
|
46 | 51 | |
|
47 | 52 | |
|
48 | 53 | class TestAsyncFrontendBase(unittest.TestCase): |
|
49 | 54 | def setUp(self): |
|
50 | 55 | """Setup the EngineService and FrontEndBase""" |
|
51 | 56 | |
|
52 | 57 | self.fb = FrontEndCallbackChecker(engine=EngineService()) |
|
53 | 58 | |
|
54 | 59 | |
|
55 | 60 | def test_implements_IFrontEnd(self): |
|
56 | 61 | assert(frontendbase.IFrontEnd.implementedBy( |
|
57 | 62 | AsyncFrontEndBase)) |
|
58 | 63 | |
|
59 | 64 | |
|
60 | 65 | def test_is_complete_returns_False_for_incomplete_block(self): |
|
61 | 66 | """""" |
|
62 | 67 | |
|
63 | 68 | block = """def test(a):""" |
|
64 | 69 | |
|
65 | 70 | assert(self.fb.is_complete(block) == False) |
|
66 | 71 | |
|
67 | 72 | def test_is_complete_returns_True_for_complete_block(self): |
|
68 | 73 | """""" |
|
69 | 74 | |
|
70 | 75 | block = """def test(a): pass""" |
|
71 | 76 | |
|
72 | 77 | assert(self.fb.is_complete(block)) |
|
73 | 78 | |
|
74 | 79 | block = """a=3""" |
|
75 | 80 | |
|
76 | 81 | assert(self.fb.is_complete(block)) |
|
77 | 82 | |
|
78 | 83 | |
|
79 | 84 | def test_blockID_added_to_result(self): |
|
80 | 85 | block = """3+3""" |
|
81 | 86 | |
|
82 | 87 | d = self.fb.execute(block, blockID='TEST_ID') |
|
83 | 88 | |
|
84 | 89 | d.addCallback(self.checkBlockID, expected='TEST_ID') |
|
85 | 90 | |
|
86 | 91 | def test_blockID_added_to_failure(self): |
|
87 | 92 | block = "raise Exception()" |
|
88 | 93 | |
|
89 | 94 | d = self.fb.execute(block,blockID='TEST_ID') |
|
90 | 95 | d.addErrback(self.checkFailureID, expected='TEST_ID') |
|
91 | 96 | |
|
92 | 97 | def checkBlockID(self, result, expected=""): |
|
93 | 98 | assert(result['blockID'] == expected) |
|
94 | 99 | |
|
95 | 100 | |
|
96 | 101 | def checkFailureID(self, failure, expected=""): |
|
97 | 102 | assert(failure.blockID == expected) |
|
98 | 103 | |
|
99 | 104 | |
|
100 | 105 | def test_callbacks_added_to_execute(self): |
|
101 | 106 | """test that |
|
102 | 107 | update_cell_prompt |
|
103 | 108 | render_result |
|
104 | 109 | |
|
105 | 110 | are added to execute request |
|
106 | 111 | """ |
|
107 | 112 | |
|
108 | 113 | d = self.fb.execute("10+10") |
|
109 | 114 | d.addCallback(self.checkCallbacks) |
|
110 | 115 | |
|
111 | 116 | |
|
112 | 117 | def checkCallbacks(self, result): |
|
113 | 118 | assert(self.fb.updateCalled) |
|
114 | 119 | assert(self.fb.renderResultCalled) |
|
115 | 120 | |
|
116 | 121 | |
|
117 | 122 | def test_error_callback_added_to_execute(self): |
|
118 | 123 | """test that render_error called on execution error""" |
|
119 | 124 | |
|
120 | 125 | d = self.fb.execute("raise Exception()") |
|
121 | 126 | d.addCallback(self.checkRenderError) |
|
122 | 127 | |
|
123 | 128 | def checkRenderError(self, result): |
|
124 | 129 | assert(self.fb.renderErrorCalled) |
|
125 | 130 | |
|
126 | 131 | def test_history_returns_expected_block(self): |
|
127 | 132 | """Make sure history browsing doesn't fail""" |
|
128 | 133 | |
|
129 | 134 | blocks = ["a=1","a=2","a=3"] |
|
130 | 135 | for b in blocks: |
|
131 | 136 | d = self.fb.execute(b) |
|
132 | 137 | |
|
133 | 138 | # d is now the deferred for the last executed block |
|
134 | 139 | d.addCallback(self.historyTests, blocks) |
|
135 | 140 | |
|
136 | 141 | |
|
137 | 142 | def historyTests(self, result, blocks): |
|
138 | 143 | """historyTests""" |
|
139 | 144 | |
|
140 | 145 | assert(len(blocks) >= 3) |
|
141 | 146 | assert(self.fb.get_history_previous("") == blocks[-2]) |
|
142 | 147 | assert(self.fb.get_history_previous("") == blocks[-3]) |
|
143 | 148 | assert(self.fb.get_history_next() == blocks[-2]) |
|
144 | 149 | |
|
145 | 150 | |
|
146 | 151 | def test_history_returns_none_at_startup(self): |
|
147 | 152 | """test_history_returns_none_at_startup""" |
|
148 | 153 | |
|
149 | 154 | assert(self.fb.get_history_previous("")==None) |
|
150 | 155 | assert(self.fb.get_history_next()==None) |
|
151 | 156 | |
|
152 | 157 |
|
1 | NO CONTENT: modified file chmod 100755 => 100644 |
|
1 | NO CONTENT: modified file chmod 100755 => 100644 |
|
1 | NO CONTENT: modified file chmod 100755 => 100644 |
|
1 | NO CONTENT: modified file chmod 100755 => 100644 |
|
1 | NO CONTENT: modified file chmod 100755 => 100644 |
@@ -1,92 +1,91 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | |
|
3 | 3 | """This file contains unittests for the enginepb.py module.""" |
|
4 | 4 | |
|
5 | 5 | __docformat__ = "restructuredtext en" |
|
6 | 6 | |
|
7 | 7 | #------------------------------------------------------------------------------- |
|
8 | 8 | # Copyright (C) 2008 The IPython Development Team |
|
9 | 9 | # |
|
10 | 10 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | 11 | # the file COPYING, distributed as part of this software. |
|
12 | 12 | #------------------------------------------------------------------------------- |
|
13 | 13 | |
|
14 | 14 | #------------------------------------------------------------------------------- |
|
15 | 15 | # Imports |
|
16 | 16 | #------------------------------------------------------------------------------- |
|
17 | 17 | |
|
18 | 18 | try: |
|
19 | 19 | from twisted.python import components |
|
20 | 20 | from twisted.internet import reactor, defer |
|
21 | 21 | from twisted.spread import pb |
|
22 | 22 | from twisted.internet.base import DelayedCall |
|
23 | 23 | DelayedCall.debug = True |
|
24 | 24 | |
|
25 | 25 | import zope.interface as zi |
|
26 | 26 | |
|
27 | 27 | from IPython.kernel.fcutil import Tub, UnauthenticatedTub |
|
28 | 28 | from IPython.kernel import engineservice as es |
|
29 | 29 | from IPython.testing.util import DeferredTestCase |
|
30 | 30 | from IPython.kernel.controllerservice import IControllerBase |
|
31 | 31 | from IPython.kernel.enginefc import FCRemoteEngineRefFromService, IEngineBase |
|
32 | 32 | from IPython.kernel.engineservice import IEngineQueued |
|
33 | 33 | from IPython.kernel.engineconnector import EngineConnector |
|
34 | 34 | |
|
35 | 35 | from IPython.kernel.tests.engineservicetest import \ |
|
36 | 36 | IEngineCoreTestCase, \ |
|
37 | 37 | IEngineSerializedTestCase, \ |
|
38 | 38 | IEngineQueuedTestCase |
|
39 | 39 | except ImportError: |
|
40 | print "we got an error!!!" | |
|
41 | raise | |
|
40 | pass | |
|
42 | 41 | else: |
|
43 | 42 | class EngineFCTest(DeferredTestCase, |
|
44 | 43 | IEngineCoreTestCase, |
|
45 | 44 | IEngineSerializedTestCase, |
|
46 | 45 | IEngineQueuedTestCase |
|
47 | 46 | ): |
|
48 | 47 | |
|
49 | 48 | zi.implements(IControllerBase) |
|
50 | 49 | |
|
51 | 50 | def setUp(self): |
|
52 | 51 | |
|
53 | 52 | # Start a server and append to self.servers |
|
54 | 53 | self.controller_reference = FCRemoteEngineRefFromService(self) |
|
55 | 54 | self.controller_tub = Tub() |
|
56 | 55 | self.controller_tub.listenOn('tcp:10105:interface=127.0.0.1') |
|
57 | 56 | self.controller_tub.setLocation('127.0.0.1:10105') |
|
58 | 57 | |
|
59 | 58 | furl = self.controller_tub.registerReference(self.controller_reference) |
|
60 | 59 | self.controller_tub.startService() |
|
61 | 60 | |
|
62 | 61 | # Start an EngineService and append to services/client |
|
63 | 62 | self.engine_service = es.EngineService() |
|
64 | 63 | self.engine_service.startService() |
|
65 | 64 | self.engine_tub = Tub() |
|
66 | 65 | self.engine_tub.startService() |
|
67 | 66 | engine_connector = EngineConnector(self.engine_tub) |
|
68 | 67 | d = engine_connector.connect_to_controller(self.engine_service, furl) |
|
69 | 68 | # This deferred doesn't fire until after register_engine has returned and |
|
70 | 69 | # thus, self.engine has been defined and the tets can proceed. |
|
71 | 70 | return d |
|
72 | 71 | |
|
73 | 72 | def tearDown(self): |
|
74 | 73 | dlist = [] |
|
75 | 74 | # Shut down the engine |
|
76 | 75 | d = self.engine_tub.stopService() |
|
77 | 76 | dlist.append(d) |
|
78 | 77 | # Shut down the controller |
|
79 | 78 | d = self.controller_tub.stopService() |
|
80 | 79 | dlist.append(d) |
|
81 | 80 | return defer.DeferredList(dlist) |
|
82 | 81 | |
|
83 | 82 | #--------------------------------------------------------------------------- |
|
84 | 83 | # Make me look like a basic controller |
|
85 | 84 | #--------------------------------------------------------------------------- |
|
86 | 85 | |
|
87 | 86 | def register_engine(self, engine_ref, id=None, ip=None, port=None, pid=None): |
|
88 | 87 | self.engine = IEngineQueued(IEngineBase(engine_ref)) |
|
89 | 88 | return {'id':id} |
|
90 | 89 | |
|
91 | 90 | def unregister_engine(self, id): |
|
92 | 91 | pass No newline at end of file |
|
1 | NO CONTENT: modified file chmod 100755 => 100644 |
General Comments 0
You need to be logged in to leave comments.
Login now