Show More
@@ -1,94 +1,91 b'' | |||||
1 | """Tests for input handlers. |
|
1 | """Tests for input handlers. | |
2 | """ |
|
2 | """ | |
3 | #----------------------------------------------------------------------------- |
|
3 | #----------------------------------------------------------------------------- | |
4 | # Module imports |
|
4 | # Module imports | |
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 |
|
6 | |||
7 | # third party |
|
|||
8 | import nose.tools as nt |
|
|||
9 |
|
||||
10 | # our own packages |
|
7 | # our own packages | |
11 | from IPython.core import autocall |
|
8 | from IPython.core import autocall | |
12 | from IPython.testing import tools as tt |
|
9 | from IPython.testing import tools as tt | |
13 |
|
10 | |||
14 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
15 | # Globals |
|
12 | # Globals | |
16 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
17 |
|
14 | |||
18 | # Get the public instance of IPython |
|
15 | # Get the public instance of IPython | |
19 |
|
16 | |||
20 | failures = [] |
|
17 | failures = [] | |
21 | num_tests = 0 |
|
18 | num_tests = 0 | |
22 |
|
19 | |||
23 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
24 | # Test functions |
|
21 | # Test functions | |
25 | #----------------------------------------------------------------------------- |
|
22 | #----------------------------------------------------------------------------- | |
26 |
|
23 | |||
27 | class CallableIndexable(object): |
|
24 | class CallableIndexable(object): | |
28 | def __getitem__(self, idx): return True |
|
25 | def __getitem__(self, idx): return True | |
29 | def __call__(self, *args, **kws): return True |
|
26 | def __call__(self, *args, **kws): return True | |
30 |
|
27 | |||
31 |
|
28 | |||
32 | class Autocallable(autocall.IPyAutocall): |
|
29 | class Autocallable(autocall.IPyAutocall): | |
33 | def __call__(self): |
|
30 | def __call__(self): | |
34 | return "called" |
|
31 | return "called" | |
35 |
|
32 | |||
36 |
|
33 | |||
37 | def run(tests): |
|
34 | def run(tests): | |
38 | """Loop through a list of (pre, post) inputs, where pre is the string |
|
35 | """Loop through a list of (pre, post) inputs, where pre is the string | |
39 | handed to ipython, and post is how that string looks after it's been |
|
36 | handed to ipython, and post is how that string looks after it's been | |
40 | transformed (i.e. ipython's notion of _i)""" |
|
37 | transformed (i.e. ipython's notion of _i)""" | |
41 | tt.check_pairs(ip.prefilter_manager.prefilter_lines, tests) |
|
38 | tt.check_pairs(ip.prefilter_manager.prefilter_lines, tests) | |
42 |
|
39 | |||
43 |
|
40 | |||
44 | def test_handlers(): |
|
41 | def test_handlers(): | |
45 | call_idx = CallableIndexable() |
|
42 | call_idx = CallableIndexable() | |
46 | ip.user_ns['call_idx'] = call_idx |
|
43 | ip.user_ns['call_idx'] = call_idx | |
47 |
|
44 | |||
48 | # For many of the below, we're also checking that leading whitespace |
|
45 | # For many of the below, we're also checking that leading whitespace | |
49 | # turns off the esc char, which it should unless there is a continuation |
|
46 | # turns off the esc char, which it should unless there is a continuation | |
50 | # line. |
|
47 | # line. | |
51 | run( |
|
48 | run( | |
52 | [('"no change"', '"no change"'), # normal |
|
49 | [('"no change"', '"no change"'), # normal | |
53 | (u"lsmagic", "get_ipython().run_line_magic('lsmagic', '')"), # magic |
|
50 | (u"lsmagic", "get_ipython().run_line_magic('lsmagic', '')"), # magic | |
54 | #("a = b # PYTHON-MODE", '_i'), # emacs -- avoids _in cache |
|
51 | #("a = b # PYTHON-MODE", '_i'), # emacs -- avoids _in cache | |
55 | ]) |
|
52 | ]) | |
56 |
|
53 | |||
57 | # Objects which are instances of IPyAutocall are *always* autocalled |
|
54 | # Objects which are instances of IPyAutocall are *always* autocalled | |
58 | autocallable = Autocallable() |
|
55 | autocallable = Autocallable() | |
59 | ip.user_ns['autocallable'] = autocallable |
|
56 | ip.user_ns['autocallable'] = autocallable | |
60 |
|
57 | |||
61 | # auto |
|
58 | # auto | |
62 | ip.magic('autocall 0') |
|
59 | ip.magic('autocall 0') | |
63 | # Only explicit escapes or instances of IPyAutocallable should get |
|
60 | # Only explicit escapes or instances of IPyAutocallable should get | |
64 | # expanded |
|
61 | # expanded | |
65 | run([ |
|
62 | run([ | |
66 | ('len "abc"', 'len "abc"'), |
|
63 | ('len "abc"', 'len "abc"'), | |
67 | ('autocallable', 'autocallable()'), |
|
64 | ('autocallable', 'autocallable()'), | |
68 | # Don't add extra brackets (gh-1117) |
|
65 | # Don't add extra brackets (gh-1117) | |
69 | ('autocallable()', 'autocallable()'), |
|
66 | ('autocallable()', 'autocallable()'), | |
70 | ]) |
|
67 | ]) | |
71 | ip.magic('autocall 1') |
|
68 | ip.magic('autocall 1') | |
72 | run([ |
|
69 | run([ | |
73 | ('len "abc"', 'len("abc")'), |
|
70 | ('len "abc"', 'len("abc")'), | |
74 | ('len "abc";', 'len("abc");'), # ; is special -- moves out of parens |
|
71 | ('len "abc";', 'len("abc");'), # ; is special -- moves out of parens | |
75 | # Autocall is turned off if first arg is [] and the object |
|
72 | # Autocall is turned off if first arg is [] and the object | |
76 | # is both callable and indexable. Like so: |
|
73 | # is both callable and indexable. Like so: | |
77 | ('len [1,2]', 'len([1,2])'), # len doesn't support __getitem__... |
|
74 | ('len [1,2]', 'len([1,2])'), # len doesn't support __getitem__... | |
78 | ('call_idx [1]', 'call_idx [1]'), # call_idx *does*.. |
|
75 | ('call_idx [1]', 'call_idx [1]'), # call_idx *does*.. | |
79 | ('call_idx 1', 'call_idx(1)'), |
|
76 | ('call_idx 1', 'call_idx(1)'), | |
80 | ('len', 'len'), # only at 2 does it auto-call on single args |
|
77 | ('len', 'len'), # only at 2 does it auto-call on single args | |
81 | ]) |
|
78 | ]) | |
82 | ip.magic('autocall 2') |
|
79 | ip.magic('autocall 2') | |
83 | run([ |
|
80 | run([ | |
84 | ('len "abc"', 'len("abc")'), |
|
81 | ('len "abc"', 'len("abc")'), | |
85 | ('len "abc";', 'len("abc");'), |
|
82 | ('len "abc";', 'len("abc");'), | |
86 | ('len [1,2]', 'len([1,2])'), |
|
83 | ('len [1,2]', 'len([1,2])'), | |
87 | ('call_idx [1]', 'call_idx [1]'), |
|
84 | ('call_idx [1]', 'call_idx [1]'), | |
88 | ('call_idx 1', 'call_idx(1)'), |
|
85 | ('call_idx 1', 'call_idx(1)'), | |
89 | # This is what's different: |
|
86 | # This is what's different: | |
90 | ('len', 'len()'), # only at 2 does it auto-call on single args |
|
87 | ('len', 'len()'), # only at 2 does it auto-call on single args | |
91 | ]) |
|
88 | ]) | |
92 | ip.magic('autocall 1') |
|
89 | ip.magic('autocall 1') | |
93 |
|
90 | |||
94 |
|
|
91 | assert failures == [] |
General Comments 0
You need to be logged in to leave comments.
Login now