brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.9 KiB · 6658579 Raw
62 lines · python
1import os2 3from clang.cindex import Config, CursorKind, SourceLocation, SourceRange, TokenKind4 5if "CLANG_LIBRARY_PATH" in os.environ:6    Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])7 8import unittest9 10from .util import get_tu11 12 13class TestTokens(unittest.TestCase):14    def test_token_to_cursor(self):15        """Ensure we can obtain a Cursor from a Token instance."""16        tu = get_tu("int i = 5;")17        r = tu.get_extent("t.c", (0, 9))18        tokens = list(tu.get_tokens(extent=r))19 20        self.assertEqual(len(tokens), 4)21        self.assertEqual(tokens[1].spelling, "i")22        self.assertEqual(tokens[1].kind, TokenKind.IDENTIFIER)23 24        cursor = tokens[1].cursor25        self.assertEqual(cursor.kind, CursorKind.VAR_DECL)26        self.assertEqual(tokens[1].cursor, tokens[2].cursor)27 28    def test_token_location(self):29        """Ensure Token.location works."""30 31        tu = get_tu("int foo = 10;")32        r = tu.get_extent("t.c", (0, 11))33 34        tokens = list(tu.get_tokens(extent=r))35        self.assertEqual(len(tokens), 4)36 37        loc = tokens[1].location38        self.assertIsInstance(loc, SourceLocation)39        self.assertEqual(loc.line, 1)40        self.assertEqual(loc.column, 5)41        self.assertEqual(loc.offset, 4)42 43    def test_token_extent(self):44        """Ensure Token.extent works."""45        tu = get_tu("int foo = 10;")46        r = tu.get_extent("t.c", (0, 11))47 48        tokens = list(tu.get_tokens(extent=r))49        self.assertEqual(len(tokens), 4)50 51        extent = tokens[1].extent52        self.assertIsInstance(extent, SourceRange)53 54        self.assertEqual(extent.start.offset, 4)55        self.assertEqual(extent.end.offset, 7)56 57    def test_null_cursor(self):58        """Ensure that the cursor property converts null cursors to None"""59        tu = get_tu("int i = 5;")60        tokens = list(tu.get_tokens(extent=tu.cursor.extent))61        self.assertEqual(tokens[-1].cursor, None)62