third_party.pylibs.pylint.src/tests/unittest_pyreverse_inspector.py

124 lines
3.7 KiB
Python
Raw Normal View History

2018-07-15 09:36:36 +00:00
# Copyright (c) 2015-2018 Claudiu Popa <pcmanticore@gmail.com>
2017-12-15 11:24:15 +00:00
# Copyright (c) 2016 Derek Gustafson <degustaf@gmail.com>
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/PyCQA/pylint/blob/master/COPYING
"""
for the visitors.diadefs module
"""
import os
import astroid
import pytest
from astroid import bases, nodes
from pylint.pyreverse import inspector
from unittest_pyreverse_writer import get_project
2016-12-08 04:15:21 +00:00
@pytest.fixture
def project():
2018-09-16 15:33:50 +00:00
project = get_project("data", "data")
2016-12-08 04:15:21 +00:00
linker = inspector.Linker(project)
linker.visit(project)
return project
def test_class_implements(project):
2018-09-16 15:33:50 +00:00
klass = project.get_module("data.clientmodule_test")["Ancestor"]
assert hasattr(klass, "implements")
2016-12-08 04:15:21 +00:00
assert len(klass.implements) == 1
assert isinstance(klass.implements[0], nodes.ClassDef)
assert klass.implements[0].name == "Interface"
def test_class_implements_specialization(project):
2018-09-16 15:33:50 +00:00
klass = project.get_module("data.clientmodule_test")["Specialization"]
assert hasattr(klass, "implements")
2016-12-08 04:15:21 +00:00
assert len(klass.implements) == 0
def test_locals_assignment_resolution(project):
2018-09-16 15:33:50 +00:00
klass = project.get_module("data.clientmodule_test")["Specialization"]
assert hasattr(klass, "locals_type")
2016-12-08 04:15:21 +00:00
type_dict = klass.locals_type
assert len(type_dict) == 2
keys = sorted(type_dict.keys())
2018-09-16 15:33:50 +00:00
assert keys == ["TYPE", "top"]
assert len(type_dict["TYPE"]) == 1
assert type_dict["TYPE"][0].value == "final class"
assert len(type_dict["top"]) == 1
assert type_dict["top"][0].value == "class"
2016-12-08 04:15:21 +00:00
def test_instance_attrs_resolution(project):
2018-09-16 15:33:50 +00:00
klass = project.get_module("data.clientmodule_test")["Specialization"]
assert hasattr(klass, "instance_attrs_type")
2016-12-08 04:15:21 +00:00
type_dict = klass.instance_attrs_type
assert len(type_dict) == 2
keys = sorted(type_dict.keys())
2018-09-16 15:33:50 +00:00
assert keys == ["_id", "relation"]
assert isinstance(type_dict["relation"][0], bases.Instance), type_dict["relation"]
assert type_dict["relation"][0].name == "DoNothing"
assert type_dict["_id"][0] is astroid.Uninferable
2016-12-08 04:15:21 +00:00
def test_concat_interfaces():
2018-09-16 15:33:50 +00:00
cls = astroid.extract_node(
'''
2016-12-08 04:15:21 +00:00
class IMachin: pass
class Correct2:
"""docstring"""
__implements__ = (IMachin,)
class BadArgument:
"""docstring"""
__implements__ = (IMachin,)
class InterfaceCanNowBeFound: #@
"""docstring"""
__implements__ = BadArgument.__implements__ + Correct2.__implements__
2018-09-16 15:33:50 +00:00
'''
)
2016-12-08 04:15:21 +00:00
interfaces = inspector.interfaces(cls)
2018-09-16 15:33:50 +00:00
assert [i.name for i in interfaces] == ["IMachin"]
2016-12-08 04:15:21 +00:00
def test_interfaces():
2018-09-16 15:33:50 +00:00
module = astroid.parse(
"""
2016-12-08 04:15:21 +00:00
class Interface(object): pass
class MyIFace(Interface): pass
class AnotherIFace(Interface): pass
class Concrete0(object):
__implements__ = MyIFace
class Concrete1:
2016-12-08 04:15:21 +00:00
__implements__ = (MyIFace, AnotherIFace)
class Concrete2:
__implements__ = (MyIFace, AnotherIFace)
class Concrete23(Concrete1): pass
2018-09-16 15:33:50 +00:00
"""
)
for klass, interfaces in (
("Concrete0", ["MyIFace"]),
("Concrete1", ["MyIFace", "AnotherIFace"]),
("Concrete2", ["MyIFace", "AnotherIFace"]),
("Concrete23", ["MyIFace", "AnotherIFace"]),
):
2016-12-08 04:15:21 +00:00
klass = module[klass]
assert [i.name for i in inspector.interfaces(klass)] == interfaces
def test_from_directory(project):
2019-06-15 05:28:42 +00:00
expected = os.path.join("tests", "data", "__init__.py")
2018-09-16 15:33:50 +00:00
assert project.name == "data"
2016-12-08 04:15:21 +00:00
assert project.path.endswith(expected)
def test_project_node(project):
2018-09-16 15:33:50 +00:00
expected = ["data", "data.clientmodule_test", "data.suppliermodule_test"]
2016-12-08 04:15:21 +00:00
assert sorted(project.keys()) == expected