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