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
|
"""
Tests of ``jedi.api.Interpreter``.
"""
from ..helpers import TestCase
import jedi
from jedi._compatibility import is_py33
from jedi.evaluate.compiled import mixed
class _GlobalNameSpace():
class SideEffectContainer():
pass
def get_completion(source, namespace):
i = jedi.Interpreter(source, [namespace])
completions = i.completions()
assert len(completions) == 1
return completions[0]
def test_builtin_details():
import keyword
class EmptyClass:
pass
variable = EmptyClass()
def func():
pass
cls = get_completion('EmptyClass', locals())
var = get_completion('variable', locals())
f = get_completion('func', locals())
m = get_completion('keyword', locals())
assert cls.type == 'class'
assert var.type == 'instance'
assert f.type == 'function'
assert m.type == 'module'
def test_nested_resolve():
class XX():
def x():
pass
cls = get_completion('XX', locals())
func = get_completion('XX.x', locals())
assert func.start_pos == (cls.start_pos[0] + 1, 12)
def test_side_effect_completion():
"""
In the repl it's possible to cause side effects that are not documented in
Python code, however we want references to Python code as well. Therefore
we need some mixed kind of magic for tests.
"""
_GlobalNameSpace.SideEffectContainer.foo = 1
side_effect = get_completion('SideEffectContainer', _GlobalNameSpace.__dict__)
# It's a class that contains MixedObject.
assert isinstance(side_effect._definition.base, mixed.MixedObject)
foo = get_completion('SideEffectContainer.foo', _GlobalNameSpace.__dict__)
assert foo.name == 'foo'
class TestInterpreterAPI(TestCase):
def check_interpreter_complete(self, source, namespace, completions,
**kwds):
script = jedi.Interpreter(source, [namespace], **kwds)
cs = script.completions()
actual = [c.name for c in cs]
self.assertEqual(sorted(actual), sorted(completions))
def test_complete_raw_function(self):
from os.path import join
self.check_interpreter_complete('join("").up',
locals(),
['upper'])
def test_complete_raw_function_different_name(self):
from os.path import join as pjoin
self.check_interpreter_complete('pjoin("").up',
locals(),
['upper'])
def test_complete_raw_module(self):
import os
self.check_interpreter_complete('os.path.join("a").up',
locals(),
['upper'])
def test_complete_raw_instance(self):
import datetime
dt = datetime.datetime(2013, 1, 1)
completions = ['time', 'timetz', 'timetuple']
if is_py33:
completions += ['timestamp']
self.check_interpreter_complete('(dt - dt).ti',
locals(),
completions)
def test_list(self):
array = ['haha', 1]
self.check_interpreter_complete('array[0].uppe',
locals(),
['upper'])
self.check_interpreter_complete('array[0].real',
locals(),
[])
# something different, no index given, still just return the right
self.check_interpreter_complete('array[int].real',
locals(),
['real'])
self.check_interpreter_complete('array[int()].real',
locals(),
['real'])
# inexistent index
self.check_interpreter_complete('array[2].upper',
locals(),
['upper'])
def test_slice(self):
class Foo():
bar = []
baz = 'xbarx'
self.check_interpreter_complete('getattr(Foo, baz[1:-1]).append',
locals(),
['append'])
def test_getitem_side_effects(self):
class Foo():
def __getitem__(self, index):
# possible side effects here, should therefore not call this.
return index
foo = Foo()
self.check_interpreter_complete('foo[0].', locals(), [])
def test_property_error(self):
class Foo():
@property
def bar(self):
raise ValueError
foo = Foo()
self.check_interpreter_complete('foo.bar', locals(), ['bar'])
self.check_interpreter_complete('foo.bar.baz', locals(), [])
def test_param_completion(self):
def foo(bar):
pass
lambd = lambda xyz: 3
self.check_interpreter_complete('foo(bar', locals(), ['bar'])
# TODO we're not yet using the Python3.5 inspect.signature, yet.
assert not jedi.Interpreter('lambd(xyz', [locals()]).completions()
|