Ejemplo n.º 1
0
    def fn():
        z: str = "Not found"
        for x in Some(42.0):
            for y in Some(int(x)):
                z = yield from Some(str(y))

        return z
Ejemplo n.º 2
0
def test_match_not_equals() -> None:
    with match(Some(42)) as case:
        if case(Some(4)):
            assert False

        if case._:
            assert True
Ejemplo n.º 3
0
def test_option_some_bind_none_fluent():
    xs = Some(42)
    ys = xs.bind(lambda x: Nothing)

    for _ in ys.match(Nothing):
        assert True
        break
    else:
        assert False
Ejemplo n.º 4
0
def test_option_some_bind_fluent():
    xs = Some(42)
    ys = xs.bind(lambda x: Some(x + 1))

    for value in ys.match(Some):
        assert value == 43
        break
    else:
        assert False
Ejemplo n.º 5
0
def test_option_some_map_fluent():
    xs = Some(42)
    ys = xs.map(lambda x: x + 1)

    for value in ys.match(Some):
        assert value == 43
        break
    else:
        assert False
Ejemplo n.º 6
0
def test_pipeline_works():
    fn: Callable[[int], Option[int]] = lambda x: Some(x * 10)
    gn: Callable[[int], Option[int]] = lambda x: Some(x + 10)

    hn = pipeline(
        fn,
        gn,
    )

    assert hn(42) == Some(430)
Ejemplo n.º 7
0
def test_option_some_bind_piped():
    binder: Callable[[int], Option[int]] = lambda x: Some(x + 1)
    xs = Some(42)
    ys = xs.pipe(option.bind(binder), )

    for value in ys.match(Some):
        assert value == 43
        break
    else:
        assert False
Ejemplo n.º 8
0
def test_option_some_map_piped():
    xs = Some(42)
    mapper: Callable[[int], int] = lambda x: x + 1
    ys: Option[int] = xs.pipe(option.map(mapper))

    for y in ys.match(Some):
        assert y == 43
        break
    else:
        assert False
Ejemplo n.º 9
0
def test_option_some_map2_piped(x: int, y: int):
    xs = Some(x)
    ys = Some(y)
    mapper: Callable[[int, int], int] = lambda x, y: x + y
    zs = pipe2((xs, ys), option.map2(mapper))

    for value in zs.match(Some):
        assert value == x + y
        break
    else:
        assert False
Ejemplo n.º 10
0
    def fn():
        x = yield from Nothing  # or a function returning Nothing

        # -- The rest of the function will never be executed --
        y = yield from Some(43)

        return x + y
Ejemplo n.º 11
0
def test_option_some_iterate():
    xs = Some(42)

    for x in option.to_list(xs):
        assert x == 42
        break
    else:
        assert False
Ejemplo n.º 12
0
def test_option_some_match():
    xs = Some(42)

    with match(xs) as case:
        for x in case(Some[int]):
            assert x == 42

        while case.default():
            assert False
Ejemplo n.º 13
0
def test_pipeline_error():
    fn: Callable[[int], Option[int]] = lambda x: Some(x * 10)
    gn: Callable[[int], Option[int]] = lambda x: Nothing

    hn = pipeline(
        fn,
        gn,
    )

    assert hn(42) == Nothing
Ejemplo n.º 14
0
def test_option_builder_yield_some_wrapped():
    @effect.option
    def fn() -> Generator[Option[int], Option[int], Option[int]]:
        x = yield Some(42)
        return x

    xs = fn()
    for value in xs.match(Some):
        assert value == Some(42)
        break
    else:
        assert False
Ejemplo n.º 15
0
    def matcher(value: str) -> Option[str]:
        with match(value) as case:
            while case("rxpy"):
                assert False

            for value in case(str):
                assert value == "expression"
                return Some(value)

            for value in case("aioreactive"):
                assert False

            if case._:
                assert False

        return Nothing
Ejemplo n.º 16
0
def test_option_some_default_value():
    xs: Option[int] = Some(42)
    zs = xs.default_value(0)

    assert zs == 42
Ejemplo n.º 17
0
def test_option_some_is_none():
    xs = Some(42)
    assert not xs.is_none()
Ejemplo n.º 18
0
def test_option_some_equals_some(a: Any, b: Any):
    xs = Some(a)
    ys = Some(b)

    assert xs == ys if a == b else xs != ys
Ejemplo n.º 19
0
def test_option_none_not_equals_some():
    xs = Some(42)
    ys = Nothing

    assert xs != ys
    assert ys != xs
Ejemplo n.º 20
0
def test_pipeline_none():

    hn = pipeline()

    assert hn(42) == Some(42)
Ejemplo n.º 21
0
def test_option_filter_some():
    xs = Some(42)
    assert xs.filter(lambda x: x > 41) == Some(42)
    assert xs.filter(lambda x: x > 42) == Nothing
Ejemplo n.º 22
0
    def fn() -> Generator[int, int, int]:
        x = yield 42
        y = yield from Some(43)

        return x + y
Ejemplo n.º 23
0
    def fn() -> Generator[int, int, int]:
        x: int
        x = yield from Nothing
        y = yield from Some(43)

        return x + y
Ejemplo n.º 24
0
 def fn() -> Generator[int, int, int]:
     x = yield from Some(42)
     return x + 1
Ejemplo n.º 25
0
 def fn() -> Generator[Option[int], Option[int], Option[int]]:
     x = yield Some(42)
     return x
Ejemplo n.º 26
0
def test_option_some_to_list():
    xs = Some(42)
    assert xs.to_list() == [42]
Ejemplo n.º 27
0
def test_option_some_to_seq():
    xs = Some(42)
    assert list(xs.to_seq()) == [42]
Ejemplo n.º 28
0
def test_option_some_to_str():
    xs = Some(42)
    assert str(xs) == f"Some {xs.value}"
Ejemplo n.º 29
0
 def unfolder(state: int) -> Option[Tuple[int, int]]:
     if state < x:
         return Some((state, state + 1))
     return Nothing
Ejemplo n.º 30
0
def test_option_some_is_some():
    xs = Some(42)
    assert xs.is_some()