Beispiel #1
0
def test_flatten() -> None:
    m_empty: Maybe[Maybe[str]] = Just(Nothing())
    m_flat: Maybe[str] = m_empty.flatten()
    assert m_flat.or_else("backup") == "backup"

    m_full: Maybe[Maybe[str]] = Just(Just("becon"))
    assert m_full.flatten().or_else("backup") == "becon"
Beispiel #2
0
def test_as_iterable() -> None:
    m_empty: Maybe[str] = Nothing()
    i = 0
    for n in m_empty:
        i = i + 1
    assert i == 0

    for n in Just("one"):
        i = i + 1
    assert i == 1

    assert len(m_empty) == 0
    assert len(Just("one")) == 1
 def __callback(self, value):
     self.semaphore.acquire()
     self.cache = Just(value)
     while len(self.subscribers) > 0:
         sub = self.subscribers.pop(0)
         t = threading.Thread(target=sub, args=[value])
         t.start()
     self.semaphore.release()
Beispiel #4
0
def test_from_ok() -> None:
    assert Just(3) == Maybe.fromResult(Ok(3))
Beispiel #5
0
def test_from_nonempty_list() -> None:
    assert Just(2) == Maybe.fromList([2, 4, 6])
Beispiel #6
0
def test_last() -> None:
    maybes: List[Maybe[int]] = [Just(1), Just(2), Nothing()]
    assert Just(2) == last(maybes)
Beispiel #7
0
def test_first() -> None:
    maybes: List[Maybe[int]] = [Nothing(), Just(1), Just(2)]
    assert Just(1) == first(maybes)
Beispiel #8
0
def test_just_withdefault() -> None:
    m: Maybe[int] = Just(5)
    assert 5 == m.withDefault(0)
Beispiel #9
0
def test_from_optional_value() -> None:
    assert Just(2) == Maybe.fromOptional(2)
def test_ok_to_maybe() -> None:
    result: Result[int, str] = Ok(6)
    assert Just(6) == result.toMaybe()
Beispiel #11
0
def test_bind_nothing() -> None:
    m: Maybe[int] = Nothing()
    increment: Callable[[int], Maybe[int]] = lambda x: Just(x + 1)
    assert Nothing() == m.bind(increment)
Beispiel #12
0
def test_bind_just() -> None:
    m: Maybe[int] = Just(5)
    increment: Callable[[int], Maybe[int]] = lambda x: Just(x + 1)
    assert Just(6) == m.bind(increment)
Beispiel #13
0
def test_or_else() -> None:
    m_empty: Maybe[str] = Nothing()
    assert m_empty.or_else("backup") == "backup"

    m_full: Maybe[str] = Just("becon")
    assert m_full.or_else("backup") == "becon"
Beispiel #14
0
def test_just_to_optional() -> None:
    assert 2 == Just(2).toOptional()
import pytest  # type: ignore
from typing import Any, Callable, Tuple, Type

from monads.list import List
from monads.monoid import Monoid, String, Addition, Multiplication
from monads.maybe import First, Last, Just

Constructor = Tuple[Type, Callable[[Any], Any]]


@pytest.fixture(
    scope="module",
    params=[
        (First, lambda x: Just(x)),
        (Last, lambda x: Just(x)),
        (String, lambda x: str(x)),
        (Addition, lambda x: x),
        (Multiplication, lambda x: x),
        (List, lambda x: [x]),
    ],
)
def constructor(request) -> Constructor:
    return request.param


def construct(constructor: Constructor, value: Any) -> Monoid:
    cls, builder = constructor
    return cls(builder(value))


def test_mappend_add_operator(constructor: Constructor) -> None:
def test_from_just() -> None:
    m: Maybe[int] = Just(6)
    result: Result[int, str] = Result.fromMaybe(m, "error")
    assert Ok(6) == result
Beispiel #17
0
def test_apply_just_to_just() -> None:
    m: Maybe[int] = Just(5)
    increment: Callable[[int], int] = lambda x: x + 1
    assert Just(6) == m.apply(Maybe.pure(increment))
Beispiel #18
0
def test_apply_nothing_to_just() -> None:
    m: Maybe[int] = Just(5)
    f: Maybe[Callable[[int], int]] = Nothing()
    assert Nothing() == m.apply(f)
Beispiel #19
0
def test_just_to_result() -> None:
    assert Ok(3) == Just(3).toResult("oops")