diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2015-11-14 09:30:58 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2015-11-14 09:30:58 -0500 |
commit | beb2d29469d62a9e0ac9bce29170382ca986542c (patch) | |
tree | 14185e167dd9446f8dc57822537d063265c9b8e5 /tests/test_phystokens.py | |
parent | cf11819662ce3cd13e8dc3f9e23a0a89c24124fb (diff) | |
download | python-coveragepy-git-beb2d29469d62a9e0ac9bce29170382ca986542c.tar.gz |
Another edge case of encoding detection. #443
Diffstat (limited to 'tests/test_phystokens.py')
-rw-r--r-- | tests/test_phystokens.py | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/tests/test_phystokens.py b/tests/test_phystokens.py index 7bdece72..e28fb176 100644 --- a/tests/test_phystokens.py +++ b/tests/test_phystokens.py @@ -102,6 +102,7 @@ ENCODING_DECLARATION_SOURCES = [ b"#!/usr/bin/python\n# vim: set fileencoding=cp850:\n", b"# This Python file uses this encoding: cp850\n", b"# This file uses a different encoding:\n# coding: cp850\n", + b"\n# coding=cp850\n\n", ] class SourceEncodingTest(CoverageTest): @@ -126,11 +127,6 @@ class SourceEncodingTest(CoverageTest): source = b'def parse(src, encoding=None):\n pass' self.assertEqual(source_encoding(source), DEF_ENCODING) - def test_detect_source_encoding_on_second_line(self): - # A coding declaration should be found despite a first blank line. - source = b"\n# coding=cp850\n\n" - self.assertEqual(source_encoding(source), 'cp850') - def test_dont_detect_source_encoding_on_third_line(self): # A coding declaration doesn't count on the third line. source = b"\n\n# coding=cp850\n\n" @@ -160,6 +156,18 @@ class NeuterEncodingDeclarationTest(CoverageTest): for 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() + self.assertEqual(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) + ) + self.assertEqual(lines_different, 1) + self.assertEqual( source_encoding(neutered), DEF_ENCODING, |