Show More
@@ -1,233 +1,238 b'' | |||
|
1 | 1 | import pytest |
|
2 | 2 | from IPython.terminal.shortcuts.auto_suggest import ( |
|
3 | 3 | accept_in_vi_insert_mode, |
|
4 | 4 | accept_token, |
|
5 | 5 | accept_character, |
|
6 | 6 | accept_word, |
|
7 | 7 | accept_and_keep_cursor, |
|
8 | 8 | NavigableAutoSuggestFromHistory, |
|
9 | 9 | swap_autosuggestion_up, |
|
10 | 10 | swap_autosuggestion_down, |
|
11 | 11 | ) |
|
12 | 12 | |
|
13 | 13 | from prompt_toolkit.history import InMemoryHistory |
|
14 | from prompt_toolkit.shortcuts import PromptSession | |
|
15 | 14 | from prompt_toolkit.buffer import Buffer |
|
16 | 15 | |
|
17 | 16 | from unittest.mock import patch, Mock |
|
18 | 17 | |
|
19 | 18 | |
|
20 | 19 | def make_event(text, cursor, suggestion): |
|
21 | 20 | event = Mock() |
|
22 | 21 | event.current_buffer = Mock() |
|
23 | 22 | event.current_buffer.suggestion = Mock() |
|
24 | 23 | event.current_buffer.text = text |
|
25 | 24 | event.current_buffer.cursor_position = cursor |
|
26 | 25 | event.current_buffer.suggestion.text = suggestion |
|
27 | 26 | event.current_buffer.document = Mock() |
|
28 | 27 | event.current_buffer.document.get_end_of_line_position = Mock(return_value=0) |
|
29 | 28 | event.current_buffer.document.text = text |
|
30 | 29 | event.current_buffer.document.cursor_position = cursor |
|
31 | 30 | return event |
|
32 | 31 | |
|
33 | 32 | |
|
34 | 33 | @pytest.mark.parametrize( |
|
35 | 34 | "text, cursor, suggestion, called", |
|
36 | 35 | [ |
|
37 | 36 | ("123456", 6, "123456789", True), |
|
38 | 37 | ("123456", 3, "123456789", False), |
|
39 | 38 | ("123456 \n789", 6, "123456789", True), |
|
40 | 39 | ], |
|
41 | 40 | ) |
|
42 | 41 | def test_autosuggest_at_EOL(text, cursor, suggestion, called): |
|
43 | 42 | """ |
|
44 | 43 | test that autosuggest is only applied at end of line. |
|
45 | 44 | """ |
|
46 | 45 | |
|
47 | 46 | event = make_event(text, cursor, suggestion) |
|
48 | 47 | event.current_buffer.insert_text = Mock() |
|
49 | 48 | accept_in_vi_insert_mode(event) |
|
50 | 49 | if called: |
|
51 | 50 | event.current_buffer.insert_text.assert_called() |
|
52 | 51 | else: |
|
53 | 52 | event.current_buffer.insert_text.assert_not_called() |
|
54 | 53 | # event.current_buffer.document.get_end_of_line_position.assert_called() |
|
55 | 54 | |
|
56 | 55 | |
|
57 | 56 | @pytest.mark.parametrize( |
|
58 | 57 | "text, suggestion, expected", |
|
59 | 58 | [ |
|
60 | 59 | ("", "def out(tag: str, n=50):", "def "), |
|
61 | 60 | ("d", "ef out(tag: str, n=50):", "ef "), |
|
62 | 61 | ("de ", "f out(tag: str, n=50):", "f "), |
|
63 | 62 | ("def", " out(tag: str, n=50):", " "), |
|
64 | 63 | ("def ", "out(tag: str, n=50):", "out("), |
|
65 | 64 | ("def o", "ut(tag: str, n=50):", "ut("), |
|
66 | 65 | ("def ou", "t(tag: str, n=50):", "t("), |
|
67 | 66 | ("def out", "(tag: str, n=50):", "("), |
|
68 | 67 | ("def out(", "tag: str, n=50):", "tag: "), |
|
69 | 68 | ("def out(t", "ag: str, n=50):", "ag: "), |
|
70 | 69 | ("def out(ta", "g: str, n=50):", "g: "), |
|
71 | 70 | ("def out(tag", ": str, n=50):", ": "), |
|
72 | 71 | ("def out(tag:", " str, n=50):", " "), |
|
73 | 72 | ("def out(tag: ", "str, n=50):", "str, "), |
|
74 | 73 | ("def out(tag: s", "tr, n=50):", "tr, "), |
|
75 | 74 | ("def out(tag: st", "r, n=50):", "r, "), |
|
76 | 75 | ("def out(tag: str", ", n=50):", ", n"), |
|
77 | 76 | ("def out(tag: str,", " n=50):", " n"), |
|
78 | 77 | ("def out(tag: str, ", "n=50):", "n="), |
|
79 | 78 | ("def out(tag: str, n", "=50):", "="), |
|
80 | 79 | ("def out(tag: str, n=", "50):", "50)"), |
|
81 | 80 | ("def out(tag: str, n=5", "0):", "0)"), |
|
82 | 81 | ("def out(tag: str, n=50", "):", "):"), |
|
83 | 82 | ("def out(tag: str, n=50)", ":", ":"), |
|
84 | 83 | ], |
|
85 | 84 | ) |
|
86 | 85 | def test_autosuggest_token(text, suggestion, expected): |
|
87 | 86 | event = make_event(text, len(text), suggestion) |
|
88 | 87 | event.current_buffer.insert_text = Mock() |
|
89 | 88 | accept_token(event) |
|
90 | 89 | assert event.current_buffer.insert_text.called |
|
91 | 90 | assert event.current_buffer.insert_text.call_args[0] == (expected,) |
|
92 | 91 | |
|
93 | 92 | |
|
94 | 93 | @pytest.mark.parametrize( |
|
95 | 94 | "text, suggestion, expected", |
|
96 | 95 | [ |
|
97 | 96 | ("", "def out(tag: str, n=50):", "d"), |
|
98 | 97 | ("d", "ef out(tag: str, n=50):", "e"), |
|
99 | 98 | ("de ", "f out(tag: str, n=50):", "f"), |
|
100 | 99 | ("def", " out(tag: str, n=50):", " "), |
|
101 | 100 | ], |
|
102 | 101 | ) |
|
103 | 102 | def test_accept_character(text, suggestion, expected): |
|
104 | 103 | event = make_event(text, len(text), suggestion) |
|
105 | 104 | event.current_buffer.insert_text = Mock() |
|
106 | 105 | accept_character(event) |
|
107 | 106 | assert event.current_buffer.insert_text.called |
|
108 | 107 | assert event.current_buffer.insert_text.call_args[0] == (expected,) |
|
109 | 108 | |
|
110 | 109 | |
|
111 | 110 | @pytest.mark.parametrize( |
|
112 | 111 | "text, suggestion, expected", |
|
113 | 112 | [ |
|
114 | 113 | ("", "def out(tag: str, n=50):", "def "), |
|
115 | 114 | ("d", "ef out(tag: str, n=50):", "ef "), |
|
116 | 115 | ("de", "f out(tag: str, n=50):", "f "), |
|
117 | 116 | ("def", " out(tag: str, n=50):", " "), |
|
118 | 117 | # (this is why we also have accept_token) |
|
119 | 118 | ("def ", "out(tag: str, n=50):", "out(tag: "), |
|
120 | 119 | ], |
|
121 | 120 | ) |
|
122 | 121 | def test_accept_word(text, suggestion, expected): |
|
123 | 122 | event = make_event(text, len(text), suggestion) |
|
124 | 123 | event.current_buffer.insert_text = Mock() |
|
125 | 124 | accept_word(event) |
|
126 | 125 | assert event.current_buffer.insert_text.called |
|
127 | 126 | assert event.current_buffer.insert_text.call_args[0] == (expected,) |
|
128 | 127 | |
|
129 | 128 | |
|
130 | 129 | @pytest.mark.parametrize( |
|
131 | 130 | "text, suggestion, expected, cursor", |
|
132 | 131 | [ |
|
133 | 132 | ("", "def out(tag: str, n=50):", "def out(tag: str, n=50):", 0), |
|
134 | 133 | ("def ", "out(tag: str, n=50):", "out(tag: str, n=50):", 4), |
|
135 | 134 | ], |
|
136 | 135 | ) |
|
137 | 136 | def test_accept_and_keep_cursor(text, suggestion, expected, cursor): |
|
138 | 137 | event = make_event(text, cursor, suggestion) |
|
139 | 138 | buffer = event.current_buffer |
|
140 | 139 | buffer.insert_text = Mock() |
|
141 | 140 | accept_and_keep_cursor(event) |
|
142 | 141 | assert buffer.insert_text.called |
|
143 | 142 | assert buffer.insert_text.call_args[0] == (expected,) |
|
144 | 143 | assert buffer.cursor_position == cursor |
|
145 | 144 | |
|
146 | 145 | |
|
147 | 146 | def test_autosuggest_token_empty(): |
|
148 | 147 | full = "def out(tag: str, n=50):" |
|
149 | 148 | event = make_event(full, len(full), "") |
|
150 | 149 | event.current_buffer.insert_text = Mock() |
|
151 | 150 | |
|
152 | 151 | with patch( |
|
153 | 152 | "prompt_toolkit.key_binding.bindings.named_commands.forward_word" |
|
154 | 153 | ) as forward_word: |
|
155 | 154 | accept_token(event) |
|
156 | 155 | assert not event.current_buffer.insert_text.called |
|
157 | 156 | assert forward_word.called |
|
158 | 157 | |
|
159 | 158 | |
|
160 | 159 | async def test_navigable_provider(): |
|
161 | 160 | provider = NavigableAutoSuggestFromHistory() |
|
162 | 161 | history = InMemoryHistory(history_strings=["very_a", "very", "very_b", "very_c"]) |
|
163 | 162 | buffer = Buffer(history=history) |
|
164 | 163 | |
|
165 | 164 | async for _ in history.load(): |
|
166 | 165 | pass |
|
167 | 166 | |
|
168 | 167 | buffer.cursor_position = 5 |
|
169 | 168 | buffer.text = "very" |
|
170 | 169 | |
|
171 | 170 | up = swap_autosuggestion_up(provider) |
|
172 | 171 | down = swap_autosuggestion_down(provider) |
|
173 | 172 | |
|
174 | 173 | event = Mock() |
|
175 | 174 | event.current_buffer = buffer |
|
176 | 175 | |
|
177 | 176 | def get_suggestion(): |
|
178 | 177 | suggestion = provider.get_suggestion(buffer, buffer.document) |
|
179 | 178 | buffer.suggestion = suggestion |
|
180 | 179 | return suggestion |
|
181 | 180 | |
|
182 | 181 | assert get_suggestion().text == "_c" |
|
183 | 182 | |
|
184 | 183 | # should go up |
|
185 | 184 | up(event) |
|
186 | 185 | assert get_suggestion().text == "_b" |
|
187 | 186 | |
|
188 | 187 | # should skip over 'very' which is identical to buffer content |
|
189 | 188 | up(event) |
|
190 | 189 | assert get_suggestion().text == "_a" |
|
191 | 190 | |
|
192 | 191 | # should cycle back to beginning |
|
193 | 192 | up(event) |
|
194 | 193 | assert get_suggestion().text == "_c" |
|
195 | 194 | |
|
196 | 195 | # should cycle back through end boundary |
|
197 | 196 | down(event) |
|
198 | 197 | assert get_suggestion().text == "_a" |
|
199 | 198 | |
|
200 | 199 | down(event) |
|
201 | 200 | assert get_suggestion().text == "_b" |
|
202 | 201 | |
|
203 | 202 | down(event) |
|
204 | 203 | assert get_suggestion().text == "_c" |
|
205 | 204 | |
|
206 | 205 | down(event) |
|
207 | 206 | assert get_suggestion().text == "_a" |
|
208 | 207 | |
|
209 | 208 | |
|
209 | def create_session_mock(): | |
|
210 | session = Mock() | |
|
211 | session.default_buffer = Buffer() | |
|
212 | return session | |
|
213 | ||
|
214 | ||
|
210 | 215 | def test_navigable_provider_connection(): |
|
211 | 216 | provider = NavigableAutoSuggestFromHistory() |
|
212 | 217 | provider.skip_lines = 1 |
|
213 | 218 | |
|
214 |
session_1 = |
|
|
219 | session_1 = create_session_mock() | |
|
215 | 220 | provider.connect(session_1) |
|
216 | 221 | |
|
217 | 222 | assert provider.skip_lines == 1 |
|
218 | 223 | session_1.default_buffer.on_text_insert.fire() |
|
219 | 224 | assert provider.skip_lines == 0 |
|
220 | 225 | |
|
221 |
session_2 = |
|
|
226 | session_2 = create_session_mock() | |
|
222 | 227 | provider.connect(session_2) |
|
223 | 228 | provider.skip_lines = 2 |
|
224 | 229 | |
|
225 | 230 | assert provider.skip_lines == 2 |
|
226 | 231 | session_2.default_buffer.on_text_insert.fire() |
|
227 | 232 | assert provider.skip_lines == 0 |
|
228 | 233 | |
|
229 | 234 | provider.skip_lines = 3 |
|
230 | 235 | provider.disconnect() |
|
231 | 236 | session_1.default_buffer.on_text_insert.fire() |
|
232 | 237 | session_2.default_buffer.on_text_insert.fire() |
|
233 | 238 | assert provider.skip_lines == 3 |
General Comments 0
You need to be logged in to leave comments.
Login now