Пример #1
0
def test_match_not_equals() -> None:
    with match(Some(42)) as case:
        if case(Some(4)):
            assert False

        if case._:
            assert True
Пример #2
0
def test_match_instance() -> None:
    with match(42) as case:
        for value in case(42):
            assert value == 42
            break
        else:
            assert False
Пример #3
0
def test_match_isinstance() -> None:
    with match(B()) as case:
        for _ in case(A):
            assert True
            break
        else:
            assert False
Пример #4
0
def test_not_match_isinstance() -> None:
    with match(A()) as case:
        if case(B):
            assert False

        if case._:
            assert True
Пример #5
0
def test_not_match_type() -> None:
    with match(42) as case:
        if case(float):
            assert False

        if case._:
            assert True
Пример #6
0
def test_match_type() -> None:
    with match(42) as case:
        for value in case(int):
            assert value == 42
            break
        else:
            assert False
Пример #7
0
def test_active_pattern_not_matches() -> None:
    text = "abc"
    with match(text) as case:
        for _ in case(ParseInteger):
            assert False

        if case._:
            assert True
Пример #8
0
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
Пример #9
0
def test_not_match_instance() -> None:
    x = 42
    with match(x) as case:
        if case(43):
            assert False

        if case._:
            assert True
Пример #10
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
Пример #11
0
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
Пример #12
0
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
Пример #13
0
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
Пример #14
0
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:
Пример #15
0
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
Пример #16
0
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
Пример #17
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
Пример #18
0
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
Пример #19
0
def test_default_matches() -> None:
    with match(42) as case:
        for value in case._:
            assert value == 42
Пример #20
0
def test_default_falsy_matches() -> None:
    with match(None) as case:
        if case._:
            assert True
        else:
            assert False
Пример #21
0
def test_match_generic_matches() -> None:
    xs = [1, 2, 3]

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