# Path: xclib/utf8.py
# def utf8(u, opts='strict'):
#     return u.encode('utf-8', opts)
# 
# def unutf8(u, opts='strict'):
#     if opts == 'illegal':
#         try:
#             return u.decode('utf-8', 'strict')
#         except UnicodeError:
#             dec = u.decode('utf-8', 'ignore')
#             logging.error('Illegal UTF-8 sequence: %r' % dec)
#             sys.stderr.write('Illegal UTF-8 sequence: %r\n' % dec)
#             traceback.print_exc()
#             return 'illegal-utf8-sequence-' + dec
#     else:
#         return u.decode('utf-8', opts)
# 
# def utf8l(l):
#     '''Encode a copy of the list, converted to UTF-8'''
#     return list(map(utf8, l))
# 
# Path: xclib/check.py
# def assertEqual(a, b):
#     if type(a) == type(b):
#         assertSimilar(a, b)
#     else:
#         raise AssertionError('Assertion failed: Type mismatch %r (%s) != %r (%s)' % (a, type(a), b, type(b)))
# 
# Path: xclib/tests/iostub.py
# class iostub:
#     def stub_stdin(testcase_inst, inputs, ioclass=io.StringIO):
#         stdin = sys.stdin
# 
#         def cleanup():
#             sys.stdin = stdin
# 
#         testcase_inst.addCleanup(cleanup)
#         sys.stdin = ioclass(inputs)
#         if ioclass == io.BytesIO:
#             # Fake 'buffer' variable
#             sys.stdin.buffer = sys.stdin
# 
#     def stub_stdout(testcase_inst, ioclass=io.StringIO):
#         stdout = sys.stdout
# 
#         def cleanup():
#             sys.stdout = stdout
# 
#         testcase_inst.addCleanup(cleanup)
#         sys.stdout = ioclass()
#         if ioclass == io.BytesIO:
#             # Fake 'buffer' variable
#             sys.stdout.buffer = sys.stdout
# 
#     def stub_stdouts(testcase_inst, ioclass=io.StringIO):
#         stderr = sys.stderr
#         stdout = sys.stdout
# 
#         def cleanup():
#             sys.stderr = stderr
#             sys.stdout = stdout
# 
#         testcase_inst.addCleanup(cleanup)
#         sys.stderr = ioclass()
#         sys.stdout = ioclass()
#         if ioclass == io.BytesIO:
#             # Fake 'buffer' variable
#             sys.stdout.buffer = sys.stdout
#             sys.stderr.buffer = sys.stderr


import sys
import io
from xclib.utf8 import utf8, unutf8, utf8l
from xclib.check import assertEqual
from xclib.tests.iostub import iostub
# Tests wheter our UTF-8 convenience functions work as they should

def test_utf8_ascii():
    assertEqual(b'hallo', utf8(u'hallo'))

def test_utf8_valid():
    assertEqual(b'Hall\xc3\xb6chen', utf8(u'Hallöchen'))

def test_unutf8_ascii():
    assertEqual(unutf8(b'Hallo'), u'Hallo')

def test_unutf8_valid():
    assertEqual(unutf8(b'Hall\xc3\xb6chen'), u'Hallöchen')

def test_unutf8_invalid_ignore():
    assertEqual(unutf8(b'Hall\xffchen', 'ignore'), u'Hallchen')

def test_unutf8_invalid_ignore2():
    assertEqual(unutf8(b'Hall\x80\x80chen', 'ignore'), u'Hallchen')

def test_unutf8_invalid_ignore3():

# Path: xclib/utf8.py
# def utf8(u, opts='strict'):
#     return u.encode('utf-8', opts)
# 
# def unutf8(u, opts='strict'):
#     if opts == 'illegal':
#         try:
#             return u.decode('utf-8', 'strict')
#         except UnicodeError:
#             dec = u.decode('utf-8', 'ignore')
#             logging.error('Illegal UTF-8 sequence: %r' % dec)
#             sys.stderr.write('Illegal UTF-8 sequence: %r\n' % dec)
#             traceback.print_exc()
#             return 'illegal-utf8-sequence-' + dec
#     else:
#         return u.decode('utf-8', opts)
# 
# def utf8l(l):
#     '''Encode a copy of the list, converted to UTF-8'''
#     return list(map(utf8, l))
# 
# Path: xclib/check.py
# def assertEqual(a, b):
#     if type(a) == type(b):
#         assertSimilar(a, b)
#     else:
#         raise AssertionError('Assertion failed: Type mismatch %r (%s) != %r (%s)' % (a, type(a), b, type(b)))
# 
# Path: xclib/tests/iostub.py
# class iostub:
#     def stub_stdin(testcase_inst, inputs, ioclass=io.StringIO):
#         stdin = sys.stdin
# 
#         def cleanup():
#             sys.stdin = stdin
# 
#         testcase_inst.addCleanup(cleanup)
#         sys.stdin = ioclass(inputs)
#         if ioclass == io.BytesIO:
#             # Fake 'buffer' variable
#             sys.stdin.buffer = sys.stdin
# 
#     def stub_stdout(testcase_inst, ioclass=io.StringIO):
#         stdout = sys.stdout
# 
#         def cleanup():
#             sys.stdout = stdout
# 
#         testcase_inst.addCleanup(cleanup)
#         sys.stdout = ioclass()
#         if ioclass == io.BytesIO:
#             # Fake 'buffer' variable
#             sys.stdout.buffer = sys.stdout
# 
#     def stub_stdouts(testcase_inst, ioclass=io.StringIO):
#         stderr = sys.stderr
#         stdout = sys.stdout
# 
#         def cleanup():
#             sys.stderr = stderr
#             sys.stdout = stdout
# 
#         testcase_inst.addCleanup(cleanup)
#         sys.stderr = ioclass()
#         sys.stdout = ioclass()
#         if ioclass == io.BytesIO:
#             # Fake 'buffer' variable
#             sys.stdout.buffer = sys.stdout
#             sys.stderr.buffer = sys.stderr


# Path: xclib/tests/10_utf8_test.py
import sys
import io
from xclib.utf8 import utf8, unutf8, utf8l
from xclib.check import assertEqual
from xclib.tests.iostub import iostub

# Tests wheter our UTF-8 convenience functions work as they should

def test_utf8_ascii():
    assertEqual(b'hallo', utf8(u'hallo'))

def test_utf8_valid():
    assertEqual(b'Hall\xc3\xb6chen', utf8(u'Hallöchen'))

def test_unutf8_ascii():
    assertEqual(unutf8(b'Hallo'), u'Hallo')

def test_unutf8_valid():
    assertEqual(unutf8(b'Hall\xc3\xb6chen'), u'Hallöchen')

def test_unutf8_invalid_ignore():
    assertEqual(unutf8(b'Hall\xffchen', 'ignore'), u'Hallchen')

def test_unutf8_invalid_ignore2():
    assertEqual(unutf8(b'Hall\x80\x80chen', 'ignore'), u'Hallchen')

def test_unutf8_invalid_ignore3():

    assertEqual(unutf8(b'Hall\x80chen', 'ignore'), u'Hallchen')