def test_not_match_isinstance() -> None: with match(A()) as case: if case(B): assert False if case._: assert True
async def message_loop(current: Option[AsyncDisposable], is_stopped: bool, current_id: int) -> TailCallResult[None]: cmd = await inbox.receive() with match(cmd) as case: for xs in case(InnerObservableMsg[TSource]): next_id = current_id + 1 for disp in current.to_list(): await disp.dispose_async() inner = await xs.subscribe_async(obv(inbox, next_id)) current, current_id = Some(inner), next_id break for idx in case(InnerCompletedMsg[Key]): if is_stopped and idx == current_id: await safe_obv.aclose() current, is_stopped = Nothing, True break while case(CompletedMsg): if current.is_none(): await safe_obv.aclose() break while case(DisposeMsg): if current.is_some(): await current.value.dispose_async() current, is_stopped = Nothing, True break return TailCall(current, is_stopped, current_id)
def test_match_type() -> None: with match(42) as case: for value in case(int): assert value == 42 break else: assert False
def test_match_instance() -> None: with match(42) as case: for value in case(42): assert value == 42 break else: assert False
def test_not_match_type() -> None: with match(42) as case: if case(float): assert False if case._: assert True
def test_match_not_equals() -> None: with match(Some(42)) as case: if case(Some(4)): assert False if case._: assert True
def test_match_isinstance() -> None: with match(B()) as case: for _ in case(A): assert True break else: assert False
def test_active_pattern_not_matches() -> None: text = "abc" with match(text) as case: for _ in case(ParseInteger): assert False if case._: assert True
def test_not_match_instance() -> None: x = 42 with match(x) as case: if case(43): assert False if case._: assert True
def test_active_pattern_matches() -> None: text = "42" with match(text) as case: for value in case(ParseInteger): assert value == int(text) if case._: assert False
def test_result_match_ok(): xs: Result[int, str] = Ok(42) with match(xs) as case: for x in case(Ok[int, str]): assert x == 42 break else: assert False
def test_result_match_error(): xs: Result[int, str] = Error("err") with match(xs) as case: for err in case(Error[int, str]): assert err == "err" 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_try_match_failure(): error = Exception("err") xs: Try[int] = Failure(error) with match(xs) as case: for err in case(Failure[int]): assert err == error break else: assert False
async def get_value(n: Notification[Any]) -> Option[Any]: with match(n) as case: for value in case(OnNext[TSource]): return Some(value) for err in case(OnError[TSource]): await safe_obv.athrow(err) while case.default(): await safe_obv.aclose()
def test_match_multiple_only_matches_first() -> None: with match("expression") as case: for value in case(str): assert value == "expression" for value in case(str): assert False if case._: assert False
async def matcher() -> None: with match(ns) as case: for x in case(OnNext[TSource]): await aobv.asend(x) return for err in case(OnError[TSource]): await aobv.athrow(err) return for x in case(OnCompleted): await aobv.aclose() return
def test_choice_choice1of2(): xs: Choice2[int, str] = Choice1of2(42) assert isinstance(xs, Choice) assert isinstance(xs, Choice2) with match(xs) as case: for x in Choice1of2.match(case): assert x == 42 break else:
def test_option_none_match(): xs = Nothing with match(xs) as case: for _ in case(Some[int]): assert False while case(Nothing): assert True while case.default(): assert False
async def text( next: HttpFunc[str, TResult], context: HttpContext, ) -> HttpFuncResult[TResult]: """Text decoding handler.""" resp = context.Response ret: str = "" with match(context.Response) as case: for resp in case(Success[ClientResponse]): ret = await resp.text() return await next(context.replace(Response=ret))
def test_match_multiple_cases() -> None: with match("expression") as case: while case("rxpy"): assert False for value in case(str): assert value == "expression" for value in case(float): assert False if case._: assert False
async def update(msg: Msg[TSource], model: Model[TSource]) -> Model[TSource]: # log.debug("update: %s, model: %s", msg, model) with match(msg) as case: for xs in case(InnerObservableMsg[TSource]): if max_concurrent == 0 or len( model.subscriptions) < max_concurrent: inner = await xs.subscribe_async(obv(model.key) ) return model.replace( subscriptions=model.subscriptions.add( model.key, inner), key=Key(model.key + 1), ) lst = FrozenList.singleton(xs) return model.replace(queue=model.queue.append(lst)) for key in case(InnerCompletedMsg[Key]): subscriptions = model.subscriptions.remove(key) if len(model.queue): xs = model.queue[0] inner = await xs.subscribe_async(obv(model.key) ) return model.replace( subscriptions=subscriptions.add( model.key, inner), key=model.key + 1, queue=model.queue.tail(), ) elif len(subscriptions): return model.replace( subscriptions=subscriptions) else: if model.is_stopped: await safe_obv.aclose() return model.replace(subscriptions=map.empty) while case(CompletedMsg): if not model.subscriptions: log.debug("merge_inner: closing!") await safe_obv.aclose() return model.replace(is_stopped=True) while case.default(): for dispose in model.subscriptions.values(): await dispose.dispose_async() return initial_model.replace(is_stopped=True)
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
async def get_latest() -> Notification[TSource]: with match(n) as case: for x in case(OnNext[TSource]): if n == latest: break try: await safe_obv.asend(x) except Exception as ex: await safe_obv.athrow(ex) break for err in case(OnError[TSource]): await safe_obv.athrow(err) break while case(OnCompleted): await safe_obv.aclose() break return n
async def message_loop( current_index: int) -> TailCallResult[NoReturn]: n, index = await inbox.receive() with match(n) as case: log.debug("debounce: %s, %d, %d", n, index, current_index) for x in case(OnNext[TSource]): if index == current_index: await safe_obv.asend(x) current_index = index elif index > current_index: current_index = index for err in case(OnError[TSource]): await safe_obv.athrow(err) while case(OnCompleted): await safe_obv.aclose() return TailCall(current_index)
async def message_loop( source_value: Option[TSource], other_value: Option[TOther] ) -> TailCallResult[NoReturn]: cn = await inbox.receive() async def get_value(n: Notification[Any]) -> Option[Any]: with match(n) as m: for value in case(OnNext[TSource]): return Some(value) for err in case(OnError): await safe_obv.athrow(err) while m.default(): await safe_obv.aclose() return Nothing with match(cn) as case: for value in case(SourceMsg[TSource]): source_value = await get_value(value) break for value in case(OtherMsg[TOther]): other_value = await get_value(value) break def binder(s: TSource) -> Option[Tuple[TSource, TOther]]: def mapper(o: TOther) -> Tuple[TSource, TOther]: return (s, o) return other_value.map(mapper) combined = source_value.bind(binder) for x in combined.to_list(): await safe_obv.asend(x) return TailCall(source_value, other_value)
def test_match_generic_matches() -> None: xs = [1, 2, 3] with match(xs) as case: for x in case(List[int]): assert x == xs
def test_default_matches() -> None: with match(42) as case: for value in case._: assert value == 42
def test_default_falsy_matches() -> None: with match(None) as case: if case._: assert True else: assert False
def test_match_destructure() -> None: xs: FrozenList[int] = FrozenList.empty().cons(42) with match(xs) as case: for (head, *_) in case(FrozenList[int]): assert head == 42