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