-
-
Notifications
You must be signed in to change notification settings - Fork 35k
bpo-29812: Improve testing of token and tokenize #681
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import filecmp | ||
| import os | ||
| import subprocess | ||
| import sys | ||
| from test import support | ||
| import token | ||
| import unittest | ||
|
|
||
|
|
||
| TOKEN_FILE = support.findfile('token.py') | ||
| TOKEN_INCLUDE_FILE = os.path.join(os.path.split(__file__)[0], | ||
| '..', '..', 'Include', 'token.h') | ||
| TEST_PY_FILE = 'token_test.py' | ||
|
|
||
|
|
||
| class TestTokenGeneration(unittest.TestCase): | ||
|
|
||
| def _copy_file_without_generated_tokens(self, source_file, dest_file): | ||
| with open(source_file, 'rb') as fp: | ||
| lines = fp.readlines() | ||
| nl = lines[0][len(lines[0].strip()):] | ||
| with open(dest_file, 'wb') as fp: | ||
| fp.writelines(lines[:lines.index(b"#--start constants--" + nl) + 1]) | ||
| fp.writelines(lines[lines.index(b"#--end constants--" + nl):]) | ||
| self.addCleanup(support.unlink, dest_file) | ||
|
|
||
| def _generate_tokens(self, skeleton_file, target_token_py_file): | ||
| proc = subprocess.Popen([sys.executable, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we use the new |
||
| TOKEN_FILE, | ||
| skeleton_file, | ||
| target_token_py_file], stderr=subprocess.PIPE) | ||
| stderr = proc.communicate()[1] | ||
| return proc.returncode, stderr | ||
|
|
||
| @unittest.skipUnless(os.path.exists(TOKEN_FILE), | ||
| 'test only works from source build directory') | ||
| def test_real_token_file(self): | ||
| self._copy_file_without_generated_tokens(TOKEN_FILE, TEST_PY_FILE) | ||
| self.assertFalse(filecmp.cmp(TOKEN_FILE, TEST_PY_FILE)) | ||
| self.assertEqual((0, b''), self._generate_tokens(TOKEN_INCLUDE_FILE, | ||
| TEST_PY_FILE)) | ||
| self.assertTrue(filecmp.cmp(TOKEN_FILE, TEST_PY_FILE)) | ||
|
|
||
| def test_missing_output_file_causes_error(self): | ||
| rc, stderr = self._generate_tokens(os.devnull, 'not_here.txt') | ||
| self.assertNotEqual(rc, 0) | ||
| self.assertIn(b'I/O error', stderr) | ||
| self.assertIn(b'not_here.txt', stderr) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| from test import support | ||
| from tokenize import (tokenize, _tokenize, untokenize, NUMBER, NAME, OP, | ||
| STRING, ENDMARKER, ENCODING, tok_name, detect_encoding, | ||
| open as tokenize_open, Untokenizer) | ||
| open as tokenize_open, Untokenizer, EXACT_TOKEN_TYPES) | ||
| from io import BytesIO | ||
| from unittest import TestCase, mock | ||
| from test.test_grammar import (VALID_UNDERSCORE_LITERALS, | ||
|
|
@@ -1351,6 +1351,28 @@ def assertExactTypeEqual(self, opstr, *optypes): | |
| self.assertEqual(tok_name[tokens[1 + num_optypes].exact_type], | ||
| tok_name[token.ENDMARKER]) | ||
|
|
||
| def test_all_literal_tokens(self): | ||
| non_literals = { | ||
| token.ENDMARKER, token.NAME, token.NUMBER, | ||
| token.STRING, token.NEWLINE, token.INDENT, | ||
| token.DEDENT, token.OP, token.ERRORTOKEN, | ||
| token.AWAIT, token.ASYNC, | ||
| token.COMMENT, token.NL, token.ENCODING, | ||
| token.N_TOKENS, token.NT_OFFSET, | ||
| } | ||
|
|
||
| for tok in range(token.N_TOKENS): | ||
| if tok in non_literals: | ||
| continue | ||
| name = tok_name[tok] | ||
| with self.subTest(name=name): | ||
| self.assertIn(tok, EXACT_TOKEN_TYPES.values()) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at CI logs of an earlier commit, I wonder if we should make the following message clearer: |
||
|
|
||
| for tok in non_literals: | ||
| name = tok_name[tok] | ||
| with self.subTest(name=name): | ||
| self.assertNotIn(tok, EXACT_TOKEN_TYPES.values()) | ||
|
|
||
| def test_exact_type(self): | ||
| self.assertExactTypeEqual('()', token.LPAR, token.RPAR) | ||
| self.assertExactTypeEqual('[]', token.LSQB, token.RSQB) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -129,33 +129,37 @@ def _main(): | |
| comment = comment_match.group(1) | ||
| tokens[prev_val]['comment'] = comment | ||
| keys = sorted(tokens.keys()) | ||
| # load the output skeleton from the target: | ||
| # load the output skeleton from the target, taking | ||
| # care to preserve its newline convention. | ||
| # newline detection code from keyword.py | ||
| try: | ||
| fp = open(outFileName) | ||
| fp = open(outFileName, newline='') | ||
| except OSError as err: | ||
| sys.stderr.write("I/O error: %s\n" % str(err)) | ||
| sys.exit(2) | ||
| with fp: | ||
| format = fp.read().split("\n") | ||
| format = fp.readlines() | ||
| nl = format[0][len(format[0].strip()):] if format else '\n' | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd use more descriptive name like |
||
|
|
||
| try: | ||
| start = format.index("#--start constants--") + 1 | ||
| end = format.index("#--end constants--") | ||
| start = format.index("#--start constants--" + nl) + 1 | ||
| end = format.index("#--end constants--" + nl) | ||
| except ValueError: | ||
| sys.stderr.write("target does not contain format markers") | ||
| sys.exit(3) | ||
| lines = [] | ||
| for key in keys: | ||
| lines.append("%s = %d" % (tokens[key]["token"], key)) | ||
| lines.append("%s = %d%s" % (tokens[key]["token"], key, nl)) | ||
| if "comment" in tokens[key]: | ||
| lines.append("# %s" % tokens[key]["comment"]) | ||
| lines.append("# %s%s" % (tokens[key]["comment"], nl)) | ||
| format[start:end] = lines | ||
| try: | ||
| fp = open(outFileName, 'w') | ||
| fp = open(outFileName, 'w', newline='') | ||
| except OSError as err: | ||
| sys.stderr.write("I/O error: %s\n" % str(err)) | ||
| sys.exit(4) | ||
| with fp: | ||
| fp.write("\n".join(format)) | ||
| fp.writelines(format) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please split stdlib and non-stdlib modules.