gh-102158: Add tests for `softkwlist` (#102159)

---------

Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
This commit is contained in:
Eclips4 2023-02-24 05:28:24 +03:00 committed by GitHub
parent 0c857865e4
commit 9f3ecd1aa3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 0 deletions

View File

@ -20,18 +20,36 @@ class Test_iskeyword(unittest.TestCase):
keyword.kwlist = ['its', 'all', 'eggs', 'beans', 'and', 'a', 'slice']
self.assertFalse(keyword.iskeyword('eggs'))
def test_changing_the_softkwlist_does_not_affect_issoftkeyword(self):
oldlist = keyword.softkwlist
self.addCleanup(setattr, keyword, "softkwlist", oldlist)
keyword.softkwlist = ["foo", "bar", "spam", "egs", "case"]
self.assertFalse(keyword.issoftkeyword("spam"))
def test_all_keywords_fail_to_be_used_as_names(self):
for key in keyword.kwlist:
with self.assertRaises(SyntaxError):
exec(f"{key} = 42")
def test_all_soft_keywords_can_be_used_as_names(self):
for key in keyword.softkwlist:
exec(f"{key} = 42")
def test_async_and_await_are_keywords(self):
self.assertIn("async", keyword.kwlist)
self.assertIn("await", keyword.kwlist)
def test_match_and_case_are_soft_keywords(self):
self.assertIn("match", keyword.softkwlist)
self.assertIn("case", keyword.softkwlist)
self.assertIn("_", keyword.softkwlist)
def test_keywords_are_sorted(self):
self.assertListEqual(sorted(keyword.kwlist), keyword.kwlist)
def test_softkeywords_are_sorted(self):
self.assertListEqual(sorted(keyword.softkwlist), keyword.softkwlist)
if __name__ == "__main__":
unittest.main()