Esempio n. 1
0
def test_should_return_nothing_when_mapping_given_just_and_a_func_that_returns_none(
):
    just_boris = Just({"name": "Boris"})
    get_age: Callable[[Mapping],
                      Optional[int]] = lambda mapping: mapping.get("age")

    assert just_boris.map(get_age).map(partial(add, 1)) == Nothing()
Esempio n. 2
0
def test_should_return_nothing_when_binding_just_given_function_returning_nothing():
    just_one = Just(1)

    make_nothing: Callable[[int], Maybe[int]] = lambda _: Nothing()

    assert just_one.bind(make_nothing) == Nothing()
    assert just_one | make_nothing == Nothing()
Esempio n. 3
0
def test_should_return_the_same_nothing_when_binding_nothing_given_function_returning_just_or_nothing():
    nothing = Nothing()

    just_add_one: Callable[[int], Maybe[int]] = lambda to_sum: choice([Just(to_sum + 1), Nothing()])

    assert nothing.bind(just_add_one) == Nothing()
    assert nothing | just_add_one == Nothing()
Esempio n. 4
0
def test_should_return_just_when_binding_just_given_function_returning_just():
    just_one = Just(1)

    just_add_one: Callable[[int], Maybe[int]] = lambda to_sum: Just(to_sum + 1)

    assert just_one.bind(just_add_one) == Just(2)
    assert just_one | just_add_one == Just(2)
Esempio n. 5
0
def test_should_return_false_when_checking_if_just_is_nothing_type_given_just(
):
    assert Just(2).is_nothing is False
Esempio n. 6
0
def test_should_return_true_when_checking_if_just_is_just_type_given_just():
    assert Just(2).is_just is True
Esempio n. 7
0
def test_should_return_internal_value_when_getting_value_from_just():
    assert Just(1).value_or("nothing here") == 1
Esempio n. 8
0
def test_should_return_just_when_mapping_given_just_and_a_func_that_return_value(
):
    maybe_one = Just(1)
    assert maybe_one.map(partial(add, 1)) == Just(2)
Esempio n. 9
0
def test_should_return_just_when_creating_maybe_from_value_given_not_none():
    assert Maybe.from_value(2) == Just(2)