예제 #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"