X Tutup
Skip to content

Commit fed23ca

Browse files
add jedi completion tests
1 parent 6a38aab commit fed23ca

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

bpython/autocomplete.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,10 @@ def locate(self, current_offset, line):
313313
import jedi
314314
except ImportError:
315315
class MultilineJediCompletion(BaseCompletionType):
316-
@classmethod
317-
def matches(cls, cursor_offset, line, **kwargs):
316+
def matches(self, cursor_offset, line, **kwargs):
318317
return None
319318
else:
320319
class JediCompletion(BaseCompletionType):
321-
@classmethod
322320
def matches(self, cursor_offset, line, history, **kwargs):
323321
if not lineparts.current_word(cursor_offset, line):
324322
return None
@@ -338,7 +336,7 @@ def matches(self, cursor_offset, line, history, **kwargs):
338336
return None # Too general - giving completions starting with multiple letters
339337
else:
340338
# case-sensitive matches only
341-
return [m for m in matches if m.startswith(first_letter)]
339+
return set([m for m in matches if m.startswith(first_letter)])
342340

343341
def locate(self, cursor_offset, line):
344342
start = self._orig_start
@@ -347,11 +345,10 @@ def locate(self, cursor_offset, line):
347345

348346

349347
class MultilineJediCompletion(JediCompletion):
350-
@classmethod
351-
def matches(cls, cursor_offset, line, current_block, history, **kwargs):
348+
def matches(self, cursor_offset, line, current_block, history, **kwargs):
352349
if '\n' in current_block:
353350
assert cursor_offset <= len(line), "%r %r" % (cursor_offset, line)
354-
results = JediCompletion.matches(cursor_offset, line, history)
351+
results = JediCompletion.matches(self, cursor_offset, line, history)
355352
return results
356353
else:
357354
return None

bpython/test/test_autocomplete.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from collections import namedtuple
12
from bpython import autocomplete
23

34
import mock
@@ -6,6 +7,12 @@
67
except ImportError:
78
import unittest
89

10+
try:
11+
import jedi
12+
has_jedi = True
13+
except ImportError:
14+
has_jedi = False
15+
916

1017
class TestSafeEval(unittest.TestCase):
1118
def test_catches_syntax_error(self):
@@ -191,4 +198,37 @@ def test_magic_methods_complete_after_double_underscores(self):
191198
block = "class Something(object)\n def __"
192199
self.assertSetEqual(com.matches(10, ' def __', block), set(autocomplete.MAGIC_METHODS))
193200

194-
201+
Comp = namedtuple('Completion', ['name', 'complete'])
202+
203+
class TestMultilineJediCompletion(unittest.TestCase):
204+
205+
@unittest.skipIf(not has_jedi, "jedi not available")
206+
def test_returns_none_with_single_line(self):
207+
com = autocomplete.MultilineJediCompletion()
208+
self.assertEqual(com.matches(2, 'Va', 'Va', []), None)
209+
210+
@unittest.skipIf(not has_jedi, "jedi not available")
211+
def test_returns_non_with_blank_second_line(self):
212+
com = autocomplete.MultilineJediCompletion()
213+
self.assertEqual(com.matches(0, '', 'class Foo():\n', ['class Foo():']), None)
214+
215+
def matches_from_completions(self, cursor, line, block, history, completions):
216+
with mock.patch('bpython.autocomplete.jedi.Script') as Script:
217+
script = Script.return_value
218+
script.completions.return_value = completions
219+
com = autocomplete.MultilineJediCompletion()
220+
return com.matches(cursor, line, block, history)
221+
222+
@unittest.skipIf(not has_jedi, "jedi not available")
223+
def test_completions_starting_with_different_letters(self):
224+
matches = self.matches_from_completions(
225+
2, ' a', 'class Foo:\n a', ['adsf'],
226+
[Comp('Abc', 'bc'), Comp('Cbc', 'bc')])
227+
self.assertEqual(matches, None)
228+
229+
@unittest.skipIf(not has_jedi, "jedi not available")
230+
def test_completions_starting_with_different_cases(self):
231+
matches = self.matches_from_completions(
232+
2, ' a', 'class Foo:\n a', ['adsf'],
233+
[Comp('Abc', 'bc'), Comp('ade', 'de')])
234+
self.assertSetEqual(matches, set(['ade']))

0 commit comments

Comments
 (0)
X Tutup