Esempio n. 1
0
    def to_box(self):
        """
        Transform Maybe to Box.

        :returns: Box monad with previous value when Maybe is not empty, in other case Box with None
        :rtype: Box[A | None]
        """
        from pymonet.box import Box

        if self.is_nothing:
            return Box(None)
        return Box(self.value)
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)
Esempio n. 3
0
    def to_box(self, *args):
        """
        Transform Lazy into Box with constructor_fn result.

        :returns: Box monad with constructor_fn result
        :rtype: Box[A]
        """
        from pymonet.box import Box

        return Box(self.get(*args))
Esempio n. 4
0
    def to_box(self):
        """
        Transform Either to Box.

        :returns: Box monad with previous value
        :rtype: Box[A]
        """
        from pymonet.box import Box

        return Box(self.value)
 def to_box_test(self):
     assert self.monad(self.value).to_box() == Box(self.value)
Esempio n. 6
0
def test_box_functor_law(integer):
    FunctorLawTester(functor=Box(integer),
                     mapper1=lambda value: value + 1,
                     mapper2=lambda value: value + 2).test()
Esempio n. 7
0
def test_box_monad_law(integer):
    MonadLawTester(monad=Box,
                   value=integer,
                   mapper1=lambda value: Box(value + 1),
                   mapper2=lambda value: Box(value + 2)).test()
Esempio n. 8
0
def test_fold_should_return_result_of_fold_function_called_with_box_value():
    box = Box(42)
    assert box.bind(increase) == 43
Esempio n. 9
0
def test_map_should_return_new_instance_of_box():
    box = Box(42)
    mapped_box = box.map(increase)
    assert box is not mapped_box
Esempio n. 10
0
def test_map_should_return_box_with_mapped_value():
    box = Box(42)
    assert box.map(increase) == Box(43)
Esempio n. 11
0
def test_eq_should_compare_only_box_value():
    assert Box(42) == Box(42)
    assert Box(43) != Box(42)
    assert Box([]) == Box([])
    assert Box({}) == Box({})
    assert Box(None) == Box(None)