Skip to content

Commit

Permalink
Add tests for new methods/properties
Browse files Browse the repository at this point in the history
  • Loading branch information
barneygale committed Dec 4, 2023
1 parent cae3774 commit d4c87c6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
12 changes: 6 additions & 6 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,12 +443,6 @@ def with_suffix(self, suffix):
else:
raise ValueError(f"Invalid suffix {suffix!r}")

def without_trailing_sep(self):
"""Return a new path without a trailing slash after its name."""
if not self.has_trailing_sep:
return self
return self._from_parsed_parts(self.drive, self.root, self._tail, False)

def with_trailing_sep(self):
"""Return a new path with a trailing slash after its name. If the
path has no name, ValueError is raised."""
Expand All @@ -459,6 +453,12 @@ def with_trailing_sep(self):
raise ValueError(f"{self!r} has an empty name")
return self._from_parsed_parts(self.drive, self.root, tail, True)

def without_trailing_sep(self):
"""Return a new path without a trailing slash after its name."""
if not self.has_trailing_sep:
return self
return self._from_parsed_parts(self.drive, self.root, self._tail, False)

def relative_to(self, other, /, *_deprecated, walk_up=False):
"""Return the relative path to another path identified by the passed
arguments. If the operation is not possible (because this is not
Expand Down
45 changes: 45 additions & 0 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,51 @@ def test_with_suffix_common(self):
self.assertRaises(ValueError, P('a/b').with_suffix, './.d')
self.assertRaises(ValueError, P('a/b').with_suffix, '.d/.')

def test_has_trailing_sep(self):
P = self.cls
self.assertFalse(P().has_trailing_sep)
self.assertFalse(P('').has_trailing_sep)
self.assertFalse(P('.').has_trailing_sep)
self.assertFalse(P('a').has_trailing_sep)
self.assertTrue(P('a/').has_trailing_sep)
self.assertFalse(P('a/b').has_trailing_sep)
self.assertTrue(P('a/b/').has_trailing_sep)
self.assertFalse(P('/').has_trailing_sep)
self.assertFalse(P('/a').has_trailing_sep)
self.assertTrue(P('/a/').has_trailing_sep)
self.assertFalse(P('/a/b').has_trailing_sep)
self.assertTrue(P('/a/b/').has_trailing_sep)

def test_with_trailing_sep(self):
P = self.cls
self.assertRaises(ValueError, P().with_trailing_sep)
self.assertRaises(ValueError, P('').with_trailing_sep)
self.assertRaises(ValueError, P('.').with_trailing_sep)
self.assertEqual(P('a/'), P('a').with_trailing_sep())
self.assertEqual(P('a/'), P('a/').with_trailing_sep())
self.assertEqual(P('a/b/'), P('a/b').with_trailing_sep())
self.assertEqual(P('a/b/'), P('a/b/').with_trailing_sep())
self.assertRaises(ValueError, P('/').with_trailing_sep)
self.assertEqual(P('/a/'), P('/a').with_trailing_sep())
self.assertEqual(P('/a/'), P('/a/').with_trailing_sep())
self.assertEqual(P('/a/b/'), P('/a/b').with_trailing_sep())
self.assertEqual(P('/a/b/'), P('/a/b/').with_trailing_sep())

def test_without_trailing_sep(self):
P = self.cls
self.assertEqual(P(), P().without_trailing_sep())
self.assertEqual(P(), P('').without_trailing_sep())
self.assertEqual(P(), P('.').without_trailing_sep())
self.assertEqual(P('a'), P('a').without_trailing_sep())
self.assertEqual(P('a'), P('a/').without_trailing_sep())
self.assertEqual(P('a/b'), P('a/b').without_trailing_sep())
self.assertEqual(P('a/b'), P('a/b/').without_trailing_sep())
self.assertEqual(P('/'), P('/').without_trailing_sep())
self.assertEqual(P('/a'), P('/a').without_trailing_sep())
self.assertEqual(P('/a'), P('/a/').without_trailing_sep())
self.assertEqual(P('/a/b'), P('/a/b').without_trailing_sep())
self.assertEqual(P('/a/b'), P('/a/b/').without_trailing_sep())

def test_relative_to_common(self):
P = self.cls
p = P('a/b')
Expand Down

0 comments on commit d4c87c6

Please sign in to comment.