PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/test/test_keyword.py

https://gitlab.com/unofficial-mirrors/cpython
Python | 138 lines | 125 code | 11 blank | 2 comment | 2 complexity | 08fc5e874b566c41ee75192d94df456f MD5 | raw file
  1. import keyword
  2. import unittest
  3. from test import support
  4. import filecmp
  5. import os
  6. import sys
  7. import subprocess
  8. import shutil
  9. import textwrap
  10. KEYWORD_FILE = support.findfile('keyword.py')
  11. GRAMMAR_FILE = os.path.join(os.path.split(__file__)[0],
  12. '..', '..', 'Python', 'graminit.c')
  13. TEST_PY_FILE = 'keyword_test.py'
  14. GRAMMAR_TEST_FILE = 'graminit_test.c'
  15. PY_FILE_WITHOUT_KEYWORDS = 'minimal_keyword.py'
  16. NONEXISTENT_FILE = 'not_here.txt'
  17. class Test_iskeyword(unittest.TestCase):
  18. def test_true_is_a_keyword(self):
  19. self.assertTrue(keyword.iskeyword('True'))
  20. def test_uppercase_true_is_not_a_keyword(self):
  21. self.assertFalse(keyword.iskeyword('TRUE'))
  22. def test_none_value_is_not_a_keyword(self):
  23. self.assertFalse(keyword.iskeyword(None))
  24. # This is probably an accident of the current implementation, but should be
  25. # preserved for backward compatibility.
  26. def test_changing_the_kwlist_does_not_affect_iskeyword(self):
  27. oldlist = keyword.kwlist
  28. self.addCleanup(setattr, keyword, 'kwlist', oldlist)
  29. keyword.kwlist = ['its', 'all', 'eggs', 'beans', 'and', 'a', 'slice']
  30. self.assertFalse(keyword.iskeyword('eggs'))
  31. class TestKeywordGeneration(unittest.TestCase):
  32. def _copy_file_without_generated_keywords(self, source_file, dest_file):
  33. with open(source_file, 'rb') as fp:
  34. lines = fp.readlines()
  35. nl = lines[0][len(lines[0].strip()):]
  36. with open(dest_file, 'wb') as fp:
  37. fp.writelines(lines[:lines.index(b"#--start keywords--" + nl) + 1])
  38. fp.writelines(lines[lines.index(b"#--end keywords--" + nl):])
  39. def _generate_keywords(self, grammar_file, target_keyword_py_file):
  40. proc = subprocess.Popen([sys.executable,
  41. KEYWORD_FILE,
  42. grammar_file,
  43. target_keyword_py_file], stderr=subprocess.PIPE)
  44. stderr = proc.communicate()[1]
  45. return proc.returncode, stderr
  46. @unittest.skipIf(not os.path.exists(GRAMMAR_FILE),
  47. 'test only works from source build directory')
  48. def test_real_grammar_and_keyword_file(self):
  49. self._copy_file_without_generated_keywords(KEYWORD_FILE, TEST_PY_FILE)
  50. self.addCleanup(support.unlink, TEST_PY_FILE)
  51. self.assertFalse(filecmp.cmp(KEYWORD_FILE, TEST_PY_FILE))
  52. self.assertEqual((0, b''), self._generate_keywords(GRAMMAR_FILE,
  53. TEST_PY_FILE))
  54. self.assertTrue(filecmp.cmp(KEYWORD_FILE, TEST_PY_FILE))
  55. def test_grammar(self):
  56. self._copy_file_without_generated_keywords(KEYWORD_FILE, TEST_PY_FILE)
  57. self.addCleanup(support.unlink, TEST_PY_FILE)
  58. with open(GRAMMAR_TEST_FILE, 'w') as fp:
  59. # Some of these are probably implementation accidents.
  60. fp.writelines(textwrap.dedent("""\
  61. {2, 1},
  62. {11, "encoding_decl", 0, 2, states_79,
  63. "\000\000\040\000\000\000\000\000\000\000\000\000"
  64. "\000\000\000\000\000\000\000\000\000"},
  65. {1, "jello"},
  66. {326, 0},
  67. {1, "turnip"},
  68. \t{1, "This one is tab indented"
  69. {278, 0},
  70. {1, "crazy but legal"
  71. "also legal" {1, "
  72. {1, "continue"},
  73. {1, "lemon"},
  74. {1, "tomato"},
  75. {1, "wigii"},
  76. {1, 'no good'}
  77. {283, 0},
  78. {1, "too many spaces"}"""))
  79. self.addCleanup(support.unlink, GRAMMAR_TEST_FILE)
  80. self._generate_keywords(GRAMMAR_TEST_FILE, TEST_PY_FILE)
  81. expected = [
  82. " 'This one is tab indented',",
  83. " 'also legal',",
  84. " 'continue',",
  85. " 'crazy but legal',",
  86. " 'jello',",
  87. " 'lemon',",
  88. " 'tomato',",
  89. " 'turnip',",
  90. " 'wigii',",
  91. ]
  92. with open(TEST_PY_FILE) as fp:
  93. lines = fp.read().splitlines()
  94. start = lines.index("#--start keywords--") + 1
  95. end = lines.index("#--end keywords--")
  96. actual = lines[start:end]
  97. self.assertEqual(actual, expected)
  98. def test_empty_grammar_results_in_no_keywords(self):
  99. self._copy_file_without_generated_keywords(KEYWORD_FILE,
  100. PY_FILE_WITHOUT_KEYWORDS)
  101. self.addCleanup(support.unlink, PY_FILE_WITHOUT_KEYWORDS)
  102. shutil.copyfile(KEYWORD_FILE, TEST_PY_FILE)
  103. self.addCleanup(support.unlink, TEST_PY_FILE)
  104. self.assertEqual((0, b''), self._generate_keywords(os.devnull,
  105. TEST_PY_FILE))
  106. self.assertTrue(filecmp.cmp(TEST_PY_FILE, PY_FILE_WITHOUT_KEYWORDS))
  107. def test_keywords_py_without_markers_produces_error(self):
  108. rc, stderr = self._generate_keywords(os.devnull, os.devnull)
  109. self.assertNotEqual(rc, 0)
  110. self.assertRegex(stderr, b'does not contain format markers')
  111. def test_missing_grammar_file_produces_error(self):
  112. rc, stderr = self._generate_keywords(NONEXISTENT_FILE, KEYWORD_FILE)
  113. self.assertNotEqual(rc, 0)
  114. self.assertRegex(stderr, b'(?ms)' + NONEXISTENT_FILE.encode())
  115. def test_missing_keywords_py_file_produces_error(self):
  116. rc, stderr = self._generate_keywords(os.devnull, NONEXISTENT_FILE)
  117. self.assertNotEqual(rc, 0)
  118. self.assertRegex(stderr, b'(?ms)' + NONEXISTENT_FILE.encode())
  119. if __name__ == "__main__":
  120. unittest.main()