From c98c83190baa078539b79136c2de348985cc61cc Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Tue, 7 Jun 2016 00:23:07 -0700 Subject: [PATCH] tests++ --- netlib/strutils.py | 8 +------- test/netlib/test_strutils.py | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/netlib/strutils.py b/netlib/strutils.py index 03b371f56..5ad41c7e9 100644 --- a/netlib/strutils.py +++ b/netlib/strutils.py @@ -120,13 +120,7 @@ def isMostlyBin(s): def isXML(s): - for i in s: - if i in "\n \t": - continue - elif i == "<": - return True - else: - return False + return s.strip().startswith("<") def clean_hanging_newline(t): diff --git a/test/netlib/test_strutils.py b/test/netlib/test_strutils.py index 734265c4b..84a0dded6 100644 --- a/test/netlib/test_strutils.py +++ b/test/netlib/test_strutils.py @@ -1,6 +1,18 @@ # coding=utf-8 +import six -from netlib import strutils +from netlib import strutils, tutils + + +def test_native(): + with tutils.raises(TypeError): + strutils.native(42) + if six.PY2: + assert strutils.native(u"foo") == b"foo" + assert strutils.native(b"foo") == b"foo" + else: + assert strutils.native(u"foo") == u"foo" + assert strutils.native(b"foo") == u"foo" def test_clean_bin(): @@ -29,6 +41,9 @@ def test_bytes_to_escaped_str(): assert strutils.bytes_to_escaped_str(b"'") == r"\'" assert strutils.bytes_to_escaped_str(b'"') == r'"' + with tutils.raises(ValueError): + strutils.bytes_to_escaped_str(u"such unicode") + def test_escaped_str_to_bytes(): assert strutils.escaped_str_to_bytes("foo") == b"foo" @@ -39,6 +54,13 @@ def test_escaped_str_to_bytes(): assert strutils.escaped_str_to_bytes(u"&!?=\\\\)") == br"&!?=\)" assert strutils.escaped_str_to_bytes(u"ΓΌ") == b'\xc3\xbc' + if six.PY2: + with tutils.raises(ValueError): + strutils.escaped_str_to_bytes(42) + else: + with tutils.raises(ValueError): + strutils.escaped_str_to_bytes(b"very byte") + def test_isBin(): assert not strutils.isBin("testing\n\r")