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