Пример #1
0
def test_output_funnel_drain(funnel: OutputFunnel) -> None:
    with funnel.plugged():
        funnel.write(b"A")
        text = funnel.drain()
        assert text == "A"

        funnel.write(b"B")
        assert response_texts(funnel) == [["B"]]
    assert written(funnel) == b"B"
Пример #2
0
def test_output_funnel_2nd_plug(funnel: OutputFunnel) -> None:
    with funnel.plugged():
        funnel.write(b"B")
        assert response_texts(funnel) == [["B"]]
        with funnel.plugged():
            funnel.write(b"C")
            assert response_texts(funnel) == [["B"], ["C"]]
        assert response_texts(funnel) == [["B", "C"]]
    assert written(funnel) == b"BC"
Пример #3
0
def test_output_funnel_context_drain(funnel: OutputFunnel) -> None:
    funnel.write(b"A")
    assert written(funnel) == b"A"
    with funnel.plugged():
        funnel.write(b"B")
        assert response_texts(funnel) == [["B"]]
        code = funnel.drain()
        assert response_texts(funnel) == [[]]
    assert code == "B"
    assert written(funnel) == b"A"
Пример #4
0
def test_output_funnel_context_raise(funnel: OutputFunnel) -> None:
    try:
        funnel.write(b"A")
        assert written(funnel) == b"A"
        with funnel.plugged():
            funnel.write(b"B")
            assert response_texts(funnel) == [["B"]]
            raise Exception("Test exception")
    except Exception as e:
        assert "%s" % e == "Test exception"
    finally:
        assert response_texts(funnel) == []
Пример #5
0
def test_output_funnel_context_nesting(funnel: OutputFunnel) -> None:
    funnel.write(b"A")
    assert written(funnel) == b"A"
    with funnel.plugged():
        funnel.write(b"B")
        assert response_texts(funnel) == [["B"]]
        with funnel.plugged():
            funnel.write(b"C")
            assert response_texts(funnel) == [["B"], ["C"]]
        assert response_texts(funnel) == [["B", "C"]]
    assert written(funnel) == b"ABC"
Пример #6
0
def test_output_funnel_plugged(funnel: OutputFunnel) -> None:
    with funnel.plugged():
        funnel.write(b"B")
        assert response_texts(funnel) == [["B"]]
Пример #7
0
def test_output_funnel_not_plugged(funnel: OutputFunnel) -> None:
    funnel.write(b"A")
    assert written(funnel) == b"A"
Пример #8
0
def test_output_funnel_try_finally(funnel: OutputFunnel) -> None:
    try:
        funnel.write(b"try1\n")
        try:
            funnel.write(b"try2\n")
            raise Exception("Error")
        except Exception:
            funnel.write(b"except2\n")
            raise
        finally:
            funnel.write(b"finally2\n")
    except Exception as e:
        funnel.write(b"except1\n")
        funnel.write(str(e).encode("ascii") + b"\n")
    finally:
        funnel.write(b"finally1\n")
    assert written(
        funnel) == b"try1\ntry2\nexcept2\nfinally2\nexcept1\nError\nfinally1\n"