fixed Message.filter_tags

This commit is contained in:
juk0de 2023-08-30 08:20:25 +02:00 committed by Oleksandr Kozachuk
parent fde0ae4652
commit 74c39070d6
2 changed files with 23 additions and 7 deletions

View File

@ -436,13 +436,14 @@ class Message():
Filter tags based on their prefix (i. e. the tag starts with a given string) Filter tags based on their prefix (i. e. the tag starts with a given string)
or some contained string. or some contained string.
""" """
res_tags = self.tags if not self.tags:
if res_tags: return set()
if prefix and len(prefix) > 0: res_tags = self.tags.copy()
res_tags -= {tag for tag in res_tags if not tag.startswith(prefix)} if prefix and len(prefix) > 0:
if contain and len(contain) > 0: res_tags -= {tag for tag in res_tags if not tag.startswith(prefix)}
res_tags -= {tag for tag in res_tags if contain not in tag} if contain and len(contain) > 0:
return res_tags or set() res_tags -= {tag for tag in res_tags if contain not in tag}
return res_tags
def tags_str(self, prefix: Optional[str] = None, contain: Optional[str] = None) -> str: def tags_str(self, prefix: Optional[str] = None, contain: Optional[str] = None) -> str:
""" """

View File

@ -746,3 +746,18 @@ class MessageTagsStrTestCase(CmmTestCase):
def test_tags_str(self) -> None: def test_tags_str(self) -> None:
self.assertEqual(self.message.tags_str(), f'{TagLine.prefix} tag1') self.assertEqual(self.message.tags_str(), f'{TagLine.prefix} tag1')
class MessageFilterTagsTestCase(CmmTestCase):
def setUp(self) -> None:
self.message = Message(Question('This is a question.'),
tags={Tag('atag1'), Tag('btag2')},
file_path=pathlib.Path('/tmp/foo/bla'))
def test_filter_tags(self) -> None:
tags_all = self.message.filter_tags()
self.assertSetEqual(tags_all, {Tag('atag1'), Tag('btag2')})
tags_pref = self.message.filter_tags(prefix='a')
self.assertSetEqual(tags_pref, {Tag('atag1')})
tags_cont = self.message.filter_tags(contain='2')
self.assertSetEqual(tags_cont, {Tag('btag2')})