| 1 |
""" |
| 2 |
Tests of various stdlib related things that could not be tested |
| 3 |
with "Black Box Tests". |
| 4 |
""" |
| 5 |
from textwrap import dedent |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
|
| 10 |
@pytest.mark.parametrize(['letter', 'expected'], [ |
| 11 |
('n', ['name']), |
| 12 |
('s', ['smart']), |
| 13 |
]) |
| 14 |
def test_namedtuple_str(letter, expected, Script): |
| 15 |
source = dedent("""\ |
| 16 |
import collections |
| 17 |
Person = collections.namedtuple('Person', 'name smart') |
| 18 |
dave = Person('Dave', False) |
| 19 |
dave.%s""") % letter |
| 20 |
result = Script(source).complete() |
| 21 |
completions = set(r.name for r in result) |
| 22 |
assert completions == set(expected) |
| 23 |
|
| 24 |
|
| 25 |
def test_namedtuple_list(Script): |
| 26 |
source = dedent("""\ |
| 27 |
import collections |
| 28 |
Cat = collections.namedtuple('Person', ['legs', u'length', 'large']) |
| 29 |
garfield = Cat(4, '85cm', True) |
| 30 |
garfield.l""") |
| 31 |
result = Script(source).complete() |
| 32 |
completions = set(r.name for r in result) |
| 33 |
assert completions == {'legs', 'length', 'large'} |
| 34 |
|
| 35 |
|
| 36 |
def test_namedtuple_content(Script): |
| 37 |
source = dedent("""\ |
| 38 |
import collections |
| 39 |
Foo = collections.namedtuple('Foo', ['bar', 'baz']) |
| 40 |
named = Foo(baz=4, bar=3.0) |
| 41 |
unnamed = Foo(4, '') |
| 42 |
""") |
| 43 |
|
| 44 |
def d(source): |
| 45 |
x, = Script(source).infer() |
| 46 |
return x.name |
| 47 |
|
| 48 |
assert d(source + 'unnamed.bar') == 'int' |
| 49 |
assert d(source + 'unnamed.baz') == 'str' |
| 50 |
assert d(source + 'named.bar') == 'float' |
| 51 |
assert d(source + 'named.baz') == 'int' |
| 52 |
|
| 53 |
|
| 54 |
def test_nested_namedtuples(Script): |
| 55 |
""" |
| 56 |
From issue #730. |
| 57 |
""" |
| 58 |
s = Script(dedent(''' |
| 59 |
import collections |
| 60 |
Dataset = collections.namedtuple('Dataset', ['data']) |
| 61 |
Datasets = collections.namedtuple('Datasets', ['train']) |
| 62 |
train_x = Datasets(train=Dataset('data_value')) |
| 63 |
train_x.train.''')) |
| 64 |
assert 'data' in [c.name for c in s.complete()] |
| 65 |
|
| 66 |
|
| 67 |
def test_namedtuple_infer(Script): |
| 68 |
source = dedent(""" |
| 69 |
from collections import namedtuple |
| 70 |
|
| 71 |
Foo = namedtuple('Foo', 'id timestamp gps_timestamp attributes') |
| 72 |
Foo""") |
| 73 |
|
| 74 |
from jedi.api import Script |
| 75 |
|
| 76 |
d1, = Script(source).infer() |
| 77 |
|
| 78 |
assert d1.get_line_code() == "class Foo(tuple):\n" |
| 79 |
assert d1.module_path is None |
| 80 |
assert d1.docstring() == 'Foo(id, timestamp, gps_timestamp, attributes)' |
| 81 |
|
| 82 |
|
| 83 |
def test_re_sub(Script, environment): |
| 84 |
""" |
| 85 |
This whole test was taken out of completion/stdlib.py, because of the |
| 86 |
version differences. |
| 87 |
""" |
| 88 |
def run(code): |
| 89 |
defs = Script(code).infer() |
| 90 |
return {d.name for d in defs} |
| 91 |
|
| 92 |
names = run("import re; re.sub('a', 'a', 'f')") |
| 93 |
assert names == {'str'} |
| 94 |
|
| 95 |
|
| 96 |
names = run("import re; re.sub('a', 'a')") |
| 97 |
assert names == {'str', 'bytes'} |