Skip to content

Commit

Permalink
AspectList: Add get() method
Browse files Browse the repository at this point in the history
Usable for bears API

Closes coala#4384
  • Loading branch information
adhikasp committed Jun 26, 2017
1 parent 79b18a9 commit 705d256
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
16 changes: 16 additions & 0 deletions coalib/bearlib/aspects/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,19 @@ def __contains__(self, aspect):
if issubaspect(aspect, item):
return True
return False

def get(self, aspect):
"""
Return first item that match or contain an aspect. See
:meth:`coalib.bearlib.aspects.aspectbase.get` for further example.
:param aspect: An aspectclass OR name of an aspect.
:return: An aspectclass OR aspectclass instance, depend on
AspectList content. Return None if no match found.
"""
if not isaspect(aspect):
aspect = coalib.bearlib.aspects[aspect]
try:
return next(filter(None, (item.get(aspect) for item in self)))
except StopIteration:
return None
11 changes: 11 additions & 0 deletions tests/bearlib/aspects/CollectionsTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,14 @@ def test__contains__(self):
assert not isaspect(str)
exc.match("<class 'str'> is not an "
'aspectclass or an instance of an aspectclass')

def test_get(self):
list_of_aspect = AspectList(
[Metadata.CommitMessage.Shortlog, Metadata.CommitMessage.Body])
self.assertIs(list_of_aspect.get(Metadata.CommitMessage.Shortlog),
Metadata.CommitMessage.Shortlog)
self.assertIs(list_of_aspect.get(Metadata.CommitMessage.Body.Length),
Metadata.CommitMessage.Body.Length)
self.assertIs(list_of_aspect.get('Body.Length'),
Metadata.CommitMessage.Body.Length)
self.assertIsNone(list_of_aspect.get(Metadata))

0 comments on commit 705d256

Please sign in to comment.