# -*- 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():
    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():
    natsort.os_sorted([9, 4.3, None, float("nan")])


def test_os_sorted_key():
    given = 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 = [
    "11111",
    "!",
    "#",
    "$",
    "%",
    "&",
    "'",
    "(",
    ")",
    "+",
    "+11111",
    "+aaaaa",
    ",",
    "-",
    ";",
    "=",
    "@",
    "[",
    "]",
    "^",
    "_",
    "`",
    "aaaaa",
    "foo0",
    "foo_0",
    "{",
    "}",
    "~",
    "§",
    "°",
    "´",
    "µ",
    "€",
    "foo1",
    "foo2",
    "foo4",
    "foo10",
    "Foo3",
]

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

elif has_icu:
    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[0:1] + given[22:25] + given[33:]
    expected = [
        "11111",
        "aaaaa",
        "foo0",
        "foo1",
        "foo2",
        "Foo3",
        "foo4",
        "foo10",
        "foo_0",
    ]


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

@@ -930,16 +930,45 @@
  given =
+ %5B%22foo0%22, %22foo2%22, %22goo1%22%5D%0A   
  expecte
@@ -1208,24 +1208,35 @@
 lorer.%0Agiven
+_characters
  = %5B%0A    %2211
@@ -1237,24 +1237,143 @@
    %2211111%22,%0A
+    %22aaaaa%22,%0A    %22foo0%22,%0A    %22foo_0%22,%0A    %22foo1%22,%0A    %22foo2%22,%0A    %22foo4%22,%0A    %22foo10%22,%0A    %22Foo3%22,%0A%5D%0Agiven_special = %5B%0A
     %22!%22,%0A   
@@ -1563,46 +1563,8 @@
 %60%22,%0A
-    %22aaaaa%22,%0A    %22foo0%22,%0A    %22foo_0%22,%0A
     
@@ -1635,69 +1635,8 @@
 %E2%82%AC%22,%0A
-    %22foo1%22,%0A    %22foo2%22,%0A    %22foo4%22,%0A    %22foo10%22,%0A    %22Foo3%22,%0A
 %5D%0A%0A#
@@ -1723,16 +1723,61 @@
 ndows%22:%0A
+    given = given_characters + given_special%0A
     expe
@@ -2341,16 +2341,61 @@
 as_icu:%0A
+    given = given_characters + given_special%0A
     expe
@@ -3258,41 +3258,19 @@
 iven
-%5B0:1%5D + given%5B22:25%5D + given%5B33:%5D
+_characters
 %0A   

Improve testing clarity and maintainablility