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
Esempio 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
Esempio 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
Esempio 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
Esempio 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
Esempio 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)
Esempio 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
Esempio 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
Esempio 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
Esempio 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
Esempio 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
Esempio 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
Esempio 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
Esempio 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
Esempio 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
Esempio n. 16
0
def test_option_some_default_value():
    xs: Option[int] = Some(42)
    zs = xs.default_value(0)

    assert zs == 42
Esempio n. 17
0
def test_option_some_is_none():
    xs = Some(42)
    assert not xs.is_none()
Esempio 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
Esempio n. 19
0
def test_option_none_not_equals_some():
    xs = Some(42)
    ys = Nothing

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

    hn = pipeline()

    assert hn(42) == Some(42)
Esempio 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
Esempio n. 22
0
    def fn() -> Generator[int, int, int]:
        x = yield 42
        y = yield from Some(43)

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

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