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
def test_match_not_equals() -> None: with match(Some(42)) as case: if case(Some(4)): assert False if case._: assert True
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
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
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
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)
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
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
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
def test_option_some_iterate(): xs = Some(42) for x in option.to_list(xs): assert x == 42 break else: assert False
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
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
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
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
def test_option_some_default_value(): xs: Option[int] = Some(42) zs = xs.default_value(0) assert zs == 42
def test_option_some_is_none(): xs = Some(42) assert not xs.is_none()
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
def test_option_none_not_equals_some(): xs = Some(42) ys = Nothing assert xs != ys assert ys != xs
def test_pipeline_none(): hn = pipeline() assert hn(42) == Some(42)
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
def fn() -> Generator[int, int, int]: x = yield 42 y = yield from Some(43) return x + y
def fn() -> Generator[int, int, int]: x: int x = yield from Nothing y = yield from Some(43) return x + y
def fn() -> Generator[int, int, int]: x = yield from Some(42) return x + 1
def fn() -> Generator[Option[int], Option[int], Option[int]]: x = yield Some(42) return x
def test_option_some_to_list(): xs = Some(42) assert xs.to_list() == [42]
def test_option_some_to_seq(): xs = Some(42) assert list(xs.to_seq()) == [42]
def test_option_some_to_str(): xs = Some(42) assert str(xs) == f"Some {xs.value}"
def unfolder(state: int) -> Option[Tuple[int, int]]: if state < x: return Some((state, state + 1)) return Nothing
def test_option_some_is_some(): xs = Some(42) assert xs.is_some()