Fix coverage

Also make sure a test that doesn't set ALLOW_UNICODE fails
on Python 2 and passes Python 3.
This commit is contained in:
Bruno Oliveira 2015-08-12 22:46:13 -03:00
parent 93aee0f814
commit d749021a31
2 changed files with 19 additions and 13 deletions

View File

@ -202,20 +202,25 @@ def _get_unicode_checker():
_literal_re = re.compile(r"(\W|^)[uU]([rR]?[\'\"])", re.UNICODE)
def _remove_u_prefixes(self, txt):
return re.sub(self._literal_re, r'\1\2', txt)
def check_output(self, want, got, optionflags):
res = doctest.OutputChecker.check_output(self, want, got, optionflags)
res = doctest.OutputChecker.check_output(self, want, got,
optionflags)
if res:
return True
if not (optionflags & _get_allow_unicode_flag()):
return False
cleaned_want = self._remove_u_prefixes(want)
cleaned_got = self._remove_u_prefixes(got)
res = doctest.OutputChecker.check_output(self, cleaned_want, cleaned_got, optionflags)
else: # pragma: no cover
# the code below will end up executed only in Python 2 in
# our tests, and our coverage check runs in Python 3 only
def remove_u_prefixes(txt):
return re.sub(self._literal_re, r'\1\2', txt)
want = remove_u_prefixes(want)
got = remove_u_prefixes(got)
res = doctest.OutputChecker.check_output(self, want, got,
optionflags)
return res
_get_unicode_checker.UnicodeOutputChecker = UnicodeOutputChecker

View File

@ -432,16 +432,17 @@ class TestDoctests:
reprec = testdir.inline_run("--doctest-modules")
reprec.assertoutcome(passed=2)
@pytest.mark.skipif(sys.version_info[0] >= 3, reason='Python 2 only')
def test_unicode_string_fails(self, testdir):
def test_unicode_string(self, testdir):
"""Test that doctests which output unicode fail in Python 2 when
the ALLOW_UNICODE option is not used.
the ALLOW_UNICODE option is not used. The same test should pass
in Python 3.
"""
testdir.maketxtfile(test_doc="""
>>> b'12'.decode('ascii') {comment}
>>> b'12'.decode('ascii')
'12'
""")
reprec = testdir.inline_run()
reprec.assertoutcome(failed=1)
passed = int(sys.version_info[0] >= 3)
reprec.assertoutcome(passed=passed, failed=int(not passed))