Skip to content

Commit

Permalink
feat: make paths equally hashable
Browse files Browse the repository at this point in the history
  • Loading branch information
matfax committed Oct 15, 2019
1 parent db6aca7 commit b95f234
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
3 changes: 3 additions & 0 deletions mutapath/immutapath.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ def __repr__(self):
def __str__(self):
return self._contained

def __hash__(self):
return hash(self._contained)

def __eq__(self, other):
if isinstance(other, Path):
return self._contained == other._contained
Expand Down
3 changes: 3 additions & 0 deletions mutapath/mutapath.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ def __repr__(self):
def __str__(self):
return self._contained

def __hash__(self):
return hash(self._contained)

def merge_tree(self, other, *args, **kwargs):
"""Move, merge and mutate this path to the given other path."""
self._contained.merge_tree(other, *args, **kwargs)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_immutapath.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,8 @@ def test_home_root(self):
actual = Path("/").home
self.assertEqual(excpected, actual)
self.assertEqual(excpected.abspath(), actual.abspath())

def test_hash(self):
expected = hash(Path("/A") / "B")
actual = hash(Path("/A/B/"))
self.assertEqual(expected, actual)
11 changes: 8 additions & 3 deletions tests/test_mutapath.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,15 @@ def test_joinpath(self):
self.assertEqual(expected.normpath(), actual.normpath())

def test_capsulation(self):
excpected = MutaPath("/A/B")
actual = MutaPath(MutaPath(excpected))
self.assertEqual(excpected, actual)
expected = MutaPath("/A/B")
actual = MutaPath(MutaPath(expected))
self.assertEqual(expected, actual)

def test_repr(self):
excpected = MutaPath("/A/B")
self.assertTrue(repr(excpected).startswith("Path"))

def test_hash(self):
expected = hash(Path("/A/B"))
actual = hash(MutaPath("/A/B/"))
self.assertEqual(expected, actual)

0 comments on commit b95f234

Please sign in to comment.