/tools/logilab/astng/test/unittest_inspector.py

https://gitlab.com/holtscomm/gae-purchase-order-system · Python · 111 lines · 56 code · 18 blank · 37 comment · 1 complexity · 7b4a819f6da4d979bc9d627be6bb092d MD5 · raw file

  1. # Copyright (c) 2003-2010 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 Lesser 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 Lesser General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU Lesser 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. # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
  17. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
  18. # copyright 2003-2010 Sylvain Thenault, all rights reserved.
  19. # contact mailto:thenault@gmail.com
  20. #
  21. # This file is part of logilab-astng.
  22. #
  23. # logilab-astng is free software: you can redistribute it and/or modify it
  24. # under the terms of the GNU Lesser General Public License as published by the
  25. # Free Software Foundation, either version 2.1 of the License, or (at your
  26. # option) any later version.
  27. #
  28. # logilab-astng is distributed in the hope that it will be useful, but
  29. # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  30. # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
  31. # for more details.
  32. #
  33. # You should have received a copy of the GNU Lesser General Public License along
  34. # with logilab-astng. If not, see <http://www.gnu.org/licenses/>.
  35. """
  36. unittest for the visitors.diadefs module
  37. """
  38. import unittest
  39. import sys
  40. from os.path import join, abspath, dirname
  41. from logilab.astng import nodes, inspector
  42. from logilab.astng.bases import Instance, YES
  43. from logilab.astng.manager import ASTNGManager, _silent_no_wrap
  44. MANAGER = ASTNGManager()
  45. def astng_wrapper(func, modname):
  46. return func(modname)
  47. DATA2 = join(dirname(abspath(__file__)), 'data2')
  48. from os.path import join, abspath, dirname
  49. class LinkerTC(unittest.TestCase):
  50. def setUp(self):
  51. self.project = MANAGER.project_from_files([DATA2], astng_wrapper)
  52. self.linker = inspector.Linker(self.project)
  53. self.linker.visit(self.project)
  54. def test_class_implements(self):
  55. klass = self.project.get_module('data2.clientmodule_test')['Ancestor']
  56. self.assert_(hasattr(klass, 'implements'))
  57. self.assertEqual(len(klass.implements), 1)
  58. self.assert_(isinstance(klass.implements[0], nodes.Class))
  59. self.assertEqual(klass.implements[0].name, "Interface")
  60. klass = self.project.get_module('data2.clientmodule_test')['Specialization']
  61. self.assert_(hasattr(klass, 'implements'))
  62. self.assertEqual(len(klass.implements), 0)
  63. def test_locals_assignment_resolution(self):
  64. klass = self.project.get_module('data2.clientmodule_test')['Specialization']
  65. self.assert_(hasattr(klass, 'locals_type'))
  66. type_dict = klass.locals_type
  67. self.assertEqual(len(type_dict), 2)
  68. keys = sorted(type_dict.keys())
  69. self.assertEqual(keys, ['TYPE', 'top'])
  70. self.assertEqual(len(type_dict['TYPE']), 1)
  71. self.assertEqual(type_dict['TYPE'][0].value, 'final class')
  72. self.assertEqual(len(type_dict['top']), 1)
  73. self.assertEqual(type_dict['top'][0].value, 'class')
  74. def test_instance_attrs_resolution(self):
  75. klass = self.project.get_module('data2.clientmodule_test')['Specialization']
  76. self.assert_(hasattr(klass, 'instance_attrs_type'))
  77. type_dict = klass.instance_attrs_type
  78. self.assertEqual(len(type_dict), 3)
  79. keys = sorted(type_dict.keys())
  80. self.assertEqual(keys, ['_id', 'relation', 'toto'])
  81. self.assert_(isinstance(type_dict['relation'][0], Instance), type_dict['relation'])
  82. self.assertEqual(type_dict['relation'][0].name, 'DoNothing')
  83. self.assert_(isinstance(type_dict['toto'][0], Instance), type_dict['toto'])
  84. self.assertEqual(type_dict['toto'][0].name, 'Toto')
  85. self.assert_(type_dict['_id'][0] is YES, type_dict['_id'])
  86. class LinkerTC2(LinkerTC):
  87. def setUp(self):
  88. self.project = MANAGER.project_from_files([DATA2], func_wrapper=_silent_no_wrap)
  89. self.linker = inspector.Linker(self.project)
  90. self.linker.visit(self.project)
  91. __all__ = ('LinkerTC', 'LinkerTC2')
  92. if __name__ == '__main__':
  93. unittest.main()