ยปCore Development>Code coverage>Lib/test/test_keyword.py

Python code coverage for Lib/test/test_keyword.py

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