Show More
@@ -4,7 +4,6 b' Line-based transformers are the simpler ones; token-based transformers are' | |||||
4 | more complex. See test_inputtransformer2_line for tests for line-based |
|
4 | more complex. See test_inputtransformer2_line for tests for line-based | |
5 | transformations. |
|
5 | transformations. | |
6 | """ |
|
6 | """ | |
7 | import nose.tools as nt |
|
|||
8 | import string |
|
7 | import string | |
9 |
|
8 | |||
10 | from IPython.core import inputtransformer2 as ipt2 |
|
9 | from IPython.core import inputtransformer2 as ipt2 | |
@@ -147,9 +146,10 b' def check_make_token_by_line_never_ends_empty():' | |||||
147 | """ |
|
146 | """ | |
148 | from string import printable |
|
147 | from string import printable | |
149 | for c in printable: |
|
148 | for c in printable: | |
150 |
|
|
149 | assert make_tokens_by_line(c)[-1] != [] | |
151 | for k in printable: |
|
150 | for k in printable: | |
152 |
|
|
151 | assert make_tokens_by_line(c + k)[-1] != [] | |
|
152 | ||||
153 |
|
153 | |||
154 | def check_find(transformer, case, match=True): |
|
154 | def check_find(transformer, case, match=True): | |
155 | sample, expected_start, _ = case |
|
155 | sample, expected_start, _ = case | |
@@ -157,21 +157,21 b' def check_find(transformer, case, match=True):' | |||||
157 | res = transformer.find(tbl) |
|
157 | res = transformer.find(tbl) | |
158 | if match: |
|
158 | if match: | |
159 | # start_line is stored 0-indexed, expected values are 1-indexed |
|
159 | # start_line is stored 0-indexed, expected values are 1-indexed | |
160 |
|
|
160 | assert (res.start_line + 1, res.start_col) == expected_start | |
161 | return res |
|
161 | return res | |
162 | else: |
|
162 | else: | |
163 |
|
|
163 | assert res is None | |
164 |
|
164 | |||
165 | def check_transform(transformer_cls, case): |
|
165 | def check_transform(transformer_cls, case): | |
166 | lines, start, expected = case |
|
166 | lines, start, expected = case | |
167 | transformer = transformer_cls(start) |
|
167 | transformer = transformer_cls(start) | |
168 |
|
|
168 | assert transformer.transform(lines) == expected | |
169 |
|
169 | |||
170 | def test_continued_line(): |
|
170 | def test_continued_line(): | |
171 | lines = MULTILINE_MAGIC_ASSIGN[0] |
|
171 | lines = MULTILINE_MAGIC_ASSIGN[0] | |
172 |
|
|
172 | assert ipt2.find_end_of_continued_line(lines, 1) == 2 | |
173 |
|
173 | |||
174 |
|
|
174 | assert ipt2.assemble_continued_line(lines, (1, 5), 2) == "foo bar" | |
175 |
|
175 | |||
176 | def test_find_assign_magic(): |
|
176 | def test_find_assign_magic(): | |
177 | check_find(ipt2.MagicAssign, MULTILINE_MAGIC_ASSIGN) |
|
177 | check_find(ipt2.MagicAssign, MULTILINE_MAGIC_ASSIGN) | |
@@ -217,12 +217,12 b' def test_find_help():' | |||||
217 | check_find(ipt2.HelpEnd, case) |
|
217 | check_find(ipt2.HelpEnd, case) | |
218 |
|
218 | |||
219 | tf = check_find(ipt2.HelpEnd, HELP_CONTINUED_LINE) |
|
219 | tf = check_find(ipt2.HelpEnd, HELP_CONTINUED_LINE) | |
220 |
|
|
220 | assert tf.q_line == 1 | |
221 |
|
|
221 | assert tf.q_col == 3 | |
222 |
|
222 | |||
223 | tf = check_find(ipt2.HelpEnd, HELP_MULTILINE) |
|
223 | tf = check_find(ipt2.HelpEnd, HELP_MULTILINE) | |
224 |
|
|
224 | assert tf.q_line == 1 | |
225 |
|
|
225 | assert tf.q_col == 8 | |
226 |
|
226 | |||
227 | # ? in a comment does not trigger help |
|
227 | # ? in a comment does not trigger help | |
228 | check_find(ipt2.HelpEnd, (["foo # bar?\n"], None, None), match=False) |
|
228 | check_find(ipt2.HelpEnd, (["foo # bar?\n"], None, None), match=False) | |
@@ -231,16 +231,16 b' def test_find_help():' | |||||
231 |
|
231 | |||
232 | def test_transform_help(): |
|
232 | def test_transform_help(): | |
233 | tf = ipt2.HelpEnd((1, 0), (1, 9)) |
|
233 | tf = ipt2.HelpEnd((1, 0), (1, 9)) | |
234 |
|
|
234 | assert tf.transform(HELP_IN_EXPR[0]) == HELP_IN_EXPR[2] | |
235 |
|
235 | |||
236 | tf = ipt2.HelpEnd((1, 0), (2, 3)) |
|
236 | tf = ipt2.HelpEnd((1, 0), (2, 3)) | |
237 |
|
|
237 | assert tf.transform(HELP_CONTINUED_LINE[0]) == HELP_CONTINUED_LINE[2] | |
238 |
|
238 | |||
239 | tf = ipt2.HelpEnd((1, 0), (2, 8)) |
|
239 | tf = ipt2.HelpEnd((1, 0), (2, 8)) | |
240 |
|
|
240 | assert tf.transform(HELP_MULTILINE[0]) == HELP_MULTILINE[2] | |
241 |
|
241 | |||
242 | tf = ipt2.HelpEnd((1, 0), (1, 0)) |
|
242 | tf = ipt2.HelpEnd((1, 0), (1, 0)) | |
243 |
|
|
243 | assert tf.transform(HELP_UNICODE[0]) == HELP_UNICODE[2] | |
244 |
|
244 | |||
245 | def test_find_assign_op_dedent(): |
|
245 | def test_find_assign_op_dedent(): | |
246 | """ |
|
246 | """ | |
@@ -250,31 +250,34 b' def test_find_assign_op_dedent():' | |||||
250 | def __init__(self, s): |
|
250 | def __init__(self, s): | |
251 | self.string = s |
|
251 | self.string = s | |
252 |
|
252 | |||
253 |
|
|
253 | assert _find_assign_op([Tk(s) for s in ("", "a", "=", "b")]) == 2 | |
254 | nt.assert_equal(_find_assign_op([Tk(s) for s in ('','(', 'a','=','b', ')', '=' ,'5')]), 6) |
|
254 | assert ( | |
|
255 | _find_assign_op([Tk(s) for s in ("", "(", "a", "=", "b", ")", "=", "5")]) == 6 | |||
|
256 | ) | |||
|
257 | ||||
255 |
|
258 | |||
256 | def test_check_complete(): |
|
259 | def test_check_complete(): | |
257 | cc = ipt2.TransformerManager().check_complete |
|
260 | cc = ipt2.TransformerManager().check_complete | |
258 |
|
|
261 | assert cc("a = 1") == ("complete", None) | |
259 |
|
|
262 | assert cc("for a in range(5):") == ("incomplete", 4) | |
260 |
|
|
263 | assert cc("for a in range(5):\n if a > 0:") == ("incomplete", 8) | |
261 |
|
|
264 | assert cc("raise = 2") == ("invalid", None) | |
262 |
|
|
265 | assert cc("a = [1,\n2,") == ("incomplete", 0) | |
263 |
|
|
266 | assert cc("(\n))") == ("incomplete", 0) | |
264 |
|
|
267 | assert cc("\\\r\n") == ("incomplete", 0) | |
265 |
|
|
268 | assert cc("a = '''\n hi") == ("incomplete", 3) | |
266 |
|
|
269 | assert cc("def a():\n x=1\n global x") == ("invalid", None) | |
267 |
|
|
270 | assert cc("a \\ ") == ("invalid", None) # Nothing allowed after backslash | |
268 |
|
|
271 | assert cc("1\\\n+2") == ("complete", None) | |
269 |
|
|
272 | assert cc("exit") == ("complete", None) | |
270 |
|
273 | |||
271 | example = dedent(""" |
|
274 | example = dedent(""" | |
272 | if True: |
|
275 | if True: | |
273 | a=1""" ) |
|
276 | a=1""" ) | |
274 |
|
277 | |||
275 |
|
|
278 | assert cc(example) == ("incomplete", 4) | |
276 |
|
|
279 | assert cc(example + "\n") == ("complete", None) | |
277 |
|
|
280 | assert cc(example + "\n ") == ("complete", None) | |
278 |
|
281 | |||
279 | # no need to loop on all the letters/numbers. |
|
282 | # no need to loop on all the letters/numbers. | |
280 | short = '12abAB'+string.printable[62:] |
|
283 | short = '12abAB'+string.printable[62:] | |
@@ -284,7 +287,8 b' def test_check_complete():' | |||||
284 | for k in short: |
|
287 | for k in short: | |
285 | cc(c+k) |
|
288 | cc(c+k) | |
286 |
|
289 | |||
287 |
|
|
290 | assert cc("def f():\n x=0\n \\\n ") == ("incomplete", 2) | |
|
291 | ||||
288 |
|
292 | |||
289 | def test_check_complete_II(): |
|
293 | def test_check_complete_II(): | |
290 | """ |
|
294 | """ | |
@@ -294,7 +298,7 b' def test_check_complete_II():' | |||||
294 |
|
298 | |||
295 | """ |
|
299 | """ | |
296 | cc = ipt2.TransformerManager().check_complete |
|
300 | cc = ipt2.TransformerManager().check_complete | |
297 |
|
|
301 | assert cc('''def foo():\n """''') == ("incomplete", 4) | |
298 |
|
302 | |||
299 |
|
303 | |||
300 | def test_check_complete_invalidates_sunken_brackets(): |
|
304 | def test_check_complete_invalidates_sunken_brackets(): | |
@@ -303,22 +307,22 b' def test_check_complete_invalidates_sunken_brackets():' | |||||
303 | interpreted as invalid |
|
307 | interpreted as invalid | |
304 | """ |
|
308 | """ | |
305 | cc = ipt2.TransformerManager().check_complete |
|
309 | cc = ipt2.TransformerManager().check_complete | |
306 |
|
|
310 | assert cc(")") == ("invalid", None) | |
307 |
|
|
311 | assert cc("]") == ("invalid", None) | |
308 |
|
|
312 | assert cc("}") == ("invalid", None) | |
309 |
|
|
313 | assert cc(")(") == ("invalid", None) | |
310 |
|
|
314 | assert cc("][") == ("invalid", None) | |
311 |
|
|
315 | assert cc("}{") == ("invalid", None) | |
312 |
|
|
316 | assert cc("]()(") == ("invalid", None) | |
313 |
|
|
317 | assert cc("())(") == ("invalid", None) | |
314 |
|
|
318 | assert cc(")[](") == ("invalid", None) | |
315 |
|
|
319 | assert cc("()](") == ("invalid", None) | |
316 |
|
320 | |||
317 |
|
321 | |||
318 | def test_null_cleanup_transformer(): |
|
322 | def test_null_cleanup_transformer(): | |
319 | manager = ipt2.TransformerManager() |
|
323 | manager = ipt2.TransformerManager() | |
320 | manager.cleanup_transforms.insert(0, null_cleanup_transformer) |
|
324 | manager.cleanup_transforms.insert(0, null_cleanup_transformer) | |
321 |
assert |
|
325 | assert manager.transform_cell("") == "" | |
322 |
|
326 | |||
323 |
|
327 | |||
324 |
|
328 |
General Comments 0
You need to be logged in to leave comments.
Login now