From ae99aba0e0f99f2b698781832699466680e05b28 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Mon, 9 May 2022 06:21:27 +0200 Subject: [PATCH] Add __lt__ and __gt__ operators to Path --- earthsnake/path.py | 9 +++++++++ tests/test_path.py | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/earthsnake/path.py b/earthsnake/path.py index 4a11128..1da6edc 100644 --- a/earthsnake/path.py +++ b/earthsnake/path.py @@ -76,6 +76,15 @@ class Path: def __repr__(self) -> str: return f'' + def __eq__(self, other: object) -> bool: + return str(self) == str(other) + + def __lt__(self, other: object) -> bool: + return str(self) < str(other) + + def __gt__(self, other: object) -> bool: + return str(self) > str(other) + def startswith(self, sub: str) -> bool: """Check if path starts with sub""" diff --git a/tests/test_path.py b/tests/test_path.py index 0fe4b68..bc4b56e 100644 --- a/tests/test_path.py +++ b/tests/test_path.py @@ -124,3 +124,44 @@ def test_endswith() -> None: assert path.endswith('.txt') assert not path.endswith('.py') + + +def test_eq_other_path() -> None: + """Test if Paths can be compared to other Paths""" + assert Path('/test.txt') == Path('/test.txt') + assert Path('/test.txt') != Path('/test.md') + + +def test_eq_str() -> None: + """Test if Paths can be compared to strings""" + + assert Path('/test.txt') == '/test.txt' + assert Path('/test.txt') != '/test.md' + + +def test_lt() -> None: + """Test if Paths can be sorted with other Paths (< direction)""" + + assert Path('/test.txt') < Path('/very.txt') + assert not Path('/test.txt') < Path('/much.txt') # pylint: disable=unneeded-not + + +def test_lt_str() -> None: + """Test if Paths can be sorted with strings (< direction)""" + + assert Path('/very.txt') > '/test.txt' + assert not Path('/much.txt') > '/test.txt' # pylint: disable=unneeded-not + + +def test_gt() -> None: + """Test if Paths can be sorted with other Paths (> direction)""" + + assert Path('/very.txt') > Path('/test.txt') + assert not Path('/much.txt') > Path('/test.txt') # pylint: disable=unneeded-not + + +def test_g_str() -> None: + """Test if Paths can be sorted with strings (> direction)""" + + assert Path('/very.txt') > '/test.txt' + assert not Path('/much.txt') > '/test.txt' # pylint: disable=unneeded-not