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