Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

unittest_pyreverse_inspector.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # Copyright (c) 2015-2017 Claudiu Popa <pcmanticore@gmail.com>
  2. # Copyright (c) 2016 Derek Gustafson <degustaf@gmail.com>
  3. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  4. # For details: https://github.com/PyCQA/pylint/blob/master/COPYING
  5. """
  6. for the visitors.diadefs module
  7. """
  8. import os
  9. import pytest
  10. import astroid
  11. from astroid import nodes
  12. from astroid import bases
  13. from pylint.pyreverse import inspector
  14. from unittest_pyreverse_writer import get_project
  15. @pytest.fixture
  16. def project():
  17. project = get_project('data', 'data')
  18. linker = inspector.Linker(project)
  19. linker.visit(project)
  20. return project
  21. def test_class_implements(project):
  22. klass = project.get_module('data.clientmodule_test')['Ancestor']
  23. assert hasattr(klass, 'implements')
  24. assert len(klass.implements) == 1
  25. assert isinstance(klass.implements[0], nodes.ClassDef)
  26. assert klass.implements[0].name == "Interface"
  27. def test_class_implements_specialization(project):
  28. klass = project.get_module('data.clientmodule_test')['Specialization']
  29. assert hasattr(klass, 'implements')
  30. assert len(klass.implements) == 0
  31. def test_locals_assignment_resolution(project):
  32. klass = project.get_module('data.clientmodule_test')['Specialization']
  33. assert hasattr(klass, 'locals_type')
  34. type_dict = klass.locals_type
  35. assert len(type_dict) == 2
  36. keys = sorted(type_dict.keys())
  37. assert keys == ['TYPE', 'top']
  38. assert len(type_dict['TYPE']) == 1
  39. assert type_dict['TYPE'][0].value == 'final class'
  40. assert len(type_dict['top']) == 1
  41. assert type_dict['top'][0].value == 'class'
  42. def test_instance_attrs_resolution(project):
  43. klass = project.get_module('data.clientmodule_test')['Specialization']
  44. assert hasattr(klass, 'instance_attrs_type')
  45. type_dict = klass.instance_attrs_type
  46. assert len(type_dict) == 2
  47. keys = sorted(type_dict.keys())
  48. assert keys == ['_id', 'relation']
  49. assert isinstance(type_dict['relation'][0], bases.Instance), \
  50. type_dict['relation']
  51. assert type_dict['relation'][0].name == 'DoNothing'
  52. assert type_dict['_id'][0] is astroid.YES
  53. def test_concat_interfaces():
  54. cls = astroid.extract_node('''
  55. class IMachin: pass
  56. class Correct2:
  57. """docstring"""
  58. __implements__ = (IMachin,)
  59. class BadArgument:
  60. """docstring"""
  61. __implements__ = (IMachin,)
  62. class InterfaceCanNowBeFound: #@
  63. """docstring"""
  64. __implements__ = BadArgument.__implements__ + Correct2.__implements__
  65. ''')
  66. interfaces = inspector.interfaces(cls)
  67. assert [i.name for i in interfaces] == ['IMachin']
  68. def test_interfaces():
  69. module = astroid.parse('''
  70. class Interface(object): pass
  71. class MyIFace(Interface): pass
  72. class AnotherIFace(Interface): pass
  73. class Concrete0(object):
  74. __implements__ = MyIFace
  75. class Concrete1:
  76. __implements__ = (MyIFace, AnotherIFace)
  77. class Concrete2:
  78. __implements__ = (MyIFace, AnotherIFace)
  79. class Concrete23(Concrete1): pass
  80. ''')
  81. for klass, interfaces in (('Concrete0', ['MyIFace']),
  82. ('Concrete1', ['MyIFace', 'AnotherIFace']),
  83. ('Concrete2', ['MyIFace', 'AnotherIFace']),
  84. ('Concrete23', ['MyIFace', 'AnotherIFace'])):
  85. klass = module[klass]
  86. assert [i.name for i in inspector.interfaces(klass)] == interfaces
  87. def test_from_directory(project):
  88. expected = os.path.join('pylint', 'test', 'data', '__init__.py')
  89. assert project.name == 'data'
  90. assert project.path.endswith(expected)
  91. def test_project_node(project):
  92. expected = [
  93. 'data', 'data.clientmodule_test',
  94. 'data.suppliermodule_test',
  95. ]
  96. assert sorted(project.keys()) == expected