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