# Path: modoboa_amavis/utils.py
# def fix_utf8_encoding(value):
#     """Fix utf-8 strings that contain utf-8 escaped characters.
# 
#     msgs.from_addr and msgs.subject potentialy contain badly escaped utf-8
#     characters, this utility function fixes that and should be used anytime
#     these fields are accesses.
# 
#     Didn't even know the raw_unicode_escape encoding existed :)
#     https://docs.python.org/2/library/codecs.html?highlight=raw_unicode_escape#python-specific-encodings
#     https://docs.python.org/3/library/codecs.html?highlight=raw_unicode_escape#python-specific-encodings
#     """
#     assert isinstance(value, six.text_type), \
#         ("value should be of type %s" % six.text_type.__name__)
# 
#     if len(value) == 0:
#         # short circuit for empty strings
#         return ""
# 
#     bytes_value = value.encode("raw_unicode_escape")
#     try:
#         value = bytes_value.decode("utf-8")
#     except UnicodeDecodeError:
#         encoding = chardet.detect(bytes_value)
#         try:
#             value = bytes_value.decode(encoding["encoding"], "replace")
#         except (TypeError, UnicodeDecodeError):
#             # ??? use the original value, we've done our best to try and
#             # convert it to a clean utf-8 string.
#             pass
# 
#     return value


from django.test import SimpleTestCase
from modoboa_amavis.utils import fix_utf8_encoding
# -*- coding: utf-8 -*-

from __future__ import unicode_literals




class FixUTF8EncodingTests(SimpleTestCase):

    """Tests for modoboa_amavis.utils.fix_utf8_encoding()."""

    def test_4_byte_unicode(self):
        value = "\xf0\x9f\x99\x88"
        expected_output = "\U0001f648"  # == See No Evil Moneky
        output = fix_utf8_encoding(value)
        self.assertEqual(output, expected_output)

    def test_truncated_4_byte_unicode(self):
        value = "\xf0\x9f\x99"
        expected_output = "\xf0\x9f\x99"

# Path: modoboa_amavis/utils.py
# def fix_utf8_encoding(value):
#     """Fix utf-8 strings that contain utf-8 escaped characters.
# 
#     msgs.from_addr and msgs.subject potentialy contain badly escaped utf-8
#     characters, this utility function fixes that and should be used anytime
#     these fields are accesses.
# 
#     Didn't even know the raw_unicode_escape encoding existed :)
#     https://docs.python.org/2/library/codecs.html?highlight=raw_unicode_escape#python-specific-encodings
#     https://docs.python.org/3/library/codecs.html?highlight=raw_unicode_escape#python-specific-encodings
#     """
#     assert isinstance(value, six.text_type), \
#         ("value should be of type %s" % six.text_type.__name__)
# 
#     if len(value) == 0:
#         # short circuit for empty strings
#         return ""
# 
#     bytes_value = value.encode("raw_unicode_escape")
#     try:
#         value = bytes_value.decode("utf-8")
#     except UnicodeDecodeError:
#         encoding = chardet.detect(bytes_value)
#         try:
#             value = bytes_value.decode(encoding["encoding"], "replace")
#         except (TypeError, UnicodeDecodeError):
#             # ??? use the original value, we've done our best to try and
#             # convert it to a clean utf-8 string.
#             pass
# 
#     return value


# Path: modoboa_amavis/tests/test_utils.py
from django.test import SimpleTestCase
from modoboa_amavis.utils import fix_utf8_encoding

# -*- coding: utf-8 -*-

from __future__ import unicode_literals




class FixUTF8EncodingTests(SimpleTestCase):

    """Tests for modoboa_amavis.utils.fix_utf8_encoding()."""

    def test_4_byte_unicode(self):
        value = "\xf0\x9f\x99\x88"
        expected_output = "\U0001f648"  # == See No Evil Moneky
        output = fix_utf8_encoding(value)
        self.assertEqual(output, expected_output)

    def test_truncated_4_byte_unicode(self):
        value = "\xf0\x9f\x99"
        expected_output = "\xf0\x9f\x99"

        output = fix_utf8_encoding(value)