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_writer.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # Copyright (c) 2008, 2010, 2013 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
  2. # Copyright (c) 2014-2017 Claudiu Popa <pcmanticore@gmail.com>
  3. # Copyright (c) 2014 Google, Inc.
  4. # Copyright (c) 2014 Arun Persaud <arun@nubati.net>
  5. # Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro>
  6. # Copyright (c) 2016 Derek Gustafson <degustaf@gmail.com>
  7. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  8. # For details: https://github.com/PyCQA/pylint/blob/master/COPYING
  9. """
  10. unit test for visitors.diadefs and extensions.diadefslib modules
  11. """
  12. import os
  13. import codecs
  14. from difflib import unified_diff
  15. import pytest
  16. from pylint.pyreverse.inspector import Linker, project_from_files
  17. from pylint.pyreverse.diadefslib import DefaultDiadefGenerator, DiadefsHandler
  18. from pylint.pyreverse.writer import DotWriter
  19. from pylint.pyreverse.utils import get_visibility
  20. _DEFAULTS = {
  21. 'all_ancestors': None, 'show_associated': None,
  22. 'module_names': None,
  23. 'output_format': 'dot', 'diadefs_file': None, 'quiet': 0,
  24. 'show_ancestors': None, 'classes': (), 'all_associated': None,
  25. 'mode': 'PUB_ONLY', 'show_builtin': False, 'only_classnames': False
  26. }
  27. class Config(object):
  28. """config object for tests"""
  29. def __init__(self):
  30. for attr, value in _DEFAULTS.items():
  31. setattr(self, attr, value)
  32. def _file_lines(path):
  33. # we don't care about the actual encoding, but python3 forces us to pick one
  34. with codecs.open(path, encoding='latin1') as stream:
  35. lines = [line.strip() for line in stream.readlines()
  36. if (line.find('squeleton generated by ') == -1 and
  37. not line.startswith('__revision__ = "$Id:'))]
  38. return [line for line in lines if line]
  39. def get_project(module, name="No Name"):
  40. """return a astroid project representation"""
  41. def _astroid_wrapper(func, modname):
  42. return func(modname)
  43. return project_from_files([module], _astroid_wrapper,
  44. project_name=name)
  45. DOT_FILES = ['packages_No_Name.dot', 'classes_No_Name.dot']
  46. @pytest.fixture(scope="module")
  47. def setup():
  48. project = get_project(os.path.join(os.path.dirname(__file__), 'data'))
  49. linker = Linker(project)
  50. CONFIG = Config()
  51. handler = DiadefsHandler(CONFIG)
  52. dd = DefaultDiadefGenerator(linker, handler).visit(project)
  53. for diagram in dd:
  54. diagram.extract_relationships()
  55. writer = DotWriter(CONFIG)
  56. writer.write(dd)
  57. yield
  58. for fname in DOT_FILES:
  59. try:
  60. os.remove(fname)
  61. except:
  62. continue
  63. @pytest.mark.usefixtures("setup")
  64. @pytest.mark.parametrize("generated_file", DOT_FILES)
  65. def test_dot_files(generated_file):
  66. expected_file = os.path.join(os.path.dirname(__file__), 'data', generated_file)
  67. generated = _file_lines(generated_file)
  68. expected = _file_lines(expected_file)
  69. generated = '\n'.join(generated)
  70. expected = '\n'.join(expected)
  71. files = "\n *** expected : %s, generated : %s \n" % (
  72. expected_file, generated_file)
  73. assert expected == generated, '%s%s' % (
  74. files, '\n'.join(line for line in unified_diff(
  75. expected.splitlines(), generated.splitlines())))
  76. os.remove(generated_file)
  77. @pytest.mark.parametrize("names, expected",
  78. [(["__reduce_ex__", "__setattr__"], "special"),
  79. (["__g_", "____dsf", "__23_9"], "private"),
  80. (["simple"], "public"),
  81. (["_", "__", "___", "____", "_____", "___e__", "_nextsimple",
  82. "_filter_it_"], "protected")])
  83. def test_get_visibility(names, expected):
  84. for name in names:
  85. got = get_visibility(name)
  86. assert got == expected, \
  87. 'got %s instead of %s for value %s' % (got, expected, name)