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): if isinstance(val, bytes):
try: try:
return val.decode('ascii') return val.encode('ascii')
except UnicodeDecodeError: except UnicodeDecodeError:
return val.encode('string-escape') return val.encode('string-escape')
else: else:

View File

@ -1,11 +1,18 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import re import re
import sys
import _pytest._code import _pytest._code
import py import py
import pytest import pytest
from _pytest import python as funcargs from _pytest import python as funcargs
import hypothesis
from hypothesis import strategies
PY3 = sys.version_info >= (3, 0)
class TestMetafunc: class TestMetafunc:
def Metafunc(self, func): def Metafunc(self, func):
# the unit tests of this class check if things work correctly # 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[2].id == "x1-a"
assert metafunc._calls[3].id == "x1-b" 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): def test_unicode_idval(self):
"""This tests that Unicode strings outside the ASCII character set get """This tests that Unicode strings outside the ASCII character set get
escaped, using byte escapes if they're in that range or unicode 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): def test_escaped_parametrized_names_xml(testdir):
testdir.makepyfile(""" testdir.makepyfile("""
import pytest import pytest
@pytest.mark.parametrize('char', ["\\x00"]) @pytest.mark.parametrize('char', [u"\\x00"])
def test_func(char): def test_func(char):
assert char assert char
""") """)

View File

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