summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeth M Morton <seth.m.morton@gmail.com>2016-04-18 19:13:07 -0700
committerSeth M Morton <seth.m.morton@gmail.com>2016-04-18 19:13:07 -0700
commitc911e4a65407949027183a690a4449ca764c9ee5 (patch)
tree223f1ed523fdefed5be9ec1eee8c7cc66df91a9c
parent9c11181aa6a43fe882dcff22a6978384efe566e4 (diff)
downloadnatsort-c911e4a65407949027183a690a4449ca764c9ee5.tar.gz
Created _parse_bytes_function.
This is a factory function that will return a function to parse a bytes string in the appropriate manner according user options.
-rw-r--r--natsort/utils.py14
-rw-r--r--test_natsort/compat/hypothesis.py1
-rw-r--r--test_natsort/test_utils.py42
3 files changed, 56 insertions, 1 deletions
diff --git a/natsort/utils.py b/natsort/utils.py
index 9898207..16a19a1 100644
--- a/natsort/utils.py
+++ b/natsort/utils.py
@@ -187,7 +187,7 @@ def _natsort_key(val, key, alg):
# Check if it is a bytes type, and if so return as a
# one element tuple.
if type(val) in (bytes,):
- return (val.lower(),) if alg & ns.IGNORECASE else (val,)
+ return _parse_bytes_function(alg)(val)
# If not strings, assume it is an iterable that must
# be parsed recursively. Do not apply the key recursively.
# If this string was split as a path, turn off 'PATH'.
@@ -228,6 +228,18 @@ def _number_extracter(s, regex, numconv, use_locale, group_letters):
null_string if use_locale else ''))
+def _parse_bytes_function(alg):
+ """Create a function that will properly format a bytes string in a tuple."""
+ if alg & ns.PATH and alg & ns.IGNORECASE:
+ return lambda x: ((x.lower(),),)
+ elif alg & ns.PATH:
+ return lambda x: ((x,),)
+ elif alg & ns.IGNORECASE:
+ return lambda x: (x.lower(),)
+ else:
+ return lambda x: (x,)
+
+
def _parse_number_function(alg, sep):
"""Create a function that will properly format a number in a tuple."""
def func(val,
diff --git a/test_natsort/compat/hypothesis.py b/test_natsort/compat/hypothesis.py
index b633ef2..198cbe7 100644
--- a/test_natsort/compat/hypothesis.py
+++ b/test_natsort/compat/hypothesis.py
@@ -21,6 +21,7 @@ if major_minor != (2, 6):
tuples,
lists,
text,
+ binary,
)
# Otherwise mock these imports, because hypothesis
# is incompatible with python 2.6.
diff --git a/test_natsort/test_utils.py b/test_natsort/test_utils.py
index 8996843..d94b648 100644
--- a/test_natsort/test_utils.py
+++ b/test_natsort/test_utils.py
@@ -28,6 +28,7 @@ from natsort.utils import (
_fix_nan,
chain_functions,
_parse_number_function,
+ _parse_bytes_function,
_pre_split_function,
_post_split_function,
_ungroupletters,
@@ -64,6 +65,7 @@ from compat.hypothesis import (
text,
floats,
integers,
+ binary,
use_hypothesis,
)
@@ -389,6 +391,46 @@ def test_parse_number_function_with_PATH_makes_function_that_returns_nested_tupl
assert _parse_number_function(ns.PATH, '')(x) == (('', x), )
+def test_parse_bytes_function_makes_function_that_returns_tuple_example():
+ assert _parse_bytes_function(0)(b'hello') == (b'hello', )
+
+
+@pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater')
+@given(binary())
+def test_parse_bytes_function_makes_function_that_returns_tuple(x):
+ assert _parse_bytes_function(0)(x) == (x, )
+
+
+def test_parse_bytes_function_with_IGNORECASE_makes_function_that_returns_tuple_with_lowercase_example():
+ assert _parse_bytes_function(ns.IGNORECASE)(b'HelLo') == (b'hello', )
+
+
+@pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater')
+@given(binary())
+def test_parse_bytes_function_with_IGNORECASE_makes_function_that_returns_tuple_with_lowercase(x):
+ assert _parse_bytes_function(ns.IGNORECASE)(x) == (x.lower(), )
+
+
+def test_parse_bytes_function_with_PATH_makes_function_that_returns_nested_tuple_example():
+ assert _parse_bytes_function(ns.PATH)(b'hello') == ((b'hello', ), )
+
+
+@pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater')
+@given(binary())
+def test_parse_bytes_function_with_PATH_makes_function_that_returns_nested_tuple(x):
+ assert _parse_bytes_function(ns.PATH)(x) == ((x, ), )
+
+
+def test_parse_bytes_function_with_PATH_and_IGNORECASE_makes_function_that_returns_nested_tuple_with_lowercase_example():
+ assert _parse_bytes_function(ns.PATH | ns.IGNORECASE)(b'HelLo') == ((b'hello', ), )
+
+
+@pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater')
+@given(binary())
+def test_parse_bytes_function_with_PATH_and_IGNORECASE_makes_function_that_returns_nested_tuple_with_lowercase(x):
+ assert _parse_bytes_function(ns.PATH | ns.IGNORECASE)(x) == ((x.lower(), ), )
+
+
def test_sep_inserter_does_nothing_if_no_numbers_example():
assert list(_sep_inserter(iter(['a', 'b', 'c']), '')) == ['a', 'b', 'c']
assert list(_sep_inserter(iter(['a']), '')) == ['a']