Exemplo n.º 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"
Exemplo n.º 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
Exemplo n.º 3
0
def test_from_nothing() -> None:
    m: Maybe[int] = Nothing()
    result: Result[int, str] = Result.fromMaybe(m, "error")
    assert Err("error") == result
Exemplo n.º 4
0
def test_from_empty_list() -> None:
    assert Nothing() == Maybe.fromList([])
Exemplo n.º 5
0
def test_last() -> None:
    maybes: List[Maybe[int]] = [Just(1), Just(2), Nothing()]
    assert Just(2) == last(maybes)
Exemplo n.º 6
0
def test_first() -> None:
    maybes: List[Maybe[int]] = [Nothing(), Just(1), Just(2)]
    assert Just(1) == first(maybes)
Exemplo n.º 7
0
def test_nothing_withdefault() -> None:
    m: Maybe[int] = Nothing()
    assert 0 == m.withDefault(0)
Exemplo n.º 8
0
def test_apply_nothing_to_nothing() -> None:
    m: Maybe[int] = Nothing()
    f: Maybe[Callable[[int], int]] = Nothing()
    assert Nothing() == m.apply(f)
Exemplo n.º 9
0
def test_nothing_to_result() -> None:
    assert Err("oops") == Nothing().toResult("oops")
Exemplo n.º 10
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)
Exemplo n.º 11
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"
Exemplo n.º 12
0
def test_nothing_to_optional() -> None:
    assert None == Nothing().toOptional()
Exemplo n.º 13
0
def test_from_optional_none() -> None:
    assert Nothing() == Maybe.fromOptional(None)
Exemplo n.º 14
0
def test_err_to_maybe() -> None:
    result: Result[int, str] = Err("error")
    assert Nothing() == result.toMaybe()
Exemplo n.º 15
0
def test_apply_just_to_nothing() -> None:
    m: Maybe[int] = Nothing()
    increment: Callable[[int], int] = lambda x: x + 1
    assert Nothing() == m.apply(Maybe.pure(increment))
Exemplo n.º 16
0
 def __init__(self, f):
     self.subscribers = []
     self.cache = Nothing()
     self.semaphore = threading.BoundedSemaphore(1)
     f(self.__callback)
Exemplo n.º 17
0
def test_from_err() -> None:
    assert Nothing() == Maybe.fromResult(Err("oops"))