move _getparent to Node and make it public
--HG-- branch : trunk
This commit is contained in:
parent
191d02aef2
commit
842d14cd33
|
@ -127,6 +127,12 @@ class Node(object):
|
||||||
def listnames(self):
|
def listnames(self):
|
||||||
return [x.name for x in self.listchain()]
|
return [x.name for x in self.listchain()]
|
||||||
|
|
||||||
|
def getparent(self, cls):
|
||||||
|
current = self
|
||||||
|
while current and not isinstance(current, cls):
|
||||||
|
current = current.parent
|
||||||
|
return current
|
||||||
|
|
||||||
def _getitembynames(self, namelist):
|
def _getitembynames(self, namelist):
|
||||||
cur = self
|
cur = self
|
||||||
for name in namelist:
|
for name in namelist:
|
||||||
|
|
|
@ -76,7 +76,7 @@ class FuncargRequest:
|
||||||
self._pyfuncitem = pyfuncitem
|
self._pyfuncitem = pyfuncitem
|
||||||
self.argname = argname
|
self.argname = argname
|
||||||
self.function = pyfuncitem.obj
|
self.function = pyfuncitem.obj
|
||||||
self.module = pyfuncitem._getparent(py.test.collect.Module).obj
|
self.module = pyfuncitem.getparent(py.test.collect.Module).obj
|
||||||
self.cls = getattr(self.function, 'im_class', None)
|
self.cls = getattr(self.function, 'im_class', None)
|
||||||
self.instance = getattr(self.function, 'im_self', None)
|
self.instance = getattr(self.function, 'im_self', None)
|
||||||
self.config = pyfuncitem.config
|
self.config = pyfuncitem.config
|
||||||
|
@ -116,7 +116,7 @@ class FuncargRequest:
|
||||||
if scope == "function":
|
if scope == "function":
|
||||||
return self._pyfuncitem
|
return self._pyfuncitem
|
||||||
elif scope == "module":
|
elif scope == "module":
|
||||||
return self._pyfuncitem._getparent(py.test.collect.Module)
|
return self._pyfuncitem.getparent(py.test.collect.Module)
|
||||||
raise ValueError("unknown finalization scope %r" %(scope,))
|
raise ValueError("unknown finalization scope %r" %(scope,))
|
||||||
|
|
||||||
def addfinalizer(self, finalizer, scope="function"):
|
def addfinalizer(self, finalizer, scope="function"):
|
||||||
|
|
|
@ -37,12 +37,6 @@ class PyobjMixin(object):
|
||||||
def _getobj(self):
|
def _getobj(self):
|
||||||
return getattr(self.parent.obj, self.name)
|
return getattr(self.parent.obj, self.name)
|
||||||
|
|
||||||
def _getparent(self, cls):
|
|
||||||
current = self
|
|
||||||
while current and not isinstance(current, cls):
|
|
||||||
current = current.parent
|
|
||||||
return current
|
|
||||||
|
|
||||||
def getmodpath(self, stopatmodule=True, includemodule=False):
|
def getmodpath(self, stopatmodule=True, includemodule=False):
|
||||||
""" return python path relative to the containing module. """
|
""" return python path relative to the containing module. """
|
||||||
chain = self.listchain()
|
chain = self.listchain()
|
||||||
|
@ -146,10 +140,10 @@ class PyCollectorMixin(PyobjMixin, py.test.collect.Collector):
|
||||||
return self._genfunctions(name, obj)
|
return self._genfunctions(name, obj)
|
||||||
|
|
||||||
def _genfunctions(self, name, funcobj):
|
def _genfunctions(self, name, funcobj):
|
||||||
module = self._getparent(Module).obj
|
module = self.getparent(Module).obj
|
||||||
# due to _buildname2items funcobj is the raw function, we need
|
# due to _buildname2items funcobj is the raw function, we need
|
||||||
# to work to get at the class
|
# to work to get at the class
|
||||||
clscol = self._getparent(Class)
|
clscol = self.getparent(Class)
|
||||||
cls = clscol and clscol.obj or None
|
cls = clscol and clscol.obj or None
|
||||||
metafunc = funcargs.Metafunc(funcobj, config=self.config, cls=cls, module=module)
|
metafunc = funcargs.Metafunc(funcobj, config=self.config, cls=cls, module=module)
|
||||||
gentesthook = self.config.hook.pytest_generate_tests.clone(extralookup=module)
|
gentesthook = self.config.hook.pytest_generate_tests.clone(extralookup=module)
|
||||||
|
|
|
@ -37,6 +37,24 @@ class TestCollector:
|
||||||
assert [1,2,3] != fn
|
assert [1,2,3] != fn
|
||||||
assert modcol != fn
|
assert modcol != fn
|
||||||
|
|
||||||
|
def test_getparent(self, testdir):
|
||||||
|
modcol = testdir.getmodulecol("""
|
||||||
|
class TestClass:
|
||||||
|
def test_foo():
|
||||||
|
pass
|
||||||
|
""")
|
||||||
|
cls = modcol.collect_by_name("TestClass")
|
||||||
|
fn = cls.collect_by_name("()").collect_by_name("test_foo")
|
||||||
|
|
||||||
|
parent = fn.getparent(py.test.collect.Module)
|
||||||
|
assert parent is modcol
|
||||||
|
|
||||||
|
parent = fn.getparent(py.test.collect.Function)
|
||||||
|
assert parent is fn
|
||||||
|
|
||||||
|
parent = fn.getparent(py.test.collect.Class)
|
||||||
|
assert parent is cls
|
||||||
|
|
||||||
def test_totrail_and_back(self, tmpdir):
|
def test_totrail_and_back(self, tmpdir):
|
||||||
a = tmpdir.ensure("a", dir=1)
|
a = tmpdir.ensure("a", dir=1)
|
||||||
tmpdir.ensure("a", "__init__.py")
|
tmpdir.ensure("a", "__init__.py")
|
||||||
|
|
|
@ -217,7 +217,7 @@ class TestGenerator:
|
||||||
class TestFunction:
|
class TestFunction:
|
||||||
def test_getmodulecollector(self, testdir):
|
def test_getmodulecollector(self, testdir):
|
||||||
item = testdir.getitem("def test_func(): pass")
|
item = testdir.getitem("def test_func(): pass")
|
||||||
modcol = item._getparent(py.test.collect.Module)
|
modcol = item.getparent(py.test.collect.Module)
|
||||||
assert isinstance(modcol, py.test.collect.Module)
|
assert isinstance(modcol, py.test.collect.Module)
|
||||||
assert hasattr(modcol.obj, 'test_func')
|
assert hasattr(modcol.obj, 'test_func')
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue