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
示例#2
0
def test_match_not_equals() -> None:
    with match(Some(42)) as case:
        if case(Some(4)):
            assert False

        if case._:
            assert True
示例#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
示例#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
示例#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
示例#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)
示例#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
示例#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
示例#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
    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
示例#11
0
def test_option_some_iterate():
    xs = Some(42)

    for x in option.to_list(xs):
        assert x == 42
        break
    else:
        assert False
示例#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
示例#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
示例#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
示例#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
示例#16
0
def test_option_some_default_value():
    xs: Option[int] = Some(42)
    zs = xs.default_value(0)

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

    assert xs != ys
    assert ys != xs
示例#20
0
def test_pipeline_none():

    hn = pipeline()

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

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

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