blob: af99f52c630d3c5fc002c637557492e5183fd977 [file] [log] [blame]
R David Murray32a23c32013-04-19 22:15:26 -04001import keyword
2import unittest
3from test import support
4import filecmp
5import os
6import sys
7import subprocess
8import shutil
9import textwrap
10
11KEYWORD_FILE = support.findfile('keyword.py')
R David Murrayf0f7cea2013-04-25 12:01:36 -040012GRAMMAR_FILE = os.path.join(os.path.split(__file__)[0],
13 '..', '..', 'Python', 'graminit.c')
R David Murray32a23c32013-04-19 22:15:26 -040014TEST_PY_FILE = 'keyword_test.py'
15GRAMMAR_TEST_FILE = 'graminit_test.c'
16PY_FILE_WITHOUT_KEYWORDS = 'minimal_keyword.py'
17NONEXISTENT_FILE = 'not_here.txt'
18
19
20class Test_iskeyword(unittest.TestCase):
21 def test_true_is_a_keyword(self):
22 self.assertTrue(keyword.iskeyword('True'))
23
24 def test_uppercase_true_is_not_a_keyword(self):
25 self.assertFalse(keyword.iskeyword('TRUE'))
26
27 def test_none_value_is_not_a_keyword(self):
28 self.assertFalse(keyword.iskeyword(None))
29
30 # This is probably an accident of the current implementation, but should be
31 # preserved for backward compatibility.
32 def test_changing_the_kwlist_does_not_affect_iskeyword(self):
R David Murray87e984c2013-04-19 22:38:58 -040033 oldlist = keyword.kwlist
R David Murrayf0f7cea2013-04-25 12:01:36 -040034 self.addCleanup(setattr, keyword, 'kwlist', oldlist)
R David Murray32a23c32013-04-19 22:15:26 -040035 keyword.kwlist = ['its', 'all', 'eggs', 'beans', 'and', 'a', 'slice']
36 self.assertFalse(keyword.iskeyword('eggs'))
37
38
39class TestKeywordGeneration(unittest.TestCase):
40
41 def _copy_file_without_generated_keywords(self, source_file, dest_file):
R David Murrayf0f7cea2013-04-25 12:01:36 -040042 with open(source_file, 'rb') as fp:
R David Murray32a23c32013-04-19 22:15:26 -040043 lines = fp.readlines()
R David Murrayf0f7cea2013-04-25 12:01:36 -040044 nl = lines[0][len(lines[0].strip()):]
45 with open(dest_file, 'wb') as fp:
46 fp.writelines(lines[:lines.index(b"#--start keywords--" + nl) + 1])
47 fp.writelines(lines[lines.index(b"#--end keywords--" + nl):])
R David Murray32a23c32013-04-19 22:15:26 -040048
49 def _generate_keywords(self, grammar_file, target_keyword_py_file):
50 proc = subprocess.Popen([sys.executable,
51 KEYWORD_FILE,
52 grammar_file,
53 target_keyword_py_file], stderr=subprocess.PIPE)
54 stderr = proc.communicate()[1]
55 return proc.returncode, stderr
56
57 @unittest.skipIf(not os.path.exists(GRAMMAR_FILE),
58 'test only works from source build directory')
59 def test_real_grammar_and_keyword_file(self):
60 self._copy_file_without_generated_keywords(KEYWORD_FILE, TEST_PY_FILE)
R David Murrayf0f7cea2013-04-25 12:01:36 -040061 self.addCleanup(support.unlink, TEST_PY_FILE)
R David Murray32a23c32013-04-19 22:15:26 -040062 self.assertFalse(filecmp.cmp(KEYWORD_FILE, TEST_PY_FILE))
R David Murrayf0f7cea2013-04-25 12:01:36 -040063 self.assertEqual((0, b''), self._generate_keywords(GRAMMAR_FILE,
64 TEST_PY_FILE))
R David Murray32a23c32013-04-19 22:15:26 -040065 self.assertTrue(filecmp.cmp(KEYWORD_FILE, TEST_PY_FILE))
66
67 def test_grammar(self):
68 self._copy_file_without_generated_keywords(KEYWORD_FILE, TEST_PY_FILE)
R David Murrayf0f7cea2013-04-25 12:01:36 -040069 self.addCleanup(support.unlink, TEST_PY_FILE)
R David Murray32a23c32013-04-19 22:15:26 -040070 with open(GRAMMAR_TEST_FILE, 'w') as fp:
71 # Some of these are probably implementation accidents.
72 fp.writelines(textwrap.dedent("""\
73 {2, 1},
74 {11, "encoding_decl", 0, 2, states_79,
75 "\000\000\040\000\000\000\000\000\000\000\000\000"
76 "\000\000\000\000\000\000\000\000\000"},
77 {1, "jello"},
78 {326, 0},
79 {1, "turnip"},
80 \t{1, "This one is tab indented"
81 {278, 0},
82 {1, "crazy but legal"
83 "also legal" {1, "
84 {1, "continue"},
85 {1, "lemon"},
86 {1, "tomato"},
87 {1, "wigii"},
88 {1, 'no good'}
89 {283, 0},
90 {1, "too many spaces"}"""))
R David Murrayf0f7cea2013-04-25 12:01:36 -040091 self.addCleanup(support.unlink, GRAMMAR_TEST_FILE)
R David Murray32a23c32013-04-19 22:15:26 -040092 self._generate_keywords(GRAMMAR_TEST_FILE, TEST_PY_FILE)
93 expected = [
R David Murrayf0f7cea2013-04-25 12:01:36 -040094 " 'This one is tab indented',",
95 " 'also legal',",
96 " 'continue',",
97 " 'crazy but legal',",
98 " 'jello',",
99 " 'lemon',",
100 " 'tomato',",
101 " 'turnip',",
102 " 'wigii',",
R David Murray32a23c32013-04-19 22:15:26 -0400103 ]
104 with open(TEST_PY_FILE) as fp:
R David Murrayf0f7cea2013-04-25 12:01:36 -0400105 lines = fp.read().splitlines()
106 start = lines.index("#--start keywords--") + 1
107 end = lines.index("#--end keywords--")
R David Murray32a23c32013-04-19 22:15:26 -0400108 actual = lines[start:end]
109 self.assertEqual(actual, expected)
110
111 def test_empty_grammar_results_in_no_keywords(self):
112 self._copy_file_without_generated_keywords(KEYWORD_FILE,
113 PY_FILE_WITHOUT_KEYWORDS)
R David Murrayf0f7cea2013-04-25 12:01:36 -0400114 self.addCleanup(support.unlink, PY_FILE_WITHOUT_KEYWORDS)
R David Murray32a23c32013-04-19 22:15:26 -0400115 shutil.copyfile(KEYWORD_FILE, TEST_PY_FILE)
R David Murrayf0f7cea2013-04-25 12:01:36 -0400116 self.addCleanup(support.unlink, TEST_PY_FILE)
117 self.assertEqual((0, b''), self._generate_keywords(os.devnull,
118 TEST_PY_FILE))
R David Murray32a23c32013-04-19 22:15:26 -0400119 self.assertTrue(filecmp.cmp(TEST_PY_FILE, PY_FILE_WITHOUT_KEYWORDS))
120
121 def test_keywords_py_without_markers_produces_error(self):
122 rc, stderr = self._generate_keywords(os.devnull, os.devnull)
123 self.assertNotEqual(rc, 0)
R David Murrayf0f7cea2013-04-25 12:01:36 -0400124 self.assertRegex(stderr, b'does not contain format markers')
R David Murray32a23c32013-04-19 22:15:26 -0400125
126 def test_missing_grammar_file_produces_error(self):
127 rc, stderr = self._generate_keywords(NONEXISTENT_FILE, KEYWORD_FILE)
128 self.assertNotEqual(rc, 0)
129 self.assertRegex(stderr, b'(?ms)' + NONEXISTENT_FILE.encode())
130
131 def test_missing_keywords_py_file_produces_error(self):
132 rc, stderr = self._generate_keywords(os.devnull, NONEXISTENT_FILE)
133 self.assertNotEqual(rc, 0)
134 self.assertRegex(stderr, b'(?ms)' + NONEXISTENT_FILE.encode())
135
136
137if __name__ == "__main__":
138 unittest.main()