diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2022-11-06 15:51:44 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2022-11-06 16:16:04 -0500 |
commit | 556344babd5210c093eba547d1b15489843f4359 (patch) | |
tree | 5ea9c3f84044722f116ebbfad4bb66cda91b8a87 /tests | |
parent | faaf0d45abcf0a11c9e5db144c5b79f581dd92eb (diff) | |
download | python-coveragepy-git-556344babd5210c093eba547d1b15489843f4359.tar.gz |
refactor: no need for special handling of compiling unicode source
This was a holdover from Python 2 days.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_parser.py | 5 | ||||
-rw-r--r-- | tests/test_phystokens.py | 115 |
2 files changed, 4 insertions, 116 deletions
diff --git a/tests/test_parser.py b/tests/test_parser.py index 6d181c9e..b13c32fe 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -3,6 +3,7 @@ """Tests for coverage.py's code parsing.""" +import ast import os.path import textwrap import warnings @@ -11,7 +12,7 @@ import pytest from coverage import env from coverage.exceptions import NotPython -from coverage.parser import ast_dump, ast_parse, PythonParser +from coverage.parser import ast_dump, PythonParser from tests.coveragetest import CoverageTest, TESTS_DIR from tests.helpers import arcz_to_arcs, re_lines, xfail_pypy_3749 @@ -530,7 +531,7 @@ def test_ast_dump(): with warnings.catch_warnings(): # stress_phystoken.tok has deprecation warnings, suppress them. warnings.filterwarnings("ignore", message=r".*invalid escape sequence",) - ast_root = ast_parse(source) + ast_root = ast.parse(source) result = [] ast_dump(ast_root, print=result.append) if num_lines < 100: diff --git a/tests/test_phystokens.py b/tests/test_phystokens.py index 8a8b8506..da37cead 100644 --- a/tests/test_phystokens.py +++ b/tests/test_phystokens.py @@ -12,7 +12,6 @@ import pytest from coverage import env from coverage.phystokens import source_token_lines, source_encoding -from coverage.phystokens import neuter_encoding_declaration, compile_unicode from coverage.python import get_python_source from tests.coveragetest import CoverageTest, TESTS_DIR @@ -147,7 +146,7 @@ class SoftKeywordTest(CoverageTest): assert tokens[11][3] == ("nam", "case") -# The default encoding is different in Python 2 and Python 3. +# The default source file encoding. DEF_ENCODING = "utf-8" @@ -206,115 +205,3 @@ class SourceEncodingTest(CoverageTest): source = b"# coding: klingon\n" with pytest.raises(SyntaxError, match="unknown encoding: klingon"): source_encoding(source) - - -class NeuterEncodingDeclarationTest(CoverageTest): - """Tests of phystokens.neuter_encoding_declaration().""" - - run_in_temp_dir = False - - def test_neuter_encoding_declaration(self): - for lines_diff_expected, source, _ in ENCODING_DECLARATION_SOURCES: - neutered = neuter_encoding_declaration(source.decode("ascii")) - neutered = neutered.encode("ascii") - - # The neutered source should have the same number of lines. - source_lines = source.splitlines() - neutered_lines = neutered.splitlines() - assert len(source_lines) == len(neutered_lines) - - # Only one of the lines should be different. - lines_different = sum( - int(nline != sline) for nline, sline in zip(neutered_lines, source_lines) - ) - assert lines_diff_expected == lines_different - - # The neutered source will be detected as having no encoding - # declaration. - assert source_encoding(neutered) == DEF_ENCODING, f"Wrong encoding in {neutered!r}" - - def test_two_encoding_declarations(self): - input_src = textwrap.dedent("""\ - # -*- coding: ascii -*- - # -*- coding: utf-8 -*- - # -*- coding: utf-16 -*- - """) - expected_src = textwrap.dedent("""\ - # (deleted declaration) -*- - # (deleted declaration) -*- - # -*- coding: utf-16 -*- - """) - output_src = neuter_encoding_declaration(input_src) - assert expected_src == output_src - - def test_one_encoding_declaration(self): - input_src = textwrap.dedent("""\ - # -*- coding: utf-16 -*- - # Just a comment. - # -*- coding: ascii -*- - """) - expected_src = textwrap.dedent("""\ - # (deleted declaration) -*- - # Just a comment. - # -*- coding: ascii -*- - """) - output_src = neuter_encoding_declaration(input_src) - assert expected_src == output_src - - -class Bug529Test(CoverageTest): - """Test of bug 529""" - - def test_bug_529(self): - # Don't over-neuter coding declarations. This happened with a test - # file which contained code in multi-line strings, all with coding - # declarations. The neutering of the file also changed the multi-line - # strings, which it shouldn't have. - self.make_file("the_test.py", '''\ - # -*- coding: utf-8 -*- - import unittest - class Bug529Test(unittest.TestCase): - def test_two_strings_are_equal(self): - src1 = u"""\\ - # -*- coding: utf-8 -*- - # Just a comment. - """ - src2 = u"""\\ - # -*- coding: utf-8 -*- - # Just a comment. - """ - self.assertEqual(src1, src2) - - if __name__ == "__main__": - unittest.main() - ''') - status, out = self.run_command_status("coverage run the_test.py") - assert status == 0 - assert "OK" in out - # If this test fails, the output will be super-confusing, because it - # has a failing unit test contained within the failing unit test. - - -class CompileUnicodeTest(CoverageTest): - """Tests of compiling Unicode strings.""" - - run_in_temp_dir = False - - def assert_compile_unicode(self, source): - """Assert that `source` will compile properly with `compile_unicode`.""" - source += "a = 42\n" - # This doesn't raise an exception: - code = compile_unicode(source, "<string>", "exec") - globs = {} - exec(code, globs) - assert globs['a'] == 42 - - def test_cp1252(self): - uni = """# coding: cp1252\n# \u201C curly \u201D\n""" - self.assert_compile_unicode(uni) - - def test_double_coding_declaration(self): - # Build this string in a weird way so that actual vim's won't try to - # interpret it... - uni = "# -*- coding:utf-8 -*-\n# v" + "im: fileencoding=utf-8\n" - self.assert_compile_unicode(uni) |