##// END OF EJS Templates
Test exclude_regexp in AutocallChecker.
Bradley M. Froehle -
Show More
@@ -1,94 +1,110 b''
1 """Tests for input manipulation machinery."""
1 """Tests for input manipulation machinery."""
2
2
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Imports
4 # Imports
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 import nose.tools as nt
6 import nose.tools as nt
7
7
8 from IPython.core.prefilter import AutocallChecker
8 from IPython.testing import tools as tt, decorators as dec
9 from IPython.testing import tools as tt, decorators as dec
9 from IPython.testing.globalipapp import get_ipython
10 from IPython.testing.globalipapp import get_ipython
10
11
11 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
12 # Tests
13 # Tests
13 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
14 ip = get_ipython()
15 ip = get_ipython()
15
16
16 @dec.parametric
17 @dec.parametric
17 def test_prefilter():
18 def test_prefilter():
18 """Test user input conversions"""
19 """Test user input conversions"""
19
20
20 # pairs of (raw, expected correct) input
21 # pairs of (raw, expected correct) input
21 pairs = [ ('2+2','2+2'),
22 pairs = [ ('2+2','2+2'),
22 ('>>> 2+2','2+2'),
23 ('>>> 2+2','2+2'),
23 ('>>> # This is a comment\n'
24 ('>>> # This is a comment\n'
24 '... 2+2',
25 '... 2+2',
25 '# This is a comment\n'
26 '# This is a comment\n'
26 '2+2'),
27 '2+2'),
27 # Some IPython input
28 # Some IPython input
28 ('In [1]: 1', '1'),
29 ('In [1]: 1', '1'),
29 ('In [2]: for i in range(5):\n'
30 ('In [2]: for i in range(5):\n'
30 ' ...: print i,',
31 ' ...: print i,',
31 'for i in range(5):\n'
32 'for i in range(5):\n'
32 ' print i,'),
33 ' print i,'),
33 ]
34 ]
34
35
35 for raw, correct in pairs:
36 for raw, correct in pairs:
36 yield nt.assert_equals(ip.prefilter(raw), correct)
37 yield nt.assert_equals(ip.prefilter(raw), correct)
37
38
38
39
39 @dec.parametric
40 @dec.parametric
40 def test_autocall_binops():
41 def test_autocall_binops():
41 """See https://github.com/ipython/ipython/issues/81"""
42 """See https://github.com/ipython/ipython/issues/81"""
42 ip.magic('autocall 2')
43 ip.magic('autocall 2')
43 f = lambda x: x
44 f = lambda x: x
44 ip.user_ns['f'] = f
45 ip.user_ns['f'] = f
45 try:
46 try:
46 yield nt.assert_equals(ip.prefilter('f 1'),'f(1)')
47 yield nt.assert_equals(ip.prefilter('f 1'),'f(1)')
47 for t in ['f +1', 'f -1']:
48 for t in ['f +1', 'f -1']:
48 yield nt.assert_equals(ip.prefilter(t), t)
49 yield nt.assert_equals(ip.prefilter(t), t)
50
51 # Run tests again with a more permissive exclude_regexp, which will
52 # allow transformation of binary operations ('f -1' -> 'f(-1)').
53 pm = ip.prefilter_manager
54 ac = AutocallChecker(shell=pm.shell, prefilter_manager=pm,
55 config=pm.config)
56 try:
57 ac.priority = 1
58 ac.exclude_regexp = r'^[,&^\|\*/]|^is |^not |^in |^and |^or '
59 pm.sort_checkers()
60
61 yield nt.assert_equals(ip.prefilter('f -1'), 'f(-1)')
62 yield nt.assert_equals(ip.prefilter('f +1'), 'f(+1)')
63 finally:
64 pm.unregister_checker(ac)
49 finally:
65 finally:
50 ip.magic('autocall 0')
66 ip.magic('autocall 0')
51 del ip.user_ns['f']
67 del ip.user_ns['f']
52
68
53
69
54 @dec.parametric
70 @dec.parametric
55 def test_issue_114():
71 def test_issue_114():
56 """Check that multiline string literals don't expand as magic
72 """Check that multiline string literals don't expand as magic
57 see http://github.com/ipython/ipython/issues/114"""
73 see http://github.com/ipython/ipython/issues/114"""
58
74
59 template = '"""\n%s\n"""'
75 template = '"""\n%s\n"""'
60 # Store the current value of multi_line_specials and turn it off before
76 # Store the current value of multi_line_specials and turn it off before
61 # running test, since it could be true (case in which the test doesn't make
77 # running test, since it could be true (case in which the test doesn't make
62 # sense, as multiline string literals *will* expand as magic in that case).
78 # sense, as multiline string literals *will* expand as magic in that case).
63 msp = ip.prefilter_manager.multi_line_specials
79 msp = ip.prefilter_manager.multi_line_specials
64 ip.prefilter_manager.multi_line_specials = False
80 ip.prefilter_manager.multi_line_specials = False
65 try:
81 try:
66 for mgk in ip.lsmagic():
82 for mgk in ip.lsmagic():
67 raw = template % mgk
83 raw = template % mgk
68 yield nt.assert_equals(ip.prefilter(raw), raw)
84 yield nt.assert_equals(ip.prefilter(raw), raw)
69 finally:
85 finally:
70 ip.prefilter_manager.multi_line_specials = msp
86 ip.prefilter_manager.multi_line_specials = msp
71
87
72
88
73 def test_prefilter_attribute_errors():
89 def test_prefilter_attribute_errors():
74 """Capture exceptions thrown by user objects on attribute access.
90 """Capture exceptions thrown by user objects on attribute access.
75
91
76 See http://github.com/ipython/ipython/issues/988."""
92 See http://github.com/ipython/ipython/issues/988."""
77
93
78 class X(object):
94 class X(object):
79 def __getattr__(self, k):
95 def __getattr__(self, k):
80 raise ValueError('broken object')
96 raise ValueError('broken object')
81 def __call__(self, x):
97 def __call__(self, x):
82 return x
98 return x
83
99
84 # Create a callable broken object
100 # Create a callable broken object
85 ip.user_ns['x'] = X()
101 ip.user_ns['x'] = X()
86 ip.magic('autocall 2')
102 ip.magic('autocall 2')
87 try:
103 try:
88 # Even if x throws an attribute error when looking at its rewrite
104 # Even if x throws an attribute error when looking at its rewrite
89 # attribute, we should not crash. So the test here is simply making
105 # attribute, we should not crash. So the test here is simply making
90 # the prefilter call and not having an exception.
106 # the prefilter call and not having an exception.
91 ip.prefilter('x 1')
107 ip.prefilter('x 1')
92 finally:
108 finally:
93 del ip.user_ns['x']
109 del ip.user_ns['x']
94 ip.magic('autocall 0')
110 ip.magic('autocall 0')
General Comments 0
You need to be logged in to leave comments. Login now