prompt-toolkit / python-prompt-toolkit

Library for building powerful interactive command line applications in Python
https://python-prompt-toolkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
9.28k stars 715 forks source link

How to unit-test multi-line prompts #1776

Open jeanlucthumm opened 1 year ago

jeanlucthumm commented 1 year ago

I'm using a mock PipeInput with .send_text to simulate user input. Recently, I've switched to multi-line inputs which can be terminated by either Meta+Enter or Esc then Enter. What string should I pass to .send_text to replicate that?

I've tried the ASCII code for Esc:

mock_input.send_text("First Reply\x1B\nSecond Reply\x1B\n")

But the program still hangs when prompt() is called

Fixture created like this:

@pytest.fixture
def mock_input():
    with create_pipe_input() as pipe_input:
        with create_app_session(input=pipe_input, output=DummyOutput()):
            yield pipe_input

Called like in this in code under test:

    msg = prompt("> ", multiline=True)