Skip to content

Commit

Permalink
feat: function assertion error text
Browse files Browse the repository at this point in the history
  • Loading branch information
petereon committed Apr 14, 2022
1 parent 8379f6d commit d8f8691
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions src/expycted/internals/function.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from typing import Any, Callable, Type

assertion_texts = {
"to_raise": "Expected function `{function}` to raise {exc} when called with: {arguments}",
"to_return": "Expected function {function} to return {value} when called with: {arguments}",
"to_return_type": "Expected value ({value}) returned by function {function} to be of type {type} when called with: {arguments}",
}


class Function:
def __init__(self, function: Callable):
Expand Down Expand Up @@ -32,6 +38,21 @@ def to_return(self, value: Any = None, type_of_value: type = None):
value=value, type_of_value=type_of_value, function=self.function
)

def format_args_kwargs(args: Any, kwargs: Any) -> str:
"""Format arguments and keyword arguments to string
Args:
args (Any): Arguments
kwargs (Any): Keyword arguments
Returns:
str: Formatted arguments and keyword arguments
"""
args_str = ", ".join(map(str, args))
kwargs_str = ", ".join(
map(lambda x: "{}={}".format(x[0], x[1]), kwargs.items())
)
return f"\n\t- arguments: {args_str} \n\t- keyword arguments: {kwargs_str}"

class ToRaise:
function: Callable
Expand All @@ -50,7 +71,10 @@ def when_called_with(self, *args, **kwargs):
try:
self.function(*args, **kwargs)
except Exception as e:
assert issubclass(type(e), self.exception)
assert issubclass(type(e), self.exception), assertion_texts["to_raise"].format(
function=self.function.__name__,
exc=self.exception,
arguments=format_args_kwargs(args, kwargs))
else:
raise AssertionError(
f"Expected '{self.exception}' to be raised, but nothing was raised"
Expand All @@ -77,8 +101,15 @@ def when_called_with(self, *args, **kwargs):
"""
ret = self.function(*args, **kwargs)
if self.value is not None:
assert ret == self.value
assert ret == self.value, assertion_texts["to_return"].format(
function=self.function.__name__,
value=self.value,
arguments=format_args_kwargs(args, kwargs))
if self.type_of_value is not None:
assert type(ret) == self.type_of_value
assert type(ret) == self.type_of_value, assertion_texts["to_return_type"].format(
function=self.function.__name__,
value=self.value,
arguments=format_args_kwargs(args, kwargs),
type=self.type_of_value)

when_called_with_args = when_called_with_arguments = when_called_with

0 comments on commit d8f8691

Please sign in to comment.