summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authorlverweijen <lverweijen>2016-03-08 19:20:22 +0100
committerlverweijen <lverweijen>2016-03-08 19:29:50 +0100
commitd14d9386c006e5f802285056a42f3173080a1b95 (patch)
tree1d7e36d1f782bfdaa99a82791a39083878e9e216 /tests
parent7ee4b45bd2619fb07c9b139940003224bedb1de3 (diff)
downloadranger-d14d9386c006e5f802285056a42f3173080a1b95.tar.gz
Make natural_sort's behaviour better defined
The old version relied on comparing integers to strings which is not
supported in Python 3 anymore and not a good practice in general anyway.
It was also the case that 'hello2' was ordered before 'hello' instead of
after, which I found to be non-intuitive.
Diffstat (limited to 'tests')
-rw-r--r--tests/ranger/container/test_fsobject.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/tests/ranger/container/test_fsobject.py b/tests/ranger/container/test_fsobject.py
new file mode 100644
index 00000000..305ab626
--- /dev/null
+++ b/tests/ranger/container/test_fsobject.py
@@ -0,0 +1,38 @@
+import pytest
+import operator
+
+from ranger.container.fsobject import FileSystemObject
+
+
+class MockFM(object):
+    """Used to fullfill the dependency by FileSystemObject."""
+
+    default_linemodes = []
+
+
+def create_filesystem_object(path):
+    """Create a FileSystemObject without an fm object."""
+    fso = FileSystemObject.__new__(FileSystemObject)
+    fso.fm = MockFM()
+    fso.__init__(path)
+    return fso
+
+
+def test_basename_natural1():
+    """Test filenames without extensions."""
+    fsos = [create_filesystem_object(path)
+            for path in "hello", "hello1", "hello2"]
+    print("fsos", repr(fsos))
+    print("sorted(fsos)", repr(sorted(fsos[::-1])))
+    assert(fsos == sorted(fsos[::-1], key=operator.attrgetter("basename_natural")))
+    assert(fsos == sorted(fsos[::-1], key=operator.attrgetter("basename_natural_lower")))
+
+
+def test_basename_natural2():
+    """Test filenames with extensions."""
+    fsos = [create_filesystem_object(path)
+            for path in "hello", "hello.txt", "hello1.txt", "hello2.txt"]
+    print("fsos", repr(fsos))
+    print("sorted(fsos)", repr(sorted(fsos[::-1])))
+    assert(fsos == sorted(fsos[::-1], key=operator.attrgetter("basename_natural")))
+    assert(fsos == sorted(fsos[::-1], key=operator.attrgetter("basename_natural_lower")))