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