summaryrefslogtreecommitdiff
path: root/tests/test_utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_utils.py')
-rw-r--r--tests/test_utils.py46
1 files changed, 27 insertions, 19 deletions
diff --git a/tests/test_utils.py b/tests/test_utils.py
index d559803..ff866b6 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -6,15 +6,16 @@ import pathlib
import string
from itertools import chain
from operator import neg as op_neg
+from typing import List, Pattern, Union
import pytest
from hypothesis import given
from hypothesis.strategies import integers, lists, sampled_from, text
from natsort import utils
-from natsort.ns_enum import ns
+from natsort.ns_enum import NS_t, ns
-def test_do_decoding_decodes_bytes_string_to_unicode():
+def test_do_decoding_decodes_bytes_string_to_unicode() -> None:
assert type(utils.do_decoding(b"bytes", "ascii")) is str
assert utils.do_decoding(b"bytes", "ascii") == "bytes"
assert utils.do_decoding(b"bytes", "ascii") == b"bytes".decode("ascii")
@@ -33,7 +34,9 @@ def test_do_decoding_decodes_bytes_string_to_unicode():
(ns.F | ns.S | ns.N, utils.NumericalRegularExpressions.float_sign_noexp()),
],
)
-def test_regex_chooser_returns_correct_regular_expression_object(alg, expected):
+def test_regex_chooser_returns_correct_regular_expression_object(
+ alg: NS_t, expected: Pattern[str]
+) -> None:
assert utils.regex_chooser(alg).pattern == expected.pattern
@@ -68,21 +71,21 @@ def test_regex_chooser_returns_correct_regular_expression_object(alg, expected):
(ns.REAL, ns.FLOAT | ns.SIGNED),
],
)
-def test_ns_enum_values_and_aliases(alg, value_or_alias):
+def test_ns_enum_values_and_aliases(alg: NS_t, value_or_alias: NS_t) -> None:
assert alg == value_or_alias
-def test_chain_functions_is_a_no_op_if_no_functions_are_given():
+def test_chain_functions_is_a_no_op_if_no_functions_are_given() -> None:
x = 2345
assert utils.chain_functions([])(x) is x
-def test_chain_functions_does_one_function_if_one_function_is_given():
+def test_chain_functions_does_one_function_if_one_function_is_given() -> None:
x = "2345"
assert utils.chain_functions([len])(x) == 4
-def test_chain_functions_combines_functions_in_given_order():
+def test_chain_functions_combines_functions_in_given_order() -> None:
x = 2345
assert utils.chain_functions([str, len, op_neg])(x) == -len(str(x))
@@ -91,33 +94,37 @@ def test_chain_functions_combines_functions_in_given_order():
# and a test that uses the hypothesis module.
-def test_groupletters_returns_letters_with_lowercase_transform_of_letter_example():
+def test_groupletters_returns_letters_with_lowercase_transform_of_letter_example() -> None:
assert utils.groupletters("HELLO") == "hHeElLlLoO"
assert utils.groupletters("hello") == "hheelllloo"
@given(text().filter(bool))
-def test_groupletters_returns_letters_with_lowercase_transform_of_letter(x):
+def test_groupletters_returns_letters_with_lowercase_transform_of_letter(
+ x: str,
+) -> None:
assert utils.groupletters(x) == "".join(
chain.from_iterable([y.casefold(), y] for y in x)
)
-def test_sep_inserter_does_nothing_if_no_numbers_example():
+def test_sep_inserter_does_nothing_if_no_numbers_example() -> None:
assert list(utils.sep_inserter(iter(["a", "b", "c"]), "")) == ["a", "b", "c"]
assert list(utils.sep_inserter(iter(["a"]), "")) == ["a"]
-def test_sep_inserter_does_nothing_if_only_one_number_example():
+def test_sep_inserter_does_nothing_if_only_one_number_example() -> None:
assert list(utils.sep_inserter(iter(["a", 5]), "")) == ["a", 5]
-def test_sep_inserter_inserts_separator_string_between_two_numbers_example():
+def test_sep_inserter_inserts_separator_string_between_two_numbers_example() -> None:
assert list(utils.sep_inserter(iter([5, 9]), "")) == ["", 5, "", 9]
@given(lists(elements=text().filter(bool) | integers(), min_size=3))
-def test_sep_inserter_inserts_separator_between_two_numbers(x):
+def test_sep_inserter_inserts_separator_between_two_numbers(
+ x: List[Union[str, int]]
+) -> None:
# Rather than just replicating the results in a different algorithm,
# validate that the "shape" of the output is as expected.
result = list(utils.sep_inserter(iter(x), ""))
@@ -127,28 +134,29 @@ def test_sep_inserter_inserts_separator_between_two_numbers(x):
assert isinstance(result[i + 1], int)
-def test_path_splitter_splits_path_string_by_separator_example():
+def test_path_splitter_splits_path_string_by_separator_example() -> None:
given = "/this/is/a/path"
expected = (os.sep, "this", "is", "a", "path")
assert tuple(utils.path_splitter(given)) == tuple(expected)
- given = pathlib.Path(given)
- assert tuple(utils.path_splitter(given)) == tuple(expected)
+ assert tuple(utils.path_splitter(pathlib.Path(given))) == tuple(expected)
@given(lists(sampled_from(string.ascii_letters), min_size=2).filter(all))
-def test_path_splitter_splits_path_string_by_separator(x):
+def test_path_splitter_splits_path_string_by_separator(x: List[str]) -> None:
z = str(pathlib.Path(*x))
assert tuple(utils.path_splitter(z)) == tuple(pathlib.Path(z).parts)
-def test_path_splitter_splits_path_string_by_separator_and_removes_extension_example():
+def test_path_splitter_splits_path_string_by_separator_and_removes_extension_example() -> None:
given = "/this/is/a/path/file.x1.10.tar.gz"
expected = (os.sep, "this", "is", "a", "path", "file.x1.10", ".tar", ".gz")
assert tuple(utils.path_splitter(given)) == tuple(expected)
@given(lists(sampled_from(string.ascii_letters), min_size=3).filter(all))
-def test_path_splitter_splits_path_string_by_separator_and_removes_extension(x):
+def test_path_splitter_splits_path_string_by_separator_and_removes_extension(
+ x: List[str],
+) -> None:
z = str(pathlib.Path(*x[:-2])) + "." + x[-1]
y = tuple(pathlib.Path(z).parts)
assert tuple(utils.path_splitter(z)) == y[:-1] + (