Show More
@@ -1,155 +1,111 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | |
|
3 | """This file contains unittests for the frontendbase module.""" | |
|
3 | """This file contains unittests for the asyncfrontendbase 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 | import unittest | |
|
19 | 18 | |
|
20 | 19 | try: |
|
20 | from twisted.trial import unittest | |
|
21 | 21 | from IPython.frontend.asyncfrontendbase import AsyncFrontEndBase |
|
22 | 22 | from IPython.frontend import frontendbase |
|
23 | 23 | from IPython.kernel.engineservice import EngineService |
|
24 | from IPython.testing.parametric import Parametric, parametric | |
|
24 | 25 | except ImportError: |
|
25 | 26 | import nose |
|
26 | 27 | raise nose.SkipTest("This test requires zope.interface, Twisted and Foolscap") |
|
27 | 28 | |
|
28 | from IPython.testing.decorators import skip | |
|
29 | 29 | |
|
30 | 30 | class FrontEndCallbackChecker(AsyncFrontEndBase): |
|
31 | 31 | """FrontEndBase subclass for checking callbacks""" |
|
32 | 32 | def __init__(self, engine=None, history=None): |
|
33 | 33 | super(FrontEndCallbackChecker, self).__init__(engine=engine, |
|
34 | 34 | history=history) |
|
35 | 35 | self.updateCalled = False |
|
36 | 36 | self.renderResultCalled = False |
|
37 | 37 | self.renderErrorCalled = False |
|
38 | 38 | |
|
39 | 39 | def update_cell_prompt(self, result, blockID=None): |
|
40 | 40 | self.updateCalled = True |
|
41 | 41 | return result |
|
42 | 42 | |
|
43 | 43 | def render_result(self, result): |
|
44 | 44 | self.renderResultCalled = True |
|
45 | 45 | return result |
|
46 | 46 | |
|
47 | ||
|
48 | 47 | def render_error(self, failure): |
|
49 | 48 | self.renderErrorCalled = True |
|
50 | 49 | return failure |
|
51 | ||
|
52 | 50 | |
|
53 | 51 | |
|
54 | ||
|
55 | 52 | class TestAsyncFrontendBase(unittest.TestCase): |
|
56 | 53 | def setUp(self): |
|
57 | 54 | """Setup the EngineService and FrontEndBase""" |
|
58 | 55 | |
|
59 | 56 | self.fb = FrontEndCallbackChecker(engine=EngineService()) |
|
60 | 57 | |
|
61 | 58 | def test_implements_IFrontEnd(self): |
|
62 | assert(frontendbase.IFrontEnd.implementedBy( | |
|
59 | self.assert_(frontendbase.IFrontEnd.implementedBy( | |
|
63 | 60 | AsyncFrontEndBase)) |
|
64 | 61 | |
|
65 | 62 | def test_is_complete_returns_False_for_incomplete_block(self): |
|
66 | """""" | |
|
67 | ||
|
68 | 63 | block = """def test(a):""" |
|
69 | ||
|
70 | assert(self.fb.is_complete(block) == False) | |
|
64 | self.assert_(self.fb.is_complete(block) == False) | |
|
71 | 65 | |
|
72 | 66 | def test_is_complete_returns_True_for_complete_block(self): |
|
73 | """""" | |
|
74 | ||
|
75 | 67 | block = """def test(a): pass""" |
|
76 | ||
|
77 | assert(self.fb.is_complete(block)) | |
|
78 | ||
|
68 | self.assert_(self.fb.is_complete(block)) | |
|
79 | 69 | block = """a=3""" |
|
80 | ||
|
81 | assert(self.fb.is_complete(block)) | |
|
70 | self.assert_(self.fb.is_complete(block)) | |
|
82 | 71 | |
|
83 | 72 | def test_blockID_added_to_result(self): |
|
84 | 73 | block = """3+3""" |
|
85 | ||
|
86 | 74 | d = self.fb.execute(block, blockID='TEST_ID') |
|
87 | ||
|
88 | d.addCallback(self.checkBlockID, expected='TEST_ID') | |
|
75 | d.addCallback(lambda r: self.assert_(r['blockID']=='TEST_ID')) | |
|
76 | return d | |
|
89 | 77 | |
|
90 | 78 | def test_blockID_added_to_failure(self): |
|
91 | 79 | block = "raise Exception()" |
|
92 | ||
|
93 | 80 | d = self.fb.execute(block,blockID='TEST_ID') |
|
94 |
d.addErrback(self. |
|
|
95 | ||
|
96 | def checkBlockID(self, result, expected=""): | |
|
97 | assert(result['blockID'] == expected) | |
|
98 | ||
|
99 | ||
|
100 | def checkFailureID(self, failure, expected=""): | |
|
101 | assert(failure.blockID == expected) | |
|
102 | ||
|
81 | d.addErrback(lambda f: self.assert_(f.blockID=='TEST_ID')) | |
|
82 | return d | |
|
103 | 83 | |
|
104 | 84 | def test_callbacks_added_to_execute(self): |
|
105 | """test that | |
|
106 | update_cell_prompt | |
|
107 | render_result | |
|
108 | ||
|
109 | are added to execute request | |
|
110 | """ | |
|
111 | ||
|
112 | 85 | d = self.fb.execute("10+10") |
|
113 | d.addCallback(self.checkCallbacks) | |
|
86 | d.addCallback(lambda r: self.assert_(self.fb.updateCalled and self.fb.renderResultCalled)) | |
|
87 | return d | |
|
114 | 88 | |
|
115 | def checkCallbacks(self, result): | |
|
116 | assert(self.fb.updateCalled) | |
|
117 | assert(self.fb.renderResultCalled) | |
|
118 | ||
|
119 | @skip("This test fails and lead to an unhandled error in a Deferred.") | |
|
120 | 89 | def test_error_callback_added_to_execute(self): |
|
121 |
""" |
|
|
90 | """Test that render_error called on execution error.""" | |
|
122 | 91 | |
|
123 | 92 | d = self.fb.execute("raise Exception()") |
|
124 | d.addCallback(self.checkRenderError) | |
|
125 | ||
|
126 | def checkRenderError(self, result): | |
|
127 | assert(self.fb.renderErrorCalled) | |
|
93 | d.addErrback(lambda f: self.assert_(self.fb.renderErrorCalled)) | |
|
94 | return d | |
|
128 | 95 | |
|
129 | 96 | def test_history_returns_expected_block(self): |
|
130 | """Make sure history browsing doesn't fail""" | |
|
97 | """Make sure history browsing doesn't fail.""" | |
|
131 | 98 | |
|
132 | 99 | blocks = ["a=1","a=2","a=3"] |
|
133 | for b in blocks: | |
|
134 |
|
|
|
135 | ||
|
136 | # d is now the deferred for the last executed block | |
|
137 |
d.addCallback(self.history |
|
|
138 | ||
|
139 | ||
|
140 | def historyTests(self, result, blocks): | |
|
141 | """historyTests""" | |
|
142 | ||
|
143 | assert(len(blocks) >= 3) | |
|
144 | assert(self.fb.get_history_previous("") == blocks[-2]) | |
|
145 | assert(self.fb.get_history_previous("") == blocks[-3]) | |
|
146 | assert(self.fb.get_history_next() == blocks[-2]) | |
|
147 | ||
|
148 | ||
|
149 | def test_history_returns_none_at_startup(self): | |
|
150 | """test_history_returns_none_at_startup""" | |
|
151 | ||
|
152 | assert(self.fb.get_history_previous("")==None) | |
|
153 | assert(self.fb.get_history_next()==None) | |
|
154 | ||
|
155 | ||
|
100 | d = self.fb.execute(blocks[0]) | |
|
101 | d.addCallback(lambda _: self.fb.execute(blocks[1])) | |
|
102 | d.addCallback(lambda _: self.fb.execute(blocks[2])) | |
|
103 | d.addCallback(lambda _: self.assert_(self.fb.get_history_previous("")==blocks[-2])) | |
|
104 | d.addCallback(lambda _: self.assert_(self.fb.get_history_previous("")==blocks[-3])) | |
|
105 | d.addCallback(lambda _: self.assert_(self.fb.get_history_next()==blocks[-2])) | |
|
106 | return d | |
|
107 | ||
|
108 | def test_history_returns_none_at_startup(self): | |
|
109 | self.assert_(self.fb.get_history_previous("")==None) | |
|
110 | self.assert_(self.fb.get_history_next()==None) | |
|
111 |
General Comments 0
You need to be logged in to leave comments.
Login now