File: test_documentation.py

package info (click to toggle)
python-jedi 0.19.1%2Bds1-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,680 kB
  • sloc: python: 28,783; makefile: 172; ansic: 13
file content (148 lines) | stat: -rw-r--r-- 3,581 bytes parent folder | download
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
from textwrap import dedent

import pytest


def test_error_leaf_keyword_doc(Script):
    d, = Script("or").help(1, 1)
    assert len(d.docstring()) > 100
    assert d.name == 'or'


def test_error_leaf_operator_doc(Script):
    d, = Script("==").help()
    assert len(d.docstring()) > 100
    assert d.name == '=='


def test_keyword_completion(Script):
    k = Script("fro").complete()[0]
    imp_start = 'The "import'
    assert k.docstring(raw=True).startswith(imp_start)
    assert k.docstring().startswith(imp_start)


def test_import_keyword(Script):
    d, = Script("import x").help(column=0)
    assert d.docstring().startswith('The "import" statement')
    # unrelated to #44


def test_import_keyword_after_newline(Script):
    d, = Script("import x\nimport y").help(line=2, column=0)
    assert d.docstring().startswith('The "import" statement')


def test_import_keyword_with_gotos(goto_or_infer):
    assert not goto_or_infer("import x", column=0)


def test_operator_doc(Script):
    d, = Script("a == b").help(1, 3)
    assert len(d.docstring()) > 100


@pytest.mark.parametrize(
    'code, help_part', [
        ('str', 'Create a new string object'),
        ('str.strip', 'Return a copy of the string'),
    ]
)
def test_stdlib_doc(Script, code, help_part):
    h, = Script(code).help()
    assert help_part in h.docstring(raw=True)


def test_lambda(Script):
    d, = Script('lambda x: x').help(column=0)
    assert d.type == 'keyword'
    assert d.docstring().startswith('Lambdas\n*******')


@pytest.mark.parametrize(
    'code, kwargs', [
        ('?', {}),
        ('""', {}),
        ('"', {}),
    ]
)
def test_help_no_returns(Script, code, kwargs):
    assert not Script(code).help(**kwargs)


@pytest.mark.parametrize(
    'to_execute, expected_doc', [
        ('X.x', 'Yeah '),
        ('X().x', 'Yeah '),
        ('X.y', 'f g '),
        ('X.z', ''),
    ]
)
def test_attribute_docstrings(goto_or_help, expected_doc, to_execute):
    code = dedent('''\
        class X:
            "ha"
            x = 3
            """ Yeah """
            y = 5
            "f g "
            z = lambda x: 1
        ''')

    d, = goto_or_help(code + to_execute)
    assert d.docstring() == expected_doc


def test_version_info(Script):
    """
    Version info is a bit special, because it needs to be fast for some ifs, so
    it's a special object that we have to check.
    """
    s = Script(dedent("""\
        import sys

        sys.version_info"""))

    c, = s.complete()
    assert c.docstring() == 'sys.version_info\n\nVersion information as a named tuple.'


def test_builtin_docstring(goto_or_help_or_infer):
    d, = goto_or_help_or_infer('open')

    doc = d.docstring()
    assert doc.startswith('open(file: ')
    assert 'Open file' in doc


def test_docstring_decorator(goto_or_help_or_infer):
    code = dedent('''
        import types

        def dec(func):
            return types.FunctionType()

        @dec
        def func(a, b):
            "hello"
            return
        func''')
    d, = goto_or_help_or_infer(code)

    doc = d.docstring()
    assert doc == 'FunctionType(*args: Any, **kwargs: Any) -> Any\n\nhello'


@pytest.mark.parametrize('code', ['', '\n', ' '])
def test_empty(Script, code):
    assert not Script(code).help(1, 0)


@pytest.mark.parametrize('code', ['f()', '(bar or baz)', 'f[3]'])
def test_no_help_for_operator(Script, code):
    assert not Script(code).help()


@pytest.mark.parametrize('code', ['()', '(1,)', '[]', '[1]', 'f[]'])
def test_help_for_operator(Script, code):
    assert Script(code).help()