Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
    def to_maybe(self):
        """
        Transform Either to Maybe.

        :returns: Empty Maybe
        :rtype: Maybe[None]
        """
        from pymonet.maybe import Maybe

        return Maybe.nothing()
Ejemplo n.º 3
0
    def to_maybe(self):
        """
        Transform Box into not empty Maybe.

        :returns: non empty Maybe monad with previous value
        :rtype: Maybe[A]
        """
        from pymonet.maybe import Maybe

        return Maybe.just(self.value)
Ejemplo n.º 4
0
    def to_maybe(self):
        """
        Transform Either to Maybe.

        :returns: Maybe with previous value
        :rtype: Maybe[A]
        """
        from pymonet.maybe import Maybe

        return Maybe.just(self.value)
Ejemplo n.º 5
0
    def to_maybe(self, *args):
        """
        Transform Lazy into not empty Maybe with constructor_fn result.

        :returns: not empty Maybe monad with constructor_fn result
        :rtype: Maybe[A]
        """
        from pymonet.maybe import Maybe

        return Maybe.just(self.get(*args))
Ejemplo n.º 6
0
def test_maybe_monad_law(integer):
    MonadLawTester(monad=Maybe.just,
                   value=integer,
                   mapper1=lambda value: Maybe.just(value + 1),
                   mapper2=lambda value: Maybe.just(value + 2)).test()
Ejemplo n.º 7
0
def test_maybe_if_filter_returns_true_method_should_return_self():
    assert Maybe.just(42).filter(lambda value: value % 2 == 0) == Maybe.just(
        42)
Ejemplo n.º 8
0
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 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)
Ejemplo n.º 10
0
def test_maybe_get_or_else_method_should_return_maybe_value_when_monad_is_not_empty(
        integer):
    assert Maybe.just(integer).get_or_else(0) is integer
Ejemplo n.º 11
0
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
Ejemplo n.º 12
0
def test_maybe_bind_should_retrun_result_of_mapper_called_with_maybe_value(
        maybe_spy):
    assert Maybe.just(42).bind(increase) == 43
Ejemplo n.º 13
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()
Ejemplo n.º 14
0
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()
Ejemplo n.º 15
0
 def binder(self, value):
     return Maybe.just(value + 1)
Ejemplo n.º 16
0
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()
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])
Ejemplo n.º 18
0
def test_maybe_get_or_else_method_should_return_argument_when_monad_is_empty(
        integer):
    assert Maybe.nothing().get_or_else(integer) is integer
Ejemplo n.º 19
0
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
Ejemplo n.º 20
0
def test_maybe_functor_law(integer):
    FunctorLawTester(functor=Maybe.just(integer),
                     mapper1=lambda value: value + 1,
                     mapper2=lambda value: value + 2).test()