def test_decode_bytes_iso_8859_1(self): """ Test SwordBible._decode_bytes when called with a Py2 str / Py3 bytes object encoded with iso-8859-1 """ # GIVEN: A Py2 str / Py3 bytes object which has been encoded from iso-8859-1 bible_instance = SwordBible('module_path') unicode_data = u'Finnish Pyhä Raamattu (1933/1938)' byte_data = unicode_data.encode(u'iso-8859-1') # WHEN: Calling `_decode_bytes` result = bible_instance._decode_bytes(byte_data) # THEN: The result should be equal to the original unicode data. `_encoding` should be set to `iso-8859-1` as # the statment block failed. self.assertEqual(unicode_data, result) self.assertEqual(bible_instance._encoding, u'cp1252')
def test_decode_bytes_utf8(self): """ Test SwordBible._decode_bytes when called with a Py2 str / Py3 bytes object encoded with utf-8 """ # GIVEN: A Py2 str / Py3 bytes object which has been encoded from utf-8 bible_instance = SwordBible('module_path') unicode_data = u'Finnish Pyhä Raamattu (1933/1938)' byte_data = unicode_data.encode('utf-8') # WHEN: Calling `_decode_bytes` result = bible_instance._decode_bytes(byte_data) # THEN: The result should be equal to the original unicode data. `_encoding` should still be None, as it was # successfully decoded in the try statment block self.assertEqual(unicode_data, result) self.assertEqual(bible_instance._encoding, None)
def test_decode_bytes_specified_encoding(self): """ Test SwordBible._decode_bytes when called with a Py2 str / Py3 bytes object encoded by the encoding specified in the initialisation of the SwordBible object """ # GIVEN: A Py2 str / Py3 bytes object which has been encoded from utf-16, and when the encoding has # been specified. bible_instance = SwordBible('module_path', encoding='utf-16') unicode_data = u'Finnish Pyhä Raamattu (1933/1938)' byte_data = unicode_data.encode('utf-16') # WHEN: Calling `_decode_bytes` result = bible_instance._decode_bytes(byte_data) # THEN: The result should be equal to the original unicode data. `_encoding` should be set to encoding specified self.assertEqual(unicode_data, result) self.assertEqual(bible_instance._encoding, 'utf-16')