From e106bf94eff07d9a59771d9ccc4406421e18ab64 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 4 May 2024 19:35:20 +0200 Subject: Adding upstream version 3.0.36. Signed-off-by: Daniel Baumann --- tests/test_shortcuts.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 tests/test_shortcuts.py (limited to 'tests/test_shortcuts.py') diff --git a/tests/test_shortcuts.py b/tests/test_shortcuts.py new file mode 100644 index 0000000..10ee73a --- /dev/null +++ b/tests/test_shortcuts.py @@ -0,0 +1,67 @@ +from prompt_toolkit.shortcuts import print_container +from prompt_toolkit.shortcuts.prompt import _split_multiline_prompt +from prompt_toolkit.shortcuts.utils import print_container +from prompt_toolkit.widgets import Frame, TextArea + + +def test_split_multiline_prompt(): + # Test 1: no newlines: + tokens = [("class:testclass", "ab")] + has_before_tokens, before, first_input_line = _split_multiline_prompt( + lambda: tokens + ) + assert has_before_tokens() is False + assert before() == [] + assert first_input_line() == [ + ("class:testclass", "a"), + ("class:testclass", "b"), + ] + + # Test 1: multiple lines. + tokens = [("class:testclass", "ab\ncd\nef")] + has_before_tokens, before, first_input_line = _split_multiline_prompt( + lambda: tokens + ) + assert has_before_tokens() is True + assert before() == [ + ("class:testclass", "a"), + ("class:testclass", "b"), + ("class:testclass", "\n"), + ("class:testclass", "c"), + ("class:testclass", "d"), + ] + assert first_input_line() == [ + ("class:testclass", "e"), + ("class:testclass", "f"), + ] + + # Edge case 1: starting with a newline. + tokens = [("class:testclass", "\nab")] + has_before_tokens, before, first_input_line = _split_multiline_prompt( + lambda: tokens + ) + assert has_before_tokens() is True + assert before() == [] + assert first_input_line() == [("class:testclass", "a"), ("class:testclass", "b")] + + # Edge case 2: starting with two newlines. + tokens = [("class:testclass", "\n\nab")] + has_before_tokens, before, first_input_line = _split_multiline_prompt( + lambda: tokens + ) + assert has_before_tokens() is True + assert before() == [("class:testclass", "\n")] + assert first_input_line() == [("class:testclass", "a"), ("class:testclass", "b")] + + +def test_print_container(tmpdir): + # Call `print_container`, render to a dummy file. + f = tmpdir.join("output") + with open(f, "w") as fd: + print_container(Frame(TextArea(text="Hello world!\n"), title="Title"), file=fd) + + # Verify rendered output. + with open(f, "r") as fd: + text = fd.read() + assert "Hello world" in text + assert "Title" in text -- cgit v1.2.3