|
| 1 | +# Copyright 2014-2017 Insight Software Consortium. |
| 2 | +# Copyright 2004-2009 Roman Yakovenko. |
| 3 | +# Distributed under the Boost Software License, Version 1.0. |
| 4 | +# See http://www.boost.org/LICENSE_1_0.txt |
| 5 | + |
| 6 | +import unittest |
| 7 | +import inspect |
| 8 | + |
| 9 | +from pygccxml import declarations |
| 10 | + |
| 11 | + |
| 12 | +class Test(unittest.TestCase): |
| 13 | + |
| 14 | + def test_types_hashes(self): |
| 15 | + """ |
| 16 | + Test if all the type_t instances implement a hash method. |
| 17 | +
|
| 18 | + The hash is part of the public API, as there are multiple tools |
| 19 | + that rely on it to compare type_t instances. |
| 20 | +
|
| 21 | + The best way to test this is to instanciate dummy type_t objects |
| 22 | + for each class that subclasses type_t, and check that the hash of |
| 23 | + these objects is not None. |
| 24 | +
|
| 25 | + """ |
| 26 | + members = inspect.getmembers(declarations, inspect.isclass) |
| 27 | + for member in members: |
| 28 | + member_type = member[1] |
| 29 | + is_type_t_subclass = issubclass(member_type, declarations.type_t) |
| 30 | + is_not_type_t = member_type != declarations.type_t |
| 31 | + if is_type_t_subclass and is_not_type_t: |
| 32 | + type_mockup = _create_type_t_mockup(member_type) |
| 33 | + self.assertIsNotNone(hash(type_mockup)) |
| 34 | + |
| 35 | + |
| 36 | +def _create_type_t_mockup(member_type): |
| 37 | + nbr_parameters = len(inspect.signature(member_type).parameters) |
| 38 | + if nbr_parameters == 0: |
| 39 | + m = member_type() |
| 40 | + else: |
| 41 | + m = member_type(_base_mockup()) |
| 42 | + m.cache.decl_string = "" |
| 43 | + return m |
| 44 | + |
| 45 | + |
| 46 | +class _base_mockup(declarations.type_t): |
| 47 | + |
| 48 | + def __init__(self): |
| 49 | + declarations.type_t.__init__(self) |
| 50 | + self.cache.decl_string = "" |
| 51 | + self._decl_string = "" |
| 52 | + self.variable_type = declarations.type_t() |
| 53 | + |
| 54 | + def build_decl_string(self, with_defaults=False): |
| 55 | + return self._decl_string |
| 56 | + |
| 57 | + |
| 58 | +def create_suite(): |
| 59 | + suite = unittest.TestSuite() |
| 60 | + suite.addTest(unittest.makeSuite(Test)) |
| 61 | + return suite |
| 62 | + |
| 63 | + |
| 64 | +def run_suite(): |
| 65 | + unittest.TextTestRunner(verbosity=2).run(create_suite()) |
| 66 | + |
| 67 | + |
| 68 | +if __name__ == "__main__": |
| 69 | + run_suite() |
0 commit comments