add testcase for safestr and safeunicode

This commit is contained in:
Prodesire 2017-10-03 23:02:14 +08:00
parent 95d8034a81
commit 10e97531d9
2 changed files with 18 additions and 3 deletions

View File

@ -1,3 +1,4 @@
# coding: utf-8
from .structures import AttrDict
from .py3helpers import PY2, itervalues, iteritems, text_type, string_types, imap, is_iter
@ -89,8 +90,8 @@ def safeunicode(obj, encoding='utf-8'):
u'hello'
>>> safeunicode(2)
u'2'
>>> safeunicode('\xe1\x88\xb4')
u'\u1234'
>>> safeunicode('\xe4\xb8\xad\xe6\x96\x87')
u'中文'
"""
t = type(obj)
if t is text_type:
@ -115,7 +116,7 @@ def safestr(obj, encoding='utf-8'):
if PY2 and isinstance(obj, unicode):
return obj.encode(encoding)
elif is_iter(obj):
elif is_iter(obj) or isinstance(obj, iters):
return imap(safestr, obj)
else:
return str(obj)

View File

@ -0,0 +1,14 @@
# coding: utf-8
from pylib.utils import safestr, safeunicode
def test_safestr():
assert safestr('hello') == 'hello'
assert safestr(1) == '1'
assert list(safestr([1, 'a'])) == ['1', 'a']
def test_safeunicode():
assert safeunicode('hello') == u'hello'
assert safeunicode(1) == u'1'
assert safeunicode('中文') == u'中文'