diff --git a/picard/coverart/image.py b/picard/coverart/image.py index fcad8f3c42..8686992c61 100644 --- a/picard/coverart/image.py +++ b/picard/coverart/image.py @@ -214,29 +214,32 @@ def imageinfo_as_string(self): self.datalength, self.tempfile_filename) - def __repr__(self): - p = [] + def _repr(self): if self.url is not None: - p.append("url=%r" % self.url.toString()) + yield "url=%r" % self.url.toString() if self.types: - p.append("types=%r" % self.types) - p.append('support_types=%r' % self.support_types) - p.append('support_multi_types=%r' % self.support_types) + yield "types=%r" % self.types + yield 'support_types=%r' % self.support_types + yield 'support_multi_types=%r' % self.support_multi_types if self.is_front is not None: - p.append("is_front=%r" % self.is_front) + yield "is_front=%r" % self.is_front if self.comment: - p.append("comment=%r" % self.comment) - return "%s(%s)" % (self.__class__.__name__, ", ".join(p)) + yield "comment=%r" % self.comment - def __str__(self): - p = ['Image'] + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, ", ".join(self._repr())) + + def _str(self): + yield 'Image' if self.url is not None: - p.append("from %s" % self.url.toString()) + yield "from %s" % self.url.toString() if self.types: - p.append("of type %s" % ','.join(self.types)) + yield "of type %s" % ','.join(self.types) if self.comment: - p.append("and comment '%s'" % self.comment) - return ' '.join(p) + yield "and comment '%s'" % self.comment + + def __str__(self): + return ' '.join(self._str()) def __eq__(self, other): if self and other: diff --git a/test/test_coverart_image.py b/test/test_coverart_image.py index 952706f2bc..50573d87ee 100644 --- a/test/test_coverart_image.py +++ b/test/test_coverart_image.py @@ -55,6 +55,23 @@ def create_image(extra_data, types=None, support_types=False, class CoverArtImageTest(PicardTestCase): + def test_repr_str_1(self): + image = CoverArtImage( + url='url', types=["booklet", "front"], + comment='comment', support_types=True, + support_multi_types=True + ) + expected = "CoverArtImage(url='url', types=['booklet', 'front'], support_types=True, support_multi_types=True, comment='comment')" + self.assertEqual(expected, repr(image)) + expected = "Image from url of type booklet,front and comment 'comment'" + self.assertEqual(expected, str(image)) + + def test_repr_str_2(self): + image = CoverArtImage() + expected = "CoverArtImage(support_types=False, support_multi_types=False)" + self.assertEqual(expected, repr(image)) + expected = "Image" + self.assertEqual(expected, str(image)) def test_is_front_image_no_types(self): image = create_image(b'a')