Skip to content

Commit

Permalink
Solve a bug when pass only one parameter to Result handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
acostapazo committed Mar 27, 2020
1 parent 683c142 commit 317753e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
12 changes: 10 additions & 2 deletions meiga/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ def unwrap_or_else(
if not self._is_success:
if on_failure:
if failure_args:
failure_args = list(failure_args)
failure_args = (
list(failure_args)
if isinstance(failure_args, tuple)
else [failure_args]
)
if Result.__id__ in failure_args:
index_meiga_result = failure_args.index(Result.__id__)
failure_args[index_meiga_result] = self
Expand All @@ -121,7 +125,11 @@ def unwrap_and(self, on_success: Callable, success_args=None):
if self._is_success:
if on_success:
if success_args:
success_args = list(success_args)
success_args = (
list(success_args)
if isinstance(success_args, tuple)
else [success_args]
)
if Result.__id__ in success_args:
index_meiga_result = success_args.index(Result.__id__)
success_args[index_meiga_result] = self
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/test_result_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,25 @@ def run():
)

run()


@pytest.mark.parametrize("result", [isSuccess, isFailure])
def test_should_execute_handler_with_only_one_parameter(result):
given_first_parameter = 1

def on_success(param_1: int):
assert param_1 == 1

def on_failure(param_1: int):
assert param_1 == 1

@meiga
def run():
result.handle(
on_success=on_success,
on_failure=on_failure,
success_args=given_first_parameter,
failure_args=given_first_parameter,
)

run()

0 comments on commit 317753e

Please sign in to comment.