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