Format `tests/` (#12760)

This commit is contained in:
Adam Turner
2024-08-11 14:58:56 +01:00
committed by GitHub
parent 620e434f65
commit d03156e078
128 changed files with 18922 additions and 10436 deletions

View File

@@ -13,7 +13,10 @@ SPHINX_MODULE_PATH = os.path.splitext(sphinx.__file__)[0] + '.py'
def test_ModuleAnalyzer_get_module_source():
assert ModuleAnalyzer.get_module_source('sphinx') == (sphinx.__file__, sphinx.__loader__.get_source('sphinx'))
assert ModuleAnalyzer.get_module_source('sphinx') == (
sphinx.__file__,
sphinx.__loader__.get_source('sphinx'),
)
# failed to obtain source information from builtin modules
with pytest.raises(PycodeError):
@@ -37,8 +40,7 @@ def test_ModuleAnalyzer_for_file():
def test_ModuleAnalyzer_for_module(rootdir):
analyzer = ModuleAnalyzer.for_module('sphinx')
assert analyzer.modname == 'sphinx'
assert analyzer.srcname in (SPHINX_MODULE_PATH,
os.path.abspath(SPHINX_MODULE_PATH))
assert analyzer.srcname in (SPHINX_MODULE_PATH, os.path.abspath(SPHINX_MODULE_PATH))
saved_path = sys.path.copy()
sys.path.insert(0, str(rootdir / 'test-pycode'))
@@ -51,38 +53,48 @@ def test_ModuleAnalyzer_for_module(rootdir):
def test_ModuleAnalyzer_find_tags():
code = ('class Foo(object):\n' # line: 1
' """class Foo!"""\n'
' def __init__(self):\n'
' pass\n'
'\n'
' def bar(self, arg1, arg2=True, *args, **kwargs):\n'
' """method Foo.bar"""\n'
' pass\n'
'\n'
' class Baz(object):\n'
' def __init__(self):\n' # line: 11
' pass\n'
'\n'
'def qux():\n'
' """function baz"""\n'
' pass\n'
'\n'
'@decorator1\n'
'@decorator2\n'
'def quux():\n'
' pass\n' # line: 21
'\n'
'class Corge(object):\n'
' @decorator1\n'
' @decorator2\n'
' def grault(self):\n'
' pass\n')
code = (
'class Foo(object):\n' # line: 1
' """class Foo!"""\n'
' def __init__(self):\n'
' pass\n'
'\n'
' def bar(self, arg1, arg2=True, *args, **kwargs):\n'
' """method Foo.bar"""\n'
' pass\n'
'\n'
' class Baz(object):\n'
' def __init__(self):\n' # line: 11
' pass\n'
'\n'
'def qux():\n'
' """function baz"""\n'
' pass\n'
'\n'
'@decorator1\n'
'@decorator2\n'
'def quux():\n'
' pass\n' # line: 21
'\n'
'class Corge(object):\n'
' @decorator1\n'
' @decorator2\n'
' def grault(self):\n'
' pass\n'
)
analyzer = ModuleAnalyzer.for_string(code, 'module')
tags = analyzer.find_tags()
assert set(tags.keys()) == {'Foo', 'Foo.__init__', 'Foo.bar',
'Foo.Baz', 'Foo.Baz.__init__', 'qux', 'quux',
'Corge', 'Corge.grault'}
assert set(tags.keys()) == {
'Foo',
'Foo.__init__',
'Foo.bar',
'Foo.Baz',
'Foo.Baz.__init__',
'qux',
'quux',
'Corge',
'Corge.grault',
}
assert tags['Foo'] == ('class', 1, 12) # type, start, end
assert tags['Foo.__init__'] == ('def', 3, 4)
assert tags['Foo.bar'] == ('def', 6, 8)
@@ -95,44 +107,48 @@ def test_ModuleAnalyzer_find_tags():
def test_ModuleAnalyzer_find_attr_docs():
code = ('class Foo(object):\n'
' """class Foo!"""\n'
' #: comment before attr1\n'
' attr1 = None\n'
' attr2 = None # attribute comment for attr2 (without colon)\n'
' attr3 = None #: attribute comment for attr3\n'
' attr4 = None #: long attribute comment\n'
' #: for attr4\n'
' #: comment before attr5\n'
' attr5 = None #: attribute comment for attr5\n'
' attr6, attr7 = 1, 2 #: this comment is ignored\n'
'\n'
' def __init__(self):\n'
' self.attr8 = None #: first attribute comment (ignored)\n'
' self.attr8 = None #: attribute comment for attr8\n'
' #: comment before attr9\n'
' self.attr9 = None #: comment after attr9\n'
' "string after attr9"\n'
'\n'
' def bar(self, arg1, arg2=True, *args, **kwargs):\n'
' """method Foo.bar"""\n'
' pass\n'
'\n'
'def baz():\n'
' """function baz"""\n'
' pass\n'
'\n'
'class Qux: attr1 = 1; attr2 = 2')
code = (
'class Foo(object):\n'
' """class Foo!"""\n'
' #: comment before attr1\n'
' attr1 = None\n'
' attr2 = None # attribute comment for attr2 (without colon)\n'
' attr3 = None #: attribute comment for attr3\n'
' attr4 = None #: long attribute comment\n'
' #: for attr4\n'
' #: comment before attr5\n'
' attr5 = None #: attribute comment for attr5\n'
' attr6, attr7 = 1, 2 #: this comment is ignored\n'
'\n'
' def __init__(self):\n'
' self.attr8 = None #: first attribute comment (ignored)\n'
' self.attr8 = None #: attribute comment for attr8\n'
' #: comment before attr9\n'
' self.attr9 = None #: comment after attr9\n'
' "string after attr9"\n'
'\n'
' def bar(self, arg1, arg2=True, *args, **kwargs):\n'
' """method Foo.bar"""\n'
' pass\n'
'\n'
'def baz():\n'
' """function baz"""\n'
' pass\n'
'\n'
'class Qux: attr1 = 1; attr2 = 2'
)
analyzer = ModuleAnalyzer.for_string(code, 'module')
docs = analyzer.find_attr_docs()
assert set(docs) == {('Foo', 'attr1'),
('Foo', 'attr3'),
('Foo', 'attr4'),
('Foo', 'attr5'),
('Foo', 'attr6'),
('Foo', 'attr7'),
('Foo', 'attr8'),
('Foo', 'attr9')}
assert set(docs) == {
('Foo', 'attr1'),
('Foo', 'attr3'),
('Foo', 'attr4'),
('Foo', 'attr5'),
('Foo', 'attr6'),
('Foo', 'attr7'),
('Foo', 'attr8'),
('Foo', 'attr9'),
}
assert docs[('Foo', 'attr1')] == ['comment before attr1', '']
assert docs[('Foo', 'attr3')] == ['attribute comment for attr3', '']
assert docs[('Foo', 'attr4')] == ['long attribute comment', '']
@@ -142,32 +158,34 @@ def test_ModuleAnalyzer_find_attr_docs():
assert docs[('Foo', 'attr7')] == ['this comment is ignored', '']
assert docs[('Foo', 'attr8')] == ['attribute comment for attr8', '']
assert docs[('Foo', 'attr9')] == ['string after attr9', '']
assert analyzer.tagorder == {'Foo': 0,
'Foo.__init__': 8,
'Foo.attr1': 1,
'Foo.attr2': 2,
'Foo.attr3': 3,
'Foo.attr4': 4,
'Foo.attr5': 5,
'Foo.attr6': 6,
'Foo.attr7': 7,
'Foo.attr8': 10,
'Foo.attr9': 12,
'Foo.bar': 13,
'baz': 14,
'Qux': 15,
'Qux.attr1': 16,
'Qux.attr2': 17}
assert analyzer.tagorder == {
'Foo': 0,
'Foo.__init__': 8,
'Foo.attr1': 1,
'Foo.attr2': 2,
'Foo.attr3': 3,
'Foo.attr4': 4,
'Foo.attr5': 5,
'Foo.attr6': 6,
'Foo.attr7': 7,
'Foo.attr8': 10,
'Foo.attr9': 12,
'Foo.bar': 13,
'baz': 14,
'Qux': 15,
'Qux.attr1': 16,
'Qux.attr2': 17,
}
def test_ModuleAnalyzer_find_attr_docs_for_posonlyargs_method():
code = ('class Foo(object):\n'
' def __init__(self, /):\n'
' self.attr = None #: attribute comment\n')
code = (
'class Foo(object):\n'
' def __init__(self, /):\n'
' self.attr = None #: attribute comment\n'
)
analyzer = ModuleAnalyzer.for_string(code, 'module')
docs = analyzer.find_attr_docs()
assert set(docs) == {('Foo', 'attr')}
assert docs[('Foo', 'attr')] == ['attribute comment', '']
assert analyzer.tagorder == {'Foo': 0,
'Foo.__init__': 1,
'Foo.attr': 2}
assert analyzer.tagorder == {'Foo': 0, 'Foo.__init__': 1, 'Foo.attr': 2}