From 78b337125ce02b59f8663a7ee353e32be0844725 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=20Garc=C3=ADa=20Salom=C3=B3n?= Date: Thu, 23 Nov 2023 10:28:22 +0100 Subject: [PATCH] feat: add repr and str impl for OnFailureException (#68) --- meiga/on_failure_exception.py | 6 ++++++ tests/unit/test_on_failure_exception.py | 28 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 tests/unit/test_on_failure_exception.py diff --git a/meiga/on_failure_exception.py b/meiga/on_failure_exception.py index e843434..6effe1b 100644 --- a/meiga/on_failure_exception.py +++ b/meiga/on_failure_exception.py @@ -12,3 +12,9 @@ class OnFailureException(Error): def __init__(self, result: "AnyResult") -> None: self.result = result Exception.__init__(self) + + def __str__(self) -> str: + return f"OnFailureException: {self.result}" + + def __repr__(self) -> str: + return f"OnFailureException: {self.result}" diff --git a/tests/unit/test_on_failure_exception.py b/tests/unit/test_on_failure_exception.py new file mode 100644 index 0000000..b2a3d0c --- /dev/null +++ b/tests/unit/test_on_failure_exception.py @@ -0,0 +1,28 @@ +import pytest + +from meiga import Error, Result +from meiga.on_failure_exception import OnFailureException + + +@pytest.mark.unit +def test_should_str_as_expected_default_error(): + result = Result(failure=Error()) + + exception = OnFailureException(result) + + assert "OnFailureException: Result[status: failure | value: Error]" == str( + exception + ) + + +@pytest.mark.unit +def test_should_str_as_expected_an_exception(): + wrapped_exception = ValueError("Something went wrong") + result = Result(failure=wrapped_exception) + + exception = OnFailureException(result) + + assert ( + f"OnFailureException: Result[status: failure | value: {wrapped_exception.__repr__()}]" + == str(exception) + )