Show More
@@ -1,489 +1,494 b'' | |||||
1 | import tokenize |
|
1 | import tokenize | |
2 | import nose.tools as nt |
|
2 | import nose.tools as nt | |
3 |
|
3 | |||
4 | from IPython.testing import tools as tt |
|
4 | from IPython.testing import tools as tt | |
5 | from IPython.utils import py3compat |
|
5 | from IPython.utils import py3compat | |
6 | u_fmt = py3compat.u_format |
|
6 | u_fmt = py3compat.u_format | |
7 |
|
7 | |||
8 | from IPython.core import inputtransformer as ipt |
|
8 | from IPython.core import inputtransformer as ipt | |
9 |
|
9 | |||
10 | def transform_and_reset(transformer): |
|
10 | def transform_and_reset(transformer): | |
11 | transformer = transformer() |
|
11 | transformer = transformer() | |
12 | def transform(inp): |
|
12 | def transform(inp): | |
13 | try: |
|
13 | try: | |
14 | return transformer.push(inp) |
|
14 | return transformer.push(inp) | |
15 | finally: |
|
15 | finally: | |
16 | transformer.reset() |
|
16 | transformer.reset() | |
17 |
|
17 | |||
18 | return transform |
|
18 | return transform | |
19 |
|
19 | |||
20 | # Transformer tests |
|
20 | # Transformer tests | |
21 | def transform_checker(tests, transformer, **kwargs): |
|
21 | def transform_checker(tests, transformer, **kwargs): | |
22 | """Utility to loop over test inputs""" |
|
22 | """Utility to loop over test inputs""" | |
23 | transformer = transformer(**kwargs) |
|
23 | transformer = transformer(**kwargs) | |
24 | try: |
|
24 | try: | |
25 | for inp, tr in tests: |
|
25 | for inp, tr in tests: | |
26 | if inp is None: |
|
26 | if inp is None: | |
27 | out = transformer.reset() |
|
27 | out = transformer.reset() | |
28 | else: |
|
28 | else: | |
29 | out = transformer.push(inp) |
|
29 | out = transformer.push(inp) | |
30 | nt.assert_equal(out, tr) |
|
30 | nt.assert_equal(out, tr) | |
31 | finally: |
|
31 | finally: | |
32 | transformer.reset() |
|
32 | transformer.reset() | |
33 |
|
33 | |||
34 | # Data for all the syntax tests in the form of lists of pairs of |
|
34 | # Data for all the syntax tests in the form of lists of pairs of | |
35 | # raw/transformed input. We store it here as a global dict so that we can use |
|
35 | # raw/transformed input. We store it here as a global dict so that we can use | |
36 | # it both within single-function tests and also to validate the behavior of the |
|
36 | # it both within single-function tests and also to validate the behavior of the | |
37 | # larger objects |
|
37 | # larger objects | |
38 |
|
38 | |||
39 | syntax = \ |
|
39 | syntax = \ | |
40 | dict(assign_system = |
|
40 | dict(assign_system = | |
41 | [(i,py3compat.u_format(o)) for i,o in \ |
|
41 | [(i,py3compat.u_format(o)) for i,o in \ | |
42 | [(u'a =! ls', "a = get_ipython().getoutput({u}'ls')"), |
|
42 | [(u'a =! ls', "a = get_ipython().getoutput({u}'ls')"), | |
43 | (u'b = !ls', "b = get_ipython().getoutput({u}'ls')"), |
|
43 | (u'b = !ls', "b = get_ipython().getoutput({u}'ls')"), | |
44 | (u'c= !ls', "c = get_ipython().getoutput({u}'ls')"), |
|
44 | (u'c= !ls', "c = get_ipython().getoutput({u}'ls')"), | |
45 | (u'd == !ls', u'd == !ls'), # Invalid syntax, but we leave == alone. |
|
45 | (u'd == !ls', u'd == !ls'), # Invalid syntax, but we leave == alone. | |
46 | ('x=1', 'x=1'), # normal input is unmodified |
|
46 | ('x=1', 'x=1'), # normal input is unmodified | |
47 | (' ',' '), # blank lines are kept intact |
|
47 | (' ',' '), # blank lines are kept intact | |
48 | # Tuple unpacking |
|
48 | # Tuple unpacking | |
49 | (u"a, b = !echo 'a\\nb'", u"a, b = get_ipython().getoutput({u}\"echo 'a\\\\nb'\")"), |
|
49 | (u"a, b = !echo 'a\\nb'", u"a, b = get_ipython().getoutput({u}\"echo 'a\\\\nb'\")"), | |
50 | (u"a,= !echo 'a'", u"a, = get_ipython().getoutput({u}\"echo 'a'\")"), |
|
50 | (u"a,= !echo 'a'", u"a, = get_ipython().getoutput({u}\"echo 'a'\")"), | |
51 | (u"a, *bc = !echo 'a\\nb\\nc'", u"a, *bc = get_ipython().getoutput({u}\"echo 'a\\\\nb\\\\nc'\")"), |
|
51 | (u"a, *bc = !echo 'a\\nb\\nc'", u"a, *bc = get_ipython().getoutput({u}\"echo 'a\\\\nb\\\\nc'\")"), | |
52 | # Tuple unpacking with regular Python expressions, not our syntax. |
|
52 | # Tuple unpacking with regular Python expressions, not our syntax. | |
53 | (u"a, b = range(2)", u"a, b = range(2)"), |
|
53 | (u"a, b = range(2)", u"a, b = range(2)"), | |
54 | (u"a, = range(1)", u"a, = range(1)"), |
|
54 | (u"a, = range(1)", u"a, = range(1)"), | |
55 | (u"a, *bc = range(3)", u"a, *bc = range(3)"), |
|
55 | (u"a, *bc = range(3)", u"a, *bc = range(3)"), | |
56 | ]], |
|
56 | ]], | |
57 |
|
57 | |||
58 | assign_magic = |
|
58 | assign_magic = | |
59 | [(i,py3compat.u_format(o)) for i,o in \ |
|
59 | [(i,py3compat.u_format(o)) for i,o in \ | |
60 | [(u'a =% who', "a = get_ipython().magic({u}'who')"), |
|
60 | [(u'a =% who', "a = get_ipython().magic({u}'who')"), | |
61 | (u'b = %who', "b = get_ipython().magic({u}'who')"), |
|
61 | (u'b = %who', "b = get_ipython().magic({u}'who')"), | |
62 | (u'c= %ls', "c = get_ipython().magic({u}'ls')"), |
|
62 | (u'c= %ls', "c = get_ipython().magic({u}'ls')"), | |
63 | (u'd == %ls', u'd == %ls'), # Invalid syntax, but we leave == alone. |
|
63 | (u'd == %ls', u'd == %ls'), # Invalid syntax, but we leave == alone. | |
64 | ('x=1', 'x=1'), # normal input is unmodified |
|
64 | ('x=1', 'x=1'), # normal input is unmodified | |
65 | (' ',' '), # blank lines are kept intact |
|
65 | (' ',' '), # blank lines are kept intact | |
66 | (u"a, b = %foo", u"a, b = get_ipython().magic({u}'foo')"), |
|
66 | (u"a, b = %foo", u"a, b = get_ipython().magic({u}'foo')"), | |
67 | ]], |
|
67 | ]], | |
68 |
|
68 | |||
69 | classic_prompt = |
|
69 | classic_prompt = | |
70 | [('>>> x=1', 'x=1'), |
|
70 | [('>>> x=1', 'x=1'), | |
71 | ('x=1', 'x=1'), # normal input is unmodified |
|
71 | ('x=1', 'x=1'), # normal input is unmodified | |
72 | (' ', ' '), # blank lines are kept intact |
|
72 | (' ', ' '), # blank lines are kept intact | |
73 | ], |
|
73 | ], | |
74 |
|
74 | |||
75 | ipy_prompt = |
|
75 | ipy_prompt = | |
76 | [('In [1]: x=1', 'x=1'), |
|
76 | [('In [1]: x=1', 'x=1'), | |
77 | ('x=1', 'x=1'), # normal input is unmodified |
|
77 | ('x=1', 'x=1'), # normal input is unmodified | |
78 | (' ',' '), # blank lines are kept intact |
|
78 | (' ',' '), # blank lines are kept intact | |
79 | ], |
|
79 | ], | |
80 |
|
80 | |||
81 | # Tests for the escape transformer to leave normal code alone |
|
81 | # Tests for the escape transformer to leave normal code alone | |
82 | escaped_noesc = |
|
82 | escaped_noesc = | |
83 | [ (' ', ' '), |
|
83 | [ (' ', ' '), | |
84 | ('x=1', 'x=1'), |
|
84 | ('x=1', 'x=1'), | |
85 | ], |
|
85 | ], | |
86 |
|
86 | |||
87 | # System calls |
|
87 | # System calls | |
88 | escaped_shell = |
|
88 | escaped_shell = | |
89 | [(i,py3compat.u_format(o)) for i,o in \ |
|
89 | [(i,py3compat.u_format(o)) for i,o in \ | |
90 | [ (u'!ls', "get_ipython().system({u}'ls')"), |
|
90 | [ (u'!ls', "get_ipython().system({u}'ls')"), | |
91 | # Double-escape shell, this means to capture the output of the |
|
91 | # Double-escape shell, this means to capture the output of the | |
92 | # subprocess and return it |
|
92 | # subprocess and return it | |
93 | (u'!!ls', "get_ipython().getoutput({u}'ls')"), |
|
93 | (u'!!ls', "get_ipython().getoutput({u}'ls')"), | |
94 | ]], |
|
94 | ]], | |
95 |
|
95 | |||
96 | # Help/object info |
|
96 | # Help/object info | |
97 | escaped_help = |
|
97 | escaped_help = | |
98 | [(i,py3compat.u_format(o)) for i,o in \ |
|
98 | [(i,py3compat.u_format(o)) for i,o in \ | |
99 | [ (u'?', 'get_ipython().show_usage()'), |
|
99 | [ (u'?', 'get_ipython().show_usage()'), | |
100 | (u'?x1', "get_ipython().magic({u}'pinfo x1')"), |
|
100 | (u'?x1', "get_ipython().magic({u}'pinfo x1')"), | |
101 | (u'??x2', "get_ipython().magic({u}'pinfo2 x2')"), |
|
101 | (u'??x2', "get_ipython().magic({u}'pinfo2 x2')"), | |
102 | (u'?a.*s', "get_ipython().magic({u}'psearch a.*s')"), |
|
102 | (u'?a.*s', "get_ipython().magic({u}'psearch a.*s')"), | |
103 | (u'?%hist1', "get_ipython().magic({u}'pinfo %hist1')"), |
|
103 | (u'?%hist1', "get_ipython().magic({u}'pinfo %hist1')"), | |
104 | (u'?%%hist2', "get_ipython().magic({u}'pinfo %%hist2')"), |
|
104 | (u'?%%hist2', "get_ipython().magic({u}'pinfo %%hist2')"), | |
105 | (u'?abc = qwe', "get_ipython().magic({u}'pinfo abc')"), |
|
105 | (u'?abc = qwe', "get_ipython().magic({u}'pinfo abc')"), | |
106 | ]], |
|
106 | ]], | |
107 |
|
107 | |||
108 | end_help = |
|
108 | end_help = | |
109 | [(i,py3compat.u_format(o)) for i,o in \ |
|
109 | [(i,py3compat.u_format(o)) for i,o in \ | |
110 | [ (u'x3?', "get_ipython().magic({u}'pinfo x3')"), |
|
110 | [ (u'x3?', "get_ipython().magic({u}'pinfo x3')"), | |
111 | (u'x4??', "get_ipython().magic({u}'pinfo2 x4')"), |
|
111 | (u'x4??', "get_ipython().magic({u}'pinfo2 x4')"), | |
112 | (u'%hist1?', "get_ipython().magic({u}'pinfo %hist1')"), |
|
112 | (u'%hist1?', "get_ipython().magic({u}'pinfo %hist1')"), | |
113 | (u'%hist2??', "get_ipython().magic({u}'pinfo2 %hist2')"), |
|
113 | (u'%hist2??', "get_ipython().magic({u}'pinfo2 %hist2')"), | |
114 | (u'%%hist3?', "get_ipython().magic({u}'pinfo %%hist3')"), |
|
114 | (u'%%hist3?', "get_ipython().magic({u}'pinfo %%hist3')"), | |
115 | (u'%%hist4??', "get_ipython().magic({u}'pinfo2 %%hist4')"), |
|
115 | (u'%%hist4??', "get_ipython().magic({u}'pinfo2 %%hist4')"), | |
116 | (u'f*?', "get_ipython().magic({u}'psearch f*')"), |
|
116 | (u'f*?', "get_ipython().magic({u}'psearch f*')"), | |
117 | (u'ax.*aspe*?', "get_ipython().magic({u}'psearch ax.*aspe*')"), |
|
117 | (u'ax.*aspe*?', "get_ipython().magic({u}'psearch ax.*aspe*')"), | |
118 | (u'a = abc?', "get_ipython().set_next_input({u}'a = abc');" |
|
118 | (u'a = abc?', "get_ipython().set_next_input({u}'a = abc');" | |
119 | "get_ipython().magic({u}'pinfo abc')"), |
|
119 | "get_ipython().magic({u}'pinfo abc')"), | |
120 | (u'a = abc.qe??', "get_ipython().set_next_input({u}'a = abc.qe');" |
|
120 | (u'a = abc.qe??', "get_ipython().set_next_input({u}'a = abc.qe');" | |
121 | "get_ipython().magic({u}'pinfo2 abc.qe')"), |
|
121 | "get_ipython().magic({u}'pinfo2 abc.qe')"), | |
122 | (u'a = *.items?', "get_ipython().set_next_input({u}'a = *.items');" |
|
122 | (u'a = *.items?', "get_ipython().set_next_input({u}'a = *.items');" | |
123 | "get_ipython().magic({u}'psearch *.items')"), |
|
123 | "get_ipython().magic({u}'psearch *.items')"), | |
124 | (u'plot(a?', "get_ipython().set_next_input({u}'plot(a');" |
|
124 | (u'plot(a?', "get_ipython().set_next_input({u}'plot(a');" | |
125 | "get_ipython().magic({u}'pinfo a')"), |
|
125 | "get_ipython().magic({u}'pinfo a')"), | |
126 | (u'a*2 #comment?', 'a*2 #comment?'), |
|
126 | (u'a*2 #comment?', 'a*2 #comment?'), | |
127 | ]], |
|
127 | ]], | |
128 |
|
128 | |||
129 | # Explicit magic calls |
|
129 | # Explicit magic calls | |
130 | escaped_magic = |
|
130 | escaped_magic = | |
131 | [(i,py3compat.u_format(o)) for i,o in \ |
|
131 | [(i,py3compat.u_format(o)) for i,o in \ | |
132 | [ (u'%cd', "get_ipython().magic({u}'cd')"), |
|
132 | [ (u'%cd', "get_ipython().magic({u}'cd')"), | |
133 | (u'%cd /home', "get_ipython().magic({u}'cd /home')"), |
|
133 | (u'%cd /home', "get_ipython().magic({u}'cd /home')"), | |
134 | # Backslashes need to be escaped. |
|
134 | # Backslashes need to be escaped. | |
135 | (u'%cd C:\\User', "get_ipython().magic({u}'cd C:\\\\User')"), |
|
135 | (u'%cd C:\\User', "get_ipython().magic({u}'cd C:\\\\User')"), | |
136 | (u' %magic', " get_ipython().magic({u}'magic')"), |
|
136 | (u' %magic', " get_ipython().magic({u}'magic')"), | |
137 | ]], |
|
137 | ]], | |
138 |
|
138 | |||
139 | # Quoting with separate arguments |
|
139 | # Quoting with separate arguments | |
140 | escaped_quote = |
|
140 | escaped_quote = | |
141 | [ (',f', 'f("")'), |
|
141 | [ (',f', 'f("")'), | |
142 | (',f x', 'f("x")'), |
|
142 | (',f x', 'f("x")'), | |
143 | (' ,f y', ' f("y")'), |
|
143 | (' ,f y', ' f("y")'), | |
144 | (',f a b', 'f("a", "b")'), |
|
144 | (',f a b', 'f("a", "b")'), | |
145 | ], |
|
145 | ], | |
146 |
|
146 | |||
147 | # Quoting with single argument |
|
147 | # Quoting with single argument | |
148 | escaped_quote2 = |
|
148 | escaped_quote2 = | |
149 | [ (';f', 'f("")'), |
|
149 | [ (';f', 'f("")'), | |
150 | (';f x', 'f("x")'), |
|
150 | (';f x', 'f("x")'), | |
151 | (' ;f y', ' f("y")'), |
|
151 | (' ;f y', ' f("y")'), | |
152 | (';f a b', 'f("a b")'), |
|
152 | (';f a b', 'f("a b")'), | |
153 | ], |
|
153 | ], | |
154 |
|
154 | |||
155 | # Simply apply parens |
|
155 | # Simply apply parens | |
156 | escaped_paren = |
|
156 | escaped_paren = | |
157 | [ ('/f', 'f()'), |
|
157 | [ ('/f', 'f()'), | |
158 | ('/f x', 'f(x)'), |
|
158 | ('/f x', 'f(x)'), | |
159 | (' /f y', ' f(y)'), |
|
159 | (' /f y', ' f(y)'), | |
160 | ('/f a b', 'f(a, b)'), |
|
160 | ('/f a b', 'f(a, b)'), | |
161 | ], |
|
161 | ], | |
162 |
|
162 | |||
163 | # Check that we transform prompts before other transforms |
|
163 | # Check that we transform prompts before other transforms | |
164 | mixed = |
|
164 | mixed = | |
165 | [(i,py3compat.u_format(o)) for i,o in \ |
|
165 | [(i,py3compat.u_format(o)) for i,o in \ | |
166 | [ (u'In [1]: %lsmagic', "get_ipython().magic({u}'lsmagic')"), |
|
166 | [ (u'In [1]: %lsmagic', "get_ipython().magic({u}'lsmagic')"), | |
167 | (u'>>> %lsmagic', "get_ipython().magic({u}'lsmagic')"), |
|
167 | (u'>>> %lsmagic', "get_ipython().magic({u}'lsmagic')"), | |
168 | (u'In [2]: !ls', "get_ipython().system({u}'ls')"), |
|
168 | (u'In [2]: !ls', "get_ipython().system({u}'ls')"), | |
169 | (u'In [3]: abs?', "get_ipython().magic({u}'pinfo abs')"), |
|
169 | (u'In [3]: abs?', "get_ipython().magic({u}'pinfo abs')"), | |
170 | (u'In [4]: b = %who', "b = get_ipython().magic({u}'who')"), |
|
170 | (u'In [4]: b = %who', "b = get_ipython().magic({u}'who')"), | |
171 | ]], |
|
171 | ]], | |
172 | ) |
|
172 | ) | |
173 |
|
173 | |||
174 | # multiline syntax examples. Each of these should be a list of lists, with |
|
174 | # multiline syntax examples. Each of these should be a list of lists, with | |
175 | # each entry itself having pairs of raw/transformed input. The union (with |
|
175 | # each entry itself having pairs of raw/transformed input. The union (with | |
176 | # '\n'.join() of the transformed inputs is what the splitter should produce |
|
176 | # '\n'.join() of the transformed inputs is what the splitter should produce | |
177 | # when fed the raw lines one at a time via push. |
|
177 | # when fed the raw lines one at a time via push. | |
178 | syntax_ml = \ |
|
178 | syntax_ml = \ | |
179 | dict(classic_prompt = |
|
179 | dict(classic_prompt = | |
180 | [ [('>>> for i in range(10):','for i in range(10):'), |
|
180 | [ [('>>> for i in range(10):','for i in range(10):'), | |
181 | ('... print i',' print i'), |
|
181 | ('... print i',' print i'), | |
182 | ('... ', ''), |
|
182 | ('... ', ''), | |
183 | ], |
|
183 | ], | |
184 | [('>>> a="""','a="""'), |
|
184 | [('>>> a="""','a="""'), | |
185 | ('... 123"""','123"""'), |
|
185 | ('... 123"""','123"""'), | |
186 | ], |
|
186 | ], | |
187 | [('a="""','a="""'), |
|
187 | [('a="""','a="""'), | |
188 | ('... 123','123'), |
|
188 | ('... 123','123'), | |
189 | ('... 456"""','456"""'), |
|
189 | ('... 456"""','456"""'), | |
190 | ], |
|
190 | ], | |
191 | [('a="""','a="""'), |
|
191 | [('a="""','a="""'), | |
192 | ('>>> 123','123'), |
|
192 | ('>>> 123','123'), | |
193 | ('... 456"""','456"""'), |
|
193 | ('... 456"""','456"""'), | |
194 | ], |
|
194 | ], | |
195 | [('a="""','a="""'), |
|
195 | [('a="""','a="""'), | |
196 | ('123','123'), |
|
196 | ('123','123'), | |
197 | ('... 456"""','... 456"""'), |
|
197 | ('... 456"""','... 456"""'), | |
198 | ], |
|
198 | ], | |
199 | [('....__class__','....__class__'), |
|
199 | [('....__class__','....__class__'), | |
200 | ], |
|
200 | ], | |
201 | [('a=5', 'a=5'), |
|
201 | [('a=5', 'a=5'), | |
202 | ('...', ''), |
|
202 | ('...', ''), | |
203 | ], |
|
203 | ], | |
204 | [('>>> def f(x):', 'def f(x):'), |
|
204 | [('>>> def f(x):', 'def f(x):'), | |
205 | ('...', ''), |
|
205 | ('...', ''), | |
206 | ('... return x', ' return x'), |
|
206 | ('... return x', ' return x'), | |
207 | ], |
|
207 | ], | |
208 | [('board = """....', 'board = """....'), |
|
208 | [('board = """....', 'board = """....'), | |
209 | ('....', '....'), |
|
209 | ('....', '....'), | |
210 | ('...."""', '...."""'), |
|
210 | ('...."""', '...."""'), | |
211 | ], |
|
211 | ], | |
212 | ], |
|
212 | ], | |
213 |
|
213 | |||
214 | ipy_prompt = |
|
214 | ipy_prompt = | |
215 | [ [('In [24]: for i in range(10):','for i in range(10):'), |
|
215 | [ [('In [24]: for i in range(10):','for i in range(10):'), | |
216 | (' ....: print i',' print i'), |
|
216 | (' ....: print i',' print i'), | |
217 | (' ....: ', ''), |
|
217 | (' ....: ', ''), | |
218 | ], |
|
218 | ], | |
219 | [('In [24]: for i in range(10):','for i in range(10):'), |
|
219 | [('In [24]: for i in range(10):','for i in range(10):'), | |
220 | # Qt console prompts expand with spaces, not dots |
|
220 | # Qt console prompts expand with spaces, not dots | |
221 | (' ...: print i',' print i'), |
|
221 | (' ...: print i',' print i'), | |
222 | (' ...: ', ''), |
|
222 | (' ...: ', ''), | |
223 | ], |
|
223 | ], | |
224 | [('In [24]: for i in range(10):','for i in range(10):'), |
|
224 | [('In [24]: for i in range(10):','for i in range(10):'), | |
225 | # Sometimes whitespace preceding '...' has been removed |
|
225 | # Sometimes whitespace preceding '...' has been removed | |
226 | ('...: print i',' print i'), |
|
226 | ('...: print i',' print i'), | |
227 | ('...: ', ''), |
|
227 | ('...: ', ''), | |
228 | ], |
|
228 | ], | |
229 | [('In [24]: for i in range(10):','for i in range(10):'), |
|
229 | [('In [24]: for i in range(10):','for i in range(10):'), | |
230 | # Space after last continuation prompt has been removed (issue #6674) |
|
230 | # Space after last continuation prompt has been removed (issue #6674) | |
231 | ('...: print i',' print i'), |
|
231 | ('...: print i',' print i'), | |
232 | ('...:', ''), |
|
232 | ('...:', ''), | |
233 | ], |
|
233 | ], | |
234 | [('In [2]: a="""','a="""'), |
|
234 | [('In [2]: a="""','a="""'), | |
235 | (' ...: 123"""','123"""'), |
|
235 | (' ...: 123"""','123"""'), | |
236 | ], |
|
236 | ], | |
237 | [('a="""','a="""'), |
|
237 | [('a="""','a="""'), | |
238 | (' ...: 123','123'), |
|
238 | (' ...: 123','123'), | |
239 | (' ...: 456"""','456"""'), |
|
239 | (' ...: 456"""','456"""'), | |
240 | ], |
|
240 | ], | |
241 | [('a="""','a="""'), |
|
241 | [('a="""','a="""'), | |
242 | ('In [1]: 123','123'), |
|
242 | ('In [1]: 123','123'), | |
243 | (' ...: 456"""','456"""'), |
|
243 | (' ...: 456"""','456"""'), | |
244 | ], |
|
244 | ], | |
245 | [('a="""','a="""'), |
|
245 | [('a="""','a="""'), | |
246 | ('123','123'), |
|
246 | ('123','123'), | |
247 | (' ...: 456"""',' ...: 456"""'), |
|
247 | (' ...: 456"""',' ...: 456"""'), | |
248 | ], |
|
248 | ], | |
249 | ], |
|
249 | ], | |
250 |
|
250 | |||
251 | multiline_datastructure_prompt = |
|
251 | multiline_datastructure_prompt = | |
252 | [ [('>>> a = [1,','a = [1,'), |
|
252 | [ [('>>> a = [1,','a = [1,'), | |
253 | ('... 2]','2]'), |
|
253 | ('... 2]','2]'), | |
254 | ], |
|
254 | ], | |
255 | ], |
|
255 | ], | |
256 |
|
256 | |||
257 | multiline_datastructure = |
|
257 | multiline_datastructure = | |
258 | [ [('b = ("%s"', None), |
|
258 | [ [('b = ("%s"', None), | |
259 | ('# comment', None), |
|
259 | ('# comment', None), | |
260 | ('%foo )', 'b = ("%s"\n# comment\n%foo )'), |
|
260 | ('%foo )', 'b = ("%s"\n# comment\n%foo )'), | |
261 | ], |
|
261 | ], | |
262 | ], |
|
262 | ], | |
263 |
|
263 | |||
264 | multiline_string = |
|
264 | multiline_string = | |
265 | [ [("'''foo?", None), |
|
265 | [ [("'''foo?", None), | |
266 | ("bar'''", "'''foo?\nbar'''"), |
|
266 | ("bar'''", "'''foo?\nbar'''"), | |
267 | ], |
|
267 | ], | |
268 | ], |
|
268 | ], | |
269 |
|
269 | |||
270 | leading_indent = |
|
270 | leading_indent = | |
271 | [ [(' print "hi"','print "hi"'), |
|
271 | [ [(' print "hi"','print "hi"'), | |
272 | ], |
|
272 | ], | |
273 | [(' for a in range(5):','for a in range(5):'), |
|
273 | [(' for a in range(5):','for a in range(5):'), | |
274 | (' a*2',' a*2'), |
|
274 | (' a*2',' a*2'), | |
275 | ], |
|
275 | ], | |
276 | [(' a="""','a="""'), |
|
276 | [(' a="""','a="""'), | |
277 | (' 123"""','123"""'), |
|
277 | (' 123"""','123"""'), | |
278 | ], |
|
278 | ], | |
279 | [('a="""','a="""'), |
|
279 | [('a="""','a="""'), | |
280 | (' 123"""',' 123"""'), |
|
280 | (' 123"""',' 123"""'), | |
281 | ], |
|
281 | ], | |
282 | ], |
|
282 | ], | |
283 |
|
283 | |||
284 | cellmagic = |
|
284 | cellmagic = | |
285 | [ [(u'%%foo a', None), |
|
285 | [ [(u'%%foo a', None), | |
286 | (None, u_fmt("get_ipython().run_cell_magic({u}'foo', {u}'a', {u}'')")), |
|
286 | (None, u_fmt("get_ipython().run_cell_magic({u}'foo', {u}'a', {u}'')")), | |
287 | ], |
|
287 | ], | |
288 | [(u'%%bar 123', None), |
|
288 | [(u'%%bar 123', None), | |
289 | (u'hello', None), |
|
289 | (u'hello', None), | |
290 | (None , u_fmt("get_ipython().run_cell_magic({u}'bar', {u}'123', {u}'hello')")), |
|
290 | (None , u_fmt("get_ipython().run_cell_magic({u}'bar', {u}'123', {u}'hello')")), | |
291 | ], |
|
291 | ], | |
292 | [(u'a=5', 'a=5'), |
|
292 | [(u'a=5', 'a=5'), | |
293 | (u'%%cellmagic', '%%cellmagic'), |
|
293 | (u'%%cellmagic', '%%cellmagic'), | |
294 | ], |
|
294 | ], | |
295 | ], |
|
295 | ], | |
296 |
|
296 | |||
297 | escaped = |
|
297 | escaped = | |
298 | [ [('%abc def \\', None), |
|
298 | [ [('%abc def \\', None), | |
299 | ('ghi', u_fmt("get_ipython().magic({u}'abc def ghi')")), |
|
299 | ('ghi', u_fmt("get_ipython().magic({u}'abc def ghi')")), | |
300 | ], |
|
300 | ], | |
301 | [('%abc def \\', None), |
|
301 | [('%abc def \\', None), | |
302 | ('ghi\\', None), |
|
302 | ('ghi\\', None), | |
303 | (None, u_fmt("get_ipython().magic({u}'abc def ghi')")), |
|
303 | (None, u_fmt("get_ipython().magic({u}'abc def ghi')")), | |
304 | ], |
|
304 | ], | |
305 | ], |
|
305 | ], | |
306 |
|
306 | |||
307 | assign_magic = |
|
307 | assign_magic = | |
308 | [ [(u'a = %bc de \\', None), |
|
308 | [ [(u'a = %bc de \\', None), | |
309 | (u'fg', u_fmt("a = get_ipython().magic({u}'bc de fg')")), |
|
309 | (u'fg', u_fmt("a = get_ipython().magic({u}'bc de fg')")), | |
310 | ], |
|
310 | ], | |
311 | [(u'a = %bc de \\', None), |
|
311 | [(u'a = %bc de \\', None), | |
312 | (u'fg\\', None), |
|
312 | (u'fg\\', None), | |
313 | (None, u_fmt("a = get_ipython().magic({u}'bc de fg')")), |
|
313 | (None, u_fmt("a = get_ipython().magic({u}'bc de fg')")), | |
314 | ], |
|
314 | ], | |
315 | ], |
|
315 | ], | |
316 |
|
316 | |||
317 | assign_system = |
|
317 | assign_system = | |
318 | [ [(u'a = !bc de \\', None), |
|
318 | [ [(u'a = !bc de \\', None), | |
319 | (u'fg', u_fmt("a = get_ipython().getoutput({u}'bc de fg')")), |
|
319 | (u'fg', u_fmt("a = get_ipython().getoutput({u}'bc de fg')")), | |
320 | ], |
|
320 | ], | |
321 | [(u'a = !bc de \\', None), |
|
321 | [(u'a = !bc de \\', None), | |
322 | (u'fg\\', None), |
|
322 | (u'fg\\', None), | |
323 | (None, u_fmt("a = get_ipython().getoutput({u}'bc de fg')")), |
|
323 | (None, u_fmt("a = get_ipython().getoutput({u}'bc de fg')")), | |
324 | ], |
|
324 | ], | |
325 | ], |
|
325 | ], | |
326 | ) |
|
326 | ) | |
327 |
|
327 | |||
328 |
|
328 | |||
329 | def test_assign_system(): |
|
329 | def test_assign_system(): | |
330 | tt.check_pairs(transform_and_reset(ipt.assign_from_system), syntax['assign_system']) |
|
330 | tt.check_pairs(transform_and_reset(ipt.assign_from_system), syntax['assign_system']) | |
331 |
|
331 | |||
332 | def test_assign_magic(): |
|
332 | def test_assign_magic(): | |
333 | tt.check_pairs(transform_and_reset(ipt.assign_from_magic), syntax['assign_magic']) |
|
333 | tt.check_pairs(transform_and_reset(ipt.assign_from_magic), syntax['assign_magic']) | |
334 |
|
334 | |||
335 | def test_classic_prompt(): |
|
335 | def test_classic_prompt(): | |
336 | tt.check_pairs(transform_and_reset(ipt.classic_prompt), syntax['classic_prompt']) |
|
336 | tt.check_pairs(transform_and_reset(ipt.classic_prompt), syntax['classic_prompt']) | |
337 | for example in syntax_ml['classic_prompt']: |
|
337 | for example in syntax_ml['classic_prompt']: | |
338 | transform_checker(example, ipt.classic_prompt) |
|
338 | transform_checker(example, ipt.classic_prompt) | |
339 | for example in syntax_ml['multiline_datastructure_prompt']: |
|
339 | for example in syntax_ml['multiline_datastructure_prompt']: | |
340 | transform_checker(example, ipt.classic_prompt) |
|
340 | transform_checker(example, ipt.classic_prompt) | |
341 |
|
341 | |||
342 | # Check that we don't transform the second line if the first is obviously |
|
342 | # Check that we don't transform the second line if the first is obviously | |
343 | # IPython syntax |
|
343 | # IPython syntax | |
344 | transform_checker([ |
|
344 | transform_checker([ | |
345 | (u'%foo', '%foo'), |
|
345 | (u'%foo', '%foo'), | |
346 | (u'>>> bar', '>>> bar'), |
|
346 | (u'>>> bar', '>>> bar'), | |
347 | ], ipt.classic_prompt) |
|
347 | ], ipt.classic_prompt) | |
348 |
|
348 | |||
349 |
|
349 | |||
350 | def test_ipy_prompt(): |
|
350 | def test_ipy_prompt(): | |
351 | tt.check_pairs(transform_and_reset(ipt.ipy_prompt), syntax['ipy_prompt']) |
|
351 | tt.check_pairs(transform_and_reset(ipt.ipy_prompt), syntax['ipy_prompt']) | |
352 | for example in syntax_ml['ipy_prompt']: |
|
352 | for example in syntax_ml['ipy_prompt']: | |
353 | transform_checker(example, ipt.ipy_prompt) |
|
353 | transform_checker(example, ipt.ipy_prompt) | |
354 |
|
354 | |||
355 | # Check that we don't transform the second line if we're inside a cell magic |
|
355 | # Check that we don't transform the second line if we're inside a cell magic | |
356 | transform_checker([ |
|
356 | transform_checker([ | |
357 | (u'%%foo', '%%foo'), |
|
357 | (u'%%foo', '%%foo'), | |
358 | (u'In [1]: bar', 'In [1]: bar'), |
|
358 | (u'In [1]: bar', 'In [1]: bar'), | |
359 | ], ipt.ipy_prompt) |
|
359 | ], ipt.ipy_prompt) | |
360 |
|
360 | |||
361 | def test_assemble_logical_lines(): |
|
361 | def test_assemble_logical_lines(): | |
362 | tests = \ |
|
362 | tests = \ | |
363 | [ [(u"a = \\", None), |
|
363 | [ [(u"a = \\", None), | |
364 | (u"123", u"a = 123"), |
|
364 | (u"123", u"a = 123"), | |
365 | ], |
|
365 | ], | |
366 | [(u"a = \\", None), # Test resetting when within a multi-line string |
|
366 | [(u"a = \\", None), # Test resetting when within a multi-line string | |
367 | (u"12 *\\", None), |
|
367 | (u"12 *\\", None), | |
368 | (None, u"a = 12 *"), |
|
368 | (None, u"a = 12 *"), | |
369 | ], |
|
369 | ], | |
370 | [(u"# foo\\", u"# foo\\"), # Comments can't be continued like this |
|
370 | [(u"# foo\\", u"# foo\\"), # Comments can't be continued like this | |
371 | ], |
|
371 | ], | |
372 | ] |
|
372 | ] | |
373 | for example in tests: |
|
373 | for example in tests: | |
374 | transform_checker(example, ipt.assemble_logical_lines) |
|
374 | transform_checker(example, ipt.assemble_logical_lines) | |
375 |
|
375 | |||
376 | def test_assemble_python_lines(): |
|
376 | def test_assemble_python_lines(): | |
377 | tests = \ |
|
377 | tests = \ | |
378 | [ [(u"a = '''", None), |
|
378 | [ [(u"a = '''", None), | |
379 | (u"abc'''", u"a = '''\nabc'''"), |
|
379 | (u"abc'''", u"a = '''\nabc'''"), | |
380 | ], |
|
380 | ], | |
381 | [(u"a = '''", None), # Test resetting when within a multi-line string |
|
381 | [(u"a = '''", None), # Test resetting when within a multi-line string | |
382 | (u"def", None), |
|
382 | (u"def", None), | |
383 | (None, u"a = '''\ndef"), |
|
383 | (None, u"a = '''\ndef"), | |
384 | ], |
|
384 | ], | |
385 | [(u"a = [1,", None), |
|
385 | [(u"a = [1,", None), | |
386 | (u"2]", u"a = [1,\n2]"), |
|
386 | (u"2]", u"a = [1,\n2]"), | |
387 | ], |
|
387 | ], | |
388 | [(u"a = [1,", None), # Test resetting when within a multi-line string |
|
388 | [(u"a = [1,", None), # Test resetting when within a multi-line string | |
389 | (u"2,", None), |
|
389 | (u"2,", None), | |
390 | (None, u"a = [1,\n2,"), |
|
390 | (None, u"a = [1,\n2,"), | |
391 | ], |
|
391 | ], | |
|
392 | [(u"a = '''", None), # Test line continuation within a multi-line string | |||
|
393 | (u"abc\\", None), | |||
|
394 | (u"def", None), | |||
|
395 | (u"'''", u"a = '''\nabc\\\ndef\n'''"), | |||
|
396 | ], | |||
392 | ] + syntax_ml['multiline_datastructure'] |
|
397 | ] + syntax_ml['multiline_datastructure'] | |
393 | for example in tests: |
|
398 | for example in tests: | |
394 | transform_checker(example, ipt.assemble_python_lines) |
|
399 | transform_checker(example, ipt.assemble_python_lines) | |
395 |
|
400 | |||
396 |
|
401 | |||
397 | def test_help_end(): |
|
402 | def test_help_end(): | |
398 | tt.check_pairs(transform_and_reset(ipt.help_end), syntax['end_help']) |
|
403 | tt.check_pairs(transform_and_reset(ipt.help_end), syntax['end_help']) | |
399 |
|
404 | |||
400 | def test_escaped_noesc(): |
|
405 | def test_escaped_noesc(): | |
401 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_noesc']) |
|
406 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_noesc']) | |
402 |
|
407 | |||
403 |
|
408 | |||
404 | def test_escaped_shell(): |
|
409 | def test_escaped_shell(): | |
405 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_shell']) |
|
410 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_shell']) | |
406 |
|
411 | |||
407 |
|
412 | |||
408 | def test_escaped_help(): |
|
413 | def test_escaped_help(): | |
409 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_help']) |
|
414 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_help']) | |
410 |
|
415 | |||
411 |
|
416 | |||
412 | def test_escaped_magic(): |
|
417 | def test_escaped_magic(): | |
413 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_magic']) |
|
418 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_magic']) | |
414 |
|
419 | |||
415 |
|
420 | |||
416 | def test_escaped_quote(): |
|
421 | def test_escaped_quote(): | |
417 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_quote']) |
|
422 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_quote']) | |
418 |
|
423 | |||
419 |
|
424 | |||
420 | def test_escaped_quote2(): |
|
425 | def test_escaped_quote2(): | |
421 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_quote2']) |
|
426 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_quote2']) | |
422 |
|
427 | |||
423 |
|
428 | |||
424 | def test_escaped_paren(): |
|
429 | def test_escaped_paren(): | |
425 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_paren']) |
|
430 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_paren']) | |
426 |
|
431 | |||
427 |
|
432 | |||
428 | def test_cellmagic(): |
|
433 | def test_cellmagic(): | |
429 | for example in syntax_ml['cellmagic']: |
|
434 | for example in syntax_ml['cellmagic']: | |
430 | transform_checker(example, ipt.cellmagic) |
|
435 | transform_checker(example, ipt.cellmagic) | |
431 |
|
436 | |||
432 | line_example = [(u'%%bar 123', None), |
|
437 | line_example = [(u'%%bar 123', None), | |
433 | (u'hello', None), |
|
438 | (u'hello', None), | |
434 | (u'' , u_fmt("get_ipython().run_cell_magic({u}'bar', {u}'123', {u}'hello')")), |
|
439 | (u'' , u_fmt("get_ipython().run_cell_magic({u}'bar', {u}'123', {u}'hello')")), | |
435 | ] |
|
440 | ] | |
436 | transform_checker(line_example, ipt.cellmagic, end_on_blank_line=True) |
|
441 | transform_checker(line_example, ipt.cellmagic, end_on_blank_line=True) | |
437 |
|
442 | |||
438 | def test_has_comment(): |
|
443 | def test_has_comment(): | |
439 | tests = [('text', False), |
|
444 | tests = [('text', False), | |
440 | ('text #comment', True), |
|
445 | ('text #comment', True), | |
441 | ('text #comment\n', True), |
|
446 | ('text #comment\n', True), | |
442 | ('#comment', True), |
|
447 | ('#comment', True), | |
443 | ('#comment\n', True), |
|
448 | ('#comment\n', True), | |
444 | ('a = "#string"', False), |
|
449 | ('a = "#string"', False), | |
445 | ('a = "#string" # comment', True), |
|
450 | ('a = "#string" # comment', True), | |
446 | ('a #comment not "string"', True), |
|
451 | ('a #comment not "string"', True), | |
447 | ] |
|
452 | ] | |
448 | tt.check_pairs(ipt.has_comment, tests) |
|
453 | tt.check_pairs(ipt.has_comment, tests) | |
449 |
|
454 | |||
450 | @ipt.TokenInputTransformer.wrap |
|
455 | @ipt.TokenInputTransformer.wrap | |
451 | def decistmt(tokens): |
|
456 | def decistmt(tokens): | |
452 | """Substitute Decimals for floats in a string of statements. |
|
457 | """Substitute Decimals for floats in a string of statements. | |
453 |
|
458 | |||
454 | Based on an example from the tokenize module docs. |
|
459 | Based on an example from the tokenize module docs. | |
455 | """ |
|
460 | """ | |
456 | result = [] |
|
461 | result = [] | |
457 | for toknum, tokval, _, _, _ in tokens: |
|
462 | for toknum, tokval, _, _, _ in tokens: | |
458 | if toknum == tokenize.NUMBER and '.' in tokval: # replace NUMBER tokens |
|
463 | if toknum == tokenize.NUMBER and '.' in tokval: # replace NUMBER tokens | |
459 | for newtok in [ |
|
464 | for newtok in [ | |
460 | (tokenize.NAME, 'Decimal'), |
|
465 | (tokenize.NAME, 'Decimal'), | |
461 | (tokenize.OP, '('), |
|
466 | (tokenize.OP, '('), | |
462 | (tokenize.STRING, repr(tokval)), |
|
467 | (tokenize.STRING, repr(tokval)), | |
463 | (tokenize.OP, ')') |
|
468 | (tokenize.OP, ')') | |
464 | ]: |
|
469 | ]: | |
465 | yield newtok |
|
470 | yield newtok | |
466 | else: |
|
471 | else: | |
467 | yield (toknum, tokval) |
|
472 | yield (toknum, tokval) | |
468 |
|
473 | |||
469 |
|
474 | |||
470 |
|
475 | |||
471 | def test_token_input_transformer(): |
|
476 | def test_token_input_transformer(): | |
472 | tests = [(u'1.2', u_fmt(u"Decimal ({u}'1.2')")), |
|
477 | tests = [(u'1.2', u_fmt(u"Decimal ({u}'1.2')")), | |
473 | (u'"1.2"', u'"1.2"'), |
|
478 | (u'"1.2"', u'"1.2"'), | |
474 | ] |
|
479 | ] | |
475 | tt.check_pairs(transform_and_reset(decistmt), tests) |
|
480 | tt.check_pairs(transform_and_reset(decistmt), tests) | |
476 | ml_tests = \ |
|
481 | ml_tests = \ | |
477 | [ [(u"a = 1.2; b = '''x", None), |
|
482 | [ [(u"a = 1.2; b = '''x", None), | |
478 | (u"y'''", u_fmt(u"a =Decimal ({u}'1.2');b ='''x\ny'''")), |
|
483 | (u"y'''", u_fmt(u"a =Decimal ({u}'1.2');b ='''x\ny'''")), | |
479 | ], |
|
484 | ], | |
480 | [(u"a = [1.2,", None), |
|
485 | [(u"a = [1.2,", None), | |
481 | (u"3]", u_fmt(u"a =[Decimal ({u}'1.2'),\n3 ]")), |
|
486 | (u"3]", u_fmt(u"a =[Decimal ({u}'1.2'),\n3 ]")), | |
482 | ], |
|
487 | ], | |
483 | [(u"a = '''foo", None), # Test resetting when within a multi-line string |
|
488 | [(u"a = '''foo", None), # Test resetting when within a multi-line string | |
484 | (u"bar", None), |
|
489 | (u"bar", None), | |
485 | (None, u"a = '''foo\nbar"), |
|
490 | (None, u"a = '''foo\nbar"), | |
486 | ], |
|
491 | ], | |
487 | ] |
|
492 | ] | |
488 | for example in ml_tests: |
|
493 | for example in ml_tests: | |
489 | transform_checker(example, decistmt) |
|
494 | transform_checker(example, decistmt) |
General Comments 0
You need to be logged in to leave comments.
Login now