Exemplo n.º 1
0
def test_bool():
    assert bool(Failure(True)) is False
    assert bool(Success(False)) is True
    for n in test_range:
        assert bool(Failure(n)) is False
        assert bool(Success(n)) is True
        assert bool(unit(n)) is True
Exemplo n.º 2
0
def test_failsafe_decorator_with_predicate():
    @failsafe(predicate=bool)
    def truth(x):
        return x

    assert truth(42) == unit(42)
    assert truth(None) == Failure(None)
    assert add_1(0) >> truth == unit(1)
    assert add_1(-1) >> truth == Failure(0)
    assert truth(False) >> double == Failure(False)
    assert double([]) >> truth == error
Exemplo n.º 3
0
def test_ftry_action():
    inc = lambda n: n + 1
    dec = lambda n: n - 1

    act = ftry(inc)
    assert act(Failure(1)) == 2
    assert act(Success(1)) == 1

    act = ftry(failure_handler=inc, success_handler=dec)
    assert act(Failure(1)) == 2
    assert act(Success(1)) == 0
Exemplo n.º 4
0
def test_recover():
    result = Failure(0).recover(lambda v: v + 1)
    assert result == 1
    
    result = Success(10).recover(lambda v: v + 1)
    assert isinstance(result, Success)
    assert result.value == 10
Exemplo n.º 5
0
def test_ftry_action_with_incompatible_type():
    inc = lambda n: n + 1

    act = ftry(inc)
    assert act(Failure(1)) == 2

    with pytest.raises(TypeError):
        act(1)
Exemplo n.º 6
0
def test_fmap_functor_laws():
    identity = lambda a: a
    f = lambda a: a + 1
    g = lambda a: a * 2
    f_g = lambda n: f(g(n))

    for n in test_range:
        ft = unit(n)
        # fmap id == id
        assert ft.fmap(identity) == identity(ft)
        # fmap (f . g) == fmap f . fmap g
        assert ft.fmap(f_g) == ft.fmap(g).fmap(f)

    value = 42
    l = Failure('Something wrong.')
    r = Success(value)
    assert l.fmap(f) is l
    assert r.fmap(f) == Success(f(42))
Exemplo n.º 7
0
def test_failsafe_decorator_with_value():
    @failsafe(failure_on_value=None)
    def truth(x):
        return x

    assert truth(42) == unit(42)
    assert truth('') == unit('')
    assert truth(0) == unit(0)
    assert truth(False) == unit(False)
    assert truth(None) == Failure(None)
Exemplo n.º 8
0
def test_ordering():
    with pytest.raises(TypeError):
        Failure(1) < 1

    with pytest.raises(TypeError):
        Success(1) < 1

    for n in test_range:
        assert (Failure(n) < Failure(n)) is False
        assert Failure(n) > Failure(n - 1)
        assert Failure(n) < Failure(n + 1)
        assert (Success(n) < Success(n)) is False
        assert Success(n) > Success(n - 1)
        assert Success(n) < Success(n + 1)
        assert Failure(n) < Success(n)
Exemplo n.º 9
0
def test_failsafe_decorator_combined():
    @failsafe(predicate=bool, failure_on_value=42)
    def wrap(x):
        return x

    assert wrap(True) == Success(True)
    assert wrap(False) == Failure(False)
    assert wrap('something') == Success('something')
    assert wrap('') == Failure('')
    assert wrap([False]) == Success([False])
    assert wrap([]) == Failure([])
    assert wrap(1) == Success(1)
    assert wrap(0) == Failure(0)
    assert wrap(None) == Failure(None)
    assert wrap(42) == Failure(42)
Exemplo n.º 10
0
def test_compare():
    for n in test_range:
        assert Failure(n) == Failure(n)
        assert Success(n) == Success(n)
        assert Failure(n) != Success(n)
Exemplo n.º 11
0
def test_match_failure_and_success():
    assert Failure(0).match(lambda v: v + 1, lambda v: v / 2) == 1
    assert Success(10).match(lambda v: v + 1, lambda v: v / 2) == 5
Exemplo n.º 12
0
def test_match_failure_only():
    assert Failure(0).match(lambda v: v + 1) == 1
    assert Success(10).match(lambda v: v + 1) == 10
Exemplo n.º 13
0
# -*- coding: utf-8 -*-
# Copyright (c) 2012-2014, Philip Xu <*****@*****.**>
# License: BSD New, see LICENSE for details.
import pytest

from smonad.actions import ftry
from smonad.decorators import failsafe
from smonad.exceptions import ExtractError
from smonad.types import Try, Failure, Success


test_range = range(-100, 100)
unit = Try.unit

error = Failure('Error')


def add_1(n):
    if isinstance(n, int):
        return unit(n + 1)
    else:
        return error


def double(n):
    if isinstance(n, int):
        return unit(n * 2)
    else:
        return error