|
| 1 | +from collections import namedtuple |
1 | 2 | from bpython import autocomplete |
2 | 3 |
|
3 | 4 | import mock |
|
6 | 7 | except ImportError: |
7 | 8 | import unittest |
8 | 9 |
|
| 10 | +try: |
| 11 | + import jedi |
| 12 | + has_jedi = True |
| 13 | +except ImportError: |
| 14 | + has_jedi = False |
| 15 | + |
9 | 16 |
|
10 | 17 | class TestSafeEval(unittest.TestCase): |
11 | 18 | def test_catches_syntax_error(self): |
@@ -191,4 +198,37 @@ def test_magic_methods_complete_after_double_underscores(self): |
191 | 198 | block = "class Something(object)\n def __" |
192 | 199 | self.assertSetEqual(com.matches(10, ' def __', block), set(autocomplete.MAGIC_METHODS)) |
193 | 200 |
|
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