PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/pylint/test/unittest_pyreverse_diadefs.py

https://gitlab.com/holtscomm/gae-purchase-order-system
Python | 174 lines | 125 code | 19 blank | 30 comment | 9 complexity | 7f0f2f086063d125d0d9b2675b05d708 MD5 | raw file
  1. # Copyright (c) 2000-2004 LOGILAB S.A. (Paris, FRANCE).
  2. # http://www.logilab.fr/ -- mailto:contact@logilab.fr
  3. #
  4. # This program is free software; you can redistribute it and/or modify it under
  5. # the terms of the GNU General Public License as published by the Free Software
  6. # Foundation; either version 2 of the License, or (at your option) any later
  7. # version.
  8. #
  9. # This program is distributed in the hope that it will be useful, but WITHOUT
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License along with
  14. # this program; if not, write to the Free Software Foundation, Inc.,
  15. # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. """
  17. unittest for the extensions.diadefslib modules
  18. """
  19. import unittest
  20. import sys
  21. from logilab import astng
  22. from logilab.astng import MANAGER
  23. from logilab.astng.inspector import Linker
  24. from pylint.pyreverse.diadefslib import *
  25. from utils import Config
  26. def astng_wrapper(func, modname):
  27. return func(modname)
  28. PROJECT = MANAGER.project_from_files(['data'], astng_wrapper)
  29. CONFIG = Config()
  30. HANDLER = DiadefsHandler(CONFIG)
  31. def _process_classes(classes):
  32. """extract class names of a list"""
  33. return sorted([(isinstance(c.node, astng.Class), c.title) for c in classes])
  34. def _process_relations(relations):
  35. """extract relation indices from a relation list"""
  36. result = []
  37. for rel_type, rels in relations.iteritems():
  38. for rel in rels:
  39. result.append( (rel_type, rel.from_object.title,
  40. rel.to_object.title) )
  41. result.sort()
  42. return result
  43. class DiaDefGeneratorTC(unittest.TestCase):
  44. def test_option_values(self):
  45. """test for ancestor, associated and module options"""
  46. handler = DiadefsHandler( Config())
  47. df_h = DiaDefGenerator(Linker(PROJECT), handler)
  48. cl_config = Config()
  49. cl_config.classes = ['Specialization']
  50. cl_h = DiaDefGenerator(Linker(PROJECT), DiadefsHandler(cl_config) )
  51. self.assertEqual( (0, 0), df_h._get_levels())
  52. self.assertEqual( False, df_h.module_names)
  53. self.assertEqual( (-1, -1), cl_h._get_levels())
  54. self.assertEqual( True, cl_h.module_names)
  55. for hndl in [df_h, cl_h]:
  56. hndl.config.all_ancestors = True
  57. hndl.config.all_associated = True
  58. hndl.config.module_names = True
  59. hndl._set_default_options()
  60. self.assertEqual( (-1, -1), hndl._get_levels())
  61. self.assertEqual( True, hndl.module_names)
  62. handler = DiadefsHandler( Config())
  63. df_h = DiaDefGenerator(Linker(PROJECT), handler)
  64. cl_config = Config()
  65. cl_config.classes = ['Specialization']
  66. cl_h = DiaDefGenerator(Linker(PROJECT), DiadefsHandler(cl_config) )
  67. for hndl in [df_h, cl_h]:
  68. hndl.config.show_ancestors = 2
  69. hndl.config.show_associated = 1
  70. hndl.config.module_names = False
  71. hndl._set_default_options()
  72. self.assertEqual( (2, 1), hndl._get_levels())
  73. self.assertEqual( False, hndl.module_names)
  74. #def test_default_values(self):
  75. """test efault values for package or class diagrams"""
  76. # TODO : should test difference between default values for package
  77. # or class diagrams
  78. class DefaultDiadefGeneratorTC(unittest.TestCase):
  79. def test_known_values1(self):
  80. dd = DefaultDiadefGenerator(Linker(PROJECT), HANDLER).visit(PROJECT)
  81. self.assertEqual(len(dd), 2)
  82. keys = [d.TYPE for d in dd]
  83. self.assertEqual(keys, ['package', 'class'])
  84. pd = dd[0]
  85. self.assertEqual(pd.title, 'packages No Name')
  86. modules = sorted([(isinstance(m.node, astng.Module), m.title)
  87. for m in pd.objects])
  88. self.assertEqual(modules, [(True, 'data'),
  89. (True, 'data.clientmodule_test'),
  90. (True, 'data.suppliermodule_test')])
  91. cd = dd[1]
  92. self.assertEqual(cd.title, 'classes No Name')
  93. classes = _process_classes(cd.objects)
  94. self.assertEqual(classes, [(True, 'Ancestor'),
  95. (True, 'DoNothing'),
  96. (True, 'Interface'),
  97. (True, 'Specialization')]
  98. )
  99. _should_rels = [('association', 'DoNothing', 'Ancestor'),
  100. ('association', 'DoNothing', 'Specialization'),
  101. ('implements', 'Ancestor', 'Interface'),
  102. ('specialization', 'Specialization', 'Ancestor')]
  103. def test_exctract_relations(self):
  104. """test extract_relations between classes"""
  105. cd = DefaultDiadefGenerator(Linker(PROJECT), HANDLER).visit(PROJECT)[1]
  106. cd.extract_relationships()
  107. relations = _process_relations(cd.relationships)
  108. self.assertEqual(relations, self._should_rels)
  109. def test_functional_relation_extraction(self):
  110. """functional test of relations extraction;
  111. different classes possibly in different modules"""
  112. # XXX should be catching pyreverse environnement problem but doesn't
  113. # pyreverse doesn't extracts the relations but this test ok
  114. project = MANAGER.project_from_files(['data'], astng_wrapper)
  115. handler = DiadefsHandler( Config() )
  116. diadefs = handler.get_diadefs(project, Linker(project, tag=True) )
  117. cd = diadefs[1]
  118. relations = _process_relations(cd.relationships)
  119. self.assertEqual(relations, self._should_rels)
  120. def test_known_values2(self):
  121. project = MANAGER.project_from_files(['data.clientmodule_test'], astng_wrapper)
  122. dd = DefaultDiadefGenerator(Linker(project), HANDLER).visit(project)
  123. self.assertEqual(len(dd), 1)
  124. keys = [d.TYPE for d in dd]
  125. self.assertEqual(keys, ['class'])
  126. cd = dd[0]
  127. self.assertEqual(cd.title, 'classes No Name')
  128. classes = _process_classes(cd.objects)
  129. self.assertEqual(classes, [(True, 'Ancestor'),
  130. (True, 'DoNothing'),
  131. (True, 'Specialization')]
  132. )
  133. class ClassDiadefGeneratorTC(unittest.TestCase):
  134. def test_known_values1(self):
  135. HANDLER.config.classes = ['Specialization']
  136. cdg = ClassDiadefGenerator(Linker(PROJECT), HANDLER)
  137. special = 'data.clientmodule_test.Specialization'
  138. cd = cdg.class_diagram(PROJECT, special)
  139. self.assertEqual(cd.title, special)
  140. classes = _process_classes(cd.objects)
  141. self.assertEqual(classes, [(True, 'data.clientmodule_test.Ancestor'),
  142. (True, special),
  143. (True, 'data.suppliermodule_test.DoNothing'),
  144. ])
  145. def test_known_values2(self):
  146. HANDLER.config.module_names = False
  147. cd = ClassDiadefGenerator(Linker(PROJECT), HANDLER).class_diagram(PROJECT, 'data.clientmodule_test.Specialization')
  148. self.assertEqual(cd.title, 'data.clientmodule_test.Specialization')
  149. classes = _process_classes(cd.objects)
  150. self.assertEqual(classes, [(True, 'Ancestor'),
  151. (True, 'DoNothing'),
  152. (True, 'Specialization')
  153. ])
  154. if __name__ == '__main__':
  155. unittest.main()