Пример #1
0
def test_should_return_left_when_binding_right_given_function_returning_left():
    right_one = Right(1)

    left_add_one: Callable[[int], Either[int, int]] = lambda to_sum: Left(to_sum + 1)

    assert right_one.bind(left_add_one) == Left(2)
    assert right_one | left_add_one == Left(2)
Пример #2
0
def test_should_return_the_same_left_when_binding_left_given_function_returning_right_or_left():
    left_one = Left(1)

    add_one: Callable[[int], Either[int, int]] = lambda to_sum: choice(
        [Right(to_sum + 1), Left(to_sum + 1)]
    )

    assert left_one.bind(add_one) == left_one
    assert left_one | add_one == left_one
Пример #3
0
def test_should_return_exit_type_from_left_mapper_when_calling_either_with_mapping_functions_given_left_container(
):
    left_container = Left(["error one", "error two"])

    left_mapper: Callable[[Either[int, List[str]]],
                          ApplicationReturnTest] = MagicMock(
                              return_value=ApplicationReturnTest(
                                  errors=["error one", "error two"]))
    right_mapper: Callable[[Either[int, List[str]]],
                           ApplicationReturnTest] = MagicMock()
    assert either(
        left_mapper=left_mapper,
        right_mapper=right_mapper,
        container=left_container,
    ) == ApplicationReturnTest(sum=None, errors=["error one", "error two"])
Пример #4
0
def test_should_return_the_same_left_when_mapping_given_left():
    left_one = Left(1)
    assert left_one.map(partial(add, 1)) == left_one
Пример #5
0
def test_should_return_false_when_checking_if_left_is_right_type_given_left():
    assert Left(2).is_right is False
Пример #6
0
def test_should_return_true_when_checking_if_left_is_left_type_given_left():
    assert Left(2).is_left is True
Пример #7
0
def test_should_return_inner_value_when_checking_value_given_left():
    assert Left(2).value == 2
Пример #8
0
def test_should_return_right_when_creating_either_from_failure():
    assert Either.from_failure("error") == Left("error")