Debian Codegraph @

test/test_api/test_documentation.py

raw symbols SCIP
1 from textwrap import dedent
2
3 import pytest
4
5
6 def test_error_leaf_keyword_doc(Script):
7 d, = Script("or").help(1, 1)
8 assert len(d.docstring()) > 100
9 assert d.name == 'or'
10
11
12 def test_error_leaf_operator_doc(Script):
13 d, = Script("==").help()
14 assert len(d.docstring()) > 100
15 assert d.name == '=='
16
17
18 def test_keyword_completion(Script):
19 k = Script("fro").complete()[0]
20 imp_start = 'The "import'
21 assert k.docstring(raw=True).startswith(imp_start)
22 assert k.docstring().startswith(imp_start)
23
24
25 def test_import_keyword(Script):
26 d, = Script("import x").help(column=0)
27 assert d.docstring().startswith('The "import" statement')
28 # unrelated to #44
29
30
31 def test_import_keyword_after_newline(Script):
32 d, = Script("import x\nimport y").help(line=2, column=0)
33 assert d.docstring().startswith('The "import" statement')
34
35
36 def test_import_keyword_with_gotos(goto_or_infer):
37 assert not goto_or_infer("import x", column=0)
38
39
40 def test_operator_doc(Script):
41 d, = Script("a == b").help(1, 3)
42 assert len(d.docstring()) > 100
43
44
45 @pytest.mark.parametrize(
46 'code, help_part', [
47 ('str', 'Create a new string object'),
48 ('str.strip', 'Return a copy of the string'),
49 ]
50 )
51 def test_stdlib_doc(Script, code, help_part):
52 h, = Script(code).help()
53 assert help_part in h.docstring(raw=True)
54
55
56 def test_lambda(Script):
57 d, = Script('lambda x: x').help(column=0)
58 assert d.type == 'keyword'
59 assert d.docstring().startswith('Lambdas\n*******')
60
61
62 @pytest.mark.parametrize(
63 'code, kwargs', [
64 ('?', {}),
65 ('""', {}),
66 ('"', {}),
67 ]
68 )
69 def test_help_no_returns(Script, code, kwargs):
70 assert not Script(code).help(**kwargs)
71
72
73 @pytest.mark.parametrize(
74 'to_execute, expected_doc', [
75 ('X.x', 'Yeah '),
76 ('X().x', 'Yeah '),
77 ('X.y', 'f g '),
78 ('X.z', ''),
79 ]
80 )
81 def test_attribute_docstrings(goto_or_help, expected_doc, to_execute):
82 code = dedent('''\
83 class X:
84 "ha"
85 x = 3
86 """ Yeah """
87 y = 5
88 "f g "
89 z = lambda x: 1
90 ''')
91
92 d, = goto_or_help(code + to_execute)
93 assert d.docstring() == expected_doc
94
95
96 def test_version_info(Script):
97 """
98 Version info is a bit special, because it needs to be fast for some ifs, so
99 it's a special object that we have to check.
100 """
101 s = Script(dedent("""\
102 import sys
103
104 sys.version_info"""))
105
106 c, = s.complete()
107 assert c.docstring() == 'sys.version_info\n\nVersion information as a named tuple.'
108
109
110 def test_builtin_docstring(goto_or_help_or_infer):
111 d, = goto_or_help_or_infer('open')
112
113 doc = d.docstring()
114 assert doc.startswith('open(file: ')
115 assert 'Open file' in doc
116
117
118 def test_docstring_decorator(goto_or_help_or_infer):
119 code = dedent('''
120 import types
121
122 def dec(func):
123 return types.FunctionType()
124
125 @dec
126 def func(a, b):
127 "hello"
128 return
129 func''')
130 d, = goto_or_help_or_infer(code)
131
132 doc = d.docstring()
133 assert doc == 'FunctionType(*args: Any, **kwargs: Any) -> Any\n\nhello'
134
135
136 @pytest.mark.parametrize('code', ['', '\n', ' '])
137 def test_empty(Script, code):
138 assert not Script(code).help(1, 0)
139
140
141 @pytest.mark.parametrize('code', ['f()', '(bar or baz)', 'f[3]'])
142 def test_no_help_for_operator(Script, code):
143 assert not Script(code).help()
144
145
146 @pytest.mark.parametrize('code', ['()', '(1,)', '[]', '[1]', 'f[]'])
147 def test_help_for_operator(Script, code):
148 assert Script(code).help()