• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from clang.cindex import CursorKind
2from clang.cindex import Index
3from clang.cindex import SourceLocation
4from clang.cindex import SourceRange
5from clang.cindex import TokenKind
6from nose.tools import eq_
7from nose.tools import ok_
8
9from .util import get_tu
10
11def test_token_to_cursor():
12    """Ensure we can obtain a Cursor from a Token instance."""
13    tu = get_tu('int i = 5;')
14    r = tu.get_extent('t.c', (0, 9))
15    tokens = list(tu.get_tokens(extent=r))
16
17    assert len(tokens) == 5
18    assert tokens[1].spelling == 'i'
19    assert tokens[1].kind == TokenKind.IDENTIFIER
20
21    cursor = tokens[1].cursor
22    assert cursor.kind == CursorKind.VAR_DECL
23    assert tokens[1].cursor == tokens[2].cursor
24
25def test_token_location():
26    """Ensure Token.location works."""
27
28    tu = get_tu('int foo = 10;')
29    r = tu.get_extent('t.c', (0, 11))
30
31    tokens = list(tu.get_tokens(extent=r))
32    eq_(len(tokens), 4)
33
34    loc = tokens[1].location
35    ok_(isinstance(loc, SourceLocation))
36    eq_(loc.line, 1)
37    eq_(loc.column, 5)
38    eq_(loc.offset, 4)
39
40def test_token_extent():
41    """Ensure Token.extent works."""
42    tu = get_tu('int foo = 10;')
43    r = tu.get_extent('t.c', (0, 11))
44
45    tokens = list(tu.get_tokens(extent=r))
46    eq_(len(tokens), 4)
47
48    extent = tokens[1].extent
49    ok_(isinstance(extent, SourceRange))
50
51    eq_(extent.start.offset, 4)
52    eq_(extent.end.offset, 7)
53