Issue 306: Used a set for the extra_keywords, and used listchain for parent iteration.

This commit is contained in:
Wouter van Ackooy 2013-05-27 18:14:35 +02:00
parent 60906f7a46
commit 212f4b4d64
3 changed files with 14 additions and 15 deletions

View File

@ -217,7 +217,7 @@ class Node(object):
self.keywords = NodeKeywords(self) self.keywords = NodeKeywords(self)
#: allow adding of extra keywords to use for matching #: allow adding of extra keywords to use for matching
self.extra_keyword_matches = [] self.extra_keyword_matches = set()
#self.extrainit() #self.extrainit()
@ -311,12 +311,11 @@ class Node(object):
return chain return chain
def listextrakeywords(self): def listextrakeywords(self):
""" Return a list of all extra keywords in self and any parents.""" """ Return a set of all extra keywords in self and any parents."""
extra_keywords = [] extra_keywords = set()
item = self item = self
while item is not None: for item in self.listchain():
extra_keywords.extend(item.extra_keyword_matches) extra_keywords.update(item.extra_keyword_matches)
item = item.parent
return extra_keywords return extra_keywords
def listnames(self): def listnames(self):

View File

@ -18,7 +18,7 @@ def pytest_addoption(parser):
"other' matches all test functions and classes whose name " "other' matches all test functions and classes whose name "
"contains 'test_method' or 'test_other'. " "contains 'test_method' or 'test_other'. "
"Additionally keywords are matched to classes and functions " "Additionally keywords are matched to classes and functions "
"containing extra names in their 'extra_keyword_matches' list, " "containing extra names in their 'extra_keyword_matches' set, "
"as well as functions which have names assigned directly to them." "as well as functions which have names assigned directly to them."
) )
@ -87,10 +87,10 @@ class MarkMapping:
:class:`MarkDecorator` items. :class:`MarkDecorator` items.
""" """
def __init__(self, keywords): def __init__(self, keywords):
mymarks = [] mymarks = set()
for key, value in keywords.items(): for key, value in keywords.items():
if isinstance(value, MarkInfo) or isinstance(value, MarkDecorator): if isinstance(value, MarkInfo) or isinstance(value, MarkDecorator):
mymarks.append(key) mymarks.add(key)
self._mymarks = mymarks self._mymarks = mymarks
def __getitem__(self, markname): def __getitem__(self, markname):
@ -122,24 +122,24 @@ def matchkeyword(colitem, keywordexpr):
Will match on the name of colitem, including the names of its parents. Will match on the name of colitem, including the names of its parents.
Only matches names of items which are either a :class:`Class` or a Only matches names of items which are either a :class:`Class` or a
:class:`Function`. :class:`Function`.
Additionally, matches on names in the 'extra_keyword_matches' list of Additionally, matches on names in the 'extra_keyword_matches' set of
any item, as well as names directly assigned to test functions. any item, as well as names directly assigned to test functions.
""" """
keywordexpr = keywordexpr.replace("-", "not ") keywordexpr = keywordexpr.replace("-", "not ")
mapped_names = [] mapped_names = set()
# Add the names of the current item and any parent items # Add the names of the current item and any parent items
for item in colitem.listchain(): for item in colitem.listchain():
if not isinstance(item, pytest.Instance): if not isinstance(item, pytest.Instance):
mapped_names.append(item.name) mapped_names.add(item.name)
# Add the names added as extra keywords to current or parent items # Add the names added as extra keywords to current or parent items
for name in colitem.listextrakeywords(): for name in colitem.listextrakeywords():
mapped_names.append(name) mapped_names.add(name)
# Add the names attached to the current function through direct assignment # Add the names attached to the current function through direct assignment
for name in colitem.function.func_dict: for name in colitem.function.func_dict:
mapped_names.append(name) mapped_names.add(name)
return eval(keywordexpr, {}, KeywordMapping(mapped_names)) return eval(keywordexpr, {}, KeywordMapping(mapped_names))

View File

@ -418,7 +418,7 @@ class TestKeywordSelection:
def pytest_pycollect_makeitem(__multicall__, name): def pytest_pycollect_makeitem(__multicall__, name):
if name == "TestClass": if name == "TestClass":
item = __multicall__.execute() item = __multicall__.execute()
item.extra_keyword_matches.append("xxx") item.extra_keyword_matches.add("xxx")
return item return item
""") """)
reprec = testdir.inline_run(p.dirpath(), '-s', '-k', keyword) reprec = testdir.inline_run(p.dirpath(), '-s', '-k', keyword)