Add Hypothesis test for _idval and fix bug it found

This commit is contained in:
Ceridwen 2016-04-01 22:45:44 -04:00
parent b631fc0bc1
commit 491b30c5d9
4 changed files with 20 additions and 2 deletions

View File

@ -1122,7 +1122,7 @@ else:
"""
if isinstance(val, bytes):
try:
return val.decode('ascii')
return val.encode('ascii')
except UnicodeDecodeError:
return val.encode('string-escape')
else:

View File

@ -1,11 +1,18 @@
# -*- coding: utf-8 -*-
import re
import sys
import _pytest._code
import py
import pytest
from _pytest import python as funcargs
import hypothesis
from hypothesis import strategies
PY3 = sys.version_info >= (3, 0)
class TestMetafunc:
def Metafunc(self, func):
# the unit tests of this class check if things work correctly
@ -121,6 +128,16 @@ class TestMetafunc:
assert metafunc._calls[2].id == "x1-a"
assert metafunc._calls[3].id == "x1-b"
@hypothesis.given(strategies.text() | strategies.binary())
def test_idval_hypothesis(self, value):
from _pytest.python import _idval
escaped = _idval(value, 'a', 6, None)
assert isinstance(escaped, str)
if PY3:
escaped.encode('ascii')
else:
escaped.decode('ascii')
def test_unicode_idval(self):
"""This tests that Unicode strings outside the ASCII character set get
escaped, using byte escapes if they're in that range or unicode

View File

@ -610,7 +610,7 @@ def test_logxml_makedir(testdir):
def test_escaped_parametrized_names_xml(testdir):
testdir.makepyfile("""
import pytest
@pytest.mark.parametrize('char', ["\\x00"])
@pytest.mark.parametrize('char', [u"\\x00"])
def test_func(char):
assert char
""")

View File

@ -10,6 +10,7 @@ envlist=
commands= py.test --lsof -rfsxX {posargs:testing}
passenv = USER USERNAME
deps=
hypothesis
nose
mock
requests