def test_maybe_transform(integer): MonadTransformTester(monad=Maybe.just, value=integer).test(run_to_maybe_test=False) assert Maybe.nothing().to_box() == Box(None) assert Maybe.nothing().to_either() == Left(None) assert Maybe.nothing().to_lazy().get() is None assert Maybe.nothing().to_try() == Try(None, is_success=False) assert Maybe.nothing().to_validation() == Validation.success(None)
def to_maybe(self): """ Transform Either to Maybe. :returns: Empty Maybe :rtype: Maybe[None] """ from pymonet.maybe import Maybe return Maybe.nothing()
def to_maybe_test(self): if self.is_fail: assert self.monad(self.value).to_maybe() == Maybe.nothing() else: assert self.monad(self.value).to_maybe() == Maybe.just(self.value)
def test_validation_transform(integer): MonadTransformTester(monad=Validation.success, value=integer).test(run_to_validation_test=False) Validation.fail([integer]).to_maybe() == Maybe.nothing() Validation.fail([integer]).to_either() == Left([integers])
def test_maybe_if_filter_returns_false_method_should_return_empty_maybe(): assert Maybe.just(41).filter( lambda value: value % 2 == 0) == Maybe.nothing()
def test_maybe_is_nothing_should_return_proper_boolean(integer): assert Maybe.just(integer).is_nothing is False assert Maybe.nothing().is_nothing is True
def test_maybe_get_or_else_method_should_return_argument_when_monad_is_empty( integer): assert Maybe.nothing().get_or_else(integer) is integer
def test_maybe_bind_should_not_call_mapper_when_monad_has_nothing(maybe_spy): Maybe.nothing().bind(maybe_spy.binder) assert maybe_spy.binder.call_count == 0
def test_maybe_map_operator_should_be_applied_only_on_just_value(integer): assert Maybe.just(42).map(increase) == Maybe.just(43) assert Maybe.nothing().map(increase) == Maybe.nothing()
def test_maybe_eq_operator_should_compare_values(integer): assert Maybe.just(integer) == Maybe.just(integer) assert Maybe.just(None) == Maybe.just(None) assert Maybe.just(integer) != Maybe.nothing()
def test_maybe_ap_on_empty_maybe_should_not_be_applied(): def lambda_fn(): raise TypeError assert Maybe.nothing().ap(Maybe.just(lambda_fn)) == Maybe.nothing() assert Maybe.just(42).ap(Maybe.nothing()) == Maybe.nothing()