From b9d0221e95308b2e67b1a14dfce34721c3d491a3 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Fri, 6 May 2022 17:20:59 +0200 Subject: [PATCH] Add startswith() and endswith() methods to Path --- earthsnake/path.py | 10 ++++++++++ tests/test_path.py | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/earthsnake/path.py b/earthsnake/path.py index 1d7adc1..4a11128 100644 --- a/earthsnake/path.py +++ b/earthsnake/path.py @@ -75,3 +75,13 @@ class Path: def __repr__(self) -> str: return f'' + + def startswith(self, sub: str) -> bool: + """Check if path starts with sub""" + + return self.path.startswith(sub) + + def endswith(self, sub: str) -> bool: + """Check if path ends with sub""" + + return self.path.endswith(sub) diff --git a/tests/test_path.py b/tests/test_path.py index d3c87f8..0fe4b68 100644 --- a/tests/test_path.py +++ b/tests/test_path.py @@ -106,3 +106,21 @@ def test_repr() -> None: """Test the __repr__ method""" assert repr(Path('/test.txt')) == '' + + +def test_startswith() -> None: + """Test the startswith method""" + + path = Path('/test.txt') + + assert path.startswith('/test') + assert not path.startswith('test') + + +def test_endswith() -> None: + """Test the endswith method""" + + path = Path('/test.txt') + + assert path.endswith('.txt') + assert not path.endswith('.py')