2017-10-24 13:30:02 +00:00
|
|
|
# coding: utf-8
|
2017-11-26 07:10:50 +00:00
|
|
|
from pydu.string import (safeencode, safeunicode, strips, lstrips, rstrips,
|
2018-01-09 00:37:00 +00:00
|
|
|
common_prefix, common_suffix, sort)
|
2017-10-24 13:30:02 +00:00
|
|
|
|
|
|
|
|
2017-11-18 02:53:05 +00:00
|
|
|
def test_safeencode():
|
2017-11-18 03:22:33 +00:00
|
|
|
assert safeencode('hello') == b'hello'
|
|
|
|
assert safeencode(1) == b'1'
|
|
|
|
assert safeencode(u'中文') == b'\xe4\xb8\xad\xe6\x96\x87'
|
2017-10-24 13:30:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_safeunicode():
|
|
|
|
assert safeunicode('hello') == u'hello'
|
|
|
|
assert safeunicode(1) == u'1'
|
|
|
|
assert safeunicode('中文') == u'中文'
|
2017-10-02 08:52:27 +00:00
|
|
|
|
2017-12-04 16:57:23 +00:00
|
|
|
assert safeunicode(u'hello') == u'hello'
|
2017-12-04 16:49:36 +00:00
|
|
|
assert safeunicode(u'中文') == u'中文'
|
|
|
|
|
2017-10-02 08:52:27 +00:00
|
|
|
|
|
|
|
def test_lstrips():
|
|
|
|
assert lstrips('foobbar', '') == 'foobbar'
|
|
|
|
assert lstrips('foobar', 'fo') == 'obar'
|
|
|
|
assert lstrips('foofoobar', 'foo') == 'foobar'
|
|
|
|
assert lstrips('foobarbaz', ('foo', 'bar')) == 'baz'
|
|
|
|
assert lstrips('foobarbaz', ('bar', 'foo')) == 'barbaz'
|
|
|
|
|
|
|
|
|
|
|
|
def test_rstrips():
|
|
|
|
assert rstrips('foobbar', '') == 'foobbar'
|
|
|
|
assert rstrips('foobbar', 'bar') == 'foob'
|
|
|
|
assert rstrips('foobarbar', 'bar') == 'foobar'
|
|
|
|
assert rstrips('fozfoobar', ('bar', 'foo')) == 'foz'
|
|
|
|
assert rstrips('fozfoobar', ('foo', 'bar')) == 'fozfoo'
|
|
|
|
|
|
|
|
|
|
|
|
def test_strips():
|
|
|
|
assert strips('foobarfoo', '') == 'foobarfoo'
|
|
|
|
assert strips('foobarfoo', 'foo') == 'bar'
|
|
|
|
assert strips('foobarfoo', ('foo', 'bar')) == ''
|
2017-11-26 07:10:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_common_prefix():
|
|
|
|
l = ['abcd', 'abc1']
|
|
|
|
assert common_prefix(l) == 'abc'
|
2017-11-26 07:18:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_common_suffix():
|
|
|
|
l = ['dabc', '1abc']
|
|
|
|
assert common_suffix(l) == 'abc'
|
2017-12-12 15:34:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_sort():
|
2017-12-13 14:06:17 +00:00
|
|
|
assert sort('acb21') == '12abc'
|
2017-12-13 13:02:33 +00:00
|
|
|
assert sort('abc21', reverse=True) == 'cba21'
|