Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: tools/telemetry/third_party/rope/ropetest/contrib/codeassisttest.py

Issue 1132103009: Example of refactoring using rope library. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # coding: utf-8
2
3 import os.path
4 try:
5 import unittest2 as unittest
6 except ImportError:
7 import unittest
8
9 from rope.base import exceptions
10 from rope.contrib.codeassist import (get_definition_location, get_doc,
11 starting_expression, code_assist,
12 sorted_proposals, starting_offset,
13 get_calltip, get_canonical_path)
14 from ropetest import testutils
15
16 try:
17 unicode
18 except NameError:
19 unicode = str
20
21 class CodeAssistTest(unittest.TestCase):
22
23 def setUp(self):
24 super(CodeAssistTest, self).setUp()
25 self.project = testutils.sample_project()
26
27 def tearDown(self):
28 testutils.remove_project(self.project)
29 super(CodeAssistTest, self).tearDown()
30
31 def _assist(self, code, offset=None, **args):
32 if offset is None:
33 offset = len(code)
34 return code_assist(self.project, code, offset, **args)
35
36 def test_simple_assist(self):
37 self._assist('', 0)
38
39 def assert_completion_in_result(self, name, scope, result, type=None):
40 for proposal in result:
41 if proposal.name == name:
42 self.assertEqual(scope, proposal.scope,
43 "proposal <%s> has wrong scope, expected "
44 "%r, got %r" % (name, scope, proposal.scope))
45 if type is not None:
46 self.assertEqual(type, proposal.type,
47 "proposal <%s> has wrong type, expected "
48 "%r, got %r" %
49 (name, type, proposal.type))
50 return
51 self.fail('completion <%s> not proposed' % name)
52
53 def assert_completion_not_in_result(self, name, scope, result):
54 for proposal in result:
55 if proposal.name == name and proposal.scope == scope:
56 self.fail('completion <%s> was proposed' % name)
57
58 def test_completing_global_variables(self):
59 code = 'my_global = 10\nt = my'
60 result = self._assist(code)
61 self.assert_completion_in_result('my_global', 'global', result)
62
63 def test_not_proposing_unmatched_vars(self):
64 code = 'my_global = 10\nt = you'
65 result = self._assist(code)
66 self.assert_completion_not_in_result('my_global', 'global', result)
67
68 def test_not_proposing_unmatched_vars_with_underlined_starting(self):
69 code = 'my_global = 10\nt = your_'
70 result = self._assist(code)
71 self.assert_completion_not_in_result('my_global', 'global', result)
72
73 def test_not_proposing_local_assigns_as_global_completions(self):
74 code = 'def f(): my_global = 10\nt = my_'
75 result = self._assist(code)
76 self.assert_completion_not_in_result('my_global', 'global', result)
77
78 def test_proposing_functions(self):
79 code = 'def my_func(): return 2\nt = my_'
80 result = self._assist(code)
81 self.assert_completion_in_result('my_func', 'global', result)
82
83 def test_proposing_classes(self):
84 code = 'class Sample(object): pass\nt = Sam'
85 result = self._assist(code)
86 self.assert_completion_in_result('Sample', 'global', result)
87
88 def test_proposing_each_name_at_most_once(self):
89 code = 'variable = 10\nvariable = 20\nt = vari'
90 result = self._assist(code)
91 count = len([x for x in result
92 if x.name == 'variable' and x.scope == 'global'])
93 self.assertEquals(1, count)
94
95 def test_throwing_exception_in_case_of_syntax_errors(self):
96 code = 'sample (sdf+)\n'
97 with self.assertRaises(exceptions.ModuleSyntaxError):
98 self._assist(code, maxfixes=0)
99
100 def test_fixing_errors_with_maxfixes(self):
101 code = 'def f():\n sldj sldj\ndef g():\n ran'
102 result = self._assist(code, maxfixes=2)
103 self.assertTrue(len(result) > 0)
104
105 def test_ignoring_errors_in_current_line(self):
106 code = 'def my_func():\n return 2\nt = '
107 result = self._assist(code)
108 self.assert_completion_in_result('my_func', 'global', result)
109
110 def test_not_reporting_variables_in_current_line(self):
111 code = 'def my_func(): return 2\nt = my_'
112 result = self._assist(code)
113 self.assert_completion_not_in_result('my_', 'global', result)
114
115 def test_completion_result(self):
116 code = 'my_global = 10\nt = my'
117 self.assertEquals(len(code) - 2, starting_offset(code, len(code)))
118
119 def test_completing_imported_names(self):
120 code = 'import sys\na = sy'
121 result = self._assist(code)
122 self.assert_completion_in_result('sys', 'imported', result)
123
124 def test_completing_imported_names_with_as(self):
125 code = 'import sys as mysys\na = mys'
126 result = self._assist(code)
127 self.assert_completion_in_result('mysys', 'imported', result)
128
129 def test_not_completing_imported_names_with_as(self):
130 code = 'import sys as mysys\na = sy'
131 result = self._assist(code)
132 self.assert_completion_not_in_result('sys', 'global', result)
133
134 def test_including_matching_builtins_types(self):
135 code = 'my_var = Excep'
136 result = self._assist(code)
137 self.assert_completion_in_result('Exception', 'builtin', result)
138 self.assert_completion_not_in_result('zip', 'builtin', result)
139
140 def test_including_matching_builtins_functions(self):
141 code = 'my_var = zi'
142 result = self._assist(code)
143 self.assert_completion_in_result('zip', 'builtin', result)
144
145 def test_builtin_instances(self):
146 # ``import_dynload_stdmods`` pref is disabled for test project.
147 # we need to have it enabled to make pycore._find_module()
148 # load ``sys`` module.
149 self.project.prefs['import_dynload_stdmods'] = True
150 code = 'from sys import stdout\nstdout.wr'
151 result = self._assist(code)
152 self.assert_completion_in_result('write', 'builtin', result)
153 self.assert_completion_in_result('writelines', 'builtin', result)
154
155 def test_including_keywords(self):
156 code = 'fo'
157 result = self._assist(code)
158 self.assert_completion_in_result('for', 'keyword', result)
159
160 def test_not_reporting_proposals_after_dot(self):
161 code = 'a_dict = {}\nkey = 3\na_dict.ke'
162 result = self._assist(code)
163 self.assert_completion_not_in_result('key', 'global', result)
164
165 def test_proposing_local_variables_in_functions(self):
166 code = 'def f(self):\n my_var = 10\n my_'
167 result = self._assist(code)
168 self.assert_completion_in_result('my_var', 'local', result)
169
170 def test_local_variables_override_global_ones(self):
171 code = 'my_var = 20\ndef f(self):\n my_var = 10\n my_'
172 result = self._assist(code)
173 self.assert_completion_in_result('my_var', 'local', result)
174
175 def test_not_including_class_body_variables(self):
176 code = 'class C(object):\n my_var = 20\n' \
177 ' def f(self):\n a = 20\n my_'
178 result = self._assist(code)
179 self.assert_completion_not_in_result('my_var', 'local', result)
180
181 def test_nested_functions(self):
182 code = 'def my_func():\n func_var = 20\n ' \
183 'def inner_func():\n a = 20\n func'
184 result = self._assist(code)
185 self.assert_completion_in_result('func_var', 'local', result)
186
187 def test_scope_endpoint_selection(self):
188 code = "def my_func():\n func_var = 20\n"
189 result = self._assist(code)
190 self.assert_completion_not_in_result('func_var', 'local', result)
191
192 def test_scope_better_endpoint_selection(self):
193 code = "if True:\n def f():\n my_var = 10\n my_"
194 result = self._assist(code)
195 self.assert_completion_not_in_result('my_var', 'local', result)
196
197 def test_imports_inside_function(self):
198 code = "def f():\n import sys\n sy"
199 result = self._assist(code)
200 self.assert_completion_in_result('sys', 'imported', result)
201
202 def test_imports_inside_function_dont_mix_with_globals(self):
203 code = "def f():\n import sys\nsy"
204 result = self._assist(code)
205 self.assert_completion_not_in_result('sys', 'local', result)
206
207 def test_nested_classes_local_names(self):
208 code = 'global_var = 10\n' \
209 'def my_func():\n' \
210 ' func_var = 20\n' \
211 ' class C(object):\n' \
212 ' def another_func(self):\n' \
213 ' local_var = 10\n' \
214 ' func'
215 result = self._assist(code)
216 self.assert_completion_in_result('func_var', 'local', result)
217
218 def test_nested_classes_global(self):
219 code = 'global_var = 10\n' \
220 'def my_func():\n' \
221 ' func_var = 20\n' \
222 ' class C(object):\n' \
223 ' def another_func(self):\n' \
224 ' local_var = 10\n' \
225 ' globa'
226 result = self._assist(code)
227 self.assert_completion_in_result('global_var', 'global', result)
228
229 def test_nested_classes_global_function(self):
230 code = 'global_var = 10\n' \
231 'def my_func():\n' \
232 ' func_var = 20\n' \
233 ' class C(object):\n' \
234 ' def another_func(self):\n' \
235 ' local_var = 10\n' \
236 ' my_f'
237 result = self._assist(code)
238 self.assert_completion_in_result('my_func', 'global', result)
239
240 def test_proposing_function_parameters_in_functions(self):
241 code = 'def my_func(my_param):\n my_var = 20\n my_'
242 result = self._assist(code)
243 self.assert_completion_in_result('my_param', 'local', result)
244
245 def test_proposing_function_keyword_parameters_in_functions(self):
246 code = 'def my_func(my_param, *my_list, **my_kws):\n' \
247 ' my_var = 20\n' \
248 ' my_'
249 result = self._assist(code)
250 self.assert_completion_in_result('my_param', 'local', result)
251 self.assert_completion_in_result('my_list', 'local', result)
252 self.assert_completion_in_result('my_kws', 'local', result)
253
254 def test_not_proposing_unmatching_function_parameters_in_functions(self):
255 code = "def my_func(my_param):\n my_var = 20\n you_"
256 result = self._assist(code)
257 self.assert_completion_not_in_result('my_param', 'local', result)
258
259 def test_ignoring_current_statement(self):
260 code = "my_var = 10\nmy_tuple = (10, \n my_"
261 result = self._assist(code)
262 self.assert_completion_in_result('my_var', 'global', result)
263
264 def test_ignoring_current_statement_brackets_continuation(self):
265 code = "my_var = 10\n'hello'[10:\n my_"
266 result = self._assist(code)
267 self.assert_completion_in_result('my_var', 'global', result)
268
269 def test_ignoring_current_statement_explicit_continuation(self):
270 code = "my_var = 10\nmy_var2 = 2 + \\\n my_"
271 result = self._assist(code)
272 self.assert_completion_in_result('my_var', 'global', result)
273
274 def test_ignor_current_statement_while_the_first_stmnt_of_the_block(self):
275 code = "my_var = 10\ndef f():\n my_"
276 result = self._assist(code)
277 self.assert_completion_in_result('my_var', 'global', result)
278
279 def test_ignor_current_stmnt_while_current_line_ends_with_a_colon(self):
280 code = "my_var = 10\nif my_:\n pass"
281 result = self._assist(code, 18)
282 self.assert_completion_in_result('my_var', 'global', result)
283
284 def test_ignoring_string_contents(self):
285 code = "my_var = '('\nmy_"
286 result = self._assist(code)
287 self.assert_completion_in_result('my_var', 'global', result)
288
289 def test_ignoring_comment_contents(self):
290 code = "my_var = 10 #(\nmy_"
291 result = self._assist(code)
292 self.assert_completion_in_result('my_var', 'global', result)
293
294 def test_ignoring_string_contents_backslash_plus_quotes(self):
295 code = "my_var = '\\''\nmy_"
296 result = self._assist(code)
297 self.assert_completion_in_result('my_var', 'global', result)
298
299 def test_ignoring_string_contents_backslash_plus_backslash(self):
300 code = "my_var = '\\\\'\nmy_"
301 result = self._assist(code)
302 self.assert_completion_in_result('my_var', 'global', result)
303
304 def test_not_proposing_later_defined_variables_in_current_block(self):
305 code = "my_\nmy_var = 10\n"
306 result = self._assist(code, 3, later_locals=False)
307 self.assert_completion_not_in_result('my_var', 'global', result)
308
309 def test_not_proposing_later_defined_variables_in_current_function(self):
310 code = "def f():\n my_\n my_var = 10\n"
311 result = self._assist(code, 16, later_locals=False)
312 self.assert_completion_not_in_result('my_var', 'local', result)
313
314 def test_ignoring_string_contents_with_triple_quotes(self):
315 code = "my_var = '''(\n'('''\nmy_"
316 result = self._assist(code)
317 self.assert_completion_in_result('my_var', 'global', result)
318
319 def test_ignoring_string_contents_with_triple_quotes_and_backslash(self):
320 code = 'my_var = """\\"""("""\nmy_'
321 result = self._assist(code)
322 self.assert_completion_in_result('my_var', 'global', result)
323
324 def test_ignor_str_contents_with_triple_quotes_and_double_backslash(self):
325 code = 'my_var = """\\\\"""\nmy_'
326 result = self._assist(code)
327 self.assert_completion_in_result('my_var', 'global', result)
328
329 def test_reporting_params_when_in_the_first_line_of_a_function(self):
330 code = 'def f(param):\n para'
331 result = self._assist(code)
332 self.assert_completion_in_result('param', 'local', result)
333
334 def test_code_assist_when_having_a_two_line_function_header(self):
335 code = 'def f(param1,\n param2):\n para'
336 result = self._assist(code)
337 self.assert_completion_in_result('param1', 'local', result)
338
339 def test_code_assist_with_function_with_two_line_return(self):
340 code = 'def f(param1, param2):\n return(param1,\n para'
341 result = self._assist(code)
342 self.assert_completion_in_result('param2', 'local', result)
343
344 def test_get_definition_location(self):
345 code = 'def a_func():\n pass\na_func()'
346 result = get_definition_location(self.project, code, len(code) - 3)
347 self.assertEquals((None, 1), result)
348
349 def test_get_definition_location_underlined_names(self):
350 code = 'def a_sample_func():\n pass\na_sample_func()'
351 result = get_definition_location(self.project, code, len(code) - 11)
352 self.assertEquals((None, 1), result)
353
354 def test_get_definition_location_dotted_names(self):
355 code = 'class AClass(object):\n' \
356 ' @staticmethod\n' \
357 ' def a_method():\n' \
358 ' pass\n' \
359 'AClass.a_method()'
360 result = get_definition_location(self.project, code, len(code) - 3)
361 self.assertEquals((None, 2), result)
362
363 def test_get_definition_location_dotted_module_names(self):
364 module_resource = testutils.create_module(self.project, 'mod')
365 module_resource.write('def a_func():\n pass\n')
366 code = 'import mod\nmod.a_func()'
367 result = get_definition_location(self.project, code, len(code) - 3)
368 self.assertEquals((module_resource, 1), result)
369
370 def test_get_definition_location_for_nested_packages(self):
371 mod1 = testutils.create_module(self.project, 'mod1')
372 pkg1 = testutils.create_package(self.project, 'pkg1')
373 pkg2 = testutils.create_package(self.project, 'pkg2', pkg1)
374 mod1.write('import pkg1.pkg2.mod2')
375
376 init_dot_py = pkg2.get_child('__init__.py')
377 found_pyname = get_definition_location(self.project, mod1.read(),
378 mod1.read().index('pkg2') + 1)
379 self.assertEquals(init_dot_py, found_pyname[0])
380
381 def test_get_definition_location_unknown(self):
382 code = 'a_func()\n'
383 result = get_definition_location(self.project, code, len(code) - 3)
384 self.assertEquals((None, None), result)
385
386 def test_get_definition_location_dot_spaces(self):
387 code = 'class AClass(object):\n ' \
388 '@staticmethod\n def a_method():\n' \
389 ' pass\nAClass.\\\n a_method()'
390 result = get_definition_location(self.project, code, len(code) - 3)
391 self.assertEquals((None, 2), result)
392
393 def test_get_definition_location_dot_line_break_inside_parens(self):
394 code = 'class A(object):\n def a_method(self):\n pass\n' + \
395 '(A.\na_method)'
396 result = get_definition_location(self.project, code,
397 code.rindex('a_method') + 1)
398 self.assertEquals((None, 2), result)
399
400 def test_if_scopes_in_other_scopes_for_get_definition_location(self):
401 code = 'def f(a_var):\n pass\na_var = 10\n' \
402 'if True:\n print a_var\n'
403 result = get_definition_location(self.project, code, len(code) - 3)
404 self.assertEquals((None, 3), result)
405
406 def test_code_assists_in_parens(self):
407 code = 'def a_func(a_var):\n pass\na_var = 10\na_func(a_'
408 result = self._assist(code)
409 self.assert_completion_in_result('a_var', 'global', result)
410
411 def test_simple_type_inferencing(self):
412 code = 'class Sample(object):\n' \
413 ' def __init__(self, a_param):\n' \
414 ' pass\n' \
415 ' def a_method(self):\n' \
416 ' pass\n' \
417 'Sample("hey").a_'
418 result = self._assist(code)
419 self.assert_completion_in_result('a_method', 'attribute', result)
420
421 def test_proposals_sorter(self):
422 code = 'def my_sample_function(self):\n' + \
423 ' my_sample_var = 20\n' + \
424 ' my_sample_'
425 proposals = sorted_proposals(self._assist(code))
426 self.assertEquals('my_sample_var', proposals[0].name)
427 self.assertEquals('my_sample_function', proposals[1].name)
428
429 def test_proposals_sorter_for_methods_and_attributes(self):
430 code = 'class A(object):\n' + \
431 ' def __init__(self):\n' + \
432 ' self.my_a_var = 10\n' + \
433 ' def my_b_func(self):\n' + \
434 ' pass\n' + \
435 ' def my_c_func(self):\n' + \
436 ' pass\n' + \
437 'a_var = A()\n' + \
438 'a_var.my_'
439 proposals = sorted_proposals(self._assist(code))
440 self.assertEquals('my_b_func', proposals[0].name)
441 self.assertEquals('my_c_func', proposals[1].name)
442 self.assertEquals('my_a_var', proposals[2].name)
443
444 def test_proposals_sorter_for_global_methods_and_funcs(self):
445 code = 'def my_b_func(self):\n' + \
446 ' pass\n' + \
447 'my_a_var = 10\n' + \
448 'my_'
449 proposals = sorted_proposals(self._assist(code))
450 self.assertEquals('my_b_func', proposals[0].name)
451 self.assertEquals('my_a_var', proposals[1].name)
452
453 def test_proposals_sorter_underlined_methods(self):
454 code = 'class A(object):\n' + \
455 ' def _my_func(self):\n' + \
456 ' self.my_a_var = 10\n' + \
457 ' def my_func(self):\n' + \
458 ' pass\n' + \
459 'a_var = A()\n' + \
460 'a_var.'
461 proposals = sorted_proposals(self._assist(code))
462 self.assertEquals('my_func', proposals[0].name)
463 self.assertEquals('_my_func', proposals[1].name)
464
465 def test_proposals_sorter_and_scope_prefs(self):
466 code = 'my_global_var = 1\n' \
467 'def func(self):\n' \
468 ' my_local_var = 2\n' \
469 ' my_'
470 result = self._assist(code)
471 proposals = sorted_proposals(result, scopepref=['global', 'local'])
472 self.assertEquals('my_global_var', proposals[0].name)
473 self.assertEquals('my_local_var', proposals[1].name)
474
475 def test_proposals_sorter_and_type_prefs(self):
476 code = 'my_global_var = 1\n' \
477 'def my_global_func(self):\n' \
478 ' pass\n' \
479 'my_'
480 result = self._assist(code)
481 proposals = sorted_proposals(result, typepref=['instance', 'function'])
482 self.assertEquals('my_global_var', proposals[0].name)
483 self.assertEquals('my_global_func', proposals[1].name)
484
485 def test_proposals_sorter_and_missing_type_in_typepref(self):
486 code = 'my_global_var = 1\n' \
487 'def my_global_func():\n' \
488 ' pass\n' \
489 'my_'
490 result = self._assist(code)
491 proposals = sorted_proposals(result, typepref=['function']) # noqa
492
493 def test_get_pydoc_unicode(self):
494 src = u'# coding: utf-8\ndef foo():\n u"юникод-объект"'
495 doc = get_doc(self.project, src, src.index('foo') + 1)
496 self.assertTrue(isinstance(doc, unicode))
497 self.assertTrue(u'юникод-объект' in doc)
498
499 def test_get_pydoc_utf8_bytestring(self):
500 src = u'# coding: utf-8\ndef foo():\n "байтстринг"'
501 doc = get_doc(self.project, src, src.index('foo') + 1)
502 self.assertTrue(isinstance(doc, unicode))
503 self.assertTrue(u'байтстринг' in doc)
504
505 def test_get_pydoc_for_functions(self):
506 src = 'def a_func():\n' \
507 ' """a function"""\n' \
508 ' a_var = 10\n' \
509 'a_func()'
510 self.assertTrue(get_doc(self.project, src, len(src) - 4).
511 endswith('a function'))
512 get_doc(self.project, src, len(src) - 4).index('a_func()')
513
514 def test_get_pydoc_for_classes(self):
515 src = 'class AClass(object):\n pass\n'
516 get_doc(self.project, src, src.index('AClass') + 1).index('AClass')
517
518 def test_get_pydoc_for_classes_with_init(self):
519 src = 'class AClass(object):\n def __init__(self):\n pass\n'
520 get_doc(self.project, src, src.index('AClass') + 1).index('AClass')
521
522 def test_get_pydoc_for_modules(self):
523 mod = testutils.create_module(self.project, 'mod')
524 mod.write('"""a module"""\n')
525 src = 'import mod\nmod'
526 self.assertEquals('a module', get_doc(self.project, src, len(src) - 1))
527
528 def test_get_pydoc_for_builtins(self):
529 src = 'print(object)\n'
530 self.assertTrue(get_doc(self.project, src,
531 src.index('obj')) is not None)
532
533 def test_get_pydoc_for_methods_should_include_class_name(self):
534 src = 'class AClass(object):\n' \
535 ' def a_method(self):\n'\
536 ' """hey"""\n' \
537 ' pass\n'
538 doc = get_doc(self.project, src, src.index('a_method') + 1)
539 doc.index('AClass.a_method')
540 doc.index('hey')
541
542 def test_get_pydoc_for_meths_should_inc_methods_from_super_classes(self):
543 src = 'class A(object):\n' \
544 ' def a_method(self):\n' \
545 ' """hey1"""\n' \
546 ' pass\n' \
547 'class B(A):\n' \
548 ' def a_method(self):\n' \
549 ' """hey2"""\n' \
550 ' pass\n'
551 doc = get_doc(self.project, src, src.rindex('a_method') + 1)
552 doc.index('A.a_method')
553 doc.index('hey1')
554 doc.index('B.a_method')
555 doc.index('hey2')
556
557 def test_get_pydoc_for_classes_should_name_super_classes(self):
558 src = 'class A(object):\n pass\n' \
559 'class B(A):\n pass\n'
560 doc = get_doc(self.project, src, src.rindex('B') + 1)
561 doc.index('B(A)')
562
563 def test_get_pydoc_for_builtin_functions(self):
564 src = 's = "hey"\ns.replace\n'
565 doc = get_doc(self.project, src, src.rindex('replace') + 1)
566 self.assertTrue(doc is not None)
567
568 def test_commenting_errors_before_offset(self):
569 src = 'lsjd lsjdf\ns = "hey"\ns.replace()\n'
570 doc = get_doc(self.project, src, src.rindex('replace') + 1) # noqa
571
572 def test_proposing_variables_defined_till_the_end_of_scope(self):
573 code = 'if True:\n a_v\na_var = 10\n'
574 result = self._assist(code, code.index('a_v') + 3)
575 self.assert_completion_in_result('a_var', 'global', result)
576
577 def test_completing_in_uncomplete_try_blocks(self):
578 code = 'try:\n a_var = 10\n a_'
579 result = self._assist(code)
580 self.assert_completion_in_result('a_var', 'global', result)
581
582 def test_completing_in_uncomplete_try_blocks_in_functions(self):
583 code = 'def a_func():\n try:\n a_var = 10\n a_'
584 result = self._assist(code)
585 self.assert_completion_in_result('a_var', 'local', result)
586
587 def test_already_complete_try_blocks_with_finally(self):
588 code = 'def a_func():\n try:\n a_var = 10\n a_'
589 result = self._assist(code)
590 self.assert_completion_in_result('a_var', 'local', result)
591
592 def test_already_complete_try_blocks_with_finally2(self):
593 code = 'try:\n a_var = 10\n a_\nfinally:\n pass\n'
594 result = self._assist(code, code.rindex('a_') + 2)
595 self.assert_completion_in_result('a_var', 'global', result)
596
597 def test_already_complete_try_blocks_with_except(self):
598 code = 'try:\n a_var = 10\n a_\nexcept Exception:\n pass\n'
599 result = self._assist(code, code.rindex('a_') + 2)
600 self.assert_completion_in_result('a_var', 'global', result)
601
602 def test_already_complete_try_blocks_with_except2(self):
603 code = 'a_var = 10\ntry:\n ' \
604 'another_var = a_\n another_var = 10\n' \
605 'except Exception:\n pass\n'
606 result = self._assist(code, code.rindex('a_') + 2)
607 self.assert_completion_in_result('a_var', 'global', result)
608
609 def test_completing_ifs_in_uncomplete_try_blocks(self):
610 code = 'try:\n if True:\n a_var = 10\n a_'
611 result = self._assist(code)
612 self.assert_completion_in_result('a_var', 'global', result)
613
614 def test_completing_ifs_in_uncomplete_try_blocks2(self):
615 code = 'try:\n if True:\n a_var = 10\n a_'
616 result = self._assist(code)
617 self.assert_completion_in_result('a_var', 'global', result)
618
619 def test_completing_excepts_in_uncomplete_try_blocks(self):
620 code = 'try:\n pass\nexcept Exc'
621 result = self._assist(code)
622 self.assert_completion_in_result('Exception', 'builtin', result)
623
624 def test_and_normal_complete_blocks_and_single_fixing(self):
625 code = 'try:\n range.\nexcept:\n pass\n'
626 result = self._assist(code, code.index('.'), maxfixes=1) # noqa
627
628 def test_nested_blocks(self):
629 code = 'a_var = 10\ntry:\n try:\n a_v'
630 result = self._assist(code)
631 self.assert_completion_in_result('a_var', 'global', result)
632
633 def test_proposing_function_keywords_when_calling(self):
634 code = 'def f(p):\n pass\nf(p'
635 result = self._assist(code)
636 self.assert_completion_in_result('p=', 'parameter_keyword', result)
637
638 def test_proposing_function_keywords_when_calling_for_non_functions(self):
639 code = 'f = 1\nf(p'
640 result = self._assist(code) # noqa
641
642 def test_proposing_function_keywords_when_calling_extra_spaces(self):
643 code = 'def f(p):\n pass\nf( p'
644 result = self._assist(code)
645 self.assert_completion_in_result('p=', 'parameter_keyword', result)
646
647 def test_proposing_function_keywords_when_calling_on_second_argument(self):
648 code = 'def f(p1, p2):\n pass\nf(1, p'
649 result = self._assist(code)
650 self.assert_completion_in_result('p2=', 'parameter_keyword', result)
651
652 def test_proposing_function_keywords_when_calling_not_proposing_args(self):
653 code = 'def f(p1, *args):\n pass\nf(1, a'
654 result = self._assist(code)
655 self.assert_completion_not_in_result('args=', 'parameter_keyword',
656 result)
657
658 def test_propos_function_kwrds_when_call_with_no_noth_after_parens(self):
659 code = 'def f(p):\n pass\nf('
660 result = self._assist(code)
661 self.assert_completion_in_result('p=', 'parameter_keyword', result)
662
663 def test_propos_function_kwrds_when_call_with_no_noth_after_parens2(self):
664 code = 'def f(p):\n pass\ndef g():\n h = f\n f('
665 result = self._assist(code)
666 self.assert_completion_in_result('p=', 'parameter_keyword', result)
667
668 def test_codeassists_before_opening_of_parens(self):
669 code = 'def f(p):\n pass\na_var = 1\nf(1)\n'
670 result = self._assist(code, code.rindex('f') + 1)
671 self.assert_completion_not_in_result('a_var', 'global', result)
672
673 def test_codeassist_before_single_line_indents(self):
674 code = 'myvar = 1\nif True:\n (myv\nif True:\n pass\n'
675 result = self._assist(code, code.rindex('myv') + 3)
676 self.assert_completion_not_in_result('myvar', 'local', result)
677
678 def test_codeassist_before_line_indents_in_a_blank_line(self):
679 code = 'myvar = 1\nif True:\n \nif True:\n pass\n'
680 result = self._assist(code, code.rindex(' ') + 4)
681 self.assert_completion_not_in_result('myvar', 'local', result)
682
683 def test_simple_get_calltips(self):
684 src = 'def f():\n pass\nvar = f()\n'
685 doc = get_calltip(self.project, src, src.rindex('f'))
686 self.assertEquals('f()', doc)
687
688 def test_get_calltips_for_classes(self):
689 src = 'class C(object):\n' \
690 ' def __init__(self):\n pass\nC('
691 doc = get_calltip(self.project, src, len(src) - 1)
692 self.assertEquals('C.__init__(self)', doc)
693
694 def test_get_calltips_for_objects_with_call(self):
695 src = 'class C(object):\n' \
696 ' def __call__(self, p):\n pass\n' \
697 'c = C()\nc(1,'
698 doc = get_calltip(self.project, src, src.rindex('c'))
699 self.assertEquals('C.__call__(self, p)', doc)
700
701 def test_get_calltips_and_including_module_name(self):
702 src = 'class C(object):\n' \
703 ' def __call__(self, p):\n pass\n' \
704 'c = C()\nc(1,'
705 mod = testutils.create_module(self.project, 'mod')
706 mod.write(src)
707 doc = get_calltip(self.project, src, src.rindex('c'), mod)
708 self.assertEquals('mod.C.__call__(self, p)', doc)
709
710 def test_get_calltips_and_including_module_name_2(self):
711 src = 'range()\n'
712 doc = get_calltip(self.project, src, 1, ignore_unknown=True)
713 self.assertTrue(doc is None)
714
715 def test_removing_self_parameter(self):
716 src = 'class C(object):\n' \
717 ' def f(self):\n'\
718 ' pass\n' \
719 'C().f()'
720 doc = get_calltip(self.project, src, src.rindex('f'), remove_self=True)
721 self.assertEquals('C.f()', doc)
722
723 def test_removing_self_parameter_and_more_than_one_parameter(self):
724 src = 'class C(object):\n' \
725 ' def f(self, p1):\n'\
726 ' pass\n' \
727 'C().f()'
728 doc = get_calltip(self.project, src, src.rindex('f'), remove_self=True)
729 self.assertEquals('C.f(p1)', doc)
730
731 def test_lambda_calltip(self):
732 src = 'foo = lambda x, y=1: None\n' \
733 'foo()'
734 doc = get_calltip(self.project, src, src.rindex('f'))
735 self.assertEqual(doc, 'lambda(x, y)')
736
737 def test_keyword_before_parens(self):
738 code = 'if (1).:\n pass'
739 result = self._assist(code, offset=len('if (1).'))
740 self.assertTrue(result)
741
742 # TESTING PROPOSAL'S KINDS AND TYPES.
743 # SEE RELATION MATRIX IN `CompletionProposal`'s DOCSTRING
744
745 def test_local_variable_completion_proposal(self):
746 code = 'def foo():\n xvar = 5\n x'
747 result = self._assist(code)
748 self.assert_completion_in_result('xvar', 'local', result, 'instance')
749
750 def test_global_variable_completion_proposal(self):
751 code = 'yvar = 5\ny'
752 result = self._assist(code)
753 self.assert_completion_in_result('yvar', 'global', result, 'instance')
754
755 def test_builtin_variable_completion_proposal(self):
756 for varname in ('False', 'True'):
757 result = self._assist(varname[0])
758 self.assert_completion_in_result(varname, 'builtin', result,
759 type='instance')
760
761 def test_attribute_variable_completion_proposal(self):
762 code = 'class AClass(object):\n def foo(self):\n ' \
763 'self.bar = 1\n self.b'
764 result = self._assist(code)
765 self.assert_completion_in_result('bar', 'attribute', result,
766 type='instance')
767
768 def test_local_class_completion_proposal(self):
769 code = 'def foo():\n class LocalClass(object): pass\n Lo'
770 result = self._assist(code)
771 self.assert_completion_in_result('LocalClass', 'local', result,
772 type='class')
773
774 def test_global_class_completion_proposal(self):
775 code = 'class GlobalClass(object): pass\nGl'
776 result = self._assist(code)
777 self.assert_completion_in_result('GlobalClass', 'global', result,
778 type='class')
779
780 def test_builtin_class_completion_proposal(self):
781 for varname in ('object', 'dict', 'file'):
782 result = self._assist(varname[0])
783 self.assert_completion_in_result(varname, 'builtin', result,
784 type='class')
785
786 def test_attribute_class_completion_proposal(self):
787 code = 'class Outer(object):\n class Inner(object): pass\nOuter.'
788 result = self._assist(code)
789 self.assert_completion_in_result('Inner', 'attribute', result,
790 type='class')
791
792 def test_local_function_completion_proposal(self):
793 code = 'def outer():\n def inner(): pass\n in'
794 result = self._assist(code)
795 self.assert_completion_in_result('inner', 'local', result,
796 type='function')
797
798 def test_global_function_completion_proposal(self):
799 code = 'def foo(): pass\nf'
800 result = self._assist(code)
801 self.assert_completion_in_result('foo', 'global', result,
802 type='function')
803
804 def test_builtin_function_completion_proposal(self):
805 code = 'a'
806 result = self._assist(code)
807 for expected in ('all', 'any', 'abs'):
808 self.assert_completion_in_result(expected, 'builtin', result,
809 type='function')
810
811 def test_attribute_function_completion_proposal(self):
812 code = 'class Some(object):\n def method(self):\n self.'
813 result = self._assist(code)
814 self.assert_completion_in_result('method', 'attribute', result,
815 type='function')
816
817 def test_local_module_completion_proposal(self):
818 code = 'def foo():\n import types\n t'
819 result = self._assist(code)
820 self.assert_completion_in_result('types', 'imported', result,
821 type='module')
822
823 def test_global_module_completion_proposal(self):
824 code = 'import operator\no'
825 result = self._assist(code)
826 self.assert_completion_in_result('operator', 'imported', result,
827 type='module')
828
829 def test_attribute_module_completion_proposal(self):
830 code = 'class Some(object):\n import os\nSome.o'
831 result = self._assist(code)
832 self.assert_completion_in_result('os', 'imported', result,
833 type='module')
834
835 def test_builtin_exception_completion_proposal(self):
836 code = 'def blah():\n Z'
837 result = self._assist(code)
838 self.assert_completion_in_result('ZeroDivisionError', 'builtin',
839 result, type='class')
840
841 def test_keyword_completion_proposal(self):
842 code = 'f'
843 result = self._assist(code)
844 self.assert_completion_in_result('for', 'keyword', result, type=None)
845 self.assert_completion_in_result('from', 'keyword', result, type=None)
846
847 def test_parameter_keyword_completion_proposal(self):
848 code = 'def func(abc, aloha, alpha, amigo): pass\nfunc(a'
849 result = self._assist(code)
850 for expected in ('abc=', 'aloha=', 'alpha=', 'amigo='):
851 self.assert_completion_in_result(expected, 'parameter_keyword',
852 result, type=None)
853
854 def test_object_path_global(self):
855 code = 'GLOBAL_VARIABLE = 42\n'
856 resource = testutils.create_module(self.project, 'mod')
857 resource.write(code)
858 result = get_canonical_path(self.project, resource, 1)
859 mod_path = os.path.join(self.project.address, 'mod.py')
860 self.assertEquals(
861 result, [(mod_path, 'MODULE'),
862 ('GLOBAL_VARIABLE', 'VARIABLE')])
863
864 def test_object_path_attribute(self):
865 code = 'class Foo(object):\n' \
866 ' attr = 42\n'
867 resource = testutils.create_module(self.project, 'mod')
868 resource.write(code)
869 result = get_canonical_path(self.project, resource, 24)
870 mod_path = os.path.join(self.project.address, 'mod.py')
871 self.assertEquals(
872 result, [(mod_path, 'MODULE'), ('Foo', 'CLASS'),
873 ('attr', 'VARIABLE')])
874
875 def test_object_path_subclass(self):
876 code = 'class Foo(object):\n' \
877 ' class Bar(object):\n' \
878 ' pass\n'
879 resource = testutils.create_module(self.project, 'mod')
880 resource.write(code)
881 result = get_canonical_path(self.project, resource, 30)
882 mod_path = os.path.join(self.project.address, 'mod.py')
883 self.assertEquals(
884 result, [(mod_path, 'MODULE'), ('Foo', 'CLASS'),
885 ('Bar', 'CLASS')])
886
887 def test_object_path_method_parameter(self):
888 code = 'class Foo(object):\n' \
889 ' def bar(self, a, b, c):\n' \
890 ' pass\n'
891 resource = testutils.create_module(self.project, 'mod')
892 resource.write(code)
893 result = get_canonical_path(self.project, resource, 41)
894 mod_path = os.path.join(self.project.address, 'mod.py')
895 self.assertEquals(
896 result, [(mod_path, 'MODULE'), ('Foo', 'CLASS'),
897 ('bar', 'FUNCTION'), ('b', 'PARAMETER')])
898
899 def test_object_path_variable(self):
900 code = 'def bar(a):\n' \
901 ' x = a + 42\n'
902 resource = testutils.create_module(self.project, 'mod')
903 resource.write(code)
904 result = get_canonical_path(self.project, resource, 17)
905 mod_path = os.path.join(self.project.address, 'mod.py')
906 self.assertEquals(
907 result, [(mod_path, 'MODULE'), ('bar', 'FUNCTION'),
908 ('x', 'VARIABLE')])
909
910
911 class CodeAssistInProjectsTest(unittest.TestCase):
912 def setUp(self):
913 super(CodeAssistInProjectsTest, self).setUp()
914 self.project = testutils.sample_project()
915 self.pycore = self.project.pycore
916 samplemod = testutils.create_module(self.project, 'samplemod')
917 code = 'class SampleClass(object):\n' \
918 ' def sample_method():\n pass\n\n' \
919 'def sample_func():\n pass\n' \
920 'sample_var = 10\n\n' \
921 'def _underlined_func():\n pass\n\n'
922 samplemod.write(code)
923 package = testutils.create_package(self.project, 'package')
924 nestedmod = testutils.create_module(self.project, # noqa
925 'nestedmod', package)
926
927 def tearDown(self):
928 testutils.remove_project(self.project)
929 super(self.__class__, self).tearDown()
930
931 def _assist(self, code, resource=None, **kwds):
932 return code_assist(self.project, code, len(code), resource, **kwds)
933
934 def assert_completion_in_result(self, name, scope, result):
935 for proposal in result:
936 if proposal.name == name and proposal.scope == scope:
937 return
938 self.fail('completion <%s> not proposed' % name)
939
940 def assert_completion_not_in_result(self, name, scope, result):
941 for proposal in result:
942 if proposal.name == name and proposal.scope == scope:
943 self.fail('completion <%s> was proposed' % name)
944
945 def test_simple_import(self):
946 code = 'import samplemod\nsample'
947 result = self._assist(code)
948 self.assert_completion_in_result('samplemod', 'imported', result)
949
950 def test_from_import_class(self):
951 code = 'from samplemod import SampleClass\nSample'
952 result = self._assist(code)
953 self.assert_completion_in_result('SampleClass', 'imported', result)
954
955 def test_from_import_function(self):
956 code = 'from samplemod import sample_func\nsample'
957 result = self._assist(code)
958 self.assert_completion_in_result('sample_func', 'imported', result)
959
960 def test_from_import_variable(self):
961 code = 'from samplemod import sample_var\nsample'
962 result = self._assist(code)
963 self.assert_completion_in_result('sample_var', 'imported', result)
964
965 def test_from_imports_inside_functions(self):
966 code = 'def f():\n from samplemod import SampleClass\n Sample'
967 result = self._assist(code)
968 self.assert_completion_in_result('SampleClass', 'imported', result)
969
970 def test_from_import_only_imports_imported(self):
971 code = 'from samplemod import sample_func\nSample'
972 result = self._assist(code)
973 self.assert_completion_not_in_result('SampleClass', 'global', result)
974
975 def test_from_import_star(self):
976 code = 'from samplemod import *\nSample'
977 result = self._assist(code)
978 self.assert_completion_in_result('SampleClass', 'imported', result)
979
980 def test_from_import_star2(self):
981 code = 'from samplemod import *\nsample'
982 result = self._assist(code)
983 self.assert_completion_in_result('sample_func', 'imported', result)
984 self.assert_completion_in_result('sample_var', 'imported', result)
985
986 def test_from_import_star_not_imporing_underlined(self):
987 code = 'from samplemod import *\n_under'
988 result = self._assist(code)
989 self.assert_completion_not_in_result('_underlined_func', 'global',
990 result)
991
992 def test_from_package_import_mod(self):
993 code = 'from package import nestedmod\nnest'
994 result = self._assist(code)
995 self.assert_completion_in_result('nestedmod', 'imported', result)
996
997 def test_completing_after_dot(self):
998 code = 'class SampleClass(object):\n' \
999 ' def sample_method(self):\n' \
1000 ' pass\n' \
1001 'SampleClass.sam'
1002 result = self._assist(code)
1003 self.assert_completion_in_result('sample_method', 'attribute', result)
1004
1005 def test_completing_after_multiple_dots(self):
1006 code = 'class Class1(object):\n' \
1007 ' class Class2(object):\n' \
1008 ' def sample_method(self):\n' \
1009 ' pass\n' \
1010 'Class1.Class2.sam'
1011 result = self._assist(code)
1012 self.assert_completion_in_result('sample_method', 'attribute', result)
1013
1014 def test_completing_after_self_dot(self):
1015 code = 'class Sample(object):\n' \
1016 ' def method1(self):\n' \
1017 ' pass\n' \
1018 ' def method2(self):\n' \
1019 ' self.m'
1020 result = self._assist(code)
1021 self.assert_completion_in_result('method1', 'attribute', result)
1022
1023 def test_result_start_offset_for_dotted_completions(self):
1024 code = 'class Sample(object):\n' \
1025 ' def method1(self):\n' \
1026 ' pass\n' \
1027 'Sample.me'
1028 self.assertEquals(len(code) - 2, starting_offset(code, len(code)))
1029
1030 def test_backslash_after_dots(self):
1031 code = 'class Sample(object):\n' \
1032 ' def a_method(self):\n' \
1033 ' pass\n' \
1034 'Sample.\\\n a_m'
1035 result = self._assist(code)
1036 self.assert_completion_in_result('a_method', 'attribute', result)
1037
1038 def test_not_proposing_global_names_after_dot(self):
1039 code = 'class Sample(object):\n' \
1040 ' def a_method(self):\n' \
1041 ' pass\n' \
1042 'Sample.'
1043 result = self._assist(code)
1044 self.assert_completion_not_in_result('Sample', 'global', result)
1045
1046 def test_assist_on_relative_imports(self):
1047 pkg = testutils.create_package(self.project, 'pkg')
1048 mod1 = testutils.create_module(self.project, 'mod1', pkg)
1049 mod2 = testutils.create_module(self.project, 'mod2', pkg)
1050 mod1.write('def a_func():\n pass\n')
1051 code = 'import mod1\nmod1.'
1052 result = self._assist(code, resource=mod2)
1053 self.assert_completion_in_result('a_func', 'imported', result)
1054
1055 def test_get_location_on_relative_imports(self):
1056 pkg = testutils.create_package(self.project, 'pkg')
1057 mod1 = testutils.create_module(self.project, 'mod1', pkg)
1058 mod2 = testutils.create_module(self.project, 'mod2', pkg)
1059 mod1.write('def a_func():\n pass\n')
1060 code = 'import mod1\nmod1.a_func\n'
1061 result = get_definition_location(self.project, code,
1062 len(code) - 2, mod2)
1063 self.assertEquals((mod1, 1), result)
1064
1065 def test_get_definition_location_for_builtins(self):
1066 code = 'import sys\n'
1067 result = get_definition_location(self.project, code,
1068 len(code) - 2)
1069 self.assertEquals((None, None), result)
1070
1071 def test_get_doc_on_relative_imports(self):
1072 pkg = testutils.create_package(self.project, 'pkg')
1073 mod1 = testutils.create_module(self.project, 'mod1', pkg)
1074 mod2 = testutils.create_module(self.project, 'mod2', pkg)
1075 mod1.write('def a_func():\n """hey"""\n pass\n')
1076 code = 'import mod1\nmod1.a_func\n'
1077 result = get_doc(self.project, code, len(code) - 2, mod2)
1078 self.assertTrue(result.endswith('hey'))
1079
1080 def test_get_doc_on_from_import_module(self):
1081 mod1 = testutils.create_module(self.project, 'mod1')
1082 mod1.write('"""mod1 docs"""\nvar = 1\n')
1083 code = 'from mod1 import var\n'
1084 result = get_doc(self.project, code, code.index('mod1'))
1085 result.index('mod1 docs')
1086
1087 def test_fixing_errors_with_maxfixes_in_resources(self):
1088 mod = testutils.create_module(self.project, 'mod')
1089 code = 'def f():\n sldj sldj\ndef g():\n ran'
1090 mod.write(code)
1091 result = self._assist(code, maxfixes=2, resource=mod)
1092 self.assertTrue(len(result) > 0)
1093
1094 def test_completing_names_after_from_import(self):
1095 mod1 = testutils.create_module(self.project, 'mod1')
1096 mod2 = testutils.create_module(self.project, 'mod2')
1097 mod1.write('myvar = None\n')
1098 result = self._assist('from mod1 import myva', resource=mod2)
1099 self.assertTrue(len(result) > 0)
1100 self.assert_completion_in_result('myvar', 'global', result)
1101
1102 def test_completing_names_after_from_import_and_sorted_proposals(self):
1103 mod1 = testutils.create_module(self.project, 'mod1')
1104 mod2 = testutils.create_module(self.project, 'mod2')
1105 mod1.write('myvar = None\n')
1106 result = self._assist('from mod1 import myva', resource=mod2)
1107 result = sorted_proposals(result)
1108 self.assertTrue(len(result) > 0)
1109 self.assert_completion_in_result('myvar', 'global', result)
1110
1111 def test_completing_names_after_from_import2(self):
1112 mod1 = testutils.create_module(self.project, 'mod1')
1113 mod2 = testutils.create_module(self.project, 'mod2')
1114 mod1.write('myvar = None\n')
1115 result = self._assist('from mod1 import ', resource=mod2)
1116 self.assertTrue(len(result) > 0)
1117 self.assert_completion_in_result('myvar', 'global', result)
1118
1119 def test_starting_expression(self):
1120 code = 'l = list()\nl.app'
1121 self.assertEquals('l.app', starting_expression(code, len(code)))
1122
1123
1124 def suite():
1125 result = unittest.TestSuite()
1126 result.addTests(unittest.makeSuite(CodeAssistTest))
1127 result.addTests(unittest.makeSuite(CodeAssistInProjectsTest))
1128 return result
1129
1130 if __name__ == '__main__':
1131 unittest.main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698