[NFC][Coroutines] Use structured binding with llvm::enumerate in CoroSplit (#116879)
[llvm-project.git] / clang / bindings / python / tests / cindex / test_tokens.py
blobb6c1fc8b836009ba8ad5a655ed46da7b3f1242fa
1 import os
3 from clang.cindex import Config, CursorKind, SourceLocation, SourceRange, TokenKind
5 if "CLANG_LIBRARY_PATH" in os.environ:
6 Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
8 import unittest
10 from .util import get_tu
13 class 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))
20 self.assertEqual(len(tokens), 4)
21 self.assertEqual(tokens[1].spelling, "i")
22 self.assertEqual(tokens[1].kind, TokenKind.IDENTIFIER)
24 cursor = tokens[1].cursor
25 self.assertEqual(cursor.kind, CursorKind.VAR_DECL)
26 self.assertEqual(tokens[1].cursor, tokens[2].cursor)
28 def test_token_location(self):
29 """Ensure Token.location works."""
31 tu = get_tu("int foo = 10;")
32 r = tu.get_extent("t.c", (0, 11))
34 tokens = list(tu.get_tokens(extent=r))
35 self.assertEqual(len(tokens), 4)
37 loc = tokens[1].location
38 self.assertIsInstance(loc, SourceLocation)
39 self.assertEqual(loc.line, 1)
40 self.assertEqual(loc.column, 5)
41 self.assertEqual(loc.offset, 4)
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))
48 tokens = list(tu.get_tokens(extent=r))
49 self.assertEqual(len(tokens), 4)
51 extent = tokens[1].extent
52 self.assertIsInstance(extent, SourceRange)
54 self.assertEqual(extent.start.offset, 4)
55 self.assertEqual(extent.end.offset, 7)