summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorSeth Morton <seth.m.morton@gmail.com>2020-11-16 22:05:01 -0800
committerSeth Morton <seth.m.morton@gmail.com>2020-11-17 22:36:23 -0800
commitd69a5414f111b9584f28c17382af1821c5e291fb (patch)
tree9ba0806d803d5376ee6f9a101a7b6876d041dafa /tests
parent0c31c245fc461641024ae9eed98ea97ca2ef79a7 (diff)
downloadnatsort-d69a5414f111b9584f28c17382af1821c5e291fb.tar.gz
Fully implement OS sorting on all platforms
ICU is used to sort on UNIX-based platforms. If not given, the results are OK, but not exactly what you would expect.
Diffstat (limited to 'tests')
-rw-r--r--tests/test_os_sorted.py194
1 files changed, 194 insertions, 0 deletions
diff --git a/tests/test_os_sorted.py b/tests/test_os_sorted.py
new file mode 100644
index 0000000..aaaffac
--- /dev/null
+++ b/tests/test_os_sorted.py
@@ -0,0 +1,194 @@
+# -*- 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")])
+
+
+# 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