• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import keyword
2import unittest
3
4
5class Test_iskeyword(unittest.TestCase):
6    def test_true_is_a_keyword(self):
7        self.assertTrue(keyword.iskeyword('True'))
8
9    def test_uppercase_true_is_not_a_keyword(self):
10        self.assertFalse(keyword.iskeyword('TRUE'))
11
12    def test_none_value_is_not_a_keyword(self):
13        self.assertFalse(keyword.iskeyword(None))
14
15    # This is probably an accident of the current implementation, but should be
16    # preserved for backward compatibility.
17    def test_changing_the_kwlist_does_not_affect_iskeyword(self):
18        oldlist = keyword.kwlist
19        self.addCleanup(setattr, keyword, 'kwlist', oldlist)
20        keyword.kwlist = ['its', 'all', 'eggs', 'beans', 'and', 'a', 'slice']
21        self.assertFalse(keyword.iskeyword('eggs'))
22
23    def test_all_keywords_fail_to_be_used_as_names(self):
24        for key in keyword.kwlist:
25            with self.assertRaises(SyntaxError):
26                exec(f"{key} = 42")
27
28    def test_async_and_await_are_keywords(self):
29        self.assertIn("async", keyword.kwlist)
30        self.assertIn("await", keyword.kwlist)
31
32    def test_keywords_are_sorted(self):
33        self.assertListEqual(sorted(keyword.kwlist), keyword.kwlist)
34
35
36if __name__ == "__main__":
37    unittest.main()
38