summaryrefslogtreecommitdiff
path: root/tests/test_main.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_main.py')
-rw-r--r--tests/test_main.py66
1 files changed, 33 insertions, 33 deletions
diff --git a/tests/test_main.py b/tests/test_main.py
index da91fdd..d2a3e28 100644
--- a/tests/test_main.py
+++ b/tests/test_main.py
@@ -5,11 +5,13 @@ Test the natsort command-line tool functions.
import re
import sys
+from typing import Any as Any_t, List, Union
import pytest
from hypothesis import given
-from hypothesis.strategies import data, floats, integers, lists
+from hypothesis.strategies import DataObject, data, floats, integers, lists
from natsort.__main__ import (
+ TypedArgs,
check_filters,
keep_entry_range,
keep_entry_value,
@@ -17,9 +19,12 @@ from natsort.__main__ import (
range_check,
sort_and_print_entries,
)
+from pytest_mock import MockerFixture
-def test_main_passes_default_arguments_with_no_command_line_options(mocker):
+def test_main_passes_default_arguments_with_no_command_line_options(
+ mocker: MockerFixture,
+) -> None:
p = mocker.patch("natsort.__main__.sort_and_print_entries")
main("num-2", "num-6", "num-1")
args = p.call_args[0][1]
@@ -34,7 +39,9 @@ def test_main_passes_default_arguments_with_no_command_line_options(mocker):
assert not args.locale
-def test_main_passes_arguments_with_all_command_line_options(mocker):
+def test_main_passes_arguments_with_all_command_line_options(
+ mocker: MockerFixture,
+) -> None:
arguments = ["--paths", "--reverse", "--locale"]
arguments.extend(["--filter", "4", "10"])
arguments.extend(["--reverse-filter", "100", "110"])
@@ -57,21 +64,6 @@ def test_main_passes_arguments_with_all_command_line_options(mocker):
assert args.locale
-class Args:
- """A dummy class to simulate the argparse Namespace object"""
-
- def __init__(self, filt, reverse_filter, exclude, as_path, reverse):
- self.filter = filt
- self.reverse_filter = reverse_filter
- self.exclude = exclude
- self.reverse = reverse
- self.number_type = "float"
- self.signed = True
- self.exp = True
- self.paths = as_path
- self.locale = 0
-
-
mock_print = "__builtin__.print" if sys.version[0] == "2" else "builtins.print"
entries = [
@@ -135,9 +127,11 @@ entries = [
([None, None, False, True, True], reversed([2, 3, 1, 0, 5, 6, 4])),
],
)
-def test_sort_and_print_entries(options, order, mocker):
+def test_sort_and_print_entries(
+ options: List[Any_t], order: List[int], mocker: MockerFixture
+) -> None:
p = mocker.patch(mock_print)
- sort_and_print_entries(entries, Args(*options))
+ sort_and_print_entries(entries, TypedArgs(*options))
e = [mocker.call(entries[i]) for i in order]
p.assert_has_calls(e)
@@ -146,13 +140,15 @@ def test_sort_and_print_entries(options, order, mocker):
# and a test that uses the hypothesis module.
-def test_range_check_returns_range_as_is_but_with_floats_example():
+def test_range_check_returns_range_as_is_but_with_floats_example() -> None:
assert range_check(10, 11) == (10.0, 11.0)
assert range_check(6.4, 30) == (6.4, 30.0)
@given(x=floats(allow_nan=False, min_value=-1e8, max_value=1e8) | integers(), d=data())
-def test_range_check_returns_range_as_is_if_first_is_less_than_second(x, d):
+def test_range_check_returns_range_as_is_if_first_is_less_than_second(
+ x: Union[int, float], d: DataObject
+) -> None:
# Pull data such that the first is less than the second.
if isinstance(x, float):
y = d.draw(floats(min_value=x + 1.0, max_value=1e9, allow_nan=False))
@@ -161,44 +157,48 @@ def test_range_check_returns_range_as_is_if_first_is_less_than_second(x, d):
assert range_check(x, y) == (x, y)
-def test_range_check_raises_value_error_if_second_is_less_than_first_example():
+def test_range_check_raises_value_error_if_second_is_less_than_first_example() -> None:
with pytest.raises(ValueError, match="low >= high"):
range_check(7, 2)
@given(x=floats(allow_nan=False), d=data())
-def test_range_check_raises_value_error_if_second_is_less_than_first(x, d):
+def test_range_check_raises_value_error_if_second_is_less_than_first(
+ x: float, d: DataObject
+) -> None:
# Pull data such that the first is greater than or equal to the second.
y = d.draw(floats(max_value=x, allow_nan=False))
with pytest.raises(ValueError, match="low >= high"):
range_check(x, y)
-def test_check_filters_returns_none_if_filter_evaluates_to_false():
+def test_check_filters_returns_none_if_filter_evaluates_to_false() -> None:
assert check_filters(()) is None
- assert check_filters(False) is None
- assert check_filters(None) is None
-def test_check_filters_returns_input_as_is_if_filter_is_valid_example():
+def test_check_filters_returns_input_as_is_if_filter_is_valid_example() -> None:
assert check_filters([(6, 7)]) == [(6, 7)]
assert check_filters([(6, 7), (2, 8)]) == [(6, 7), (2, 8)]
@given(x=lists(integers(), min_size=1), d=data())
-def test_check_filters_returns_input_as_is_if_filter_is_valid(x, d):
+def test_check_filters_returns_input_as_is_if_filter_is_valid(
+ x: List[int], d: DataObject
+) -> None:
# ensure y is element-wise greater than x
y = [d.draw(integers(min_value=val + 1)) for val in x]
assert check_filters(list(zip(x, y))) == [(i, j) for i, j in zip(x, y)]
-def test_check_filters_raises_value_error_if_filter_is_invalid_example():
+def test_check_filters_raises_value_error_if_filter_is_invalid_example() -> None:
with pytest.raises(ValueError, match="Error in --filter: low >= high"):
check_filters([(7, 2)])
@given(x=lists(integers(), min_size=1), d=data())
-def test_check_filters_raises_value_error_if_filter_is_invalid(x, d):
+def test_check_filters_raises_value_error_if_filter_is_invalid(
+ x: List[int], d: DataObject
+) -> None:
# ensure y is element-wise less than or equal to x
y = [d.draw(integers(max_value=val)) for val in x]
with pytest.raises(ValueError, match="Error in --filter: low >= high"):
@@ -212,11 +212,11 @@ def test_check_filters_raises_value_error_if_filter_is_invalid(x, d):
# 3. No portion is between the bounds => False.
[([0], [100], True), ([1, 88], [20, 90], True), ([1], [20], False)],
)
-def test_keep_entry_range(lows, highs, truth):
+def test_keep_entry_range(lows: List[int], highs: List[int], truth: bool) -> None:
assert keep_entry_range("a56b23c89", lows, highs, int, re.compile(r"\d+")) is truth
# 1. Values not in entry => True. 2. Values in entry => False.
@pytest.mark.parametrize("values, truth", [([100, 45], True), ([23], False)])
-def test_keep_entry_value(values, truth):
+def test_keep_entry_value(values: List[int], truth: bool) -> None:
assert keep_entry_value("a56b23c89", values, int, re.compile(r"\d+")) is truth