Skip to content

Commit e12e04c

Browse files
committed
pythongh-94606: Condense unit tests for issue into one parameterized test
1 parent f9ff8d2 commit e12e04c

File tree

1 file changed

+29
-36
lines changed

1 file changed

+29
-36
lines changed

Lib/test/test_email/test_message.py

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,35 @@ def test_iter_attachments_mutation(self):
746746
self.assertEqual(len(list(m.iter_attachments())), 2)
747747
self.assertEqual(m.get_payload(), orig)
748748

749+
get_payload_surrogate_params = {
750+
751+
'good_surrogateescape': (
752+
"String that can be encod\udcc3\udcabd with surrogateescape",
753+
b'String that can be encod\xc3\xabd with surrogateescape'
754+
),
755+
756+
'string_with_utf8': (
757+
"String with utf-8 charactër",
758+
b'String with utf-8 charact\xebr'
759+
),
760+
761+
'surrogate_and_utf8': (
762+
"String that cannot be ëncod\udcc3\udcabd with surrogateescape",
763+
b'String that cannot be \xebncod\\udcc3\\udcabd with surrogateescape'
764+
),
765+
766+
'out_of_range_surrogate': (
767+
"String with \udfff cannot be encoded with surrogateescape",
768+
b'String with \\udfff cannot be encoded with surrogateescape'
769+
),
770+
}
771+
772+
def get_payload_surrogate_as_gh_94606(self, msg, expected):
773+
"""test for GH issue 94606"""
774+
m = self._str_msg(msg)
775+
payload = m.get_payload(decode=True)
776+
self.assertEqual(expected, payload)
777+
749778

750779
class TestEmailMessage(TestEmailMessageBase, TestEmailBase):
751780
message = EmailMessage
@@ -954,42 +983,6 @@ def test_get_body_malformed(self):
954983
# AttributeError: 'str' object has no attribute 'is_attachment'
955984
m.get_body()
956985

957-
def test_get_payload_unicode_surrogate1(self):
958-
"""test that fix for GH issue 94606 does not break this"""
959-
msg = "String that could have been decod\udcc3\udcabd with surrogateescape"
960-
expected = b'String that could have been decod\xc3\xabd with surrogateescape'
961-
m = self._str_msg(msg)
962-
payload = m.get_payload(decode=True)
963-
self.assertEqual(expected, payload)
964-
965-
def test_get_payload_unicode_surrogate2(self):
966-
"""test that fix for GH issue 94606 does not break this"""
967-
msg = "Unicode string with a utf-8 charactër"
968-
expected = b'Unicode string with a utf-8 charact\xebr'
969-
m = self._str_msg(msg)
970-
payload = m.get_payload(decode=True)
971-
self.assertEqual(expected, payload)
972-
973-
def test_get_payload_unicode_surrogate3(self):
974-
"""test for GH issue 94606"""
975-
msg = "String that could not have been dëcod\udcc3\udcabd with surrogateescape"
976-
expected = b'String that could not have been d\xebcod\\udcc3\udcabd with surrogateescape'
977-
m = self._str_msg(msg)
978-
# In GH issue 94606, this would raise
979-
# UnicodeEncodeError: 'ascii' codec can't encode character '\xeb' in position 33: ordinal not in range(128)
980-
payload = m.get_payload(decode=True)
981-
self.assertEqual(expected, payload)
982-
983-
def test_get_payload_unicode_surrogate4(self):
984-
"""test for GH issue 94606"""
985-
msg = "Different reason \udfff could not have been decoded with surrogateescape"
986-
expected = b'Different reason \\udfff could not have been decoded with surrogateescape'
987-
m = self._str_msg(msg)
988-
# In GH issue 94606, this would raise
989-
# UnicodeEncodeError: 'ascii' codec can't encode character '\udfff' in position 17: ordinal not in range(128)
990-
payload = m.get_payload(decode=True)
991-
self.assertEqual(expected, payload)
992-
993986

994987
class TestMIMEPart(TestEmailMessageBase, TestEmailBase):
995988
# Doing the full test run here may seem a bit redundant, since the two

0 commit comments

Comments
 (0)