From 4c6ad448e83a60166fc0be2054246a992a1edfe6 Mon Sep 17 00:00:00 2001 From: Yury Selivanov Date: Sun, 1 Apr 2018 17:20:15 -0400 Subject: Implement __repr__ --- tests/test_map.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/test_map.py b/tests/test_map.py index a17e005..06c82b9 100644 --- a/tests/test_map.py +++ b/tests/test_map.py @@ -16,6 +16,8 @@ class HashKey: self.error_on_eq_to = error_on_eq_to def __repr__(self): + if self._crasher is not None and self._crasher.error_on_repr: + raise ReprError return ''.format(self.name, self.hash) def __hash__(self): @@ -51,12 +53,19 @@ class KeyStr(str): raise EqError return super().__eq__(other) + def __repr__(self, other): + if HashKey._crasher is not None and HashKey._crasher.error_on_repr: + raise ReprError + return super().__eq__(other) + class HaskKeyCrasher: - def __init__(self, *, error_on_hash=False, error_on_eq=False): + def __init__(self, *, error_on_hash=False, error_on_eq=False, + error_on_repr=False): self.error_on_hash = error_on_hash self.error_on_eq = error_on_eq + self.error_on_repr = error_on_repr def __enter__(self): if HashKey._crasher is not None: @@ -75,6 +84,10 @@ class EqError(Exception): pass +class ReprError(Exception): + pass + + class MapTest(unittest.TestCase): def test_hashkey_helper_1(self): @@ -705,6 +718,45 @@ class MapTest(unittest.TestCase): with HaskKeyCrasher(error_on_hash=True): h[AA] + def test_repr_1(self): + h = Map() + self.assertTrue(repr(h).startswith('