Add __lt__ and __gt__ operators to Path

This commit is contained in:
Gergely Polonkai 2022-05-09 06:21:27 +02:00
parent 6e8f0fa5a1
commit ae99aba0e0
No known key found for this signature in database
GPG Key ID: 2D2885533B869ED4
2 changed files with 50 additions and 0 deletions

View File

@ -76,6 +76,15 @@ class Path:
def __repr__(self) -> str: def __repr__(self) -> str:
return f'<Path {self.path}>' return f'<Path {self.path}>'
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: def startswith(self, sub: str) -> bool:
"""Check if path starts with sub""" """Check if path starts with sub"""

View File

@ -124,3 +124,44 @@ def test_endswith() -> None:
assert path.endswith('.txt') assert path.endswith('.txt')
assert not path.endswith('.py') 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