summaryrefslogtreecommitdiff
path: root/tests/test_phystokens.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2016-01-02 16:28:02 -0500
committerNed Batchelder <ned@nedbatchelder.com>2016-01-02 16:28:02 -0500
commita0aa685214e9cccc361cddafea937346bd6dfdad (patch)
treec089983f78e4d1a13282b28407450631c89ed64d /tests/test_phystokens.py
parent3440e214df5ddd0f507ecd76c2350eb8d9dd6a75 (diff)
parent8b110d3a3f7fbddfcbdaa1b090776e0f388a312e (diff)
downloadpython-coveragepy-git-a0aa685214e9cccc361cddafea937346bd6dfdad.tar.gz
Merge in the default branch
--HG-- branch : ast-branch
Diffstat (limited to 'tests/test_phystokens.py')
-rw-r--r--tests/test_phystokens.py36
1 files changed, 26 insertions, 10 deletions
diff --git a/tests/test_phystokens.py b/tests/test_phystokens.py
index e28fb176..380f36ff 100644
--- a/tests/test_phystokens.py
+++ b/tests/test_phystokens.py
@@ -97,12 +97,13 @@ else:
ENCODING_DECLARATION_SOURCES = [
# Various forms from http://www.python.org/dev/peps/pep-0263/
- b"# coding=cp850\n\n",
- b"#!/usr/bin/python\n# -*- coding: cp850 -*-\n",
- 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",
+ (1, b"# coding=cp850\n\n"),
+ (1, b"#!/usr/bin/python\n# -*- coding: cp850 -*-\n"),
+ (1, b"#!/usr/bin/python\n# vim: set fileencoding=cp850:\n"),
+ (1, b"# This Python file uses this encoding: cp850\n"),
+ (1, b"# This file uses a different encoding:\n# coding: cp850\n"),
+ (1, b"\n# coding=cp850\n\n"),
+ (2, b"# -*- coding:cp850 -*-\n# vim: fileencoding=cp850\n"),
]
class SourceEncodingTest(CoverageTest):
@@ -111,7 +112,7 @@ class SourceEncodingTest(CoverageTest):
run_in_temp_dir = False
def test_detect_source_encoding(self):
- for source in ENCODING_DECLARATION_SOURCES:
+ for _, source in ENCODING_DECLARATION_SOURCES:
self.assertEqual(
source_encoding(source),
'cp850',
@@ -153,7 +154,7 @@ class NeuterEncodingDeclarationTest(CoverageTest):
run_in_temp_dir = False
def test_neuter_encoding_declaration(self):
- for source in ENCODING_DECLARATION_SOURCES:
+ for lines_diff_expected, source in ENCODING_DECLARATION_SOURCES:
neutered = neuter_encoding_declaration(source.decode("ascii"))
neutered = neutered.encode("ascii")
@@ -166,8 +167,10 @@ class NeuterEncodingDeclarationTest(CoverageTest):
lines_different = sum(
int(nline != sline) for nline, sline in zip(neutered_lines, source_lines)
)
- self.assertEqual(lines_different, 1)
+ self.assertEqual(lines_diff_expected, lines_different)
+ # The neutered source will be detected as having no encoding
+ # declaration.
self.assertEqual(
source_encoding(neutered),
DEF_ENCODING,
@@ -180,6 +183,19 @@ class CompileUnicodeTest(CoverageTest):
run_in_temp_dir = False
+ def assert_compile_unicode(self, source):
+ """Assert that `source` will compile properly with `compile_unicode`."""
+ source += u"a = 42\n"
+ # This doesn't raise an exception:
+ code = compile_unicode(source, "<string>", "exec")
+ globs = {}
+ exec(code, globs)
+ self.assertEqual(globs['a'], 42)
+
def test_cp1252(self):
uni = u"""# coding: cp1252\n# \u201C curly \u201D\n"""
- compile_unicode(uni, "<string>", "exec")
+ self.assert_compile_unicode(uni)
+
+ def test_double_coding_declaration(self):
+ uni = u"""# -*- coding:utf-8 -*-\n# vim: fileencoding=utf-8\n"""
+ self.assert_compile_unicode(uni)