def test_format_method(self): formatted = asciistr("{}").format(self.input_data) self.assertEqual(formatted, self.input_data) self.assertIs(type(formatted), binary_type) formatted_int = asciistr("{:d}").format(42) # asciistr also avoids the byte constructor length init quirk self.assertEqual(formatted_int, asciistr(42)) self.assertIs(type(formatted_int), binary_type)
def test_mod_formatting(self): formatted = asciistr("%s") % self.input_data self.assertEqual(formatted, self.input_data) self.assertIs(type(formatted), self.output_type) formatted_int = asciistr("%d") % 42 # asciistr also avoids the byte constructor length init quirk self.assertEqual(formatted_int, asciistr(42)) self.assertIs(type(formatted_int), binary_type)
# Suggested name for Benno :) from asciicompat import asciistr # Developing the tests on Python 2 try: text_type = unicode except: text_type = str binary_type = bytes asciistr = str # Some test values TEXT = u"text" BINARY = b"binary" HYBRID = asciistr("ascii") class TestHybridAddition(unittest.TestCase): def test_text_addition(self): self.assertEqual(TEXT + HYBRID, u"textascii") self.assertIsInstance(TEXT + HYBRID, text_type) self.assertEqual(HYBRID + TEXT, u"asciitext") self.assertIsInstance(HYBRID + TEXT, text_type) def test_binary_addition(self): self.assertEqual(BINARY + HYBRID, b"binaryascii") self.assertIsInstance(BINARY + HYBRID, binary_type) # Next two are likely to be affected by # http://bugs.python.org/issue11477
def test_asciistr_from_str_plus_str(self): candidate = asciistr("hello") + "world" self.assertEqual(candidate, "helloworld") self.assertIs(type(candidate), str)
def test_bytes_plus_asciistr_from_bytes(self): candidate = b"hello" + asciistr(b"world") self.assertEqual(candidate, b"helloworld") self.assertIs(type(candidate), bytes)