diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2021-10-27 08:51:43 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2021-10-27 09:12:30 -0400 |
commit | 055a9f4b38aebdabc8cb80dcc40a5b29b8f71781 (patch) | |
tree | 2ef5112132ccebdb9bb0e9553fddd8de8d20c93f /tests/test_parser.py | |
parent | 15aff0ad7d6c92851bc21fc39863a3949edbb9c6 (diff) | |
download | python-coveragepy-git-055a9f4b38aebdabc8cb80dcc40a5b29b8f71781.tar.gz |
fix(debug): ast_dump failed on a few things
Diffstat (limited to 'tests/test_parser.py')
-rw-r--r-- | tests/test_parser.py | 37 |
1 files changed, 21 insertions, 16 deletions
diff --git a/tests/test_parser.py b/tests/test_parser.py index ff7d9ef9..3197ffb4 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -4,7 +4,6 @@ """Tests for coverage.py's code parsing.""" import os.path -import re import textwrap import pytest @@ -14,7 +13,7 @@ from coverage.exceptions import NotPython from coverage.parser import ast_dump, ast_parse, PythonParser from tests.coveragetest import CoverageTest, TESTS_DIR -from tests.helpers import arcz_to_arcs +from tests.helpers import arcz_to_arcs, re_lines class PythonParserTest(CoverageTest): @@ -485,17 +484,23 @@ def test_ast_dump(): # Run the AST_DUMP code to make sure it doesn't fail, with some light # assertions. Use parser.py as the test code since it is the longest file, # and fitting, since it's the AST_DUMP code. - parser_py = os.path.join(TESTS_DIR, "../coverage/parser.py") - with open(parser_py) as f: - ast_root = ast_parse(f.read()) - result = [] - ast_dump(ast_root, print=result.append) - assert len(result) > 10000 - assert result[0] == "<Module" - assert result[-1] == ">" - - def count(pat): - return sum(1 for line in result if re.search(pat, line)) - - assert count(r"^\s+>") > 2000 - assert count(r"<Name @ \d+,\d+(:\d+)? id: '\w+'>") > 1000 + files = [ + os.path.join(TESTS_DIR, "../coverage/parser.py"), + os.path.join(TESTS_DIR, "stress_phystoken.tok"), + ] + for fname in files: + with open(fname) as f: + source = f.read() + num_lines = len(source.splitlines()) + print(f"file {fname} has {num_lines} lines") + ast_root = ast_parse(source) + result = [] + ast_dump(ast_root, print=result.append) + if num_lines < 100: + continue + assert len(result) > 5 * num_lines + assert result[0] == "<Module" + assert result[-1] == ">" + result_text = "\n".join(result) + assert len(re_lines(result_text, r"^\s+>")) > num_lines + assert len(re_lines(result_text, r"<Name @ \d+,\d+(:\d+)? id: '\w+'>")) > num_lines // 2 |