From d14d9386c006e5f802285056a42f3173080a1b95 Mon Sep 17 00:00:00 2001 From: lverweijen Date: Tue, 8 Mar 2016 19:20:22 +0100 Subject: 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. --- tests/ranger/container/test_fsobject.py | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 tests/ranger/container/test_fsobject.py (limited to 'tests') 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"))) -- cgit 1.4.1-2-gfad0 From 4c6117fded0bce5844c1aa46a60c2946d50568c3 Mon Sep 17 00:00:00 2001 From: lverweijen Date: Tue, 22 Mar 2016 15:43:20 +0100 Subject: Remove accidental prints for debugging --- tests/ranger/container/test_fsobject.py | 4 ---- 1 file changed, 4 deletions(-) (limited to 'tests') diff --git a/tests/ranger/container/test_fsobject.py b/tests/ranger/container/test_fsobject.py index 305ab626..4e521806 100644 --- a/tests/ranger/container/test_fsobject.py +++ b/tests/ranger/container/test_fsobject.py @@ -22,8 +22,6 @@ 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"))) @@ -32,7 +30,5 @@ 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"))) -- cgit 1.4.1-2-gfad0