From 842d14cd336e9b0e868499c40b5024e099bac9b4 Mon Sep 17 00:00:00 2001 From: Samuele Pedroni Date: Tue, 19 May 2009 18:13:33 +0200 Subject: [PATCH 1/2] move _getparent to Node and make it public --HG-- branch : trunk --- py/test/collect.py | 6 ++++++ py/test/funcargs.py | 4 ++-- py/test/pycollect.py | 10 ++-------- py/test/testing/test_collect.py | 18 ++++++++++++++++++ py/test/testing/test_pycollect.py | 2 +- 5 files changed, 29 insertions(+), 11 deletions(-) diff --git a/py/test/collect.py b/py/test/collect.py index 6a76e3b66..31eb30970 100644 --- a/py/test/collect.py +++ b/py/test/collect.py @@ -127,6 +127,12 @@ class Node(object): def listnames(self): 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): cur = self for name in namelist: diff --git a/py/test/funcargs.py b/py/test/funcargs.py index 84531ad34..2d022cfab 100644 --- a/py/test/funcargs.py +++ b/py/test/funcargs.py @@ -76,7 +76,7 @@ class FuncargRequest: self._pyfuncitem = pyfuncitem self.argname = argname 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.instance = getattr(self.function, 'im_self', None) self.config = pyfuncitem.config @@ -116,7 +116,7 @@ class FuncargRequest: if scope == "function": return self._pyfuncitem 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,)) def addfinalizer(self, finalizer, scope="function"): diff --git a/py/test/pycollect.py b/py/test/pycollect.py index 3a8774a09..420ef1dac 100644 --- a/py/test/pycollect.py +++ b/py/test/pycollect.py @@ -37,12 +37,6 @@ class PyobjMixin(object): def _getobj(self): 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): """ return python path relative to the containing module. """ chain = self.listchain() @@ -146,10 +140,10 @@ class PyCollectorMixin(PyobjMixin, py.test.collect.Collector): return self._genfunctions(name, obj) 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 # to work to get at the class - clscol = self._getparent(Class) + clscol = self.getparent(Class) cls = clscol and clscol.obj or None metafunc = funcargs.Metafunc(funcobj, config=self.config, cls=cls, module=module) gentesthook = self.config.hook.pytest_generate_tests.clone(extralookup=module) diff --git a/py/test/testing/test_collect.py b/py/test/testing/test_collect.py index 2e25662e6..0d1d3112f 100644 --- a/py/test/testing/test_collect.py +++ b/py/test/testing/test_collect.py @@ -37,6 +37,24 @@ class TestCollector: assert [1,2,3] != 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): a = tmpdir.ensure("a", dir=1) tmpdir.ensure("a", "__init__.py") diff --git a/py/test/testing/test_pycollect.py b/py/test/testing/test_pycollect.py index b662cabba..a4fe30538 100644 --- a/py/test/testing/test_pycollect.py +++ b/py/test/testing/test_pycollect.py @@ -217,7 +217,7 @@ class TestGenerator: class TestFunction: def test_getmodulecollector(self, testdir): 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 hasattr(modcol.obj, 'test_func') From 3b23b98cb57e216f54452ccbb17d2af5059b77b2 Mon Sep 17 00:00:00 2001 From: Samuele Pedroni Date: Tue, 19 May 2009 18:28:51 +0200 Subject: [PATCH 2/2] sorting should be an option collector->child interface --HG-- branch : trunk --- py/test/collect.py | 10 ---------- py/test/funcargs.py | 12 +++++++++++- py/test/pycollect.py | 8 +------- py/test/testing/test_collect.py | 6 +----- py/test/testing/test_pycollect.py | 18 +++++++----------- 5 files changed, 20 insertions(+), 34 deletions(-) diff --git a/py/test/collect.py b/py/test/collect.py index 31eb30970..7726c4ee0 100644 --- a/py/test/collect.py +++ b/py/test/collect.py @@ -79,13 +79,6 @@ class Node(object): def __hash__(self): return hash((self.name, self.parent)) - - def __cmp__(self, other): - if not isinstance(other, Node): - return -1 - s1 = self._getsortvalue() - s2 = other._getsortvalue() - return cmp(s1, s2) def setup(self): pass @@ -230,9 +223,6 @@ class Node(object): return True return False - def _getsortvalue(self): - return self.name - def _prunetraceback(self, traceback): return traceback diff --git a/py/test/funcargs.py b/py/test/funcargs.py index 2d022cfab..ef70b442e 100644 --- a/py/test/funcargs.py +++ b/py/test/funcargs.py @@ -64,7 +64,17 @@ class FunctionCollector(py.test.collect.Collector): function = self.parent.Function(name=name, parent=self, callspec=callspec, callobj=self.obj) l.append(function) - return l + return l + + def reportinfo(self): + try: + return self._fslineno, self.name + except AttributeError: + pass + fspath, lineno = py.code.getfslineno(self.obj) + self._fslineno = fspath, lineno + return fspath, lineno, self.name + class FuncargRequest: _argprefix = "pytest_funcarg__" diff --git a/py/test/pycollect.py b/py/test/pycollect.py index 420ef1dac..620d39a7d 100644 --- a/py/test/pycollect.py +++ b/py/test/pycollect.py @@ -93,7 +93,7 @@ class PyCollectorMixin(PyobjMixin, py.test.collect.Collector): return l name2items = self._buildname2items() colitems = name2items.values() - colitems.sort() + colitems.sort(key=lambda item: item.reportinfo()[:2]) return colitems def _buildname2items(self): @@ -205,9 +205,6 @@ class Class(PyCollectorMixin, py.test.collect.Collector): teardown_class = getattr(teardown_class, 'im_func', teardown_class) teardown_class(self.obj) - def _getsortvalue(self): - return self._getfslineno() - class Instance(PyCollectorMixin, py.test.collect.Collector): def _getobj(self): return self.parent.obj() @@ -230,9 +227,6 @@ class FunctionMixin(PyobjMixin): """ mixin for the code common to Function and Generator. """ - def _getsortvalue(self): - return self._getfslineno() - def setup(self): """ perform setup for this test function. """ if hasattr(self.obj, 'im_self'): diff --git a/py/test/testing/test_collect.py b/py/test/testing/test_collect.py index 0d1d3112f..544df1fa4 100644 --- a/py/test/testing/test_collect.py +++ b/py/test/testing/test_collect.py @@ -6,7 +6,7 @@ class TestCollector: assert not issubclass(Collector, Item) assert not issubclass(Item, Collector) - def test_check_equality_and_cmp_basic(self, testdir): + def test_check_equality(self, testdir): modcol = testdir.getmodulecol(""" def test_pass(): pass def test_fail(): assert 0 @@ -25,11 +25,7 @@ class TestCollector: assert isinstance(fn3, py.test.collect.Function) assert not (fn1 == fn3) assert fn1 != fn3 - assert cmp(fn1, fn3) == -1 - assert cmp(fn1, 10) == -1 - assert cmp(fn2, 10) == -1 - assert cmp(fn3, 10) == -1 for fn in fn1,fn2,fn3: assert fn != 3 assert fn != modcol diff --git a/py/test/testing/test_pycollect.py b/py/test/testing/test_pycollect.py index a4fe30538..0412ce3c9 100644 --- a/py/test/testing/test_pycollect.py +++ b/py/test/testing/test_pycollect.py @@ -269,7 +269,7 @@ class TestFunction: assert not (f5 == f5b) class TestSorting: - def test_check_equality_and_cmp_basic(self, testdir): + def test_check_equality(self, testdir): modcol = testdir.getmodulecol(""" def test_pass(): pass def test_fail(): assert 0 @@ -288,11 +288,7 @@ class TestSorting: assert isinstance(fn3, py.test.collect.Function) assert not (fn1 == fn3) assert fn1 != fn3 - assert cmp(fn1, fn3) == -1 - assert cmp(fn1, 10) == -1 - assert cmp(fn2, 10) == -1 - assert cmp(fn3, 10) == -1 for fn in fn1,fn2,fn3: assert fn != 3 assert fn != modcol @@ -308,18 +304,18 @@ class TestSorting: return g - def test_a(y): - pass - test_a = dec(test_a) - def test_b(y): pass test_b = dec(test_b) + + def test_a(y): + pass + test_a = dec(test_a) """) colitems = modcol.collect() assert len(colitems) == 2 - f1, f2 = colitems - assert cmp(f2, f1) > 0 + assert [item.name for item in colitems] == ['test_b', 'test_a'] + class TestConftestCustomization: def test_extra_python_files_and_functions(self, testdir):