##// END OF EJS Templates
[core][tests][inputtransformer2] Remove nose
Samuel Gaist -
Show More
@@ -1,355 +1,359 b''
1 1 """Tests for the token-based transformers in IPython.core.inputtransformer2
2 2
3 3 Line-based transformers are the simpler ones; token-based transformers are
4 4 more complex. See test_inputtransformer2_line for tests for line-based
5 5 transformations.
6 6 """
7 import nose.tools as nt
8 7 import string
9 8
10 9 from IPython.core import inputtransformer2 as ipt2
11 10 from IPython.core.inputtransformer2 import make_tokens_by_line, _find_assign_op
12 11
13 12 from textwrap import dedent
14 13
15 14 MULTILINE_MAGIC = ("""\
16 15 a = f()
17 16 %foo \\
18 17 bar
19 18 g()
20 19 """.splitlines(keepends=True), (2, 0), """\
21 20 a = f()
22 21 get_ipython().run_line_magic('foo', ' bar')
23 22 g()
24 23 """.splitlines(keepends=True))
25 24
26 25 INDENTED_MAGIC = ("""\
27 26 for a in range(5):
28 27 %ls
29 28 """.splitlines(keepends=True), (2, 4), """\
30 29 for a in range(5):
31 30 get_ipython().run_line_magic('ls', '')
32 31 """.splitlines(keepends=True))
33 32
34 33 CRLF_MAGIC = ([
35 34 "a = f()\n",
36 35 "%ls\r\n",
37 36 "g()\n"
38 37 ], (2, 0), [
39 38 "a = f()\n",
40 39 "get_ipython().run_line_magic('ls', '')\n",
41 40 "g()\n"
42 41 ])
43 42
44 43 MULTILINE_MAGIC_ASSIGN = ("""\
45 44 a = f()
46 45 b = %foo \\
47 46 bar
48 47 g()
49 48 """.splitlines(keepends=True), (2, 4), """\
50 49 a = f()
51 50 b = get_ipython().run_line_magic('foo', ' bar')
52 51 g()
53 52 """.splitlines(keepends=True))
54 53
55 54 MULTILINE_SYSTEM_ASSIGN = ("""\
56 55 a = f()
57 56 b = !foo \\
58 57 bar
59 58 g()
60 59 """.splitlines(keepends=True), (2, 4), """\
61 60 a = f()
62 61 b = get_ipython().getoutput('foo bar')
63 62 g()
64 63 """.splitlines(keepends=True))
65 64
66 65 #####
67 66
68 67 MULTILINE_SYSTEM_ASSIGN_AFTER_DEDENT = ("""\
69 68 def test():
70 69 for i in range(1):
71 70 print(i)
72 71 res =! ls
73 72 """.splitlines(keepends=True), (4, 7), '''\
74 73 def test():
75 74 for i in range(1):
76 75 print(i)
77 76 res =get_ipython().getoutput(\' ls\')
78 77 '''.splitlines(keepends=True))
79 78
80 79 ######
81 80
82 81 AUTOCALL_QUOTE = (
83 82 [",f 1 2 3\n"], (1, 0),
84 83 ['f("1", "2", "3")\n']
85 84 )
86 85
87 86 AUTOCALL_QUOTE2 = (
88 87 [";f 1 2 3\n"], (1, 0),
89 88 ['f("1 2 3")\n']
90 89 )
91 90
92 91 AUTOCALL_PAREN = (
93 92 ["/f 1 2 3\n"], (1, 0),
94 93 ['f(1, 2, 3)\n']
95 94 )
96 95
97 96 SIMPLE_HELP = (
98 97 ["foo?\n"], (1, 0),
99 98 ["get_ipython().run_line_magic('pinfo', 'foo')\n"]
100 99 )
101 100
102 101 DETAILED_HELP = (
103 102 ["foo??\n"], (1, 0),
104 103 ["get_ipython().run_line_magic('pinfo2', 'foo')\n"]
105 104 )
106 105
107 106 MAGIC_HELP = (
108 107 ["%foo?\n"], (1, 0),
109 108 ["get_ipython().run_line_magic('pinfo', '%foo')\n"]
110 109 )
111 110
112 111 HELP_IN_EXPR = (
113 112 ["a = b + c?\n"], (1, 0),
114 113 ["get_ipython().set_next_input('a = b + c');"
115 114 "get_ipython().run_line_magic('pinfo', 'c')\n"]
116 115 )
117 116
118 117 HELP_CONTINUED_LINE = ("""\
119 118 a = \\
120 119 zip?
121 120 """.splitlines(keepends=True), (1, 0),
122 121 [r"get_ipython().set_next_input('a = \\\nzip');get_ipython().run_line_magic('pinfo', 'zip')" + "\n"]
123 122 )
124 123
125 124 HELP_MULTILINE = ("""\
126 125 (a,
127 126 b) = zip?
128 127 """.splitlines(keepends=True), (1, 0),
129 128 [r"get_ipython().set_next_input('(a,\nb) = zip');get_ipython().run_line_magic('pinfo', 'zip')" + "\n"]
130 129 )
131 130
132 131 HELP_UNICODE = (
133 132 ["Ο€.foo?\n"], (1, 0),
134 133 ["get_ipython().run_line_magic('pinfo', 'Ο€.foo')\n"]
135 134 )
136 135
137 136
138 137 def null_cleanup_transformer(lines):
139 138 """
140 139 A cleanup transform that returns an empty list.
141 140 """
142 141 return []
143 142
144 143 def check_make_token_by_line_never_ends_empty():
145 144 """
146 145 Check that not sequence of single or double characters ends up leading to en empty list of tokens
147 146 """
148 147 from string import printable
149 148 for c in printable:
150 nt.assert_not_equal(make_tokens_by_line(c)[-1], [])
149 assert make_tokens_by_line(c)[-1] != []
151 150 for k in printable:
152 nt.assert_not_equal(make_tokens_by_line(c+k)[-1], [])
151 assert make_tokens_by_line(c + k)[-1] != []
152
153 153
154 154 def check_find(transformer, case, match=True):
155 155 sample, expected_start, _ = case
156 156 tbl = make_tokens_by_line(sample)
157 157 res = transformer.find(tbl)
158 158 if match:
159 159 # start_line is stored 0-indexed, expected values are 1-indexed
160 nt.assert_equal((res.start_line+1, res.start_col), expected_start)
160 assert (res.start_line + 1, res.start_col) == expected_start
161 161 return res
162 162 else:
163 nt.assert_is(res, None)
163 assert res is None
164 164
165 165 def check_transform(transformer_cls, case):
166 166 lines, start, expected = case
167 167 transformer = transformer_cls(start)
168 nt.assert_equal(transformer.transform(lines), expected)
168 assert transformer.transform(lines) == expected
169 169
170 170 def test_continued_line():
171 171 lines = MULTILINE_MAGIC_ASSIGN[0]
172 nt.assert_equal(ipt2.find_end_of_continued_line(lines, 1), 2)
172 assert ipt2.find_end_of_continued_line(lines, 1) == 2
173 173
174 nt.assert_equal(ipt2.assemble_continued_line(lines, (1, 5), 2), "foo bar")
174 assert ipt2.assemble_continued_line(lines, (1, 5), 2) == "foo bar"
175 175
176 176 def test_find_assign_magic():
177 177 check_find(ipt2.MagicAssign, MULTILINE_MAGIC_ASSIGN)
178 178 check_find(ipt2.MagicAssign, MULTILINE_SYSTEM_ASSIGN, match=False)
179 179 check_find(ipt2.MagicAssign, MULTILINE_SYSTEM_ASSIGN_AFTER_DEDENT, match=False)
180 180
181 181 def test_transform_assign_magic():
182 182 check_transform(ipt2.MagicAssign, MULTILINE_MAGIC_ASSIGN)
183 183
184 184 def test_find_assign_system():
185 185 check_find(ipt2.SystemAssign, MULTILINE_SYSTEM_ASSIGN)
186 186 check_find(ipt2.SystemAssign, MULTILINE_SYSTEM_ASSIGN_AFTER_DEDENT)
187 187 check_find(ipt2.SystemAssign, (["a = !ls\n"], (1, 5), None))
188 188 check_find(ipt2.SystemAssign, (["a=!ls\n"], (1, 2), None))
189 189 check_find(ipt2.SystemAssign, MULTILINE_MAGIC_ASSIGN, match=False)
190 190
191 191 def test_transform_assign_system():
192 192 check_transform(ipt2.SystemAssign, MULTILINE_SYSTEM_ASSIGN)
193 193 check_transform(ipt2.SystemAssign, MULTILINE_SYSTEM_ASSIGN_AFTER_DEDENT)
194 194
195 195 def test_find_magic_escape():
196 196 check_find(ipt2.EscapedCommand, MULTILINE_MAGIC)
197 197 check_find(ipt2.EscapedCommand, INDENTED_MAGIC)
198 198 check_find(ipt2.EscapedCommand, MULTILINE_MAGIC_ASSIGN, match=False)
199 199
200 200 def test_transform_magic_escape():
201 201 check_transform(ipt2.EscapedCommand, MULTILINE_MAGIC)
202 202 check_transform(ipt2.EscapedCommand, INDENTED_MAGIC)
203 203 check_transform(ipt2.EscapedCommand, CRLF_MAGIC)
204 204
205 205 def test_find_autocalls():
206 206 for case in [AUTOCALL_QUOTE, AUTOCALL_QUOTE2, AUTOCALL_PAREN]:
207 207 print("Testing %r" % case[0])
208 208 check_find(ipt2.EscapedCommand, case)
209 209
210 210 def test_transform_autocall():
211 211 for case in [AUTOCALL_QUOTE, AUTOCALL_QUOTE2, AUTOCALL_PAREN]:
212 212 print("Testing %r" % case[0])
213 213 check_transform(ipt2.EscapedCommand, case)
214 214
215 215 def test_find_help():
216 216 for case in [SIMPLE_HELP, DETAILED_HELP, MAGIC_HELP, HELP_IN_EXPR]:
217 217 check_find(ipt2.HelpEnd, case)
218 218
219 219 tf = check_find(ipt2.HelpEnd, HELP_CONTINUED_LINE)
220 nt.assert_equal(tf.q_line, 1)
221 nt.assert_equal(tf.q_col, 3)
220 assert tf.q_line == 1
221 assert tf.q_col == 3
222 222
223 223 tf = check_find(ipt2.HelpEnd, HELP_MULTILINE)
224 nt.assert_equal(tf.q_line, 1)
225 nt.assert_equal(tf.q_col, 8)
224 assert tf.q_line == 1
225 assert tf.q_col == 8
226 226
227 227 # ? in a comment does not trigger help
228 228 check_find(ipt2.HelpEnd, (["foo # bar?\n"], None, None), match=False)
229 229 # Nor in a string
230 230 check_find(ipt2.HelpEnd, (["foo = '''bar?\n"], None, None), match=False)
231 231
232 232 def test_transform_help():
233 233 tf = ipt2.HelpEnd((1, 0), (1, 9))
234 nt.assert_equal(tf.transform(HELP_IN_EXPR[0]), HELP_IN_EXPR[2])
234 assert tf.transform(HELP_IN_EXPR[0]) == HELP_IN_EXPR[2]
235 235
236 236 tf = ipt2.HelpEnd((1, 0), (2, 3))
237 nt.assert_equal(tf.transform(HELP_CONTINUED_LINE[0]), HELP_CONTINUED_LINE[2])
237 assert tf.transform(HELP_CONTINUED_LINE[0]) == HELP_CONTINUED_LINE[2]
238 238
239 239 tf = ipt2.HelpEnd((1, 0), (2, 8))
240 nt.assert_equal(tf.transform(HELP_MULTILINE[0]), HELP_MULTILINE[2])
240 assert tf.transform(HELP_MULTILINE[0]) == HELP_MULTILINE[2]
241 241
242 242 tf = ipt2.HelpEnd((1, 0), (1, 0))
243 nt.assert_equal(tf.transform(HELP_UNICODE[0]), HELP_UNICODE[2])
243 assert tf.transform(HELP_UNICODE[0]) == HELP_UNICODE[2]
244 244
245 245 def test_find_assign_op_dedent():
246 246 """
247 247 be careful that empty token like dedent are not counted as parens
248 248 """
249 249 class Tk:
250 250 def __init__(self, s):
251 251 self.string = s
252 252
253 nt.assert_equal(_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)
253 assert _find_assign_op([Tk(s) for s in ("", "a", "=", "b")]) == 2
254 assert (
255 _find_assign_op([Tk(s) for s in ("", "(", "a", "=", "b", ")", "=", "5")]) == 6
256 )
257
255 258
256 259 def test_check_complete():
257 260 cc = ipt2.TransformerManager().check_complete
258 nt.assert_equal(cc("a = 1"), ("complete", None))
259 nt.assert_equal(cc("for a in range(5):"), ("incomplete", 4))
260 nt.assert_equal(cc("for a in range(5):\n if a > 0:"), ("incomplete", 8))
261 nt.assert_equal(cc("raise = 2"), ("invalid", None))
262 nt.assert_equal(cc("a = [1,\n2,"), ("incomplete", 0))
263 nt.assert_equal(cc("(\n))"), ("incomplete", 0))
264 nt.assert_equal(cc("\\\r\n"), ("incomplete", 0))
265 nt.assert_equal(cc("a = '''\n hi"), ("incomplete", 3))
266 nt.assert_equal(cc("def a():\n x=1\n global x"), ("invalid", None))
267 nt.assert_equal(cc("a \\ "), ("invalid", None)) # Nothing allowed after backslash
268 nt.assert_equal(cc("1\\\n+2"), ("complete", None))
269 nt.assert_equal(cc("exit"), ("complete", None))
261 assert cc("a = 1") == ("complete", None)
262 assert cc("for a in range(5):") == ("incomplete", 4)
263 assert cc("for a in range(5):\n if a > 0:") == ("incomplete", 8)
264 assert cc("raise = 2") == ("invalid", None)
265 assert cc("a = [1,\n2,") == ("incomplete", 0)
266 assert cc("(\n))") == ("incomplete", 0)
267 assert cc("\\\r\n") == ("incomplete", 0)
268 assert cc("a = '''\n hi") == ("incomplete", 3)
269 assert cc("def a():\n x=1\n global x") == ("invalid", None)
270 assert cc("a \\ ") == ("invalid", None) # Nothing allowed after backslash
271 assert cc("1\\\n+2") == ("complete", None)
272 assert cc("exit") == ("complete", None)
270 273
271 274 example = dedent("""
272 275 if True:
273 276 a=1""" )
274 277
275 nt.assert_equal(cc(example), ('incomplete', 4))
276 nt.assert_equal(cc(example+'\n'), ('complete', None))
277 nt.assert_equal(cc(example+'\n '), ('complete', None))
278 assert cc(example) == ("incomplete", 4)
279 assert cc(example + "\n") == ("complete", None)
280 assert cc(example + "\n ") == ("complete", None)
278 281
279 282 # no need to loop on all the letters/numbers.
280 283 short = '12abAB'+string.printable[62:]
281 284 for c in short:
282 285 # test does not raise:
283 286 cc(c)
284 287 for k in short:
285 288 cc(c+k)
286 289
287 nt.assert_equal(cc("def f():\n x=0\n \\\n "), ('incomplete', 2))
290 assert cc("def f():\n x=0\n \\\n ") == ("incomplete", 2)
291
288 292
289 293 def test_check_complete_II():
290 294 """
291 295 Test that multiple line strings are properly handled.
292 296
293 297 Separate test function for convenience
294 298
295 299 """
296 300 cc = ipt2.TransformerManager().check_complete
297 nt.assert_equal(cc('''def foo():\n """'''), ('incomplete', 4))
301 assert cc('''def foo():\n """''') == ("incomplete", 4)
298 302
299 303
300 304 def test_check_complete_invalidates_sunken_brackets():
301 305 """
302 306 Test that a single line with more closing brackets than the opening ones is
303 307 interpreted as invalid
304 308 """
305 309 cc = ipt2.TransformerManager().check_complete
306 nt.assert_equal(cc(")"), ("invalid", None))
307 nt.assert_equal(cc("]"), ("invalid", None))
308 nt.assert_equal(cc("}"), ("invalid", None))
309 nt.assert_equal(cc(")("), ("invalid", None))
310 nt.assert_equal(cc("]["), ("invalid", None))
311 nt.assert_equal(cc("}{"), ("invalid", None))
312 nt.assert_equal(cc("]()("), ("invalid", None))
313 nt.assert_equal(cc("())("), ("invalid", None))
314 nt.assert_equal(cc(")[]("), ("invalid", None))
315 nt.assert_equal(cc("()]("), ("invalid", None))
310 assert cc(")") == ("invalid", None)
311 assert cc("]") == ("invalid", None)
312 assert cc("}") == ("invalid", None)
313 assert cc(")(") == ("invalid", None)
314 assert cc("][") == ("invalid", None)
315 assert cc("}{") == ("invalid", None)
316 assert cc("]()(") == ("invalid", None)
317 assert cc("())(") == ("invalid", None)
318 assert cc(")[](") == ("invalid", None)
319 assert cc("()](") == ("invalid", None)
316 320
317 321
318 322 def test_null_cleanup_transformer():
319 323 manager = ipt2.TransformerManager()
320 324 manager.cleanup_transforms.insert(0, null_cleanup_transformer)
321 325 assert manager.transform_cell("") == ""
322 326
323 327
324 328
325 329
326 330 def test_side_effects_I():
327 331 count = 0
328 332 def counter(lines):
329 333 nonlocal count
330 334 count += 1
331 335 return lines
332 336
333 337 counter.has_side_effects = True
334 338
335 339 manager = ipt2.TransformerManager()
336 340 manager.cleanup_transforms.insert(0, counter)
337 341 assert manager.check_complete("a=1\n") == ('complete', None)
338 342 assert count == 0
339 343
340 344
341 345
342 346
343 347 def test_side_effects_II():
344 348 count = 0
345 349 def counter(lines):
346 350 nonlocal count
347 351 count += 1
348 352 return lines
349 353
350 354 counter.has_side_effects = True
351 355
352 356 manager = ipt2.TransformerManager()
353 357 manager.line_transforms.insert(0, counter)
354 358 assert manager.check_complete("b=1\n") == ('complete', None)
355 359 assert count == 0
General Comments 0
You need to be logged in to leave comments. Login now