Skip to content

Commit

Permalink
chore(assert_failure): improve assertion text when raise an Assertion… (
Browse files Browse the repository at this point in the history
#73)

* chore(assert_failure): improve assertion text when raise an AssertionException

* fix: static analysis error
  • Loading branch information
acostapazo authored Mar 18, 2024
1 parent d4329a6 commit 6f60303
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
6 changes: 5 additions & 1 deletion meiga/assertions/assert_failure.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ def assert_failure(
result.is_failure
), f"result is not failure as expected. Given failure value is {result.value}"
if value_is_instance_of:
base = type(result.value)
if isinstance(base, type) and base == type:
base = result.value.__bases__

assert isinstance(result.value, value_is_instance_of), (
f"Value is not instance of {value_is_instance_of}. "
f"Given value is {result.value} of {type({result.value})}"
f"Given value is {result.value} of {base}"
)
if value_is_equal_to:
assert result.value == value_is_equal_to, (
Expand Down
14 changes: 13 additions & 1 deletion tests/unit/test_result_assertions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
from typing import Any

import pytest

from meiga import Error, Result


class MyError(Error):
pass


@pytest.mark.unit
class TestResultAssertion:
def should_assert_a_success(self):
Expand All @@ -25,6 +31,12 @@ def should_assert_a_failure_checking_instance(self):
result = Result(failure=Error())
result.assert_failure(value_is_instance_of=Error)

def test_should_assert_a_failure_result_checking_instance_and_value(self):
def should_assert_a_failure_result_checking_instance_and_value(self):
result = Result(failure=5)
result.assert_failure(value_is_instance_of=int, value_is_equal_to=5)

@pytest.mark.parametrize("error", [Error(), MyError(), Exception()])
def should_raise_an_assertion_error_when_instance_is_not_correct(self, error: Any):
result = Result(failure=error) # type:ignore
with pytest.raises(AssertionError):
result.assert_failure(value_is_instance_of=bool)

0 comments on commit 6f60303

Please sign in to comment.