Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork252
Expand file tree
/
Copy pathtest_repl.py
More file actions
630 lines (511 loc) · 21.4 KB
/
test_repl.py
File metadata and controls
630 lines (511 loc) · 21.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
importcollections
importinspect
importsocket
importsys
importtempfile
importunittest
fromitertoolsimportislice
frompathlibimportPath
fromunittestimportmock
frombpythonimportconfig,repl,cli,autocomplete
frombpython.testimportMagicIterMock,FixLanguageTestCaseasTestCase
frombpython.testimportTEST_CONFIG
pypy="PyPy"insys.version
defsetup_config(conf):
config_struct=config.Config(TEST_CONFIG)
ifconfisnotNoneand"autocomplete_mode"inconf:
config_struct.autocomplete_mode=conf["autocomplete_mode"]
returnconfig_struct
classFakeHistory(repl.History):
def__init__(self):
pass
defreset(self):
pass
classFakeRepl(repl.Repl):
def__init__(self,conf=None):
repl.Repl.__init__(self,repl.Interpreter(),setup_config(conf))
self.current_line=""
self.cursor_offset=0
classFakeCliRepl(cli.CLIRepl,FakeRepl):
def__init__(self):
self.s=""
self.cpos=0
self.rl_history=FakeHistory()
classTestMatchesIterator(unittest.TestCase):
defsetUp(self):
self.matches= ["bobby","bobbies","bobberina"]
self.matches_iterator=repl.MatchesIterator()
self.matches_iterator.current_word="bob"
self.matches_iterator.orig_line="bob"
self.matches_iterator.orig_cursor_offset=len("bob")
self.matches_iterator.matches=self.matches
deftest_next(self):
self.assertEqual(next(self.matches_iterator),self.matches[0])
forxinrange(len(self.matches)-1):
next(self.matches_iterator)
self.assertEqual(next(self.matches_iterator),self.matches[0])
self.assertEqual(next(self.matches_iterator),self.matches[1])
self.assertNotEqual(next(self.matches_iterator),self.matches[1])
deftest_previous(self):
self.assertEqual(self.matches_iterator.previous(),self.matches[2])
forxinrange(len(self.matches)-1):
self.matches_iterator.previous()
self.assertNotEqual(self.matches_iterator.previous(),self.matches[0])
self.assertEqual(self.matches_iterator.previous(),self.matches[1])
self.assertEqual(self.matches_iterator.previous(),self.matches[0])
deftest_nonzero(self):
"""self.matches_iterator should be False at start,
then True once we active a match.
"""
self.assertFalse(self.matches_iterator)
next(self.matches_iterator)
self.assertTrue(self.matches_iterator)
deftest_iter(self):
slice=islice(self.matches_iterator,0,9)
self.assertEqual(list(slice),self.matches*3)
deftest_current(self):
withself.assertRaises(ValueError):
self.matches_iterator.current()
next(self.matches_iterator)
self.assertEqual(self.matches_iterator.current(),self.matches[0])
deftest_update(self):
slice=islice(self.matches_iterator,0,3)
self.assertEqual(list(slice),self.matches)
newmatches= ["string","str","set"]
completer=mock.Mock()
completer.locate.return_value= (0,1,"s")
self.matches_iterator.update(1,"s",newmatches,completer)
newslice=islice(newmatches,0,3)
self.assertNotEqual(list(slice),self.matches)
self.assertEqual(list(newslice),newmatches)
deftest_cur_line(self):
completer=mock.Mock()
completer.locate.return_value= (
0,
self.matches_iterator.orig_cursor_offset,
self.matches_iterator.orig_line,
)
self.matches_iterator.completer=completer
withself.assertRaises(ValueError):
self.matches_iterator.cur_line()
self.assertEqual(next(self.matches_iterator),self.matches[0])
self.assertEqual(
self.matches_iterator.cur_line(),
(len(self.matches[0]),self.matches[0]),
)
deftest_is_cseq(self):
self.assertTrue(self.matches_iterator.is_cseq())
classTestArgspec(unittest.TestCase):
defsetUp(self):
self.repl=FakeRepl()
self.repl.push("def spam(a, b, c):\n",False)
self.repl.push(" pass\n",False)
self.repl.push("\n",False)
self.repl.push("class Spam(object):\n",False)
self.repl.push(" def spam(self, a, b, c):\n",False)
self.repl.push(" pass\n",False)
self.repl.push("\n",False)
self.repl.push("class SpammitySpam(object):\n",False)
self.repl.push(" def __init__(self, a, b, c):\n",False)
self.repl.push(" pass\n",False)
self.repl.push("\n",False)
self.repl.push("class WonderfulSpam(object):\n",False)
self.repl.push(" def __new__(self, a, b, c):\n",False)
self.repl.push(" pass\n",False)
self.repl.push("\n",False)
self.repl.push("o = Spam()\n",False)
self.repl.push("\n",False)
defset_input_line(self,line):
"""Set current input line of the test REPL."""
self.repl.current_line=line
self.repl.cursor_offset=len(line)
deftest_func_name(self):
for (line,expected_name)in [
("spam(","spam"),
# map pydoc has no signature in pypy
("spam(any([]","any")ifpypyelse ("spam(map([]","map"),
("spam((), ","spam"),
]:
self.set_input_line(line)
self.assertTrue(self.repl.get_args())
self.assertEqual(self.repl.current_func.__name__,expected_name)
deftest_func_name_method_issue_479(self):
for (line,expected_name)in [
("o.spam(","spam"),
# map pydoc has no signature in pypy
("o.spam(any([]","any")ifpypyelse ("o.spam(map([]","map"),
("o.spam((), ","spam"),
]:
self.set_input_line(line)
self.assertTrue(self.repl.get_args())
self.assertEqual(self.repl.current_func.__name__,expected_name)
deftest_syntax_error_parens(self):
forlinein ["spam(]","spam([)","spam())"]:
self.set_input_line(line)
# Should not explode
self.repl.get_args()
deftest_kw_arg_position(self):
self.set_input_line("spam(a=0")
self.assertTrue(self.repl.get_args())
self.assertEqual(self.repl.arg_pos,"a")
self.set_input_line("spam(1, b=1")
self.assertTrue(self.repl.get_args())
self.assertEqual(self.repl.arg_pos,"b")
self.set_input_line("spam(1, c=2")
self.assertTrue(self.repl.get_args())
self.assertEqual(self.repl.arg_pos,"c")
deftest_lambda_position(self):
self.set_input_line("spam(lambda a, b: 1, ")
self.assertTrue(self.repl.get_args())
self.assertTrue(self.repl.funcprops)
# Argument position
self.assertEqual(self.repl.arg_pos,1)
@unittest.skipIf(pypy,"range pydoc has no signature in pypy")
deftest_issue127(self):
self.set_input_line("x=range(")
self.assertTrue(self.repl.get_args())
self.assertEqual(self.repl.current_func.__name__,"range")
self.set_input_line("{x:range(")
self.assertTrue(self.repl.get_args())
self.assertEqual(self.repl.current_func.__name__,"range")
self.set_input_line("foo(1, 2, x,range(")
self.assertEqual(self.repl.current_func.__name__,"range")
self.set_input_line("(x,range(")
self.assertEqual(self.repl.current_func.__name__,"range")
deftest_nonexistent_name(self):
self.set_input_line("spamspamspam(")
self.assertFalse(self.repl.get_args())
deftest_issue572(self):
self.set_input_line("SpammitySpam(")
self.assertTrue(self.repl.get_args())
self.set_input_line("WonderfulSpam(")
self.assertTrue(self.repl.get_args())
@unittest.skipIf(pypy,"pypy pydoc doesn't have this")
deftest_issue583(self):
self.repl=FakeRepl()
self.repl.push("a = 1.2\n",False)
self.set_input_line("a.is_integer(")
self.repl.set_docstring()
self.assertIsNot(self.repl.docstring,None)
deftest_methods_of_expressions(self):
self.set_input_line("'a'.capitalize(")
self.assertTrue(self.repl.get_args())
self.set_input_line("(1 + 1.1).as_integer_ratio(")
self.assertTrue(self.repl.get_args())
classTestArgspecInternal(unittest.TestCase):
deftest_function_expressions(self):
te=self.assertTupleEqual
fa=lambdaline:repl.Repl._funcname_and_argnum(line)
forline, (func,argnum)in [
("spam(", ("spam",0)),
("spam((), ", ("spam",1)),
("spam.eggs((), ", ("spam.eggs",1)),
("spam[abc].eggs((), ", ("spam[abc].eggs",1)),
("spam[0].eggs((), ", ("spam[0].eggs",1)),
("spam[a + b]eggs((), ", ("spam[a + b]eggs",1)),
("spam().eggs((), ", ("spam().eggs",1)),
("spam(1, 2).eggs((), ", ("spam(1, 2).eggs",1)),
("spam(1, f(1)).eggs((), ", ("spam(1, f(1)).eggs",1)),
("[0].eggs((), ", ("[0].eggs",1)),
("[0][0]((), {}).eggs((), ", ("[0][0]((), {}).eggs",1)),
("a + spam[0].eggs((), ", ("spam[0].eggs",1)),
("spam(", ("spam",0)),
("spam(map([]", ("map",0)),
("spam((), ", ("spam",1)),
]:
te(fa(line), (func,argnum))
classTestGetSource(unittest.TestCase):
defsetUp(self):
self.repl=FakeRepl()
defset_input_line(self,line):
"""Set current input line of the test REPL."""
self.repl.current_line=line
self.repl.cursor_offset=len(line)
defassert_get_source_error_for_current_function(self,func,msg):
self.repl.current_func=func
withself.assertRaises(repl.SourceNotFound):
self.repl.get_source_of_current_name()
try:
self.repl.get_source_of_current_name()
exceptrepl.SourceNotFoundase:
self.assertEqual(e.args[0],msg)
else:
self.fail("Should have raised SourceNotFound")
deftest_current_function(self):
self.set_input_line("INPUTLINE")
self.repl.current_func=inspect.getsource
self.assertIn(
"text of the source code",self.repl.get_source_of_current_name()
)
self.assert_get_source_error_for_current_function(
[],"No source code found for INPUTLINE"
)
self.assert_get_source_error_for_current_function(
list.pop,"No source code found for INPUTLINE"
)
@unittest.skipIf(pypy,"different errors for PyPy")
deftest_current_function_cpython(self):
self.set_input_line("INPUTLINE")
self.assert_get_source_error_for_current_function(
collections.defaultdict.copy,"No source code found for INPUTLINE"
)
self.assert_get_source_error_for_current_function(
collections.defaultdict,"could not find class definition"
)
deftest_current_line(self):
self.repl.interp.locals["a"]=socket.socket
self.set_input_line("a")
self.assertIn("dup(self)",self.repl.get_source_of_current_name())
# TODO add tests for various failures without using current function
classTestEditConfig(TestCase):
defsetUp(self):
self.repl=FakeRepl()
self.repl.interact.confirm=lambdamsg:True
self.repl.interact.notify=lambdamsg:None
self.repl.config.editor="true"
deftest_create_config(self):
withtempfile.TemporaryDirectory()astmp_dir:
config_path=Path(tmp_dir)/"newdir"/"config"
self.repl.config.config_path=config_path
self.repl.edit_config()
self.assertTrue(config_path.exists())
classTestRepl(unittest.TestCase):
defset_input_line(self,line):
"""Set current input line of the test REPL."""
self.repl.current_line=line
self.repl.cursor_offset=len(line)
defsetUp(self):
self.repl=FakeRepl()
deftest_current_string(self):
self.set_input_line('a = "2"')
# TODO factor cpos out of repl.Repl
self.repl.cpos=0
self.assertEqual(self.repl.current_string(),'"2"')
self.set_input_line('a = "2" + 2')
self.assertEqual(self.repl.current_string(),"")
deftest_push(self):
self.repl=FakeRepl()
self.repl.push("foobar = 2")
self.assertEqual(self.repl.interp.locals["foobar"],2)
# COMPLETE TESTS
# 1. Global tests
deftest_simple_global_complete(self):
self.repl=FakeRepl(
{"autocomplete_mode":autocomplete.AutocompleteModes.SIMPLE}
)
self.set_input_line("d")
self.assertTrue(self.repl.complete())
self.assertTrue(hasattr(self.repl.matches_iter,"matches"))
self.assertEqual(
self.repl.matches_iter.matches,
["def","del","delattr(","dict(","dir(","divmod("],
)
deftest_substring_global_complete(self):
self.repl=FakeRepl(
{"autocomplete_mode":autocomplete.AutocompleteModes.SUBSTRING}
)
self.set_input_line("time")
self.assertTrue(self.repl.complete())
self.assertTrue(hasattr(self.repl.matches_iter,"matches"))
self.assertEqual(
self.repl.matches_iter.matches, ["RuntimeError(","RuntimeWarning("]
)
deftest_fuzzy_global_complete(self):
self.repl=FakeRepl(
{"autocomplete_mode":autocomplete.AutocompleteModes.FUZZY}
)
self.set_input_line("doc")
self.assertTrue(self.repl.complete())
self.assertTrue(hasattr(self.repl.matches_iter,"matches"))
self.assertEqual(
self.repl.matches_iter.matches,
["ChildProcessError(","UnboundLocalError(","__doc__"],
)
# 2. Attribute tests
deftest_simple_attribute_complete(self):
self.repl=FakeRepl(
{"autocomplete_mode":autocomplete.AutocompleteModes.SIMPLE}
)
self.set_input_line("Foo.b")
code="class Foo():\n\tdef bar(self):\n\t\tpass\n"
forlineincode.split("\n"):
self.repl.push(line)
self.assertTrue(self.repl.complete())
self.assertTrue(hasattr(self.repl.matches_iter,"matches"))
self.assertEqual(self.repl.matches_iter.matches, ["Foo.bar"])
deftest_substring_attribute_complete(self):
self.repl=FakeRepl(
{"autocomplete_mode":autocomplete.AutocompleteModes.SUBSTRING}
)
self.set_input_line("Foo.az")
code="class Foo():\n\tdef baz(self):\n\t\tpass\n"
forlineincode.split("\n"):
self.repl.push(line)
self.assertTrue(self.repl.complete())
self.assertTrue(hasattr(self.repl.matches_iter,"matches"))
self.assertEqual(self.repl.matches_iter.matches, ["Foo.baz"])
deftest_fuzzy_attribute_complete(self):
self.repl=FakeRepl(
{"autocomplete_mode":autocomplete.AutocompleteModes.FUZZY}
)
self.set_input_line("Foo.br")
code="class Foo():\n\tdef bar(self):\n\t\tpass\n"
forlineincode.split("\n"):
self.repl.push(line)
self.assertTrue(self.repl.complete())
self.assertTrue(hasattr(self.repl.matches_iter,"matches"))
self.assertEqual(self.repl.matches_iter.matches, ["Foo.bar"])
# 3. Edge cases
deftest_updating_namespace_complete(self):
self.repl=FakeRepl(
{"autocomplete_mode":autocomplete.AutocompleteModes.SIMPLE}
)
self.set_input_line("foo")
self.repl.push("foobar = 2")
self.assertTrue(self.repl.complete())
self.assertTrue(hasattr(self.repl.matches_iter,"matches"))
self.assertEqual(self.repl.matches_iter.matches, ["foobar"])
deftest_file_should_not_appear_in_complete(self):
self.repl=FakeRepl(
{"autocomplete_mode":autocomplete.AutocompleteModes.SIMPLE}
)
self.set_input_line("_")
self.assertTrue(self.repl.complete())
self.assertTrue(hasattr(self.repl.matches_iter,"matches"))
self.assertNotIn("__file__",self.repl.matches_iter.matches)
# 4. Parameter names
deftest_paremeter_name_completion(self):
self.repl=FakeRepl(
{"autocomplete_mode":autocomplete.AutocompleteModes.SIMPLE}
)
self.set_input_line("foo(ab")
code="def foo(abc=1, abd=2, xyz=3):\n\tpass\n"
forlineincode.split("\n"):
self.repl.push(line)
self.assertTrue(self.repl.complete())
self.assertTrue(hasattr(self.repl.matches_iter,"matches"))
self.assertEqual(
self.repl.matches_iter.matches, ["abc=","abd=","abs("]
)
classTestCliRepl(unittest.TestCase):
defsetUp(self):
self.repl=FakeCliRepl()
deftest_atbol(self):
self.assertTrue(self.repl.atbol())
self.repl.s="\t\t"
self.assertTrue(self.repl.atbol())
self.repl.s="\t\tnot an empty line"
self.assertFalse(self.repl.atbol())
deftest_addstr(self):
self.repl.complete=mock.Mock(True)
self.repl.s="foo"
self.repl.addstr("bar")
self.assertEqual(self.repl.s,"foobar")
self.repl.cpos=3
self.repl.addstr("buzz")
self.assertEqual(self.repl.s,"foobuzzbar")
classTestCliReplTab(unittest.TestCase):
defsetUp(self):
self.repl=FakeCliRepl()
# 3 Types of tab complete
deftest_simple_tab_complete(self):
self.repl.matches_iter=MagicIterMock()
self.repl.matches_iter.__bool__.return_value=False
self.repl.complete=mock.Mock()
self.repl.print_line=mock.Mock()
self.repl.matches_iter.is_cseq.return_value=False
self.repl.show_list=mock.Mock()
self.repl.funcprops=mock.Mock()
self.repl.arg_pos=mock.Mock()
self.repl.matches_iter.cur_line.return_value= (None,"foobar")
self.repl.s="foo"
self.repl.tab()
self.assertTrue(self.repl.complete.called)
self.repl.complete.assert_called_with(tab=True)
self.assertEqual(self.repl.s,"foobar")
@unittest.skip("disabled while non-simple completion is disabled")
deftest_substring_tab_complete(self):
self.repl.s="bar"
self.repl.config.autocomplete_mode= (
autocomplete.AutocompleteModes.FUZZY
)
self.repl.tab()
self.assertEqual(self.repl.s,"foobar")
self.repl.tab()
self.assertEqual(self.repl.s,"foofoobar")
@unittest.skip("disabled while non-simple completion is disabled")
deftest_fuzzy_tab_complete(self):
self.repl.s="br"
self.repl.config.autocomplete_mode= (
autocomplete.AutocompleteModes.FUZZY
)
self.repl.tab()
self.assertEqual(self.repl.s,"foobar")
# Edge Cases
deftest_normal_tab(self):
"""make sure pressing the tab key will
still in some cases add a tab"""
self.repl.s=""
self.repl.config=mock.Mock()
self.repl.config.tab_length=4
self.repl.complete=mock.Mock()
self.repl.print_line=mock.Mock()
self.repl.tab()
self.assertEqual(self.repl.s," ")
deftest_back_parameter(self):
self.repl.matches_iter=mock.Mock()
self.repl.matches_iter.matches=True
self.repl.matches_iter.previous.return_value="previtem"
self.repl.matches_iter.is_cseq.return_value=False
self.repl.show_list=mock.Mock()
self.repl.funcprops=mock.Mock()
self.repl.arg_pos=mock.Mock()
self.repl.matches_iter.cur_line.return_value= (None,"previtem")
self.repl.print_line=mock.Mock()
self.repl.s="foo"
self.repl.cpos=0
self.repl.tab(back=True)
self.assertTrue(self.repl.matches_iter.previous.called)
self.assertTrue(self.repl.s,"previtem")
# Attribute Tests
@unittest.skip("disabled while non-simple completion is disabled")
deftest_fuzzy_attribute_tab_complete(self):
"""Test fuzzy attribute with no text"""
self.repl.s="Foo."
self.repl.config.autocomplete_mode= (
autocomplete.AutocompleteModes.FUZZY
)
self.repl.tab()
self.assertEqual(self.repl.s,"Foo.foobar")
@unittest.skip("disabled while non-simple completion is disabled")
deftest_fuzzy_attribute_tab_complete2(self):
"""Test fuzzy attribute with some text"""
self.repl.s="Foo.br"
self.repl.config.autocomplete_mode= (
autocomplete.AutocompleteModes.FUZZY
)
self.repl.tab()
self.assertEqual(self.repl.s,"Foo.foobar")
# Expand Tests
deftest_simple_expand(self):
self.repl.s="f"
self.cpos=0
self.repl.matches_iter=mock.Mock()
self.repl.matches_iter.is_cseq.return_value=True
self.repl.matches_iter.substitute_cseq.return_value= (3,"foo")
self.repl.print_line=mock.Mock()
self.repl.tab()
self.assertEqual(self.repl.s,"foo")
@unittest.skip("disabled while non-simple completion is disabled")
deftest_substring_expand_forward(self):
self.repl.config.autocomplete_mode= (
autocomplete.AutocompleteModes.SUBSTRING
)
self.repl.s="ba"
self.repl.tab()
self.assertEqual(self.repl.s,"bar")
@unittest.skip("disabled while non-simple completion is disabled")
deftest_fuzzy_expand(self):
pass
if__name__=="__main__":
unittest.main()