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