• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import os
2from clang.cindex import Config
3if 'CLANG_LIBRARY_PATH' in os.environ:
4    Config.set_library_path(os.environ['CLANG_LIBRARY_PATH'])
5
6from clang.cindex import CursorKind
7
8import unittest
9
10
11class TestCursorKind(unittest.TestCase):
12    def test_name(self):
13        self.assertEqual(CursorKind.UNEXPOSED_DECL.name, 'UNEXPOSED_DECL')
14
15    def test_get_all_kinds(self):
16        kinds = CursorKind.get_all_kinds()
17        self.assertIn(CursorKind.UNEXPOSED_DECL, kinds)
18        self.assertIn(CursorKind.TRANSLATION_UNIT, kinds)
19        self.assertIn(CursorKind.VARIABLE_REF, kinds)
20        self.assertIn(CursorKind.LAMBDA_EXPR, kinds)
21        self.assertIn(CursorKind.OBJ_BOOL_LITERAL_EXPR, kinds)
22        self.assertIn(CursorKind.OBJ_SELF_EXPR, kinds)
23        self.assertIn(CursorKind.MS_ASM_STMT, kinds)
24        self.assertIn(CursorKind.MODULE_IMPORT_DECL, kinds)
25        self.assertIn(CursorKind.TYPE_ALIAS_TEMPLATE_DECL, kinds)
26
27    def test_kind_groups(self):
28        """Check that every kind classifies to exactly one group."""
29
30        self.assertTrue(CursorKind.UNEXPOSED_DECL.is_declaration())
31        self.assertTrue(CursorKind.TYPE_REF.is_reference())
32        self.assertTrue(CursorKind.DECL_REF_EXPR.is_expression())
33        self.assertTrue(CursorKind.UNEXPOSED_STMT.is_statement())
34        self.assertTrue(CursorKind.INVALID_FILE.is_invalid())
35
36        self.assertTrue(CursorKind.TRANSLATION_UNIT.is_translation_unit())
37        self.assertFalse(CursorKind.TYPE_REF.is_translation_unit())
38
39        self.assertTrue(CursorKind.PREPROCESSING_DIRECTIVE.is_preprocessing())
40        self.assertFalse(CursorKind.TYPE_REF.is_preprocessing())
41
42        self.assertTrue(CursorKind.UNEXPOSED_DECL.is_unexposed())
43        self.assertFalse(CursorKind.TYPE_REF.is_unexposed())
44
45        for k in CursorKind.get_all_kinds():
46            group = [n for n in ('is_declaration', 'is_reference', 'is_expression',
47                                 'is_statement', 'is_invalid', 'is_attribute')
48                     if getattr(k, n)()]
49
50            if k in (   CursorKind.TRANSLATION_UNIT,
51                        CursorKind.MACRO_DEFINITION,
52                        CursorKind.MACRO_INSTANTIATION,
53                        CursorKind.INCLUSION_DIRECTIVE,
54                        CursorKind.PREPROCESSING_DIRECTIVE,
55                        CursorKind.OVERLOAD_CANDIDATE):
56                self.assertEqual(len(group), 0)
57            else:
58                self.assertEqual(len(group), 1)
59