| 1 |
import pytest |
| 2 |
|
| 3 |
from ..helpers import test_dir |
| 4 |
|
| 5 |
|
| 6 |
def test_import_references(Script): |
| 7 |
s = Script("from .. import foo", path=test_dir.joinpath("foo.py")) |
| 8 |
assert [usage.line for usage in s.get_references()] == [1] |
| 9 |
|
| 10 |
|
| 11 |
def test_exclude_builtin_modules(Script): |
| 12 |
def get(include): |
| 13 |
references = Script(source).get_references(include_builtins=include) |
| 14 |
return [(d.line, d.column) for d in references] |
| 15 |
source = '''import sys\nsys.setprofile''' |
| 16 |
places = get(include=True) |
| 17 |
assert len(places) >= 3 |
| 18 |
|
| 19 |
places = get(include=False) |
| 20 |
|
| 21 |
assert places == [(2, 4)] |
| 22 |
|
| 23 |
|
| 24 |
@pytest.mark.parametrize('code, places', [ |
| 25 |
('', [(1, 7), (4, 6)]), |
| 26 |
('', [(2, 5)]), |
| 27 |
('', [(2, 24), (7, 10), (11, 10)]), |
| 28 |
('', [(6, 4), (14, 0)]), |
| 29 |
('', [(7, 4), (8, 11)]), |
| 30 |
('', [(7, 22), (11, 22)]), |
| 31 |
('', [(11, 4), (12, 11)]), |
| 32 |
('from datetime', [(1, 5)]), |
| 33 |
('''from datetime import datetime |
| 34 |
d1 = datetime.now() |
| 35 |
d2 = datetime.now() |
| 36 |
''', [(2, 14), (3, 14)]), |
| 37 |
('''from datetime import timedelta |
| 38 |
t1 = timedelta(seconds=1) |
| 39 |
t2 = timedelta(seconds=2) |
| 40 |
''', [(2, 15), (3, 15)]) |
| 41 |
]) |
| 42 |
def test_references_scope(Script, code, places): |
| 43 |
if not code: |
| 44 |
code = '''import sys |
| 45 |
from collections import defaultdict |
| 46 |
|
| 47 |
print(sys.path) |
| 48 |
|
| 49 |
def foo(bar): |
| 50 |
baz = defaultdict(int) |
| 51 |
return baz |
| 52 |
|
| 53 |
def bar(foo): |
| 54 |
baz = defaultdict(int) |
| 55 |
return baz |
| 56 |
|
| 57 |
foo() |
| 58 |
''' |
| 59 |
from jedi.api.project import Project |
| 60 |
project = Project('', sys_path=[], smart_sys_path=False) |
| 61 |
script = Script(code, project=project) |
| 62 |
|
| 63 |
for place in places: |
| 64 |
assert places == [(n.line, n.column) for n in script.get_references(scope='file', *place)] |