summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeth M Morton <seth.m.morton@gmail.com>2016-05-05 16:35:19 -0700
committerSeth M Morton <seth.m.morton@gmail.com>2016-05-05 16:35:19 -0700
commitc099dec56ff22b262489beebfb46941d586e121c (patch)
tree9695a1a21df28ee3e5891a2c3c470be9a96ac43c
parentc3c11fe4d3c3d486be10e5775d699f15547c973c (diff)
downloadnatsort-c099dec56ff22b262489beebfb46941d586e121c.tar.gz
Pulled groupletters out of locale_help.
This function has been moved to utils. Additionally, the python version checking has been unified.
-rw-r--r--natsort/compat/fake_fastnumbers.py4
-rw-r--r--natsort/compat/locale.py13
-rw-r--r--natsort/compat/py23.py1
-rw-r--r--natsort/locale_help.py10
-rw-r--r--natsort/utils.py15
-rw-r--r--test_natsort/compat/hypothesis.py6
-rw-r--r--test_natsort/test_fake_fastnumbers.py4
-rw-r--r--test_natsort/test_locale_help.py22
-rw-r--r--test_natsort/test_natsort_key.py32
-rw-r--r--test_natsort/test_natsort_keygen.py17
-rw-r--r--test_natsort/test_natsorted.py4
-rw-r--r--test_natsort/test_natsorted_convenience.py4
-rw-r--r--test_natsort/test_parse_string_function.py5
-rw-r--r--test_natsort/test_post_split_function.py20
-rw-r--r--test_natsort/test_pre_split_function.py10
-rw-r--r--test_natsort/test_utils.py14
16 files changed, 79 insertions, 102 deletions
diff --git a/natsort/compat/fake_fastnumbers.py b/natsort/compat/fake_fastnumbers.py
index da64d71..a536ef2 100644
--- a/natsort/compat/fake_fastnumbers.py
+++ b/natsort/compat/fake_fastnumbers.py
@@ -14,8 +14,8 @@ from __future__ import (
# Std. lib imports.
import sys
import unicodedata
-is_py2 = sys.version[0] == '2'
-if not is_py2:
+from natsort.compat.py23 import PY_VERSION
+if PY_VERSION >= 3:
long = int
diff --git a/natsort/compat/locale.py b/natsort/compat/locale.py
index 5412cf5..6989710 100644
--- a/natsort/compat/locale.py
+++ b/natsort/compat/locale.py
@@ -6,9 +6,6 @@ from __future__ import (
absolute_import
)
-# Std. lib imports
-import sys
-
# Local imports.
from natsort.compat.py23 import PY_VERSION, cmp_to_key
@@ -45,7 +42,7 @@ try:
except ImportError:
import locale
- if sys.version[0] == '2':
+ if PY_VERSION < 3:
from locale import strcoll
strxfrm = cmp_to_key(strcoll)
null_string = strxfrm('')
@@ -64,11 +61,3 @@ except ImportError:
# order. We will try to detect this and compensate.
def dumb_sort():
return strxfrm('A') < strxfrm('a')
-
-
-if PY_VERSION >= 3.3:
- def _low(x):
- return x.casefold()
-else:
- def _low(x):
- return x.lower()
diff --git a/natsort/compat/py23.py b/natsort/compat/py23.py
index 9e92027..31a2944 100644
--- a/natsort/compat/py23.py
+++ b/natsort/compat/py23.py
@@ -16,6 +16,7 @@ import sys
# Numeric form of version
PY_VERSION = float(sys.version[:3])
+NEWPY = PY_VERSION >= 3.3
# Assume all strings are Unicode in Python 2
py23_str = str if sys.version[0] == '3' else unicode
diff --git a/natsort/locale_help.py b/natsort/locale_help.py
index 0f76d2c..48907cf 100644
--- a/natsort/locale_help.py
+++ b/natsort/locale_help.py
@@ -11,22 +11,14 @@ from __future__ import (
absolute_import
)
-# Std. lib imports.
-from itertools import chain
-
# Local imports.
-from natsort.compat.locale import use_pyicu, _low
+from natsort.compat.locale import use_pyicu
if use_pyicu:
from natsort.compat.locale import get_pyicu_transform
else:
from natsort.compat.locale import strxfrm
-def groupletters(x):
- """Double all characters, making doubled letters lowercase."""
- return ''.join(chain.from_iterable([_low(y), y] for y in x))
-
-
def locale_convert_function():
"""
Return a function that will use the appropriate locale tranformation.
diff --git a/natsort/utils.py b/natsort/utils.py
index 985adb3..5157591 100644
--- a/natsort/utils.py
+++ b/natsort/utils.py
@@ -11,7 +11,6 @@ from __future__ import (
)
# Std. lib imports.
-import sys
import re
from warnings import warn
from os import curdir as os_curdir, pardir as os_pardir
@@ -24,7 +23,7 @@ from operator import methodcaller
# Local imports.
from natsort.ns_enum import ns
from natsort.unicode_numbers import digits, numeric
-from natsort.locale_help import locale_convert_function, groupletters
+from natsort.locale_help import locale_convert_function
from natsort.compat.pathlib import PurePath, has_pathlib
from natsort.compat.locale import (
get_thousands_sep,
@@ -36,12 +35,13 @@ from natsort.compat.py23 import (
py23_map,
py23_filter,
PY_VERSION,
+ NEWPY,
)
from natsort.compat.fastnumbers import (
fast_float,
fast_int,
)
-if sys.version[0] == '3':
+if PY_VERSION >= 3:
long = int
# The regex that locates floats - include Unicode numerals.
@@ -236,7 +236,7 @@ def _pre_split_function(alg):
if (dumb and not lowfirst) or (lowfirst and not dumb):
function_chain.append(methodcaller('swapcase'))
if alg & ns.IGNORECASE:
- if PY_VERSION >= 3.3:
+ if NEWPY:
function_chain.append(methodcaller('casefold'))
else:
function_chain.append(methodcaller('lower'))
@@ -263,7 +263,7 @@ def _post_split_function(alg):
# Build the chain of functions to execute in order.
func_chain = []
if group_letters:
- func_chain.append(groupletters)
+ func_chain.append(_groupletters)
if use_locale:
func_chain.append(locale_convert_function())
kwargs = {'key': chain_functions(func_chain)} if func_chain else {}
@@ -305,6 +305,11 @@ def _post_string_parse_function(alg, sep):
return lambda split_val, val: tuple(split_val)
+def _groupletters(x, _low=methodcaller('casefold' if NEWPY else 'lower')):
+ """Double all characters, making doubled letters lowercase."""
+ return ''.join(ichain.from_iterable((_low(y), y) for y in x))
+
+
def chain_functions(functions):
"""
Chain a list of single-argument functions together and return.
diff --git a/test_natsort/compat/hypothesis.py b/test_natsort/compat/hypothesis.py
index 9e007da..c07381c 100644
--- a/test_natsort/compat/hypothesis.py
+++ b/test_natsort/compat/hypothesis.py
@@ -5,13 +5,11 @@ from __future__ import (
unicode_literals,
absolute_import
)
-import sys
import compat.mock
-
-major_minor = sys.version_info[:2]
+from natsort.compat.py23 import PY_VERSION
# Use hypothesis if not on python 2.6.
-if major_minor != (2, 6):
+if PY_VERSION > 2.6:
use_hypothesis = True
from hypothesis import assume, given, example
from hypothesis.strategies import (
diff --git a/test_natsort/test_fake_fastnumbers.py b/test_natsort/test_fake_fastnumbers.py
index a08dcbd..f93642c 100644
--- a/test_natsort/test_fake_fastnumbers.py
+++ b/test_natsort/test_fake_fastnumbers.py
@@ -4,10 +4,10 @@ Test the fake fastnumbers module.
"""
from __future__ import unicode_literals
-import sys
import pytest
import unicodedata
from math import isnan
+from natsort.compat.py23 import PY_VERSION
from natsort.compat.fake_fastnumbers import (
fast_float,
fast_int,
@@ -21,7 +21,7 @@ from compat.hypothesis import (
use_hypothesis,
)
-if sys.version[0] == '3':
+if PY_VERSION >= 3:
long = int
diff --git a/test_natsort/test_locale_help.py b/test_natsort/test_locale_help.py
index 3b8148d..fc89776 100644
--- a/test_natsort/test_locale_help.py
+++ b/test_natsort/test_locale_help.py
@@ -6,24 +6,17 @@ from __future__ import unicode_literals
import locale
import pytest
-from math import isnan
-from itertools import chain
from natsort.compat.fake_fastnumbers import fast_float
-from natsort.locale_help import groupletters, locale_convert_function
-from natsort.compat.py23 import py23_str
-from natsort.compat.locale import use_pyicu
+from natsort.locale_help import locale_convert_function
from compat.locale import (
load_locale,
- has_locale_de_DE,
get_strxfrm,
- low,
bad_uni_chars,
)
from compat.hypothesis import (
assume,
given,
text,
- floats,
use_hypothesis,
)
@@ -32,19 +25,6 @@ from compat.hypothesis import (
# and a test that uses the hypothesis module.
-def test_groupletters_returns_letters_with_lowercase_transform_of_letter_example():
- assert groupletters('HELLO') == 'hHeElLlLoO'
- assert groupletters('hello') == 'hheelllloo'
-
-
-@pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater')
-@given(text())
-def test_groupeletters_returns_letters_with_lowercase_transform_of_letter(x):
- assume(bool(x))
- assume(type(fast_float(x)) is not float)
- assert groupletters(x) == ''.join(chain.from_iterable([low(y), y] for y in x))
-
-
def test_locale_convert_transforms_string_to_strxfrm_string_example():
load_locale('en_US')
strxfrm = get_strxfrm()
diff --git a/test_natsort/test_natsort_key.py b/test_natsort/test_natsort_key.py
index 62e258e..91736b4 100644
--- a/test_natsort/test_natsort_key.py
+++ b/test_natsort/test_natsort_key.py
@@ -2,9 +2,9 @@
"""These test the utils.py functions."""
from __future__ import unicode_literals
-import sys
import pytest
from math import isnan
+from natsort.compat.py23 import PY_VERSION
from natsort.ns_enum import ns
from natsort.utils import (
_natsort_key,
@@ -28,7 +28,7 @@ from compat.hypothesis import (
use_hypothesis,
)
-if sys.version[0] == '3':
+if PY_VERSION >= 3:
long = int
@@ -50,14 +50,14 @@ def test__natsort_key_with_numeric_input_and_PATH_returns_number_in_nested_tuple
assert _natsort_key(10, None, sfunc, bytes_func, num_func) == (('', 10),)
-if sys.version[0] == '3':
- def test__natsort_key_with_bytes_input_and_PATH_returns_number_in_nested_tuple():
- # It gracefully handles as_path for numeric input by putting an extra tuple around it
- # so it will sort against the other as_path results.
- sfunc = _parse_path_function(string_func)
- bytes_func = _parse_bytes_function(ns.PATH)
- num_func = _parse_number_function(ns.PATH, '')
- assert _natsort_key(b'/hello/world', None, sfunc, bytes_func, num_func) == ((b'/hello/world',),)
+@pytest.mark.skipif(PY_VERSION < 3, reason='only valid on python3')
+def test__natsort_key_with_bytes_input_and_PATH_returns_number_in_nested_tuple():
+ # It gracefully handles as_path for numeric input by putting an extra tuple around it
+ # so it will sort against the other as_path results.
+ sfunc = _parse_path_function(string_func)
+ bytes_func = _parse_bytes_function(ns.PATH)
+ num_func = _parse_number_function(ns.PATH, '')
+ assert _natsort_key(b'/hello/world', None, sfunc, bytes_func, num_func) == ((b'/hello/world',),)
def test__natsort_key_with_tuple_of_paths_and_PATH_returns_triply_nested_tuple():
@@ -79,12 +79,12 @@ def test__natsort_key_with_numeric_input_takes_number_path(x):
assert _natsort_key(x, None, string_func, bytes_func, num_func) == num_func(x)
-if sys.version[0] == '3':
- @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater')
- @given(binary())
- def test__natsort_key_with_bytes_input_takes_bytes_path(x):
- assume(x)
- assert _natsort_key(x, None, string_func, bytes_func, num_func) == bytes_func(x)
+@pytest.mark.skipif(PY_VERSION < 3, reason='only valid on python3')
+@pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater')
+@given(binary())
+def test__natsort_key_with_bytes_input_takes_bytes_path(x):
+ assume(x)
+ assert _natsort_key(x, None, string_func, bytes_func, num_func) == bytes_func(x)
@pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater')
diff --git a/test_natsort/test_natsort_keygen.py b/test_natsort/test_natsort_keygen.py
index 80c9319..0ecc47d 100644
--- a/test_natsort/test_natsort_keygen.py
+++ b/test_natsort/test_natsort_keygen.py
@@ -5,7 +5,6 @@ See the README or the natsort homepage for more details.
"""
from __future__ import unicode_literals, print_function
-import sys
import warnings
from pytest import raises
from natsort import (
@@ -14,11 +13,11 @@ from natsort import (
natsort_keygen,
ns,
)
+from natsort.compat.py23 import PY_VERSION
from natsort.compat.locale import null_string
from compat.locale import get_strxfrm
from compat.mock import patch
-IS_PY3 = sys.version[0] == '3'
INPUT = ['6A-5.034e+1', '/Folder (1)/Foo', 56.7]
@@ -59,17 +58,17 @@ def test_natsort_keygen_returns_key_that_can_be_used_to_sort_list_in_place_with_
def test_natsort_keygen_splits_input_with_defaults():
assert natsort_keygen()(INPUT) == (('', 6, 'A-', 5, '.', 34, 'e+', 1), ('/Folder (', 1, ')/Foo'), ('', 56.7))
- if IS_PY3: assert natsort_keygen()(b'6A-5.034e+1') == (b'6A-5.034e+1',)
+ if PY_VERSION >= 3: assert natsort_keygen()(b'6A-5.034e+1') == (b'6A-5.034e+1',)
def test_natsort_keygen_splits_input_with_real():
assert natsort_keygen(alg=ns.R)(INPUT) == (('', 6.0, 'A', -50.34), ('/Folder (', 1.0, ')/Foo'), ('', 56.7))
- if IS_PY3: assert natsort_keygen(alg=ns.R)(b'6A-5.034e+1') == (b'6A-5.034e+1',)
+ if PY_VERSION >= 3: assert natsort_keygen(alg=ns.R)(b'6A-5.034e+1') == (b'6A-5.034e+1',)
def test_natsort_keygen_splits_input_with_lowercasefirst_noexp_float():
assert natsort_keygen(alg=ns.LF | ns.F | ns.N)(INPUT) == (('', 6.0, 'a-', 5.034, 'E+', 1.0), ('/fOLDER (', 1.0, ')/fOO'), ('', 56.7))
- if IS_PY3: assert natsort_keygen(alg=ns.LF | ns.F | ns.N)(b'6A-5.034e+1') == (b'6A-5.034e+1',)
+ if PY_VERSION >= 3: assert natsort_keygen(alg=ns.LF | ns.F | ns.N)(b'6A-5.034e+1') == (b'6A-5.034e+1',)
def test_natsort_keygen_splits_input_with_locale():
@@ -78,21 +77,21 @@ def test_natsort_keygen_splits_input_with_locale():
assert natsort_keygen(alg=ns.L)(INPUT) == ((null_string, 6, strxfrm('A-'), 5, strxfrm('.'), 34, strxfrm('e+'), 1), (strxfrm('/Folder ('), 1, strxfrm(')/Foo')), (null_string, 56.7))
with patch('natsort.compat.locale.dumb_sort', return_value=True):
assert natsort_keygen(alg=ns.L)(INPUT) == ((null_string, 6, strxfrm('aa--'), 5, strxfrm('..'), 34, strxfrm('eE++'), 1), (strxfrm('//ffoOlLdDeErR (('), 1, strxfrm('))//ffoOoO')), (null_string, 56.7))
- if IS_PY3: assert natsort_keygen(alg=ns.L)(b'6A-5.034e+1') == (b'6A-5.034e+1',)
+ if PY_VERSION >= 3: assert natsort_keygen(alg=ns.L)(b'6A-5.034e+1') == (b'6A-5.034e+1',)
def test_natsort_keygen_splits_input_with_locale_and_capitalfirst():
strxfrm = get_strxfrm()
with patch('natsort.compat.locale.dumb_sort', return_value=False):
assert natsort_keygen(alg=ns.L | ns.C)(INPUT) == ((('',), (null_string, 6, strxfrm('A-'), 5, strxfrm('.'), 34, strxfrm('e+'), 1)), (('/',), (strxfrm('/Folder ('), 1, strxfrm(')/Foo'))), (('',), (null_string, 56.7)))
- if IS_PY3: assert natsort_keygen(alg=ns.L | ns.C)(b'6A-5.034e+1') == (b'6A-5.034e+1',)
+ if PY_VERSION >= 3: assert natsort_keygen(alg=ns.L | ns.C)(b'6A-5.034e+1') == (b'6A-5.034e+1',)
def test_natsort_keygen_splits_input_with_path():
assert natsort_keygen(alg=ns.P | ns.G)(INPUT) == ((('', 6, 'aA--', 5, '..', 34, 'ee++', 1),), (('//',), ('fFoollddeerr ((', 1, '))'), ('fFoooo',)), (('', 56.7),))
- if IS_PY3: assert natsort_keygen(alg=ns.P | ns.G)(b'6A-5.034e+1') == ((b'6A-5.034e+1',),)
+ if PY_VERSION >= 3: assert natsort_keygen(alg=ns.P | ns.G)(b'6A-5.034e+1') == ((b'6A-5.034e+1',),)
def test_natsort_keygen_splits_input_with_ignorecase():
assert natsort_keygen(alg=ns.IC)(INPUT) == (('', 6, 'a-', 5, '.', 34, 'e+', 1), ('/folder (', 1, ')/foo'), ('', 56.7))
- if IS_PY3: assert natsort_keygen(alg=ns.IC)(b'6A-5.034e+1') == (b'6a-5.034e+1',)
+ if PY_VERSION >= 3: assert natsort_keygen(alg=ns.IC)(b'6A-5.034e+1') == (b'6a-5.034e+1',)
diff --git a/test_natsort/test_natsorted.py b/test_natsort/test_natsorted.py
index b749b0d..8802825 100644
--- a/test_natsort/test_natsorted.py
+++ b/test_natsort/test_natsorted.py
@@ -5,8 +5,8 @@ See the README or the natsort homepage for more details.
"""
from __future__ import unicode_literals, print_function
import pytest
-import sys
import locale
+from natsort.compat.py23 import PY_VERSION
from operator import itemgetter
from pytest import raises
from natsort import (
@@ -98,7 +98,7 @@ def test_natsorted_with_nan_input_returns_sorted_results_with_nan_first_without_
def test_natsorted_with_mixed_input_raises_TypeError_if_bytes_type_is_involved_on_Python3():
- if sys.version[0] == '3':
+ if PY_VERSION >= 3:
with raises(TypeError) as e:
assert natsorted(['รค', b'b'])
assert 'bytes' in str(e.value)
diff --git a/test_natsort/test_natsorted_convenience.py b/test_natsort/test_natsorted_convenience.py
index f094c0b..9e04493 100644
--- a/test_natsort/test_natsorted_convenience.py
+++ b/test_natsort/test_natsorted_convenience.py
@@ -4,8 +4,8 @@ Here are a collection of examples of how this module can be used.
See the README or the natsort homepage for more details.
"""
from __future__ import unicode_literals, print_function
-import sys
from operator import itemgetter
+from natsort.compat.py23 import PY_VERSION
from natsort import (
natsorted,
index_natsorted,
@@ -29,7 +29,7 @@ def test_decoder_returns_function_that_can_decode_bytes_but_return_non_bytes_as_
b = 14
assert f(b'bytes') == a
assert f(b) is b # returns as-is, same object ID
- if sys.version[0] == '3':
+ if PY_VERSION >= 3:
assert f(a) is a # same object returned on Python3 b/c only bytes has decode
else:
assert f(a) is not a
diff --git a/test_natsort/test_parse_string_function.py b/test_natsort/test_parse_string_function.py
index 9c550d2..88bf871 100644
--- a/test_natsort/test_parse_string_function.py
+++ b/test_natsort/test_parse_string_function.py
@@ -2,7 +2,6 @@
"""These test the utils.py functions."""
from __future__ import unicode_literals
-import sys
import pytest
from math import isnan
from pytest import raises
@@ -17,7 +16,7 @@ from natsort.utils import (
_parse_string_function,
_parse_path_function,
)
-from natsort.compat.py23 import py23_str
+from natsort.compat.py23 import py23_str, PY_VERSION
from natsort.compat.fastnumbers import (
fast_float,
fast_int,
@@ -37,7 +36,7 @@ from compat.hypothesis import (
use_hypothesis,
)
-if sys.version[0] == '3':
+if PY_VERSION >= 3:
long = int
diff --git a/test_natsort/test_post_split_function.py b/test_natsort/test_post_split_function.py
index e4bffe2..47388d0 100644
--- a/test_natsort/test_post_split_function.py
+++ b/test_natsort/test_post_split_function.py
@@ -4,11 +4,11 @@ from __future__ import unicode_literals
import pytest
from natsort.ns_enum import ns
-from natsort.utils import _post_split_function
-from natsort.locale_help import (
- locale_convert_function,
- groupletters,
+from natsort.utils import (
+ _post_split_function,
+ _groupletters,
)
+from natsort.locale_help import locale_convert_function
from natsort.compat.py23 import py23_str
from natsort.compat.fastnumbers import (
fast_float,
@@ -65,14 +65,14 @@ def test_post_split_function_with_FLOAT_and_NANLAST_returns_fast_float_with_pos_
def test_post_split_function_with_GROUPLETTERS_returns_fast_int_and_groupletters_example():
x = 'hello'
- assert _post_split_function(ns.GROUPLETTERS)(x) == fast_int(x, key=groupletters)
+ assert _post_split_function(ns.GROUPLETTERS)(x) == fast_int(x, key=_groupletters)
@pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater')
@given(text())
def test_post_split_function_with_GROUPLETTERS_returns_fast_int_and_groupletters(x):
assume(x)
- assert _post_split_function(ns.GROUPLETTERS)(x) == fast_int(x, key=groupletters)
+ assert _post_split_function(ns.GROUPLETTERS)(x) == fast_int(x, key=_groupletters)
def test_post_split_function_with_LOCALE_returns_fast_int_and_groupletters_example():
@@ -90,7 +90,7 @@ def test_post_split_function_with_LOCALE_returns_fast_int_and_groupletters(x):
def test_post_split_function_with_LOCALE_and_GROUPLETTERS_returns_fast_int_and_groupletters_and_locale_convert_example():
x = 'hello'
- assert _post_split_function(ns.GROUPLETTERS | ns.LOCALE)(x) == fast_int(x, key=lambda x: locale_convert_function()(groupletters(x)))
+ assert _post_split_function(ns.GROUPLETTERS | ns.LOCALE)(x) == fast_int(x, key=lambda x: locale_convert_function()(_groupletters(x)))
@pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater')
@@ -98,7 +98,7 @@ def test_post_split_function_with_LOCALE_and_GROUPLETTERS_returns_fast_int_and_g
def test_post_split_function_with_LOCALE_and_GROUPLETTERS_returns_fast_int_and_groupletters_and_locale_convert(x):
assume(x)
try:
- assert _post_split_function(ns.GROUPLETTERS | ns.LOCALE)(x) == fast_int(x, key=lambda x: locale_convert_function()(groupletters(x)))
+ assert _post_split_function(ns.GROUPLETTERS | ns.LOCALE)(x) == fast_int(x, key=lambda x: locale_convert_function()(_groupletters(x)))
except ValueError as e: # handle broken locale lib on BSD.
if 'is not in range' not in str(e):
raise
@@ -106,7 +106,7 @@ def test_post_split_function_with_LOCALE_and_GROUPLETTERS_returns_fast_int_and_g
def test_post_split_function_with_LOCALE_and_DUMB_returns_fast_int_and_groupletters_and_locale_convert_example():
x = 'hello'
- assert _post_split_function(ns._DUMB | ns.LOCALE)(x) == fast_int(x, key=lambda x: locale_convert_function()(groupletters(x)))
+ assert _post_split_function(ns._DUMB | ns.LOCALE)(x) == fast_int(x, key=lambda x: locale_convert_function()(_groupletters(x)))
@pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater')
@@ -114,7 +114,7 @@ def test_post_split_function_with_LOCALE_and_DUMB_returns_fast_int_and_grouplett
def test_post_split_function_with_LOCALE_and_DUMB_returns_fast_int_and_groupletters_and_locale_convert(x):
assume(x)
try:
- assert _post_split_function(ns._DUMB | ns.LOCALE)(x) == fast_int(x, key=lambda x: locale_convert_function()(groupletters(x)))
+ assert _post_split_function(ns._DUMB | ns.LOCALE)(x) == fast_int(x, key=lambda x: locale_convert_function()(_groupletters(x)))
except ValueError as e: # handle broken locale lib on BSD.
if 'is not in range' not in str(e):
raise
diff --git a/test_natsort/test_pre_split_function.py b/test_natsort/test_pre_split_function.py
index d32c95b..58cc108 100644
--- a/test_natsort/test_pre_split_function.py
+++ b/test_natsort/test_pre_split_function.py
@@ -2,10 +2,10 @@
"""These test the utils.py functions."""
from __future__ import unicode_literals
-import sys
import pytest
from natsort.ns_enum import ns
from natsort.utils import _pre_split_function
+from natsort.compat.py23 import NEWPY
from compat.hypothesis import (
given,
text,
@@ -30,7 +30,7 @@ def test_pre_split_function_is_no_op_for_no_alg_options(x):
def test_pre_split_function_performs_casefold_with_IGNORECASE_examples():
x = 'feijGGAd'
- if sys.version_info[0:2] >= (3, 3):
+ if NEWPY:
assert _pre_split_function(ns.IGNORECASE)(x) == x.casefold()
else:
assert _pre_split_function(ns.IGNORECASE)(x) == x.lower()
@@ -39,7 +39,7 @@ def test_pre_split_function_performs_casefold_with_IGNORECASE_examples():
@pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater')
@given(text())
def test_pre_split_function_performs_casefold_with_IGNORECASE(x):
- if sys.version_info[0:2] >= (3, 3):
+ if NEWPY:
assert _pre_split_function(ns.IGNORECASE)(x) == x.casefold()
else:
assert _pre_split_function(ns.IGNORECASE)(x) == x.lower()
@@ -81,7 +81,7 @@ def test_pre_split_function_is_no_op_with_both_LOWERCASEFIRST_AND_DUMB(x):
def test_pre_split_function_performs_swapcase_and_casefold_both_LOWERCASEFIRST_AND_IGNORECASE_example():
x = 'feijGGAd'
- if sys.version_info[0:2] >= (3, 3):
+ if NEWPY:
assert _pre_split_function(ns.IGNORECASE | ns.LOWERCASEFIRST)(x) == x.swapcase().casefold()
else:
assert _pre_split_function(ns.IGNORECASE | ns.LOWERCASEFIRST)(x) == x.swapcase().lower()
@@ -90,7 +90,7 @@ def test_pre_split_function_performs_swapcase_and_casefold_both_LOWERCASEFIRST_A
@pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater')
@given(text())
def test_pre_split_function_performs_swapcase_and_casefold_both_LOWERCASEFIRST_AND_IGNORECASE(x):
- if sys.version_info[0:2] >= (3, 3):
+ if NEWPY:
assert _pre_split_function(ns.IGNORECASE | ns.LOWERCASEFIRST)(x) == x.swapcase().casefold()
else:
assert _pre_split_function(ns.IGNORECASE | ns.LOWERCASEFIRST)(x) == x.swapcase().lower()
diff --git a/test_natsort/test_utils.py b/test_natsort/test_utils.py
index f7aeff2..406eed8 100644
--- a/test_natsort/test_utils.py
+++ b/test_natsort/test_utils.py
@@ -5,6 +5,7 @@ from __future__ import unicode_literals
import pathlib
import pytest
import string
+from itertools import chain
from operator import neg as op_neg
from pytest import raises
from natsort.ns_enum import ns
@@ -20,6 +21,7 @@ from natsort.utils import (
_int_sign_re,
_do_decoding,
_path_splitter,
+ _groupletters,
chain_functions,
)
from natsort.compat.py23 import py23_str
@@ -28,6 +30,7 @@ from slow_splitters import (
sep_inserter,
add_leading_space_if_first_is_num,
)
+from compat.locale import low
from compat.hypothesis import (
assume,
given,
@@ -168,6 +171,17 @@ def test_chain_functions_combines_functions_in_given_order():
# Each test has an "example" version for demonstrative purposes,
# and a test that uses the hypothesis module.
+def test_groupletters_returns_letters_with_lowercase_transform_of_letter_example():
+ assert _groupletters('HELLO') == 'hHeElLlLoO'
+ assert _groupletters('hello') == 'hheelllloo'
+
+
+@pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater')
+@given(text())
+def test_groupeletters_returns_letters_with_lowercase_transform_of_letter(x):
+ assume(bool(x))
+ assert _groupletters(x) == ''.join(chain.from_iterable([low(y), y] for y in x))
+
def test_sep_inserter_does_nothing_if_no_numbers_example():
assert list(_sep_inserter(iter(['a', 'b', 'c']), '')) == ['a', 'b', 'c']