remove complexity from match_keywords

This commit is contained in:
Ronny Pfannschmidt 2018-02-22 14:30:32 +01:00
parent cef0423b27
commit be2e3a973e
1 changed files with 22 additions and 18 deletions

View File

@ -37,6 +37,27 @@ class KeywordMapping(object):
def __init__(self, names): def __init__(self, names):
self._names = names self._names = names
@classmethod
def from_item(cls, item):
mapped_names = set()
# Add the names of the current item and any parent items
import pytest
for item in item.listchain():
if not isinstance(item, pytest.Instance):
mapped_names.add(item.name)
# Add the names added as extra keywords to current or parent items
for name in item.listextrakeywords():
mapped_names.add(name)
# Add the names attached to the current function through direct assignment
if hasattr(item, 'function'):
for name in item.function.__dict__:
mapped_names.add(name)
return cls(mapped_names)
def __getitem__(self, subname): def __getitem__(self, subname):
for name in self._names: for name in self._names:
if subname in name: if subname in name:
@ -61,24 +82,7 @@ def matchkeyword(colitem, keywordexpr):
Additionally, matches on names in the 'extra_keyword_matches' set 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.
""" """
mapped_names = set() mapping = KeywordMapping.from_item(colitem)
# Add the names of the current item and any parent items
import pytest
for item in colitem.listchain():
if not isinstance(item, pytest.Instance):
mapped_names.add(item.name)
# Add the names added as extra keywords to current or parent items
for name in colitem.listextrakeywords():
mapped_names.add(name)
# Add the names attached to the current function through direct assignment
if hasattr(colitem, 'function'):
for name in colitem.function.__dict__:
mapped_names.add(name)
mapping = KeywordMapping(mapped_names)
if " " not in keywordexpr: if " " not in keywordexpr:
# special case to allow for simple "-k pass" and "-k 1.3" # special case to allow for simple "-k pass" and "-k 1.3"
return mapping[keywordexpr] return mapping[keywordexpr]