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