def test_validation_monad_law(integer):
    MonadLawTester(
        monad=Validation.success,
        value=integer,
        mapper1=lambda value: Validation.success(value + 1),
        mapper2=lambda value: Validation.success(value + 2)
    ).test()
Ejemplo n.º 2
0
    def to_validation(self):
        """
        Transform Maybe into Validation.

        :returns: successfull Validation monad with previous value or None when Maybe is empty
        :rtype: Validation[A, []]
        """
        from pymonet.validation import Validation

        if self.is_nothing:
            return Validation.success(None)
        return Validation.success(self.value)
def test_validation_applicative():
    assert validate('Success$') == Validation.success('Success$')

    assert validate('Success') == Validation(value='Success', errors=['value not contains special character'])

    assert validate('success$') == Validation(value='success$', errors=['value not uppercase'])
    assert validate('S$') == Validation(value='S$', errors=['value not long enough'])

    assert validate('success') == Validation(value='success', errors=[
        'value not uppercase',
        'value not contains special character'
    ])
    assert validate('s$') == Validation(value='s$', errors=[
        'value not long enough',
        'value not uppercase'
    ])
    assert validate('success') == Validation(value='success', errors=[
        'value not uppercase',
        'value not contains special character'
    ])

    assert validate('s') == Validation(value='s', errors=[
        'value not long enough',
        'value not uppercase',
        'value not contains special character'
    ])
Ejemplo n.º 4
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.º 5
0
    def to_validation(self):
        """
        Transform Either into Validation.

        :returns: successfull Validation monad with previous value
        :rtype: Validation[A, []]
        """
        from pymonet.validation import Validation

        return Validation.success(self.value)
Ejemplo n.º 6
0
    def to_validation(self):
        """
        Transform Box into Validation.

        :returns: failed Validation monad with previous value as error
        :rtype: Validation[None, [A]]
        """
        from pymonet.validation import Validation

        return Validation.fail([self.value])
Ejemplo n.º 7
0
    def to_validation(self, *args):
        """
        Transform Lazy into successful Validation with constructor_fn result.

        :returns: successfull Validation monad with previous value
        :rtype: Validation[A, []]
        """
        from pymonet.validation import Validation

        return Validation.success(self.get(*args))
def validate_length(value):
    if len(value) < 5:
        return Validation.fail(['value not long enough'])
    return Validation.success()
 def to_validation_test(self):
     if self.is_fail:
         assert self.monad(self.value).to_validation() == Validation.fail([self.value])
     else:
         assert self.monad(self.value).to_validation() == Validation.success(self.value)
def test_validation_functor_law(integer):
    FunctorLawTester(
        functor=Validation.success(integer),
        mapper1=lambda value: value + 1,
        mapper2=lambda value: value + 2
    ).test()
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 validate(value):
    return (Validation.success(value)
            .ap(validate_length)
            .ap(validate_uppercase)
            .ap(validate_contains_special_character))
def validate_contains_special_character(value):
    if re.match(r'^[a-zA-Z0-9_]*$', value):
        return Validation.fail(['value not contains special character'])
    return Validation.success()
def test_validation_is_fail():
    assert Validation.fail(['fail']).is_fail()
def test_validation_is_success():
    assert Validation.success().is_success()
def test_validation_bind():
    assert (Validation
            .success(42)
            .bind(lambda value: Validation.success(value + 1))) == Validation.success(43)
def test_validation_map():
    assert Validation.success(42).map(increase) == Validation.success(43)
Ejemplo n.º 18
0
def test_transform_to_validation_should_validation(integer):
    assert Lazy(lambda: integer).to_validation() == Validation.success(integer)
def validate_uppercase(value):
    if value[0].upper() != value[0]:
        return Validation.fail(['value not uppercase'])
    return Validation.success()