summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeth M Morton <seth.m.morton@gmail.com>2018-08-27 22:35:07 -0700
committerSeth M Morton <seth.m.morton@gmail.com>2018-08-27 22:35:07 -0700
commit49f251b28d5289acf6b93457a4903830542ab613 (patch)
treec9066caec9b61e07abd977dab911cda74ef2e878
parentb946046721c5f74638e17a9fa4feb9fa001354f9 (diff)
downloadnatsort-49f251b28d5289acf6b93457a4903830542ab613.tar.gz
Refactor test_input_string_transform_factory.py.
Better use of fixtures and parameterization, less unnecessary hypothesis testing.
-rw-r--r--test_natsort/test_input_string_transform_factory.py253
1 files changed, 74 insertions, 179 deletions
diff --git a/test_natsort/test_input_string_transform_factory.py b/test_natsort/test_input_string_transform_factory.py
index af8b5ca..55e6153 100644
--- a/test_natsort/test_input_string_transform_factory.py
+++ b/test_natsort/test_input_string_transform_factory.py
@@ -2,209 +2,104 @@
"""These test the utils.py functions."""
from __future__ import unicode_literals
-import locale
-from operator import methodcaller
-
import pytest
-from hypothesis import given
-from hypothesis.strategies import integers, lists, text
+from hypothesis import given, example
+from hypothesis.strategies import integers, text
from natsort.compat.py23 import NEWPY
from natsort.ns_enum import ns, ns_DUMB
from natsort.utils import input_string_transform_factory
-# Each test has an "example" version for demonstrative purposes,
-# and a test that uses the hypothesis module.
-
-
-def test_input_string_transform_factory_is_no_op_for_no_alg_options_examples():
- x = "feijGGAd"
- assert input_string_transform_factory(0)(x) is x
-
-@given(text())
-def test_input_string_transform_factory_is_no_op_for_no_alg_options(x):
- assert input_string_transform_factory(0)(x) is x
-
-
-def test_input_string_transform_factory_performs_casefold_with_IGNORECASE_examples():
- x = "feijGGAd"
+def lower(x):
+ """Call the appropriate lower method for the Python version."""
if NEWPY:
- assert input_string_transform_factory(ns.IGNORECASE)(x) == x.casefold()
+ return x.casefold()
else:
- assert input_string_transform_factory(ns.IGNORECASE)(x) == x.lower()
+ return x.lower()
-@given(text())
-def test_input_string_transform_factory_performs_casefold_with_IGNORECASE(x):
- if NEWPY:
- assert input_string_transform_factory(ns.IGNORECASE)(x) == x.casefold()
- else:
- assert input_string_transform_factory(ns.IGNORECASE)(x) == x.lower()
-
-
-def test_input_string_transform_factory_performs_swapcase_with_DUMB_examples():
- x = "feijGGAd"
- assert input_string_transform_factory(ns_DUMB)(x) == x.swapcase()
-
-
-@given(text())
-def test_input_string_transform_factory_performs_swapcase_with_DUMB(x):
- assert input_string_transform_factory(ns_DUMB)(x) == x.swapcase()
-
-
-def test_input_string_transform_factory_performs_swapcase_with_LOWERCASEFIRST_example():
- x = "feijGGAd"
- assert input_string_transform_factory(ns.LOWERCASEFIRST)(x) == x.swapcase()
+def thousands_separated_int(n):
+ """Insert thousands separators in an int."""
+ new_int = ""
+ for i, y in enumerate(reversed(n), 1):
+ new_int = y + new_int
+ # For every third digit, insert a thousands separator.
+ if i % 3 == 0 and i != len(n):
+ new_int = "," + new_int
+ return new_int
@given(text())
-def test_input_string_transform_factory_performs_swapcase_with_LOWERCASEFIRST(x):
- x = "feijGGAd"
- assert input_string_transform_factory(ns.LOWERCASEFIRST)(x) == x.swapcase()
-
-
-def test_input_string_transform_factory_is_no_op_with_both_LOWERCASEFIRST_AND_DUMB_example():
- x = "feijGGAd"
- assert input_string_transform_factory(ns_DUMB | ns.LOWERCASEFIRST)(x) is x
-
-
-@given(text())
-def test_input_string_transform_factory_is_no_op_with_both_LOWERCASEFIRST_AND_DUMB(x):
- assert input_string_transform_factory(ns_DUMB | ns.LOWERCASEFIRST)(x) is x
-
-
-def test_input_string_transform_factory_performs_swapcase_and_casefold_both_LOWERCASEFIRST_AND_IGNORECASE_example():
- x = "feijGGAd"
- if NEWPY:
- assert (
- input_string_transform_factory(ns.IGNORECASE | ns.LOWERCASEFIRST)(x)
- == x.swapcase().casefold()
- )
- else:
- assert (
- input_string_transform_factory(ns.IGNORECASE | ns.LOWERCASEFIRST)(x)
- == x.swapcase().lower()
- )
+def test_input_string_transform_factory_is_no_op_for_no_alg_options(x):
+ input_string_transform_func = input_string_transform_factory(ns.DEFAULT)
+ assert input_string_transform_func(x) is x
+
+
+@pytest.mark.parametrize(
+ "alg, example_func",
+ [
+ (ns.IGNORECASE, lower),
+ (ns_DUMB, lambda x: x.swapcase()),
+ (ns.LOWERCASEFIRST, lambda x: x.swapcase()),
+ (ns_DUMB | ns.LOWERCASEFIRST, lambda x: x), # No-op
+ (ns.IGNORECASE | ns.LOWERCASEFIRST, lambda x: lower(x.swapcase())),
+ ],
+)
+@given(x=text())
+def test_input_string_transform_factory(x, alg, example_func):
+ input_string_transform_func = input_string_transform_factory(alg)
+ assert input_string_transform_func(x) == example_func(x)
-@given(text())
-def test_input_string_transform_factory_performs_swapcase_and_casefold_both_LOWERCASEFIRST_AND_IGNORECASE(
- x
-):
- if NEWPY:
- assert (
- input_string_transform_factory(ns.IGNORECASE | ns.LOWERCASEFIRST)(x)
- == x.swapcase().casefold()
- )
- else:
- assert (
- input_string_transform_factory(ns.IGNORECASE | ns.LOWERCASEFIRST)(x)
- == x.swapcase().lower()
- )
+@example(12543642642534980) # 12,543,642,642,534,980 => 12543642642534980
+@given(x=integers(min_value=1000))
+@pytest.mark.usefixtures("with_locale_en_us")
+def test_input_string_transform_factory_cleans_thousands(x):
+ int_str = str(x).rstrip("lL")
+ thousands_int_str = thousands_separated_int(int_str)
+ assert thousands_int_str.replace(",", "") != thousands_int_str
+ input_string_transform_func = input_string_transform_factory(ns.LOCALE)
+ assert input_string_transform_func(thousands_int_str) == int_str
-@pytest.mark.usefixtures("with_locale_en_us")
-def test_input_string_transform_factory_removes_thousands_separator_with_LOCALE_example():
- x = "12,543,642,642.534,534,980" # Without FLOAT it does not account for decimal.
- assert input_string_transform_factory(ns.LOCALE)(x) == "12543642642.534534980"
- x = (
- "12,543,642,642.534,534,980"
- ) # LOCALEALPHA doesn't do anything... need LOCALENUM
- assert (
- input_string_transform_factory(ns.LOCALEALPHA)(x)
- == "12,543,642,642.534,534,980"
- )
- locale.setlocale(locale.LC_ALL, str(""))
-
-
-@given(lists(elements=integers(), min_size=4, max_size=20))
-@pytest.mark.usefixtures("with_locale_en_us")
-def test_input_string_transform_factory_removes_thousands_separator_with_LOCALE(x):
- t = "".join(
- map(methodcaller("rstrip", "lL"), map(str, map(abs, x)))
- ) # Remove negative signs trailing L
- s = ""
- for i, y in enumerate(reversed(t), 1):
- s = y + s
- if i % 3 == 0 and i != len(t):
- s = "," + s
- assert input_string_transform_factory(ns.LOCALE)(s) == t
- locale.setlocale(locale.LC_ALL, str(""))
-
-
-def test_input_string_transform_factory_removes_thousands_separator_and_is_float_aware_with_LOCALE_and_FLOAT_example():
- x = "12,543,642,642.534,534,980"
- assert (
- input_string_transform_factory(ns.LOCALE | ns.FLOAT)(x)
- == "12543642642.534,534980"
- )
-
-
-@given(
- lists(elements=integers(), min_size=4, max_size=20),
- lists(elements=integers(), min_size=4, max_size=20),
-)
-@pytest.mark.usefixtures("with_locale_en_us")
-def test_input_string_transform_factory_removes_thousands_separator_and_is_float_aware_with_LOCALE_and_FLOAT(
- x, y
-):
- t = "".join(
- map(methodcaller("rstrip", "lL"), map(str, map(abs, x)))
- ) # Remove negative signs trailing L
- s = ""
- for i, z in enumerate(reversed(t), 1):
- s = z + s
- if i % 3 == 0 and i != len(t):
- s = "," + s
- u = "".join(
- map(methodcaller("rstrip", "lL"), map(str, map(abs, y)))
- ) # Remove negative signs trailing L
- v = ""
- for i, z in enumerate(reversed(u), 1):
- v = z + v
- if i % 3 == 0 and i != len(u):
- v = "," + v
- # Remove all but first comma.
- a = v.split(",", 1)
- p = a[0] + "," + a[1].replace(",", "")
- assert input_string_transform_factory(ns.LOCALE)(".".join([s, v])) == ".".join(
- [t, u]
- )
- assert input_string_transform_factory(ns.LOCALE | ns.FLOAT)(
- ".".join([s, v])
- ) == ".".join([t, p])
- locale.setlocale(locale.LC_ALL, str(""))
+ # Using LOCALEALPHA does not affect numbers.
+ input_string_transform_func_no_op = input_string_transform_factory(ns.LOCALEALPHA)
+ assert input_string_transform_func_no_op(thousands_int_str) == thousands_int_str
# These might be too much to test with hypothesis.
+@pytest.mark.parametrize(
+ "x, expected",
+ [
+ ("12,543,642642.5345,34980", "12543,642642.5345,34980"),
+ ("12,59443,642,642.53,4534980", "12,59443,642642.53,4534980"), # No change
+ ("12543,642,642.5,34534980", "12543,642642.5,34534980"),
+ ],
+)
@pytest.mark.usefixtures("with_locale_en_us")
-def test_input_string_transform_factory_leaves_invalid_thousands_separator_with_LOCALE_example():
- x = "12,543,642642.5345,34980"
- assert input_string_transform_factory(ns.LOCALE)(x) == "12543,642642.5345,34980"
- x = "12,59443,642,642.53,4534980"
- assert input_string_transform_factory(ns.LOCALE)(x) == "12,59443,642642.53,4534980"
- x = "12543,642,642.5,34534980"
- assert input_string_transform_factory(ns.LOCALE)(x) == "12543,642642.5,34534980"
- locale.setlocale(locale.LC_ALL, str(""))
-
-
+def test_input_string_transform_factory_handles_us_locale(x, expected):
+ input_string_transform_func = input_string_transform_factory(ns.LOCALE)
+ assert input_string_transform_func(x) == expected
+
+
+@pytest.mark.parametrize(
+ "alg, expected",
+ [
+ (ns.LOCALE, "1543,753"), # Does nothing without FLOAT
+ (ns.LOCALE | ns.FLOAT, "1543.753"),
+ (ns.LOCALEALPHA, "1543,753"), # LOCALEALPHA won't do anything, need LOCALENUM
+ ],
+)
@pytest.mark.usefixtures("with_locale_de_de")
-def test_input_string_transform_factory_replaces_decimal_separator_with_LOCALE_example():
- x = "1543,753"
- assert (
- input_string_transform_factory(ns.LOCALE)(x) == "1543,753"
- ) # Does nothing without FLOAT
- assert input_string_transform_factory(ns.LOCALE | ns.FLOAT)(x) == "1543.753"
- assert (
- input_string_transform_factory(ns.LOCALEALPHA)(x) == "1543,753"
- ) # LOCALEALPHA doesn't do anything... need LOCALENUM
- locale.setlocale(locale.LC_ALL, str(""))
+def test_input_string_transform_factory_handles_german_locale(alg, expected):
+ input_string_transform_func = input_string_transform_factory(alg)
+ assert input_string_transform_func("1543,753") == expected
@pytest.mark.usefixtures("with_locale_de_de")
-def test_input_string_transform_factory_does_not_replace_invalid_decimal_separator_with_LOCALE_example():
- x = "154s,t53"
- assert input_string_transform_factory(ns.LOCALE | ns.FLOAT)(x) == "154s,t53"
- locale.setlocale(locale.LC_ALL, str(""))
+def test_input_string_transform_factory_does_nothing_with_non_num_input():
+ input_string_transform_func = input_string_transform_factory(ns.LOCALE | ns.FLOAT)
+ expected = "154s,t53"
+ assert input_string_transform_func("154s,t53") == expected