2013-04-19 23:15:26 -03:00
|
|
|
import keyword
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
|
|
|
class Test_iskeyword(unittest.TestCase):
|
|
|
|
def test_true_is_a_keyword(self):
|
|
|
|
self.assertTrue(keyword.iskeyword('True'))
|
|
|
|
|
|
|
|
def test_uppercase_true_is_not_a_keyword(self):
|
|
|
|
self.assertFalse(keyword.iskeyword('TRUE'))
|
|
|
|
|
|
|
|
def test_none_value_is_not_a_keyword(self):
|
|
|
|
self.assertFalse(keyword.iskeyword(None))
|
|
|
|
|
|
|
|
# This is probably an accident of the current implementation, but should be
|
|
|
|
# preserved for backward compatibility.
|
|
|
|
def test_changing_the_kwlist_does_not_affect_iskeyword(self):
|
2013-04-19 23:38:58 -03:00
|
|
|
oldlist = keyword.kwlist
|
2013-04-25 13:01:36 -03:00
|
|
|
self.addCleanup(setattr, keyword, 'kwlist', oldlist)
|
2013-04-19 23:15:26 -03:00
|
|
|
keyword.kwlist = ['its', 'all', 'eggs', 'beans', 'and', 'a', 'slice']
|
|
|
|
self.assertFalse(keyword.iskeyword('eggs'))
|
|
|
|
|
2023-02-23 22:28:24 -04:00
|
|
|
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"))
|
|
|
|
|
2019-03-25 19:01:12 -03:00
|
|
|
def test_all_keywords_fail_to_be_used_as_names(self):
|
|
|
|
for key in keyword.kwlist:
|
|
|
|
with self.assertRaises(SyntaxError):
|
|
|
|
exec(f"{key} = 42")
|
2013-04-19 23:15:26 -03:00
|
|
|
|
2023-02-23 22:28:24 -04:00
|
|
|
def test_all_soft_keywords_can_be_used_as_names(self):
|
|
|
|
for key in keyword.softkwlist:
|
|
|
|
exec(f"{key} = 42")
|
|
|
|
|
2019-03-25 19:01:12 -03:00
|
|
|
def test_async_and_await_are_keywords(self):
|
|
|
|
self.assertIn("async", keyword.kwlist)
|
|
|
|
self.assertIn("await", keyword.kwlist)
|
2013-04-19 23:15:26 -03:00
|
|
|
|
2023-05-16 00:36:23 -03:00
|
|
|
def test_soft_keywords(self):
|
|
|
|
self.assertIn("type", keyword.softkwlist)
|
2023-02-23 22:28:24 -04:00
|
|
|
self.assertIn("match", keyword.softkwlist)
|
|
|
|
self.assertIn("case", keyword.softkwlist)
|
|
|
|
self.assertIn("_", keyword.softkwlist)
|
|
|
|
|
2019-03-25 19:01:12 -03:00
|
|
|
def test_keywords_are_sorted(self):
|
|
|
|
self.assertListEqual(sorted(keyword.kwlist), keyword.kwlist)
|
2013-04-19 23:15:26 -03:00
|
|
|
|
2023-02-23 22:28:24 -04:00
|
|
|
def test_softkeywords_are_sorted(self):
|
|
|
|
self.assertListEqual(sorted(keyword.softkwlist), keyword.softkwlist)
|
|
|
|
|
2013-04-19 23:15:26 -03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|