2016-05-12 17:03:57 +00:00
|
|
|
# coding=utf-8
|
2016-05-29 11:33:20 +00:00
|
|
|
|
2015-09-05 16:15:47 +00:00
|
|
|
from netlib import utils, tutils
|
2015-04-29 21:04:22 +00:00
|
|
|
|
2016-05-29 11:33:20 +00:00
|
|
|
|
2015-04-29 21:04:22 +00:00
|
|
|
def test_bidi():
|
|
|
|
b = utils.BiDi(a=1, b=2)
|
|
|
|
assert b.a == 1
|
2015-04-30 00:10:08 +00:00
|
|
|
assert b.get_name(1) == "a"
|
|
|
|
assert b.get_name(5) is None
|
2015-04-29 21:04:22 +00:00
|
|
|
tutils.raises(AttributeError, getattr, b, "c")
|
2015-04-30 00:10:08 +00:00
|
|
|
tutils.raises(ValueError, utils.BiDi, one=1, two=1)
|
2015-04-29 21:04:22 +00:00
|
|
|
|
2012-09-23 23:21:48 +00:00
|
|
|
|
|
|
|
def test_hexdump():
|
2015-09-17 15:32:59 +00:00
|
|
|
assert list(utils.hexdump(b"one\0" * 10))
|
2012-09-23 23:21:48 +00:00
|
|
|
|
|
|
|
|
2015-09-17 00:14:14 +00:00
|
|
|
def test_clean_bin():
|
2015-09-12 15:03:09 +00:00
|
|
|
assert utils.clean_bin(b"one") == b"one"
|
|
|
|
assert utils.clean_bin(b"\00ne") == b".ne"
|
|
|
|
assert utils.clean_bin(b"\nne") == b"\nne"
|
|
|
|
assert utils.clean_bin(b"\nne", False) == b".ne"
|
|
|
|
assert utils.clean_bin(u"\u2605".encode("utf8")) == b"..."
|
|
|
|
|
|
|
|
assert utils.clean_bin(u"one") == u"one"
|
|
|
|
assert utils.clean_bin(u"\00ne") == u".ne"
|
|
|
|
assert utils.clean_bin(u"\nne") == u"\nne"
|
|
|
|
assert utils.clean_bin(u"\nne", False) == u".ne"
|
|
|
|
assert utils.clean_bin(u"\u2605") == u"\u2605"
|
2015-04-30 00:10:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_pretty_size():
|
|
|
|
assert utils.pretty_size(100) == "100B"
|
|
|
|
assert utils.pretty_size(1024) == "1kB"
|
2015-05-27 09:18:54 +00:00
|
|
|
assert utils.pretty_size(1024 + (1024 / 2.0)) == "1.5kB"
|
|
|
|
assert utils.pretty_size(1024 * 1024) == "1MB"
|
2015-08-01 08:39:14 +00:00
|
|
|
|
|
|
|
|
2016-04-02 12:38:33 +00:00
|
|
|
def test_safe_subn():
|
|
|
|
assert utils.safe_subn("foo", u"bar", "\xc2foo")
|
2016-05-12 17:03:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_bytes_to_escaped_str():
|
|
|
|
assert utils.bytes_to_escaped_str(b"foo") == "foo"
|
|
|
|
assert utils.bytes_to_escaped_str(b"\b") == r"\x08"
|
|
|
|
assert utils.bytes_to_escaped_str(br"&!?=\)") == r"&!?=\\)"
|
|
|
|
assert utils.bytes_to_escaped_str(b'\xc3\xbc') == r"\xc3\xbc"
|
2016-05-26 03:11:34 +00:00
|
|
|
assert utils.bytes_to_escaped_str(b"'") == r"\'"
|
|
|
|
assert utils.bytes_to_escaped_str(b'"') == r'"'
|
2016-05-12 17:03:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_escaped_str_to_bytes():
|
|
|
|
assert utils.escaped_str_to_bytes("foo") == b"foo"
|
2016-05-26 02:16:02 +00:00
|
|
|
assert utils.escaped_str_to_bytes("\x08") == b"\b"
|
|
|
|
assert utils.escaped_str_to_bytes("&!?=\\\\)") == br"&!?=\)"
|
|
|
|
assert utils.escaped_str_to_bytes("ü") == b'\xc3\xbc'
|
|
|
|
assert utils.escaped_str_to_bytes(u"\\x08") == b"\b"
|
|
|
|
assert utils.escaped_str_to_bytes(u"&!?=\\\\)") == br"&!?=\)"
|
2016-05-29 11:33:20 +00:00
|
|
|
assert utils.escaped_str_to_bytes(u"ü") == b'\xc3\xbc'
|