diff options
| author | Seth M Morton <seth.m.morton@gmail.com> | 2016-05-03 20:16:18 -0700 |
|---|---|---|
| committer | Seth M Morton <seth.m.morton@gmail.com> | 2016-05-03 20:16:18 -0700 |
| commit | 9c1fdfe799380545e4091238c601da0299fc4383 (patch) | |
| tree | 05eb9e24ddc68ee5085bbf896d288b16208acd9b | |
| parent | 851b8c16d62f22e781a9272d9295b92c4b94d91b (diff) | |
| download | natsort-9c1fdfe799380545e4091238c601da0299fc4383.tar.gz | |
Added tests for natsort_keygen.
These tests are not entirely thorough, but they demonstrate the
function well enough. The rest of the testing is good.
| -rw-r--r-- | natsort/natsort.py | 9 | ||||
| -rw-r--r-- | setup.cfg | 1 | ||||
| -rw-r--r-- | test_natsort/test_natsort_keygen.py | 51 |
3 files changed, 53 insertions, 8 deletions
diff --git a/natsort/natsort.py b/natsort/natsort.py index df2f32a..7239b2f 100644 --- a/natsort/natsort.py +++ b/natsort/natsort.py @@ -23,15 +23,12 @@ from functools import partial from warnings import warn # Local imports. +import natsort.compat.locale from natsort.ns_enum import ns from natsort.compat.py23 import ( u_format, py23_str, ) -from natsort.compat.locale import ( - null_string, - dumb_sort, -) from natsort.utils import ( _natsort_key, _args_to_enum, @@ -205,11 +202,11 @@ def natsort_keygen(key=None, alg=0, **_kwargs): raise ValueError(msg+', got {0}'.format(py23_str(alg))) # Add the _DUMB option if the locale library is broken. - if alg & ns.LOCALE and dumb_sort(): + if alg & ns.LOCALE and natsort.compat.locale.dumb_sort(): alg |= ns._DUMB # Set some variable that will be passed to the factory functions - sep = null_string if alg & ns.LOCALE else '' + sep = natsort.compat.locale.null_string if alg & ns.LOCALE else '' regex = _regex_chooser[alg & ns._NUMERIC_ONLY] # Create the functions that will be used to split strings. @@ -17,6 +17,7 @@ flakes-ignore = pep8ignore = natsort/ns_enum.py E126 E241 E123 E221 test_natsort/test_*.py E501 E241 E221 + test_natsort/test_natsort_keygen.py E501 E241 E221 E701 test_natsort/profile_natsorted.py ALL docs/source/conf.py ALL diff --git a/test_natsort/test_natsort_keygen.py b/test_natsort/test_natsort_keygen.py index 7073851..b7be41e 100644 --- a/test_natsort/test_natsort_keygen.py +++ b/test_natsort/test_natsort_keygen.py @@ -4,6 +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 import warnings from pytest import raises from natsort import ( @@ -12,11 +14,15 @@ from natsort import ( natsort_keygen, ns, ) -# from natsort.utils import _natsort_key +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] def test_natsort_key_public_raises_DeprecationWarning_when_called(): - # Identical to _natsort_key # But it raises a deprecation warning with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") @@ -49,3 +55,44 @@ def test_natsort_keygen_returns_key_that_can_be_used_to_sort_list_in_place_with_ b = a[:] a.sort(key=natsort_keygen(alg=ns.F)) assert a == natsorted(b, alg=ns.F) + + +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',) + + +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',) + + +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',) + + +def test_natsort_keygen_splits_input_with_locale(): + strxfrm = get_strxfrm() + with patch('natsort.compat.locale.dumb_sort', return_value=False): + 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',) + + +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,), (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',) + + +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',),) + + +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',) |
