Show More
@@ -1,109 +1,106 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """This file contains unittests for the asyncfrontendbase module.""" |
|
3 | ||
|
4 | # Tell nose to skip this module | |
|
5 | __test__ = {} | |
|
6 | 3 | |
|
7 | 4 | #--------------------------------------------------------------------------- |
|
8 | 5 | # Copyright (C) 2008-2009 The IPython Development Team |
|
9 | 6 | # |
|
10 | 7 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | 8 | # the file COPYING, distributed as part of this software. |
|
12 | 9 | #--------------------------------------------------------------------------- |
|
13 | 10 | |
|
14 | 11 | #--------------------------------------------------------------------------- |
|
15 | 12 | # Imports |
|
16 | 13 | #--------------------------------------------------------------------------- |
|
17 | 14 | |
|
18 | 15 | from twisted.trial import unittest |
|
19 | 16 | |
|
20 | 17 | from IPython.frontend.asyncfrontendbase import AsyncFrontEndBase |
|
21 | 18 | from IPython.frontend import frontendbase |
|
22 | 19 | from IPython.kernel.engineservice import EngineService |
|
23 | 20 | from IPython.testing.parametric import Parametric, parametric |
|
24 | 21 | |
|
25 | 22 | #----------------------------------------------------------------------------- |
|
26 | 23 | # Classes and functions |
|
27 | 24 | #----------------------------------------------------------------------------- |
|
28 | 25 | |
|
29 | 26 | class FrontEndCallbackChecker(AsyncFrontEndBase): |
|
30 | 27 | """FrontEndBase subclass for checking callbacks""" |
|
31 | 28 | def __init__(self, engine=None, history=None): |
|
32 | 29 | super(FrontEndCallbackChecker, self).__init__(engine=engine, |
|
33 | 30 | history=history) |
|
34 | 31 | self.updateCalled = False |
|
35 | 32 | self.renderResultCalled = False |
|
36 | 33 | self.renderErrorCalled = False |
|
37 | 34 | |
|
38 | 35 | def update_cell_prompt(self, result, blockID=None): |
|
39 | 36 | self.updateCalled = True |
|
40 | 37 | return result |
|
41 | 38 | |
|
42 | 39 | def render_result(self, result): |
|
43 | 40 | self.renderResultCalled = True |
|
44 | 41 | return result |
|
45 | 42 | |
|
46 | 43 | def render_error(self, failure): |
|
47 | 44 | self.renderErrorCalled = True |
|
48 | 45 | return failure |
|
49 | 46 | |
|
50 | 47 | |
|
51 | 48 | class TestAsyncFrontendBase(unittest.TestCase): |
|
52 | 49 | def setUp(self): |
|
53 | 50 | """Setup the EngineService and FrontEndBase""" |
|
54 | 51 | |
|
55 | 52 | self.fb = FrontEndCallbackChecker(engine=EngineService()) |
|
56 | 53 | |
|
57 | 54 | def test_implements_IFrontEnd(self): |
|
58 | 55 | self.assert_(frontendbase.IFrontEnd.implementedBy( |
|
59 | 56 | AsyncFrontEndBase)) |
|
60 | 57 | |
|
61 | 58 | def test_is_complete_returns_False_for_incomplete_block(self): |
|
62 | 59 | block = """def test(a):""" |
|
63 | 60 | self.assert_(self.fb.is_complete(block) == False) |
|
64 | 61 | |
|
65 | 62 | def test_is_complete_returns_True_for_complete_block(self): |
|
66 | 63 | block = """def test(a): pass""" |
|
67 | 64 | self.assert_(self.fb.is_complete(block)) |
|
68 | 65 | block = """a=3""" |
|
69 | 66 | self.assert_(self.fb.is_complete(block)) |
|
70 | 67 | |
|
71 | 68 | def test_blockID_added_to_result(self): |
|
72 | 69 | block = """3+3""" |
|
73 | 70 | d = self.fb.execute(block, blockID='TEST_ID') |
|
74 | 71 | d.addCallback(lambda r: self.assert_(r['blockID']=='TEST_ID')) |
|
75 | 72 | return d |
|
76 | 73 | |
|
77 | 74 | def test_blockID_added_to_failure(self): |
|
78 | 75 | block = "raise Exception()" |
|
79 | 76 | d = self.fb.execute(block,blockID='TEST_ID') |
|
80 | 77 | d.addErrback(lambda f: self.assert_(f.blockID=='TEST_ID')) |
|
81 | 78 | return d |
|
82 | 79 | |
|
83 | 80 | def test_callbacks_added_to_execute(self): |
|
84 | 81 | d = self.fb.execute("10+10") |
|
85 | 82 | d.addCallback(lambda r: self.assert_(self.fb.updateCalled and self.fb.renderResultCalled)) |
|
86 | 83 | return d |
|
87 | 84 | |
|
88 | 85 | def test_error_callback_added_to_execute(self): |
|
89 | 86 | """Test that render_error called on execution error.""" |
|
90 | 87 | |
|
91 | 88 | d = self.fb.execute("raise Exception()") |
|
92 | 89 | d.addErrback(lambda f: self.assert_(self.fb.renderErrorCalled)) |
|
93 | 90 | return d |
|
94 | 91 | |
|
95 | 92 | def test_history_returns_expected_block(self): |
|
96 | 93 | """Make sure history browsing doesn't fail.""" |
|
97 | 94 | |
|
98 | 95 | blocks = ["a=1","a=2","a=3"] |
|
99 | 96 | d = self.fb.execute(blocks[0]) |
|
100 | 97 | d.addCallback(lambda _: self.fb.execute(blocks[1])) |
|
101 | 98 | d.addCallback(lambda _: self.fb.execute(blocks[2])) |
|
102 | 99 | d.addCallback(lambda _: self.assert_(self.fb.get_history_previous("")==blocks[-2])) |
|
103 | 100 | d.addCallback(lambda _: self.assert_(self.fb.get_history_previous("")==blocks[-3])) |
|
104 | 101 | d.addCallback(lambda _: self.assert_(self.fb.get_history_next()==blocks[-2])) |
|
105 | 102 | return d |
|
106 | 103 | |
|
107 | 104 | def test_history_returns_none_at_startup(self): |
|
108 | 105 | self.assert_(self.fb.get_history_previous("")==None) |
|
109 | 106 | self.assert_(self.fb.get_history_next()==None) |
General Comments 0
You need to be logged in to leave comments.
Login now