test_tokens.py 1.4 KB

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