def test_to_ascii_encodes_to_bytes(self): # Yes, this is how you really spell "smorgasbord." Look it up. unicode_string = "Sm\xf6rg\xe5sbord" expected_byte_string = b"Sm?rg?sbord" converted_string = ExternalProcessError._to_ascii(unicode_string) self.assertIsInstance(converted_string, bytes) self.assertEqual(expected_byte_string, converted_string)
def test_to_ascii_defers_to_bytes(self): # Byte strings and non-unicode strings are handed to bytes() to # undergo Python's normal coercion strategy. (For byte strings # this is actually a no-op, but it's cheaper to do this than # special-case byte strings.) self.assertEqual( str(self).encode("ascii"), ExternalProcessError._to_ascii(self))
def test_to_ascii_removes_non_printable_chars(self): # After conversion to a byte string, all non-printable and # non-ASCII characters are replaced with question marks. byte_string = b"*How* many roads\x01\x02\xb2\xfe" expected_byte_string = b"*How* many roads????" converted_string = ExternalProcessError._to_ascii(byte_string) self.assertIsInstance(converted_string, bytes) self.assertEqual(expected_byte_string, converted_string)