-
Notifications
You must be signed in to change notification settings - Fork 791
Expand file tree
/
Copy pathtest_shortcuts.py
More file actions
100 lines (85 loc) · 3.17 KB
/
test_shortcuts.py
File metadata and controls
100 lines (85 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from __future__ import annotations
from unittest.mock import patch
from prompt_toolkit.shortcuts import print_container
from prompt_toolkit.shortcuts.prompt import PromptSession, _split_multiline_prompt
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_prompt_per_call_override_restore():
"""Per-call kwargs to prompt() should not permanently change session state (#967)."""
session = PromptSession()
# Verify defaults.
assert session.is_password is False
assert session.multiline is False
assert session.wrap_lines is True
assert session.message == ""
# Call prompt() with overrides. app.run will raise (no terminal), but the
# finally block should still restore the original values.
with patch.object(session.app, "run", side_effect=EOFError):
try:
session.prompt(
"test> ",
is_password=True,
multiline=True,
wrap_lines=False,
)
except EOFError:
pass
# All overridden attributes should be restored.
assert session.is_password is False
assert session.multiline is False
assert session.wrap_lines is True
assert session.message == ""
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) as fd:
text = fd.read()
assert "Hello world" in text
assert "Title" in text