• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import os
2from clang.cindex import Config
3if 'CLANG_LIBRARY_PATH' in os.environ:
4    Config.set_library_path(os.environ['CLANG_LIBRARY_PATH'])
5
6from clang.cindex import TokenKind
7
8import unittest
9
10
11class TestTokenKind(unittest.TestCase):
12    def test_constructor(self):
13        """Ensure TokenKind constructor works as expected."""
14
15        t = TokenKind(5, 'foo')
16
17        self.assertEqual(t.value, 5)
18        self.assertEqual(t.name, 'foo')
19
20    def test_bad_register(self):
21        """Ensure a duplicate value is rejected for registration."""
22
23        with self.assertRaises(ValueError):
24            TokenKind.register(2, 'foo')
25
26    def test_unknown_value(self):
27        """Ensure trying to fetch an unknown value raises."""
28
29        with self.assertRaises(ValueError):
30            TokenKind.from_value(-1)
31
32    def test_registration(self):
33        """Ensure that items registered appear as class attributes."""
34        self.assertTrue(hasattr(TokenKind, 'LITERAL'))
35        literal = TokenKind.LITERAL
36
37        self.assertIsInstance(literal, TokenKind)
38
39    def test_from_value(self):
40        """Ensure registered values can be obtained from from_value()."""
41        t = TokenKind.from_value(3)
42        self.assertIsInstance(t, TokenKind)
43        self.assertEqual(t, TokenKind.LITERAL)
44
45    def test_repr(self):
46        """Ensure repr() works."""
47
48        r = repr(TokenKind.LITERAL)
49        self.assertEqual(r, 'TokenKind.LITERAL')
50