Show More
@@ -0,0 +1,75 b'' | |||
|
1 | # -*- coding: utf-8 -*- | |
|
2 | """Tests for CommandChainDispatcher.""" | |
|
3 | ||
|
4 | from __future__ import absolute_import | |
|
5 | ||
|
6 | #----------------------------------------------------------------------------- | |
|
7 | # Imports | |
|
8 | #----------------------------------------------------------------------------- | |
|
9 | ||
|
10 | import nose.tools as nt | |
|
11 | from IPython.core.error import TryNext | |
|
12 | from IPython.core.hooks import CommandChainDispatcher | |
|
13 | ||
|
14 | #----------------------------------------------------------------------------- | |
|
15 | # Local utilities | |
|
16 | #----------------------------------------------------------------------------- | |
|
17 | ||
|
18 | # Define two classes, one which succeeds and one which raises TryNext. Each | |
|
19 | # sets the attribute `called` to True when it is called. | |
|
20 | class Okay(object): | |
|
21 | def __init__(self, message): | |
|
22 | self.message = message | |
|
23 | self.called = False | |
|
24 | def __call__(self): | |
|
25 | self.called = True | |
|
26 | return self.message | |
|
27 | ||
|
28 | class Fail(object): | |
|
29 | def __init__(self, message): | |
|
30 | self.message = message | |
|
31 | self.called = False | |
|
32 | def __call__(self): | |
|
33 | self.called = True | |
|
34 | raise TryNext(self.message) | |
|
35 | ||
|
36 | #----------------------------------------------------------------------------- | |
|
37 | # Test functions | |
|
38 | #----------------------------------------------------------------------------- | |
|
39 | ||
|
40 | def test_command_chain_dispatcher_ff(): | |
|
41 | """Test two failing hooks""" | |
|
42 | fail1 = Fail(u'fail1') | |
|
43 | fail2 = Fail(u'fail2') | |
|
44 | dp = CommandChainDispatcher([(0, fail1), | |
|
45 | (10, fail2)]) | |
|
46 | ||
|
47 | try: | |
|
48 | dp() | |
|
49 | except TryNext as e: | |
|
50 | nt.assert_equal(str(e), u'fail2') | |
|
51 | else: | |
|
52 | assert False, "Expected exception was not raised." | |
|
53 | ||
|
54 | nt.assert_true(fail1.called) | |
|
55 | nt.assert_true(fail2.called) | |
|
56 | ||
|
57 | def test_command_chain_dispatcher_fofo(): | |
|
58 | """Test a mixture of failing and succeeding hooks.""" | |
|
59 | fail1 = Fail(u'fail1') | |
|
60 | fail2 = Fail(u'fail2') | |
|
61 | okay1 = Okay(u'okay1') | |
|
62 | okay2 = Okay(u'okay2') | |
|
63 | ||
|
64 | dp = CommandChainDispatcher([(0, fail1), | |
|
65 | # (5, okay1), # add this later | |
|
66 | (10, fail2), | |
|
67 | (15, okay2)]) | |
|
68 | dp.add(okay1, 5) | |
|
69 | ||
|
70 | nt.assert_equal(dp(), u'okay1') | |
|
71 | ||
|
72 | nt.assert_true(fail1.called) | |
|
73 | nt.assert_true(okay1.called) | |
|
74 | nt.assert_false(fail2.called) | |
|
75 | nt.assert_false(okay2.called) |
General Comments 0
You need to be logged in to leave comments.
Login now