summaryrefslogtreecommitdiff
path: root/tests/test_os_sorted.py
blob: f714437fb39a57b6032a12b797a4420014fae54d (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
# -*- coding: utf-8 -*-
"""
Testing for the OS sorting
"""
import platform

import natsort
import pytest

try:
    import icu  # noqa: F401
except ImportError:
    has_icu = False
else:
    has_icu = True


def test_os_sorted_compound() -> None:
    given = [
        "/p/Folder (10)/file.tar.gz",
        "/p/Folder (1)/file (1).tar.gz",
        "/p/Folder/file.x1.9.tar.gz",
        "/p/Folder (2)/file.tar.gz",
        "/p/Folder (1)/file.tar.gz",
        "/p/Folder/file.x1.10.tar.gz",
    ]
    expected = [
        "/p/Folder/file.x1.9.tar.gz",
        "/p/Folder/file.x1.10.tar.gz",
        "/p/Folder (1)/file.tar.gz",
        "/p/Folder (1)/file (1).tar.gz",
        "/p/Folder (2)/file.tar.gz",
        "/p/Folder (10)/file.tar.gz",
    ]
    result = natsort.os_sorted(given)
    assert result == expected


def test_os_sorted_misc_no_fail() -> None:
    natsort.os_sorted([9, 4.3, None, float("nan")])


def test_os_sorted_key() -> None:
    given = ["foo0", "foo2", "goo1"]
    expected = ["foo0", "goo1", "foo2"]
    result = natsort.os_sorted(given, key=lambda x: x.replace("g", "f"))
    assert result == expected


# The following is a master list of things that might give trouble
# when sorting like the file explorer.
given_characters = [
    "11111",
    "aaaaa",
    "foo0",
    "foo_0",
    "foo1",
    "foo2",
    "foo4",
    "foo10",
    "Foo3",
]
given_special = [
    "!",
    "#",
    "$",
    "%",
    "&",
    "'",
    "(",
    ")",
    "+",
    "+11111",
    "+aaaaa",
    ",",
    "-",
    ";",
    "=",
    "@",
    "[",
    "]",
    "^",
    "_",
    "`",
    "{",
    "}",
    "~",
    "§",
    "°",
    "´",
    "µ",
    "€",
]

# The expceted values change based on the environment
if platform.system() == "Windows":
    given = given_characters + given_special
    expected = [
        "'",
        "-",
        "!",
        "#",
        "$",
        "%",
        "&",
        "(",
        ")",
        ",",
        ";",
        "@",
        "[",
        "]",
        "^",
        "_",
        "`",
        "{",
        "}",
        "~",
        "´",
        "€",
        "+",
        "+11111",
        "+aaaaa",
        "=",
        "§",
        "°",
        "µ",
        "11111",
        "aaaaa",
        "foo_0",
        "foo0",
        "foo1",
        "foo2",
        "Foo3",
        "foo4",
        "foo10",
    ]

elif has_icu:
    given = given_characters + given_special
    expected = [
        "_",
        "-",
        ",",
        ";",
        "!",
        "'",
        "(",
        ")",
        "[",
        "]",
        "{",
        "}",
        "§",
        "@",
        "&",
        "#",
        "%",
        "`",
        "´",
        "^",
        "°",
        "+",
        "+11111",
        "+aaaaa",
        "=",
        "~",
        "$",
        "€",
        "11111",
        "aaaaa",
        "foo_0",
        "foo0",
        "foo1",
        "foo2",
        "Foo3",
        "foo4",
        "foo10",
        "µ",
    ]
else:
    # For non-ICU UNIX, the order is all over the place
    # from platform to platform, distribution to distribution.
    # It's not really possible to predict the order across all
    # the different OS. To work around this, we will exclude
    # the special characters from the sort.
    given = given_characters
    expected = [
        "11111",
        "aaaaa",
        "foo0",
        "foo1",
        "foo2",
        "Foo3",
        "foo4",
        "foo10",
        "foo_0",
    ]


@pytest.mark.usefixtures("with_locale_en_us")
def test_os_sorted_corpus() -> None:
    result = natsort.os_sorted(given)
    print(result)
    assert result == expected