Debian Codegraph @

tests/test_handlers.py

raw
1 """Tests for input handlers."""
2
3 # -----------------------------------------------------------------------------
4 # Module imports
5 # -----------------------------------------------------------------------------
6
7 # our own packages
8 from IPython.core import autocall
9 from IPython.testing import tools as tt
10 import pytest
11 from collections.abc import Callable
12
13 # -----------------------------------------------------------------------------
14 # Globals
15 # -----------------------------------------------------------------------------
16
17 # Test functions
18 # -----------------------------------------------------------------------------
19
20
21 class CallableIndexable(object):
22 def __getitem__(self, idx):
23 return True
24
25 def __call__(self, *args, **kws):
26 return True
27
28
29 class Autocallable(autocall.IPyAutocall):
30 def __call__(self):
31 return "called"
32
33
34 @pytest.mark.parametrize(
35 "autocall, input, output",
36 [
37 # For many of the below, we're also checking that leading whitespace
38 # turns off the esc char, which it should unless there is a continuation
39 # line.
40 ("1", '"no change"', '"no change"'), # normal
41 ("1", "lsmagic", "get_ipython().run_line_magic('lsmagic', '')"), # magic
42 # Only explicit escapes or instances of IPyAutocallable should get
43 # expanded
44 ("0", 'len "abc"', 'len "abc"'),
45 ("0", "autocallable", "autocallable()"),
46 # Don't add extra brackets (gh-1117)
47 ("0", "autocallable()", "autocallable()"),
48 ("1", 'len "abc"', 'len("abc")'),
49 ("1", 'len "abc";', 'len("abc");'), # ; is special -- moves out of parens
50 # Autocall is turned off if first arg is [] and the object
51 # is both callable and indexable. Like so:
52 ("1", "len [1,2]", "len([1,2])"), # len doesn't support __getitem__...
53 ("1", "call_idx [1]", "call_idx [1]"), # call_idx *does*..
54 ("1", "call_idx 1", "call_idx(1)"),
55 ("1", "len", "len"), # only at 2 does it auto-call on single args
56 ("2", 'len "abc"', 'len("abc")'),
57 ("2", 'len "abc";', 'len("abc");'),
58 ("2", "len [1,2]", "len([1,2])"),
59 ("2", "call_idx [1]", "call_idx [1]"),
60 ("2", "call_idx 1", "call_idx(1)"),
61 # T his is what's different:
62 ("2", "len", "len()"), # only at 2 does it auto-call on single args
63 ("0", "Callable[[int], None]", "Callable[[int], None]"),
64 ("1", "Callable[[int], None]", "Callable[[int], None]"),
65 ("1", "Callable[[int], None]", "Callable[[int], None]"),
66 ],
67 )
68 def test_handlers_I(autocall, input, output):
69 autocallable = Autocallable()
70 ip.user_ns["autocallable"] = autocallable
71
72 call_idx = CallableIndexable()
73 ip.user_ns["call_idx"] = call_idx
74
75 ip.user_ns["Callable"] = Callable
76
77 ip.run_line_magic("autocall", autocall)
78 assert ip.prefilter_manager.prefilter_lines(input) == output
79 ip.run_line_magic("autocall", "1")