introduce Function._genid, also used for more correct equality implementation

--HG--
branch : trunk
This commit is contained in:
holger krekel 2009-05-25 10:46:04 +02:00
parent e57543780f
commit 3bdcd2f793
4 changed files with 20 additions and 19 deletions

View File

@ -33,9 +33,11 @@ class Metafunc:
self._calls = []
self._ids = py.builtin.set()
def addcall(self, funcargs=None, id=None, param=_notexists):
def addcall(self, funcargs=None, id=_notexists, param=_notexists):
assert funcargs is None or isinstance(funcargs, dict)
if id is None:
raise ValueError("id=None not allowed")
if id is _notexists:
id = len(self._calls)
id = str(id)
if id in self._ids:

View File

@ -313,6 +313,7 @@ class Function(FunctionMixin, py.test.collect.Item):
""" a Function Item is responsible for setting up
and executing a Python callable test object.
"""
_genid = None
def __init__(self, name, parent=None, config=None, args=(),
callspec=None, callobj=_dummy):
super(Function, self).__init__(name, parent, config=config)
@ -320,9 +321,13 @@ class Function(FunctionMixin, py.test.collect.Item):
if args:
assert not callspec, "yielded functions (deprecated) cannot have funcargs"
else:
self.funcargs = callspec and callspec.funcargs or {}
if callspec is not None:
self.funcargs = callspec.funcargs or {}
self._genid = callspec.id
if hasattr(callspec, "param"):
self._requestparam = callspec.param
else:
self.funcargs = {}
if callobj is not _dummy:
self._obj = callobj
@ -351,8 +356,8 @@ class Function(FunctionMixin, py.test.collect.Item):
self._args == other._args and
self.parent == other.parent and
self.obj == other.obj and
getattr(self, '_requestparam', None) ==
getattr(other, '_requestparam', None)
getattr(self, '_genid', None) ==
getattr(other, '_genid', None)
)
except AttributeError:
pass

View File

@ -298,6 +298,8 @@ class TestMetafunc:
def test_addcall_id(self):
def func(arg1): pass
metafunc = funcargs.Metafunc(func)
py.test.raises(ValueError, "metafunc.addcall(id=None)")
metafunc.addcall(id=1)
py.test.raises(ValueError, "metafunc.addcall(id=1)")
py.test.raises(ValueError, "metafunc.addcall(id='1')")
@ -338,6 +340,7 @@ class TestGenfuncFunctional:
metafunc.addcall(param=metafunc)
def pytest_funcarg__metafunc(request):
assert request._pyfuncitem._genid == "0"
return request.param
def test_function(metafunc):

View File

@ -242,25 +242,16 @@ class TestFunction:
assert f1 == f1_b
assert not f1 != f1_b
def test_function_equality_with_callspec(self, tmpdir):
config = py.test.config._reparse([tmpdir])
class callspec1:
param = 1
funcargs = {}
id = "hello"
class callspec2:
param = 2
funcargs = {}
f5 = py.test.collect.Function(name="name", config=config,
callspec=callspec1, callobj=isinstance)
f5b = py.test.collect.Function(name="name", config=config,
callspec=callspec2, callobj=isinstance)
assert f5 != f5b
assert not (f5 == f5b)
class callspec1:
param = 1
funcargs = {}
class callspec2:
param = 2
funcargs = {}
id = "world"
f5 = py.test.collect.Function(name="name", config=config,
callspec=callspec1, callobj=isinstance)
f5b = py.test.collect.Function(name="name", config=config,