tags: TagLine constructor now supports multiline taglines and multiple spaces

This commit is contained in:
juk0de 2023-08-19 08:30:24 +02:00
parent 568646e782
commit ad7ef32c38
2 changed files with 12 additions and 1 deletions

View File

@ -102,10 +102,12 @@ class TagLine(str):
def __new__(cls: Type[TagLineInst], string: str) -> TagLineInst: def __new__(cls: Type[TagLineInst], string: str) -> TagLineInst:
""" """
Make sure the tagline string starts with the prefix. Make sure the tagline string starts with the prefix. Also replace newlines
and multiple spaces with ' ', in order to support multiline TagLines.
""" """
if not string.startswith(cls.prefix): if not string.startswith(cls.prefix):
raise TagError(f"TagLine '{string}' is missing prefix '{cls.prefix}'") raise TagError(f"TagLine '{string}' is missing prefix '{cls.prefix}'")
string = ' '.join(string.split())
instance = super().__new__(cls, string) instance = super().__new__(cls, string)
return instance return instance

View File

@ -256,6 +256,10 @@ class TestTagLine(CmmTestCase):
tagline = TagLine('TAGS: tag1 tag2') tagline = TagLine('TAGS: tag1 tag2')
self.assertEqual(tagline, 'TAGS: tag1 tag2') self.assertEqual(tagline, 'TAGS: tag1 tag2')
def test_valid_tagline_with_newline(self) -> None:
tagline = TagLine('TAGS: tag1\n tag2')
self.assertEqual(tagline, 'TAGS: tag1 tag2')
def test_invalid_tagline(self) -> None: def test_invalid_tagline(self) -> None:
with self.assertRaises(TagError): with self.assertRaises(TagError):
TagLine('tag1 tag2') TagLine('tag1 tag2')
@ -273,6 +277,11 @@ class TestTagLine(CmmTestCase):
tags = tagline.tags() tags = tagline.tags()
self.assertEqual(tags, {Tag('tag1'), Tag('tag2')}) self.assertEqual(tags, {Tag('tag1'), Tag('tag2')})
def test_tags_with_newline(self) -> None:
tagline = TagLine('TAGS: tag1\n tag2')
tags = tagline.tags()
self.assertEqual(tags, {Tag('tag1'), Tag('tag2')})
def test_merge(self) -> None: def test_merge(self) -> None:
tagline1 = TagLine('TAGS: tag1 tag2') tagline1 = TagLine('TAGS: tag1 tag2')
tagline2 = TagLine('TAGS: tag2 tag3') tagline2 = TagLine('TAGS: tag2 tag3')