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()
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()
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()
def test_should_return_false_when_checking_if_nothing_is_just_type_given_nothing( ): assert Nothing().is_just is False
def test_should_return_true_when_checking_if_nothing_is_nothing_type_given_nothing( ): assert Nothing().is_nothing is True
def test_should_return_default_value_when_getting_value_from_nothing(): assert Nothing().value_or("nothing here") == "nothing here"
def test_should_not_return_container_inner_value_when_calculating_nothing_str_representation(): nothing = Nothing() assert str(nothing) == "<Nothing>"
def test_should_return_nothing_when_mapping_given_nothing(): maybe_nothing = Nothing() assert maybe_nothing.map(partial(add, 1)) == Nothing()
def test_should_return_nothing_when_creating_maybe_from_value_given_none(): assert Maybe.from_value(None) == Nothing()