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