Show More
@@ -1,119 +1,97 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.testing.globalipapp import get_ipython |
|
14 | 14 | from IPython.utils import py3compat |
|
15 | 15 | |
|
16 | 16 | #----------------------------------------------------------------------------- |
|
17 | 17 | # Globals |
|
18 | 18 | #----------------------------------------------------------------------------- |
|
19 | 19 | |
|
20 | 20 | # Get the public instance of IPython |
|
21 | 21 | ip = get_ipython() |
|
22 | 22 | |
|
23 | 23 | failures = [] |
|
24 | 24 | num_tests = 0 |
|
25 | 25 | |
|
26 | 26 | #----------------------------------------------------------------------------- |
|
27 | 27 | # Test functions |
|
28 | 28 | #----------------------------------------------------------------------------- |
|
29 | 29 | |
|
30 | 30 | class CallableIndexable(object): |
|
31 | 31 | def __getitem__(self, idx): return True |
|
32 | 32 | def __call__(self, *args, **kws): return True |
|
33 | 33 | |
|
34 | 34 | |
|
35 | 35 | class Autocallable(autocall.IPyAutocall): |
|
36 | 36 | def __call__(self): |
|
37 | 37 | return "called" |
|
38 | 38 | |
|
39 | 39 | |
|
40 | 40 | def run(tests): |
|
41 | 41 | """Loop through a list of (pre, post) inputs, where pre is the string |
|
42 | 42 | handed to ipython, and post is how that string looks after it's been |
|
43 | 43 | transformed (i.e. ipython's notion of _i)""" |
|
44 | 44 | tt.check_pairs(ip.prefilter_manager.prefilter_lines, tests) |
|
45 | 45 | |
|
46 | 46 | |
|
47 | 47 | def test_handlers(): |
|
48 | # alias expansion | |
|
49 | ||
|
50 | # We're using 'true' as our syscall of choice because it doesn't | |
|
51 | # write anything to stdout. | |
|
52 | ||
|
53 | # Turn off actual execution of aliases, because it's noisy | |
|
54 | old_system_cmd = ip.system | |
|
55 | ip.system = lambda cmd: None | |
|
56 | ||
|
57 | ||
|
58 | ip.alias_manager.alias_table['an_alias'] = (0, 'true') | |
|
59 | # These are useful for checking a particular recursive alias issue | |
|
60 | ip.alias_manager.alias_table['top'] = (0, 'd:/cygwin/top') | |
|
61 | ip.alias_manager.alias_table['d'] = (0, 'true') | |
|
62 | run([(i,py3compat.u_format(o)) for i,o in \ | |
|
63 | [("an_alias", "get_ipython().system({u}'true ')"), # alias | |
|
64 | # Below: recursive aliases should expand whitespace-surrounded | |
|
65 | # chars, *not* initial chars which happen to be aliases: | |
|
66 | ("top", "get_ipython().system({u}'d:/cygwin/top ')"), | |
|
67 | ]]) | |
|
68 | ip.system = old_system_cmd | |
|
69 | ||
|
70 | 48 | call_idx = CallableIndexable() |
|
71 | 49 | ip.user_ns['call_idx'] = call_idx |
|
72 | 50 | |
|
73 | 51 | # For many of the below, we're also checking that leading whitespace |
|
74 | 52 | # turns off the esc char, which it should unless there is a continuation |
|
75 | 53 | # line. |
|
76 | 54 | run([(i,py3compat.u_format(o)) for i,o in \ |
|
77 | 55 | [('"no change"', '"no change"'), # normal |
|
78 | 56 | (u"lsmagic", "get_ipython().magic({u}'lsmagic ')"), # magic |
|
79 | 57 | #("a = b # PYTHON-MODE", '_i'), # emacs -- avoids _in cache |
|
80 | 58 | ]]) |
|
81 | 59 | |
|
82 | 60 | # Objects which are instances of IPyAutocall are *always* autocalled |
|
83 | 61 | autocallable = Autocallable() |
|
84 | 62 | ip.user_ns['autocallable'] = autocallable |
|
85 | 63 | |
|
86 | 64 | # auto |
|
87 | 65 | ip.magic('autocall 0') |
|
88 | 66 | # Only explicit escapes or instances of IPyAutocallable should get |
|
89 | 67 | # expanded |
|
90 | 68 | run([ |
|
91 | 69 | ('len "abc"', 'len "abc"'), |
|
92 | 70 | ('autocallable', 'autocallable()'), |
|
93 | 71 | # Don't add extra brackets (gh-1117) |
|
94 | 72 | ('autocallable()', 'autocallable()'), |
|
95 | 73 | ]) |
|
96 | 74 | ip.magic('autocall 1') |
|
97 | 75 | run([ |
|
98 | 76 | ('len "abc"', 'len("abc")'), |
|
99 | 77 | ('len "abc";', 'len("abc");'), # ; is special -- moves out of parens |
|
100 | 78 | # Autocall is turned off if first arg is [] and the object |
|
101 | 79 | # is both callable and indexable. Like so: |
|
102 | 80 | ('len [1,2]', 'len([1,2])'), # len doesn't support __getitem__... |
|
103 | 81 | ('call_idx [1]', 'call_idx [1]'), # call_idx *does*.. |
|
104 | 82 | ('call_idx 1', 'call_idx(1)'), |
|
105 | 83 | ('len', 'len'), # only at 2 does it auto-call on single args |
|
106 | 84 | ]) |
|
107 | 85 | ip.magic('autocall 2') |
|
108 | 86 | run([ |
|
109 | 87 | ('len "abc"', 'len("abc")'), |
|
110 | 88 | ('len "abc";', 'len("abc");'), |
|
111 | 89 | ('len [1,2]', 'len([1,2])'), |
|
112 | 90 | ('call_idx [1]', 'call_idx [1]'), |
|
113 | 91 | ('call_idx 1', 'call_idx(1)'), |
|
114 | 92 | # This is what's different: |
|
115 | 93 | ('len', 'len()'), # only at 2 does it auto-call on single args |
|
116 | 94 | ]) |
|
117 | 95 | ip.magic('autocall 1') |
|
118 | 96 | |
|
119 | 97 | nt.assert_equal(failures, []) |
General Comments 0
You need to be logged in to leave comments.
Login now