summaryrefslogtreecommitdiff
path: root/tests/test_natsorted.py
blob: bd1cc391671805757602f04073fcb776c6b201d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# -*- coding: utf-8 -*-
"""\
Here are a collection of examples of how this module can be used.
See the README or the natsort homepage for more details.
"""

import math
from operator import itemgetter
from pathlib import PurePosixPath
from typing import List, Tuple, Union

import pytest
from natsort import as_utf8, natsorted, ns
from natsort.ns_enum import NSType
from pytest import raises


@pytest.fixture
def float_list() -> List[str]:
    return ["a50", "a51.", "a50.31", "a-50", "a50.4", "a5.034e1", "a50.300"]


@pytest.fixture
def fruit_list() -> List[str]:
    return ["Apple", "corn", "Corn", "Banana", "apple", "banana"]


@pytest.fixture
def mixed_list() -> List[Union[str, int, float]]:
    return ["Ä", "0", "ä", 3, "b", 1.5, "2", "Z"]


def test_natsorted_numbers_in_ascending_order() -> None:
    given = ["a2", "a5", "a9", "a1", "a4", "a10", "a6"]
    expected = ["a1", "a2", "a4", "a5", "a6", "a9", "a10"]
    assert natsorted(given) == expected


def test_natsorted_can_sort_as_signed_floats_with_exponents(
    float_list: List[str],
) -> None:
    expected = ["a-50", "a50", "a50.300", "a50.31", "a5.034e1", "a50.4", "a51."]
    assert natsorted(float_list, alg=ns.REAL) == expected


@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: List[str], alg: NSType
) -> None:
    expected = ["a5.034e1", "a50", "a50.300", "a50.31", "a50.4", "a51.", "a-50"]
    assert natsorted(float_list, alg=alg) == expected


# DEFAULT and INT are all equivalent.
@pytest.mark.parametrize("alg", [ns.DEFAULT, ns.INT])
def test_natsorted_can_sort_as_unsigned_ints_which_is_default(
    float_list: List[str], alg: NSType
) -> None:
    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: List[str]) -> None:
    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: NSType, expected: List[str]
) -> None:
    given = ["a-5", "a7", "a+2"]
    assert natsorted(given, alg=alg) == expected


def test_natsorted_can_sort_as_version_numbers() -> None:
    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) == expected


def test_natsorted_can_sorts_paths_same_as_strings() -> None:
    paths = [
        PurePosixPath("a/1/something"),
        PurePosixPath("a/2/something"),
        PurePosixPath("a/10/something"),
    ]
    assert [str(p) for p in natsorted(paths)] == natsorted([str(p) for p in paths])


@pytest.mark.parametrize(
    "alg, expected",
    [
        (ns.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: List[Union[str, int, float]],
    alg: NSType,
    expected: List[Union[str, int, float]],
) -> None:
    assert natsorted(mixed_list, alg=alg) == expected


@pytest.mark.parametrize(
    "alg, expected",
    [
        (ns.DEFAULT, [float("nan"), None, float("-inf"), 5, "25", 1e40, float("inf")]),
        (ns.NANLAST, [float("-inf"), 5, "25", 1e40, float("inf"), None, float("nan")]),
    ],
)
def test_natsorted_consistent_ordering_with_nan_and_friends(
    alg: NSType, expected: List[Union[str, float, None, int]]
) -> None:
    sentinel = math.pi
    expected = [sentinel if x != x else x for x in expected]
    given: List[Union[str, float, None, int]] = [
        float("inf"),
        float("-inf"),
        "25",
        5,
        float("nan"),
        1e40,
        None,
    ]
    result = natsorted(given, alg=alg)
    result = [sentinel if x != x else x for x in result]
    assert result == expected


def test_natsorted_with_mixed_bytes_and_str_input_raises_type_error() -> None:
    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_raises_type_error_for_non_iterable_input() -> None:
    with raises(TypeError, match="'int' object is not iterable"):
        natsorted(100)  # type: ignore


def test_natsorted_recurses_into_nested_lists() -> None:
    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() -> None:
    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(
    float_list: List[str],
) -> None:
    expected = natsorted(float_list)[::-1]
    assert natsorted(float_list, reverse=True) == expected


def test_natsorted_handles_filesystem_paths() -> None:
    given = [
        "/p/Folder (10)/file.tar.gz",
        "/p/Folder (1)/file (1).tar.gz",
        "/p/Folder/file.x1.9.tar.gz",
        "/p/Folder (1)/file.tar.gz",
        "/p/Folder/file.x1.10.tar.gz",
    ]
    expected_correct = [
        "/p/Folder/file.x1.10.tar.gz",
        "/p/Folder/file.x1.9.tar.gz",
        "/p/Folder (1)/file.tar.gz",
        "/p/Folder (1)/file (1).tar.gz",
        "/p/Folder (10)/file.tar.gz",
    ]
    expected_incorrect = [
        "/p/Folder (1)/file (1).tar.gz",
        "/p/Folder (1)/file.tar.gz",
        "/p/Folder (10)/file.tar.gz",
        "/p/Folder/file.x1.10.tar.gz",
        "/p/Folder/file.x1.9.tar.gz",
    ]
    # Is incorrect by default.
    assert natsorted(given, alg=ns.FLOAT) == expected_incorrect
    # Need ns.PATH to make it correct.
    assert natsorted(given, alg=ns.FLOAT | ns.PATH) == expected_correct


def test_natsorted_handles_numbers_and_filesystem_paths_simultaneously() -> None:
    # You can sort paths and numbers, not that you'd want to
    given: List[Union[str, int]] = ["/Folder (9)/file.exe", 43]
    expected: List[Union[str, int]] = [43, "/Folder (9)/file.exe"]
    assert natsorted(given, alg=ns.PATH) == expected


def test_natsorted_path_extensions_heuristic() -> None:
    # https://github.com/SethMMorton/natsort/issues/145
    given = [
        "Try.Me.Bug - 09 - One.Two.Three.[text].mkv",
        "Try.Me.Bug - 07 - One.Two.5.[text].mkv",
        "Try.Me.Bug - 08 - One.Two.Three[text].mkv",
    ]
    expected = [
        "Try.Me.Bug - 07 - One.Two.5.[text].mkv",
        "Try.Me.Bug - 08 - One.Two.Three[text].mkv",
        "Try.Me.Bug - 09 - One.Two.Three.[text].mkv",
    ]
    assert natsorted(given, alg=ns.PATH) == expected


@pytest.mark.parametrize(
    "alg, expected",
    [
        (ns.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: NSType, expected: List[str], fruit_list: List[str]
) -> None:
    assert natsorted(fruit_list, alg=alg) == expected


@pytest.mark.parametrize(
    "alg, expected",
    [
        (ns.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: NSType, expected: List[Tuple[str, str]]
) -> None:
    given = [("A5", "a6"), ("a3", "a1")]
    assert natsorted(given, alg=alg) == expected


@pytest.mark.parametrize(
    "alg, expected",
    [
        (ns.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_can_sort_using_locale(
    fruit_list: List[str], alg: NSType, expected: List[str]
) -> None:
    assert natsorted(fruit_list, alg=ns.LOCALE | alg) == expected


@pytest.mark.usefixtures("with_locale_en_us")
def test_natsorted_can_sort_locale_specific_numbers_en() -> None:
    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_can_sort_locale_specific_numbers_de() -> None:
    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.usefixtures("with_locale_de_de")
def test_natsorted_locale_bug_regression_test_109() -> None:
    # https://github.com/SethMMorton/natsort/issues/109
    given = ["462166", "461761"]
    expected = ["461761", "462166"]
    assert natsorted(given, alg=ns.LOCALE) == expected


@pytest.mark.usefixtures("with_locale_cs_cz")
def test_natsorted_locale_bug_regression_test_140() -> None:
    # https://github.com/SethMMorton/natsort/issues/140
    given = ["Aš", "Cheb", "Česko", "Cibulov", "Znojmo", "Žilina"]
    expected = ["Aš", "Cibulov", "Česko", "Cheb", "Znojmo", "Žilina"]
    assert natsorted(given, alg=ns.LOCALE) == expected


@pytest.mark.parametrize(
    "alg, expected",
    [
        (ns.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_handles_mixed_types_with_locale(
    mixed_list: List[Union[str, int, float]],
    alg: NSType,
    expected: List[Union[str, int, float]],
) -> None:
    assert natsorted(mixed_list, alg=ns.LOCALE | alg) == expected


@pytest.mark.parametrize(
    "alg, expected",
    [
        (ns.DEFAULT, ["73", "5039", "Banana", "apple", "corn", "~~~~~~"]),
        (ns.NUMAFTER, ["Banana", "apple", "corn", "~~~~~~", "73", "5039"]),
    ],
)
def test_natsorted_sorts_an_odd_collection_of_strings(
    alg: NSType, expected: List[str]
) -> None:
    given = ["apple", "Banana", "73", "5039", "corn", "~~~~~~"]
    assert natsorted(given, alg=alg) == expected


def test_natsorted_sorts_mixed_ascii_and_non_ascii_numbers() -> None:
    given = [
        "1st street",
        "10th street",
        "2nd street",
        "2 street",
        "1 street",
        "1street",
        "11 street",
        "street 2",
        "street 1",
        "Street 11",
        "۲ street",
        "۱ street",
        "۱street",
        "۱۲street",
        "۱۱ street",
        "street ۲",
        "street ۱",
        "street ۱",
        "street ۱۲",
        "street ۱۱",
    ]
    expected = [
        "1 street",
        "۱ street",
        "1st street",
        "1street",
        "۱street",
        "2 street",
        "۲ street",
        "2nd street",
        "10th street",
        "11 street",
        "۱۱ street",
        "۱۲street",
        "street 1",
        "street ۱",
        "street ۱",
        "street 2",
        "street ۲",
        "Street 11",
        "street ۱۱",
        "street ۱۲",
    ]
    assert natsorted(given, alg=ns.IGNORECASE) == expected


def test_natsort_sorts_consistently_with_presort() -> None:
    # Demonstrate the problem:
    # Sorting is order-dependent for values that have different
    # string representations are equiavlent numerically.
    given = ["a01", "a1.4500", "a1", "a1.45"]
    expected = ["a01", "a1", "a1.4500", "a1.45"]
    result = natsorted(given, alg=ns.FLOAT)
    assert result == expected

    given = ["a1", "a1.45", "a01", "a1.4500"]
    expected = ["a1", "a01", "a1.45", "a1.4500"]
    result = natsorted(given, alg=ns.FLOAT)
    assert result == expected

    # The solution - use "presort" which will sort the
    # input by its string representation before sorting
    # with natsorted, which gives consitent results even
    # if the numeric representation is identical
    expected = ["a01", "a1", "a1.45", "a1.4500"]

    given = ["a01", "a1.4500", "a1", "a1.45"]
    result = natsorted(given, alg=ns.FLOAT | ns.PRESORT)
    assert result == expected

    given = ["a1", "a1.45", "a01", "a1.4500"]
    result = natsorted(given, alg=ns.FLOAT | ns.PRESORT)
    assert result == expected