Skip to content

Commit

Permalink
Add unwrap_and. Refactor handle (now returns itself)
Browse files Browse the repository at this point in the history
  • Loading branch information
acostapazo committed Jan 21, 2020
1 parent d0264fa commit 04066d5
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 24 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ A discriminated union that encapsulates successful outcome with a value of type
| `unwrap_or(failure_value)` | Returns the encapsulated value if this instance represents success or the selected `failure_value` if it is failure. |
| `unwrap_or_throw()` | Returns the encapsulated value if this instance represents success or throws the encapsulated exception if it is failure. |
| `unwrap_or_else(on_failure)` | Returns the encapsulated value if this instance represents success or execute the `on_failure` function when it is failure. |
| `unwrap_and(on_success)` | Returns the encapsulated value if this instance represents success and execute the `on_success` function when it is success. |
| `handle(on_success,on_failure)` | Returns itself and execute the `on_success`function when the instance represemts success and the `on_failure` function when it is failure. |
| `map(transform)` | Returns a transformed result applying `transform` function applied to encapsulated value if this instance represents success or failure |

Expand Down
35 changes: 21 additions & 14 deletions meiga/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,9 @@ def unwrap_or_throw(self):
else:
return self.value

def unwrap_or_else(self, on_failure: Callable):
if not self._is_success:
on_failure(self.value)
else:
return self.value

def handle(
self,
on_success: Callable = None,
on_failure: Callable = None,
success_args=None,
failure_args=None,
) -> Any:
def unwrap_or_else(
self, on_failure: Callable, failure_args=None, failure_value: Any = None
):
if not self._is_success:
if on_failure:
if failure_args:
Expand All @@ -107,8 +97,12 @@ def handle(
on_failure()
else:
on_failure(self.value)
raise OnFailureException(self)
return failure_value
else:
return self.value

def unwrap_and(self, on_success: Callable, success_args=None):
if self._is_success:
if on_success:
if success_args:
on_success(*success_args)
Expand All @@ -118,6 +112,19 @@ def handle(
else:
on_success(self.value)
return self.value
else:
return None

def handle(
self,
on_success: Callable = None,
on_failure: Callable = None,
success_args=None,
failure_args=None,
):
self.unwrap_or_else(on_failure, failure_args)
self.unwrap_and(on_success, success_args)
return self

def map(self, transform: Callable):
new_value = transform(self.value)
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/test_result_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from meiga import Result, Error, isFailure, isSuccess
from meiga.decorators import meiga
from meiga.on_failure_exception import OnFailureException


@pytest.mark.unit
Expand All @@ -24,8 +23,9 @@ def on_failure(failure_value):
assert isinstance(failure_value, Error)

result = Result(success="Hi!")
result.handle(on_success=on_success, on_failure=on_failure)
new_result = result.handle(on_success=on_success, on_failure=on_failure)

assert new_result == result
assert called_on_success is True
assert called_on_failure is False

Expand All @@ -48,9 +48,9 @@ def on_failure(result: Result):

result = Result(failure=Error())

with pytest.raises(OnFailureException):
result.handle(on_success=on_success, on_failure=on_failure)
new_result = result.handle(on_success=on_success, on_failure=on_failure)

assert new_result == result
assert called_on_success is False
assert called_on_failure is True

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_result_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def is_positive(value: int) -> Result[bool, Error]:
)
def sum_positive_values(first_value: int, second_value: int) -> Result[int, Error]:

is_positive(first_value).handle()
is_positive(second_value).handle()
is_positive(first_value).unwrap_or_throw()
is_positive(second_value).unwrap_or_throw()

return Result(success=first_value + second_value)

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_result_on_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def is_positive(value: int) -> Result[bool, Error]:
@meiga
def sum_positive_values(first_value: int, second_value: int) -> Result[int, Error]:

is_positive(first_value).handle()
is_positive(second_value).handle()
is_positive(first_value).unwrap_or_throw()
is_positive(second_value).unwrap_or_throw()

return Result(success=first_value + second_value)

Expand Down
64 changes: 62 additions & 2 deletions tests/unit/test_result_unwrap.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from meiga import Result, Error, Failure
from meiga import Result, Error, Failure, Success
from meiga.on_failure_exception import OnFailureException


Expand Down Expand Up @@ -41,7 +41,7 @@ def test_should_raise_an_exception_when_unwrap_or_throw_with_a_failure_result():


@pytest.mark.unit
def test_should_call_on_Failure_when_unwrap_or_else_with_a_result_failure():
def test_should_call_on_failure_when_unwrap_or_else_with_a_result_failure():

global called_on_failure
called_on_failure = False
Expand All @@ -56,3 +56,63 @@ def on_failure(failure_value):
_ = result.unwrap_or_else(on_failure)

assert called_on_failure


@pytest.mark.unit
def test_should_call_on_failure_when_unwrap_or_else_with_failure_result_and_custom_args():
param1 = 1
param2 = "param2"

global called_on_failure
called_on_failure = False

def on_failure(param1, param2):
global called_on_failure
called_on_failure = True
assert isinstance(param1, int)
assert isinstance(param2, str)

result = Failure(Error())

_ = result.unwrap_or_else(on_failure, failure_args=(param1, param2))

assert called_on_failure


@pytest.mark.unit
def test_should_call_on_success_when_unwrap_and_with_a_result_success():

global called_on_success
called_on_success = False

def on_success(success_value):
global called_on_success
called_on_success = True
assert isinstance(success_value, str)

result = Success("Hi!")

_ = result.unwrap_and(on_success)

assert called_on_success


@pytest.mark.unit
def test_should_call_on_success_when_unwrap_and_with_a_result_success_and_custom_args():
param1 = 1
param2 = "param2"

global called_on_success
called_on_success = False

def on_success(param1, param2):
global called_on_success
called_on_success = True
assert isinstance(param1, int)
assert isinstance(param2, str)

result = Success("Hi!")

_ = result.unwrap_and(on_success, success_args=(param1, param2))

assert called_on_success

0 comments on commit 04066d5

Please sign in to comment.