diff options
author | Seth M Morton <seth.m.morton@gmail.com> | 2018-08-29 21:27:28 -0700 |
---|---|---|
committer | Seth M Morton <seth.m.morton@gmail.com> | 2018-08-29 21:27:28 -0700 |
commit | 328b5ff7789afe43321e7ffe40fc727d11b54972 (patch) | |
tree | be2717a65409a8e11e44bbe25b3ddabf14bf3350 | |
parent | 49f251b28d5289acf6b93457a4903830542ab613 (diff) | |
download | natsort-328b5ff7789afe43321e7ffe40fc727d11b54972.tar.gz |
Refactor test_string_component_transform_factory.py.
All tests have been consolidated into a single parametrized test. Much
nicer.
Additionally, some updates to the fake_fastnumbers were added as they
were noticed during refactoring.
-rw-r--r-- | natsort/compat/fake_fastnumbers.py | 18 | ||||
-rw-r--r-- | test_natsort/test_string_component_transform_factory.py | 156 |
2 files changed, 53 insertions, 121 deletions
diff --git a/natsort/compat/fake_fastnumbers.py b/natsort/compat/fake_fastnumbers.py index e577f09..c4b8ada 100644 --- a/natsort/compat/fake_fastnumbers.py +++ b/natsort/compat/fake_fastnumbers.py @@ -44,8 +44,8 @@ def fast_float( x, key=lambda x: x, nan=None, - uni=unicodedata.numeric, - nan_inf=NAN_INF, + _uni=unicodedata.numeric, + _nan_inf=NAN_INF, _first_char=frozenset(decimal_chars + list(ASCII_NUMS + ".")), ): """ @@ -68,18 +68,18 @@ def fast_float( *str* or *float* """ - if x[0] in _first_char or x.lstrip()[:3] in nan_inf: + if x[0] in _first_char or x.lstrip()[:3] in _nan_inf: try: x = float(x) return nan if nan is not None and x != x else x except ValueError: try: - return uni(x, key(x)) if len(x) == 1 else key(x) + return _uni(x, key(x)) if len(x) == 1 else key(x) except TypeError: # pragma: no cover return key(x) else: try: - return uni(x, key(x)) if len(x) == 1 else key(x) + return _uni(x, key(x)) if len(x) == 1 else key(x) except TypeError: # pragma: no cover return key(x) @@ -88,8 +88,7 @@ def fast_float( def fast_int( x, key=lambda x: x, - nan=None, - uni=unicodedata.digit, + _uni=unicodedata.digit, _first_char=frozenset(decimal_chars + list(ASCII_NUMS)), ): """ @@ -110,17 +109,16 @@ def fast_int( *str* or *int* """ - del nan # explicitly indicate we are not using the nan argument if x[0] in _first_char: try: return long(x) except ValueError: try: - return uni(x, key(x)) if len(x) == 1 else key(x) + return _uni(x, key(x)) if len(x) == 1 else key(x) except TypeError: # pragma: no cover return key(x) else: try: - return uni(x, key(x)) if len(x) == 1 else key(x) + return _uni(x, key(x)) if len(x) == 1 else key(x) except TypeError: # pragma: no cover return key(x) diff --git a/test_natsort/test_string_component_transform_factory.py b/test_natsort/test_string_component_transform_factory.py index 046b913..73371c4 100644 --- a/test_natsort/test_string_component_transform_factory.py +++ b/test_natsort/test_string_component_transform_factory.py @@ -2,11 +2,14 @@ """These test the utils.py functions.""" from __future__ import unicode_literals -from hypothesis import given +from functools import partial + +import pytest +from hypothesis import example, given from hypothesis.strategies import floats, integers, text from natsort.compat.fastnumbers import fast_float, fast_int from natsort.compat.locale import get_strxfrm -from natsort.compat.py23 import py23_str, py23_unichr, py23_range +from natsort.compat.py23 import py23_range, py23_str, py23_unichr from natsort.ns_enum import ns, ns_DUMB from natsort.utils import groupletters, string_component_transform_factory @@ -20,122 +23,53 @@ except ValueError: bad_uni_chars = set() -def no_null(x): - return "\0" not in x - - -# Each test has an "example" version for demonstrative purposes, -# and a test that uses the hypothesis module. - - -def test_string_component_transform_factory_returns_fast_int_example(): - x = "hello" - assert string_component_transform_factory(0)(x) is fast_int(x) - assert string_component_transform_factory(0)("5007") == fast_int("5007") - - -@given(text().filter(bool) | floats() | integers()) -def test_string_component_transform_factory_returns_fast_int(x): - assert string_component_transform_factory(0)(py23_str(x)) == fast_int(py23_str(x)) - - -def test_string_component_transform_factory_with_FLOAT_returns_fast_float_example(): - x = "hello" - assert string_component_transform_factory(ns.FLOAT)(x) is fast_float(x) - assert string_component_transform_factory(ns.FLOAT)("5007") == fast_float("5007") - - -@given(text().filter(bool) | floats() | integers()) -def test_string_component_transform_factory_with_FLOAT_returns_fast_float(x): - assert string_component_transform_factory(ns.FLOAT)(py23_str(x)) == fast_float( - py23_str(x), nan=float("-inf") - ) - - -def test_string_component_transform_factory_with_FLOAT_returns_fast_float_with_neg_inf_replacing_nan(): - assert string_component_transform_factory(ns.FLOAT)("nan") == fast_float( - "nan", nan=float("-inf") - ) - - -def test_string_component_transform_factory_with_FLOAT_and_NANLAST_returns_fast_float_with_pos_inf_replacing_nan(): - assert string_component_transform_factory(ns.FLOAT | ns.NANLAST)( - "nan" - ) == fast_float("nan", nan=float("+inf")) - +def no_bad_uni_chars(x, _bad_chars=frozenset(bad_uni_chars)): + """Ensure text does not contain bad unicode characters""" + return not any(y in _bad_chars for y in x) -def test_string_component_transform_factory_with_GROUPLETTERS_returns_fast_int_and_groupletters_example(): - x = "hello" - assert string_component_transform_factory(ns.GROUPLETTERS)(x) == fast_int( - x, key=groupletters - ) - -@given(text().filter(bool)) -def test_string_component_transform_factory_with_GROUPLETTERS_returns_fast_int_and_groupletters( - x -): - assert string_component_transform_factory(ns.GROUPLETTERS)(x) == fast_int( - x, key=groupletters - ) - - -def test_string_component_transform_factory_with_LOCALE_returns_fast_int_and_groupletters_example(): - x = "hello" - assert string_component_transform_factory(ns.LOCALE)(x) == fast_int( - x, key=get_strxfrm() - ) +def no_null(x): + """Ensure text does not contain a null character.""" + return "\0" not in x +@pytest.mark.parametrize( + "alg, example_func", + [ + (ns.INT, fast_int), + (ns.DEFAULT, fast_int), + (ns.FLOAT, partial(fast_float, nan=float("-inf"))), + (ns.FLOAT | ns.NANLAST, partial(fast_float, nan=float("+inf"))), + (ns.GROUPLETTERS, partial(fast_int, key=groupletters)), + (ns.LOCALE, partial(fast_int, key=get_strxfrm())), + ( + ns.GROUPLETTERS | ns.LOCALE, + partial(fast_int, key=lambda x: get_strxfrm()(groupletters(x))), + ), + ( + ns_DUMB | ns.LOCALE, + partial(fast_int, key=lambda x: get_strxfrm()(groupletters(x))), + ), + ( + ns.GROUPLETTERS | ns.LOCALE | ns.FLOAT | ns.NANLAST, + partial( + fast_float, + key=lambda x: get_strxfrm()(groupletters(x)), + nan=float("+inf"), + ), + ), + ], +) +@example(x=float("nan")) @given( - text() - .filter(bool) - .filter(lambda x: not any(y in bad_uni_chars for y in x)) - .filter(no_null) + x=integers() + | floats() + | text().filter(bool).filter(no_bad_uni_chars).filter(no_null) ) -def test_string_component_transform_factory_with_LOCALE_returns_fast_int_and_groupletters( - x -): - assert string_component_transform_factory(ns.LOCALE)(x) == fast_int( - x, key=get_strxfrm() - ) - - -def test_string_component_transform_factory_with_LOCALE_and_GROUPLETTERS_returns_fast_int_and_groupletters_and_locale_convert_example(): - x = "hello" - assert string_component_transform_factory(ns.GROUPLETTERS | ns.LOCALE)( - x - ) == fast_int(x, key=lambda x: get_strxfrm()(groupletters(x))) - - -@given(text().filter(bool).filter(no_null)) -def test_string_component_transform_factory_with_LOCALE_and_GROUPLETTERS_returns_fast_int_and_groupletters_and_locale_convert( - x -): - try: - assert string_component_transform_factory(ns.GROUPLETTERS | ns.LOCALE)( - x - ) == fast_int(x, key=lambda x: get_strxfrm()(groupletters(x))) - except ValueError as e: # handle broken locale lib on BSD. - if "is not in range" not in str(e): - raise - - -def test_string_component_transform_factory_with_LOCALE_and_DUMB_returns_fast_int_and_groupletters_and_locale_convert_example(): - x = "hello" - assert string_component_transform_factory(ns_DUMB | ns.LOCALE)(x) == fast_int( - x, key=lambda x: get_strxfrm()(groupletters(x)) - ) - - -@given(text().filter(bool).filter(no_null)) -def test_string_component_transform_factory_with_LOCALE_and_DUMB_returns_fast_int_and_groupletters_and_locale_convert( - x -): +def test_string_component_transform_factory(x, alg, example_func): + string_component_transform_func = string_component_transform_factory(alg) try: - assert string_component_transform_factory(ns_DUMB | ns.LOCALE)(x) == fast_int( - x, key=lambda x: get_strxfrm()(groupletters(x)) - ) + assert string_component_transform_func(py23_str(x)) == example_func(py23_str(x)) except ValueError as e: # handle broken locale lib on BSD. if "is not in range" not in str(e): raise |