summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeth M Morton <seth.m.morton@gmail.com>2018-08-25 19:36:44 -0700
committerSeth M Morton <seth.m.morton@gmail.com>2018-08-25 19:36:44 -0700
commite01c6cbc935640b7c6ed15edd12036a7ecc57759 (patch)
tree2c3b478ba4693f1086f8fcd0af91f5fb10391db8
parent5f2341d74be83e8c8d1a71aee7a4ea6299c49fdf (diff)
downloadnatsort-e01c6cbc935640b7c6ed15edd12036a7ecc57759.tar.gz
Refactor test_natsorted.py.
This refactor uses parametrize wherever possible, and also places expected output into an "expected" variable for clarity.
-rw-r--r--test_natsort/test_natsorted.py606
1 files changed, 189 insertions, 417 deletions
diff --git a/test_natsort/test_natsorted.py b/test_natsort/test_natsorted.py
index 382403f..140e254 100644
--- a/test_natsort/test_natsorted.py
+++ b/test_natsort/test_natsorted.py
@@ -5,484 +5,256 @@ See the README or the natsort homepage for more details.
"""
from __future__ import print_function, unicode_literals
-import locale
from operator import itemgetter
import pytest
-from natsort import natsorted, ns
+from natsort import natsorted, ns, as_utf8
from natsort.compat.py23 import PY_VERSION
from pytest import raises
+DEFAULT = 0
-def test_natsorted_returns_strings_with_numbers_in_ascending_order():
- a = ["a2", "a5", "a9", "a1", "a4", "a10", "a6"]
- assert natsorted(a) == ["a1", "a2", "a4", "a5", "a6", "a9", "a10"]
+@pytest.fixture
+def float_list():
+ return ["a50", "a51.", "a50.31", "a-50", "a50.4", "a5.034e1", "a50.300"]
-def test_natsorted_returns_list_of_numbers_sorted_as_signed_floats_with_exponents():
- a = ["a50", "a51.", "a50.31", "a-50", "a50.4", "a5.034e1", "a50.300"]
- assert natsorted(a, alg=ns.REAL) == [
- "a-50",
- "a50",
- "a50.300",
- "a50.31",
- "a5.034e1",
- "a50.4",
- "a51.",
- ]
-
-
-def test_natsorted_returns_list_of_numbers_sorted_as_unsigned_floats_without_exponents_with_NOEXP_option():
- a = ["a50", "a51.", "a50.31", "a-50", "a50.4", "a5.034e1", "a50.300"]
- assert natsorted(a, alg=ns.N | ns.F | ns.U) == [
- "a5.034e1",
- "a50",
- "a50.300",
- "a50.31",
- "a50.4",
- "a51.",
- "a-50",
- ]
- # UNSIGNED is default
- assert natsorted(a, alg=ns.NOEXP | ns.FLOAT) == [
- "a5.034e1",
- "a50",
- "a50.300",
- "a50.31",
- "a50.4",
- "a51.",
- "a-50",
- ]
-
-def test_natsorted_returns_list_of_numbers_sorted_as_unsigned_ints_with_INT_option():
- a = ["a50", "a51.", "a50.31", "a-50", "a50.4", "a5.034e1", "a50.300"]
- assert natsorted(a, alg=ns.INT) == [
- "a5.034e1",
- "a50",
- "a50.4",
- "a50.31",
- "a50.300",
- "a51.",
- "a-50",
- ]
- # INT is default
- assert natsorted(a) == [
- "a5.034e1",
- "a50",
- "a50.4",
- "a50.31",
- "a50.300",
- "a51.",
- "a-50",
- ]
+@pytest.fixture
+def fruit_list():
+ return ["Apple", "corn", "Corn", "Banana", "apple", "banana"]
-def test_natsorted_returns_list_of_numbers_sorted_as_unsigned_ints_with_DIGIT_and_VERSION_option():
- a = ["a50", "a51.", "a50.31", "a-50", "a50.4", "a5.034e1", "a50.300"]
- assert natsorted(a, alg=ns.DIGIT) == [
- "a5.034e1",
- "a50",
- "a50.4",
- "a50.31",
- "a50.300",
- "a51.",
- "a-50",
- ]
- assert natsorted(a, alg=ns.VERSION) == [
- "a5.034e1",
- "a50",
- "a50.4",
- "a50.31",
- "a50.300",
- "a51.",
- "a-50",
- ]
+@pytest.fixture
+def mixed_list():
+ return ["Á", "0", "ä", 3, "b", 1.5, "2", "Z"]
-def test_natsorted_returns_list_of_numbers_sorted_as_signed_ints_with_SIGNED_option():
- a = ["a50", "a51.", "a50.31", "a-50", "a50.4", "a5.034e1", "a50.300"]
- assert natsorted(a, alg=ns.SIGNED) == [
- "a-50",
- "a5.034e1",
- "a50",
- "a50.4",
- "a50.31",
- "a50.300",
- "a51.",
- ]
+def test_natsorted_numbers_in_ascending_order():
+ given = ["a2", "a5", "a9", "a1", "a4", "a10", "a6"]
+ expected = ["a1", "a2", "a4", "a5", "a6", "a9", "a10"]
+ assert natsorted(given) == expected
-def test_natsorted_returns_list_of_numbers_sorted_accounting_for_sign_with_SIGNED_option():
- a = ["a-5", "a7", "a+2"]
- assert natsorted(a, alg=ns.SIGNED) == ["a-5", "a+2", "a7"]
+def test_natsorted_can_sort_as_signed_floats_with_exponents(float_list):
+ expected = ["a-50", "a50", "a50.300", "a50.31", "a5.034e1", "a50.4", "a51."]
+ assert natsorted(float_list, alg=ns.REAL) == expected
-def test_natsorted_returns_list_of_numbers_sorted_not_accounting_for_sign_without_SIGNED_option():
- a = ["a-5", "a7", "a+2"]
- assert natsorted(a) == ["a7", "a+2", "a-5"]
-
-
-def test_natsorted_returns_sorted_list_of_version_numbers_by_default_or_with_VERSION_option():
- a = ["1.9.9a", "1.11", "1.9.9b", "1.11.4", "1.10.1"]
- assert natsorted(a) == ["1.9.9a", "1.9.9b", "1.10.1", "1.11", "1.11.4"]
- assert natsorted(a, alg=ns.VERSION) == [
- "1.9.9a",
- "1.9.9b",
- "1.10.1",
- "1.11",
- "1.11.4",
- ]
-
-
-def test_natsorted_returns_sorted_list_with_mixed_type_input_and_does_not_raise_TypeError_on_Python3():
- # You can mix types with natsorted. This can get around the new
- # 'unorderable types' issue with Python 3.
- a = [6, 4.5, "7", "2.5", "a"]
- assert natsorted(a) == ["2.5", 4.5, 6, "7", "a"]
- a = [46, "5a5b2", "af5", "5a5-4"]
- assert natsorted(a) == ["5a5-4", "5a5b2", 46, "af5"]
-
-
-def test_natsorted_with_mixed_input_returns_sorted_results_without_error():
- a = ["0", "Á", "2", "Z"]
- assert natsorted(a) == ["0", "2", "Á", "Z"]
- assert natsorted(a, alg=ns.NUMAFTER) == ["Á", "Z", "0", "2"]
- a = ["2", "ä", "b", 1.5, 3]
- assert natsorted(a) == [1.5, "2", 3, "ä", "b"]
- assert natsorted(a, alg=ns.NUMAFTER) == ["ä", "b", 1.5, "2", 3]
-
-
-def test_natsorted_with_nan_input_returns_sorted_results_with_nan_last_with_NANLAST():
- a = ["25", 5, float("nan"), 1E40]
+@pytest.mark.parametrize(
+ # UNSIGNED is default
+ "alg",
+ [ns.NOEXP | ns.FLOAT | ns.UNSIGNED, ns.NOEXP | ns.FLOAT],
+)
+def test_natsorted_can_sort_as_unsigned_and_ignore_exponents(float_list, alg):
+ expected = ["a5.034e1", "a50", "a50.300", "a50.31", "a50.4", "a51.", "a-50"]
+ assert natsorted(float_list, alg=alg) == expected
+
+
+# INT, DIGIT, and VERSION are all equivalent.
+@pytest.mark.parametrize("alg", [DEFAULT, ns.INT, ns.DIGIT, ns.VERSION])
+def test_natsorted_can_sort_as_unsigned_ints_which_is_default(float_list, alg):
+ expected = ["a5.034e1", "a50", "a50.4", "a50.31", "a50.300", "a51.", "a-50"]
+ assert natsorted(float_list, alg=alg) == expected
+
+
+def test_natsorted_can_sort_as_signed_ints(float_list):
+ expected = ["a-50", "a5.034e1", "a50", "a50.4", "a50.31", "a50.300", "a51."]
+ assert natsorted(float_list, alg=ns.SIGNED) == expected
+
+
+@pytest.mark.parametrize(
+ "alg, expected",
+ [(ns.UNSIGNED, ["a7", "a+2", "a-5"]), (ns.SIGNED, ["a-5", "a+2", "a7"])],
+)
+def test_natsorted_can_sort_with_or_without_accounting_for_sign(alg, expected):
+ given = ["a-5", "a7", "a+2"]
+ assert natsorted(given, alg=alg) == expected
+
+
+@pytest.mark.parametrize("alg", [DEFAULT, ns.VERSION])
+def test_natsorted_can_sort_as_version_numbers(alg):
+ given = ["1.9.9a", "1.11", "1.9.9b", "1.11.4", "1.10.1"]
+ expected = ["1.9.9a", "1.9.9b", "1.10.1", "1.11", "1.11.4"]
+ assert natsorted(given, alg=alg) == expected
+
+
+@pytest.mark.parametrize(
+ "alg, expected",
+ [
+ (DEFAULT, ["0", 1.5, "2", 3, "Á", "Z", "ä", "b"]),
+ (ns.NUMAFTER, ["Á", "Z", "ä", "b", "0", 1.5, "2", 3]),
+ ],
+)
+def test_natsorted_handles_mixed_types(mixed_list, alg, expected):
+ assert natsorted(mixed_list, alg=alg) == expected
+
+
+@pytest.mark.parametrize(
+ "alg, expected, slc",
+ [
+ (DEFAULT, [float("nan"), 5, "25", 1E40], slice(1, None)),
+ (ns.NANLAST, [5, "25", 1E40, float("nan")], slice(None, 3)),
+ ],
+)
+def test_natsorted_handles_nan(alg, expected, slc):
+ given = ["25", 5, float("nan"), 1E40]
# The slice is because NaN != NaN
- assert natsorted(a, alg=ns.NANLAST)[:3] == [5, "25", 1E40, float("nan")][:3]
+ # noinspection PyUnresolvedReferences
+ assert natsorted(given, alg=alg)[slc] == expected[slc]
-def test_natsorted_with_nan_input_returns_sorted_results_with_nan_first_without_NANLAST():
- a = ["25", 5, float("nan"), 1E40]
- # The slice is because NaN != NaN
- assert natsorted(a)[1:] == [float("nan"), 5, "25", 1E40][1:]
+@pytest.mark.skipif(PY_VERSION < 3.0, reason="error is only raised on Python 3")
+def test_natsorted_with_mixed_bytes_and_str_input_raises_type_error():
+ with raises(TypeError, match="bytes"):
+ natsorted(["ä", b"b"])
+ # ...unless you use as_utf (or some other decoder).
+ assert natsorted(["ä", b"b"], key=as_utf8) == ["ä", b"b"]
-def test_natsorted_with_mixed_input_raises_TypeError_if_bytes_type_is_involved_on_Python3():
- if PY_VERSION >= 3:
- with raises(TypeError) as e:
- assert natsorted(["ä", b"b"])
- assert "bytes" in str(e.value)
- else:
- assert True
-
-def test_natsorted_raises_ValueError_for_non_iterable_input():
- with raises(TypeError) as err:
+def test_natsorted_raises_type_error_for_non_iterable_input():
+ with raises(TypeError, match="'int' object is not iterable"):
natsorted(100)
- assert str(err.value) == "'int' object is not iterable"
-def test_natsorted_recursivley_applies_key_to_nested_lists_to_return_sorted_nested_list():
- data = [["a1", "a5"], ["a1", "a40"], ["a10", "a1"], ["a2", "a5"]]
- assert natsorted(data) == [["a1", "a5"], ["a1", "a40"], ["a2", "a5"], ["a10", "a1"]]
+def test_natsorted_recurses_into_nested_lists():
+ given = [["a1", "a5"], ["a1", "a40"], ["a10", "a1"], ["a2", "a5"]]
+ expected = [["a1", "a5"], ["a1", "a40"], ["a2", "a5"], ["a10", "a1"]]
+ assert natsorted(given) == expected
def test_natsorted_applies_key_to_each_list_element_before_sorting_list():
- b = [("a", "num3"), ("b", "num5"), ("c", "num2")]
- assert natsorted(b, key=itemgetter(1)) == [
- ("c", "num2"),
- ("a", "num3"),
- ("b", "num5"),
- ]
+ given = [("a", "num3"), ("b", "num5"), ("c", "num2")]
+ expected = [("c", "num2"), ("a", "num3"), ("b", "num5")]
+ assert natsorted(given, key=itemgetter(1)) == expected
-def test_natsorted_returns_list_in_reversed_order_with_reverse_option():
- a = ["a50", "a51.", "a50.31", "a50.4", "a5.034e1", "a50.300"]
- assert natsorted(a, reverse=True) == natsorted(a)[::-1]
+def test_natsorted_returns_list_in_reversed_order_with_reverse_option(float_list):
+ expected = natsorted(float_list)[::-1]
+ assert natsorted(float_list, reverse=True) == expected
-def test_natsorted_sorts_OS_generated_paths_incorrectly_without_PATH_option():
- a = [
+def test_natsorted_handles_filesystem_paths():
+ given = [
"/p/Folder (10)/file.tar.gz",
"/p/Folder/file.tar.gz",
"/p/Folder (1)/file (1).tar.gz",
"/p/Folder (1)/file.tar.gz",
]
- assert natsorted(a) == [
- "/p/Folder (1)/file (1).tar.gz",
+ expected_correct = [
+ "/p/Folder/file.tar.gz",
"/p/Folder (1)/file.tar.gz",
+ "/p/Folder (1)/file (1).tar.gz",
"/p/Folder (10)/file.tar.gz",
- "/p/Folder/file.tar.gz",
]
-
-
-def test_natsorted_sorts_OS_generated_paths_correctly_with_PATH_option():
- a = [
- "/p/Folder (10)/file.tar.gz",
- "/p/Folder/file.tar.gz",
+ expected_incorrect = [
"/p/Folder (1)/file (1).tar.gz",
"/p/Folder (1)/file.tar.gz",
- ]
- assert natsorted(a, alg=ns.PATH) == [
- "/p/Folder/file.tar.gz",
- "/p/Folder (1)/file.tar.gz",
- "/p/Folder (1)/file (1).tar.gz",
"/p/Folder (10)/file.tar.gz",
+ "/p/Folder/file.tar.gz",
]
+ # Is incorrect by default.
+ assert natsorted(given) == expected_incorrect
+ # Need ns.PATH to make it correct.
+ assert natsorted(given, alg=ns.PATH) == expected_correct
-def test_natsorted_can_handle_sorting_paths_and_numbers_with_PATH():
+def test_natsorted_handles_numbers_and_filesystem_paths_simultaneously():
# You can sort paths and numbers, not that you'd want to
- a = ["/Folder (9)/file.exe", 43]
- assert natsorted(a, alg=ns.PATH) == [43, "/Folder (9)/file.exe"]
-
-
-def test_natsorted_returns_results_in_ASCII_order_with_no_case_options():
- a = ["Apple", "corn", "Corn", "Banana", "apple", "banana"]
- assert natsorted(a) == ["Apple", "Banana", "Corn", "apple", "banana", "corn"]
-
-
-def test_natsorted_returns_results_sorted_by_lowercase_ASCII_order_with_IGNORECASE():
- a = ["Apple", "corn", "Corn", "Banana", "apple", "banana"]
- assert natsorted(a, alg=ns.IGNORECASE) == [
- "Apple",
- "apple",
- "Banana",
- "banana",
- "corn",
- "Corn",
- ]
-
-
-def test_natsorted_returns_results_in_ASCII_order_but_with_lowercase_letters_first_with_LOWERCASEFIRST():
- a = ["Apple", "corn", "Corn", "Banana", "apple", "banana"]
- assert natsorted(a, alg=ns.LOWERCASEFIRST) == [
- "apple",
- "banana",
- "corn",
- "Apple",
- "Banana",
- "Corn",
- ]
-
-
-def test_natsorted_returns_results_with_uppercase_and_lowercase_letters_grouped_together_with_GROUPLETTERS():
- a = ["Apple", "corn", "Corn", "Banana", "apple", "banana"]
- assert natsorted(a, alg=ns.GROUPLETTERS) == [
- "Apple",
- "apple",
- "Banana",
- "banana",
- "Corn",
- "corn",
- ]
-
-
-def test_natsorted_returns_results_in_natural_order_with_GROUPLETTERS_and_LOWERCASEFIRST():
- a = ["Apple", "corn", "Corn", "Banana", "apple", "banana"]
- assert natsorted(a, alg=ns.G | ns.LF) == [
- "apple",
- "Apple",
- "banana",
- "Banana",
- "corn",
- "Corn",
- ]
-
-
-def test_natsorted_places_uppercase_letters_before_lowercase_letters_for_nested_input():
- b = [("A5", "a6"), ("a3", "a1")]
- assert natsorted(b) == [("A5", "a6"), ("a3", "a1")]
-
-
-def test_natsorted_with_LOWERCASEFIRST_places_lowercase_letters_before_uppercase_letters_for_nested_input():
- b = [("A5", "a6"), ("a3", "a1")]
- assert natsorted(b, alg=ns.LOWERCASEFIRST) == [("a3", "a1"), ("A5", "a6")]
-
-
-def test_natsorted_with_IGNORECASE_sorts_without_regard_to_case_for_nested_input():
- b = [("A5", "a6"), ("a3", "a1")]
- assert natsorted(b, alg=ns.IGNORECASE) == [("a3", "a1"), ("A5", "a6")]
-
-
-@pytest.mark.usefixtures("with_locale_en_us")
-def test_natsorted_with_LOCALE_returns_results_sorted_by_lowercase_first_and_grouped_letters():
- a = ["Apple", "corn", "Corn", "Banana", "apple", "banana"]
- assert natsorted(a, alg=ns.LOCALE) == [
- "apple",
- "Apple",
- "banana",
- "Banana",
- "corn",
- "Corn",
- ]
- locale.setlocale(locale.LC_ALL, str(""))
-
-
-@pytest.mark.usefixtures("with_locale_en_us")
-def test_natsorted_with_LOCALE_and_CAPITALFIRST_returns_results_sorted_by_capital_first_and_ungrouped():
- a = ["Apple", "corn", "Corn", "Banana", "apple", "banana"]
- assert natsorted(a, alg=ns.LOCALE | ns.CAPITALFIRST) == [
- "Apple",
- "Banana",
- "Corn",
- "apple",
- "banana",
- "corn",
- ]
- locale.setlocale(locale.LC_ALL, str(""))
-
-
+ given = ["/Folder (9)/file.exe", 43]
+ expected = [43, "/Folder (9)/file.exe"]
+ assert natsorted(given, alg=ns.PATH) == expected
+
+
+@pytest.mark.parametrize(
+ "alg, expected",
+ [
+ (DEFAULT, ["Apple", "Banana", "Corn", "apple", "banana", "corn"]),
+ (ns.IGNORECASE, ["Apple", "apple", "Banana", "banana", "corn", "Corn"]),
+ (ns.LOWERCASEFIRST, ["apple", "banana", "corn", "Apple", "Banana", "Corn"]),
+ (ns.GROUPLETTERS, ["Apple", "apple", "Banana", "banana", "Corn", "corn"]),
+ (ns.G | ns.LF, ["apple", "Apple", "banana", "Banana", "corn", "Corn"]),
+ ],
+)
+def test_natsorted_supports_case_handling(alg, expected, fruit_list):
+ assert natsorted(fruit_list, alg=alg) == expected
+
+
+@pytest.mark.parametrize(
+ "alg, expected",
+ [
+ (DEFAULT, [("A5", "a6"), ("a3", "a1")]),
+ (ns.LOWERCASEFIRST, [("a3", "a1"), ("A5", "a6")]),
+ (ns.IGNORECASE, [("a3", "a1"), ("A5", "a6")]),
+ ],
+)
+def test_natsorted_supports_nested_case_handling(alg, expected):
+ given = [("A5", "a6"), ("a3", "a1")]
+ assert natsorted(given, alg=alg) == expected
+
+
+@pytest.mark.parametrize(
+ "alg, expected",
+ [
+ (DEFAULT, ["apple", "Apple", "banana", "Banana", "corn", "Corn"]),
+ (ns.CAPITALFIRST, ["Apple", "Banana", "Corn", "apple", "banana", "corn"]),
+ (ns.LOWERCASEFIRST, ["Apple", "apple", "Banana", "banana", "Corn", "corn"]),
+ (ns.C | ns.LF, ["apple", "banana", "corn", "Apple", "Banana", "Corn"]),
+ ],
+)
@pytest.mark.usefixtures("with_locale_en_us")
-def test_natsorted_with_LOCALE_and_LOWERCASEFIRST_returns_results_sorted_by_uppercase_first_and_grouped_letters():
- a = ["Apple", "corn", "Corn", "Banana", "apple", "banana"]
- assert natsorted(a, alg=ns.LOCALE | ns.LOWERCASEFIRST) == [
- "Apple",
- "apple",
- "Banana",
- "banana",
- "Corn",
- "corn",
- ]
- locale.setlocale(locale.LC_ALL, str(""))
+def test_natsorted_can_sort_using_locale(fruit_list, alg, expected):
+ assert natsorted(fruit_list, alg=ns.LOCALE | alg) == expected
@pytest.mark.usefixtures("with_locale_en_us")
-def test_natsorted_with_LOCALE_and_CAPITALFIRST_and_LOWERCASE_returns_results_sorted_by_capital_last_and_ungrouped():
- a = ["Apple", "corn", "Corn", "Banana", "apple", "banana"]
- assert natsorted(a, alg=ns.LOCALE | ns.CAPITALFIRST | ns.LOWERCASEFIRST) == [
- "apple",
- "banana",
- "corn",
- "Apple",
- "Banana",
- "Corn",
- ]
- locale.setlocale(locale.LC_ALL, str(""))
-
-
-@pytest.mark.usefixtures("with_locale_en_us")
-def test_natsorted_with_LOCALE_and_en_setting_returns_results_sorted_by_en_language():
- a = ["c", "a5,467.86", "ä", "b", "a5367.86", "a5,6", "a5,50"]
- assert natsorted(a, alg=ns.LOCALE | ns.F) == [
- "a5,6",
- "a5,50",
- "a5367.86",
- "a5,467.86",
- "ä",
- "b",
- "c",
- ]
- locale.setlocale(locale.LC_ALL, str(""))
+def test_natsorted_can_sort_locale_specific_numbers_en():
+ given = ["c", "a5,467.86", "ä", "b", "a5367.86", "a5,6", "a5,50"]
+ expected = ["a5,6", "a5,50", "a5367.86", "a5,467.86", "ä", "b", "c"]
+ assert natsorted(given, alg=ns.LOCALE | ns.F) == expected
@pytest.mark.usefixtures("with_locale_de_de")
-def test_natsorted_with_LOCALE_and_de_setting_returns_results_sorted_by_de_language():
- a = ["c", "a5.467,86", "ä", "b", "a5367.86", "a5,6", "a5,50"]
- assert natsorted(a, alg=ns.LOCALE | ns.F) == [
- "a5,50",
- "a5,6",
- "a5367.86",
- "a5.467,86",
- "ä",
- "b",
- "c",
- ]
- locale.setlocale(locale.LC_ALL, str(""))
-
-
-@pytest.mark.usefixtures("with_locale_en_us")
-def test_natsorted_with_LOCALE_and_mixed_input_returns_sorted_results_without_error():
- a = ["0", "Á", "2", "Z"]
- assert natsorted(a, alg=ns.LOCALE) == ["0", "2", "Á", "Z"]
- assert natsorted(a, alg=ns.LOCALE | ns.NUMAFTER) == ["Á", "Z", "0", "2"]
- a = ["2", "ä", "b", 1.5, 3]
- assert natsorted(a, alg=ns.LOCALE) == [1.5, "2", 3, "ä", "b"]
- assert natsorted(a, alg=ns.LOCALE | ns.NUMAFTER) == ["ä", "b", 1.5, "2", 3]
- locale.setlocale(locale.LC_ALL, str(""))
-
-
+def test_natsorted_can_sort_locale_specific_numbers_de():
+ given = ["c", "a5.467,86", "ä", "b", "a5367.86", "a5,6", "a5,50"]
+ expected = ["a5,50", "a5,6", "a5367.86", "a5.467,86", "ä", "b", "c"]
+ assert natsorted(given, alg=ns.LOCALE | ns.F) == expected
+
+
+@pytest.mark.parametrize(
+ "alg, expected",
+ [
+ (DEFAULT, ["0", 1.5, "2", 3, "ä", "Á", "b", "Z"]),
+ (ns.NUMAFTER, ["ä", "Á", "b", "Z", "0", 1.5, "2", 3]),
+ (ns.UNGROUPLETTERS, ["0", 1.5, "2", 3, "Á", "Z", "ä", "b"]),
+ (ns.UG | ns.NA, ["Á", "Z", "ä", "b", "0", 1.5, "2", 3]),
+ # Adding PATH changes nothing.
+ (ns.PATH, ["0", 1.5, "2", 3, "ä", "Á", "b", "Z"]),
+ (ns.PATH | ns.NUMAFTER, ["ä", "Á", "b", "Z", "0", 1.5, "2", 3]),
+ (ns.PATH | ns.UNGROUPLETTERS, ["0", 1.5, "2", 3, "Á", "Z", "ä", "b"]),
+ (ns.PATH | ns.UG | ns.NA, ["Á", "Z", "ä", "b", "0", 1.5, "2", 3]),
+ ],
+)
@pytest.mark.usefixtures("with_locale_en_us")
-def test_natsorted_with_LOCALE_and_UNGROUPLETTERS_and_mixed_input_returns_sorted_results_without_error():
- a = ["0", "Á", "2", "Z"]
- assert natsorted(a, alg=ns.LOCALE | ns.UNGROUPLETTERS) == ["0", "2", "Á", "Z"]
- assert natsorted(a, alg=ns.LOCALE | ns.UNGROUPLETTERS | ns.NUMAFTER) == [
- "Á",
- "Z",
- "0",
- "2",
- ]
- a = ["2", "ä", "b", 1.5, 3]
- assert natsorted(a, alg=ns.LOCALE | ns.UNGROUPLETTERS) == [1.5, "2", 3, "ä", "b"]
- assert natsorted(a, alg=ns.LOCALE | ns.UNGROUPLETTERS | ns.NUMAFTER) == [
- "ä",
- "b",
- 1.5,
- "2",
- 3,
- ]
- locale.setlocale(locale.LC_ALL, str(""))
+def test_natsorted_handles_mixed_types_with_locale(mixed_list, alg, expected):
+ assert natsorted(mixed_list, alg=ns.LOCALE | alg) == expected
-@pytest.mark.usefixtures("with_locale_en_us")
-def test_natsorted_with_PATH_and_LOCALE_and_UNGROUPLETTERS_and_mixed_input_returns_sorted_results_without_error():
- a = ["0", "Á", "2", "Z"]
- assert natsorted(a, alg=ns.PATH | ns.LOCALE | ns.UNGROUPLETTERS) == [
- "0",
- "2",
- "Á",
- "Z",
- ]
- assert natsorted(a, alg=ns.PATH | ns.LOCALE | ns.UNGROUPLETTERS | ns.NUMAFTER) == [
- "Á",
- "Z",
- "0",
- "2",
- ]
- a = ["2", "ä", "b", 1.5, 3]
- assert natsorted(a, alg=ns.PATH | ns.LOCALE | ns.UNGROUPLETTERS) == [
- 1.5,
- "2",
- 3,
- "ä",
- "b",
- ]
- assert natsorted(a, alg=ns.PATH | ns.LOCALE | ns.UNGROUPLETTERS | ns.NUMAFTER) == [
- "ä",
- "b",
- 1.5,
- "2",
- 3,
- ]
- locale.setlocale(locale.LC_ALL, str(""))
-
-
-def test_natsorted_sorts_an_odd_collection_of_string():
- a = ["Corn", "apple", "Banana", "73", "Apple", "5039", "corn", "~~~~~~", "banana"]
- assert natsorted(a) == [
- "73",
- "5039",
- "Apple",
- "Banana",
- "Corn",
- "apple",
- "banana",
- "corn",
- "~~~~~~",
- ]
- assert natsorted(a, alg=ns.NUMAFTER) == [
- "Apple",
- "Banana",
- "Corn",
- "apple",
- "banana",
- "corn",
- "~~~~~~",
- "73",
- "5039",
- ]
+@pytest.mark.parametrize(
+ "alg, expected",
+ [
+ (DEFAULT, ["73", "5039", "Banana", "apple", "corn", "~~~~~~"]),
+ (ns.NUMAFTER, ["Banana", "apple", "corn", "~~~~~~", "73", "5039"]),
+ ],
+)
+def test_natsorted_sorts_an_odd_collection_of_strings(alg, expected):
+ given = ["apple", "Banana", "73", "5039", "corn", "~~~~~~"]
+ assert natsorted(given, alg=alg) == expected
def test_natsorted_sorts_mixed_ascii_and_non_ascii_numbers():
- a = [
+ given = [
"1st street",
"10th street",
"2nd street",
@@ -526,4 +298,4 @@ def test_natsorted_sorts_mixed_ascii_and_non_ascii_numbers():
"street ۱۱",
"street ۱۲",
]
- assert natsorted(a, alg=ns.IGNORECASE) == expected
+ assert natsorted(given, alg=ns.IGNORECASE) == expected