##// END OF EJS Templates
created test for reset_search_buffer
Abdullah Habib -
Show More
@@ -1,468 +1,487 b''
1 import pytest
1 import pytest
2 from IPython.terminal.shortcuts.auto_suggest import (
2 from IPython.terminal.shortcuts.auto_suggest import (
3 accept,
3 accept,
4 accept_or_jump_to_end,
4 accept_or_jump_to_end,
5 accept_token,
5 accept_token,
6 accept_character,
6 accept_character,
7 accept_word,
7 accept_word,
8 accept_and_keep_cursor,
8 accept_and_keep_cursor,
9 discard,
9 discard,
10 NavigableAutoSuggestFromHistory,
10 NavigableAutoSuggestFromHistory,
11 swap_autosuggestion_up,
11 swap_autosuggestion_up,
12 swap_autosuggestion_down,
12 swap_autosuggestion_down,
13 )
13 )
14 from IPython.terminal.shortcuts.auto_match import skip_over
14 from IPython.terminal.shortcuts.auto_match import skip_over
15 from IPython.terminal.shortcuts import create_ipython_shortcuts
15 from IPython.terminal.shortcuts import create_ipython_shortcuts,reset_search_buffer
16
16
17 from prompt_toolkit.history import InMemoryHistory
17 from prompt_toolkit.history import InMemoryHistory
18 from prompt_toolkit.buffer import Buffer
18 from prompt_toolkit.buffer import Buffer
19 from prompt_toolkit.document import Document
19 from prompt_toolkit.document import Document
20 from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
20 from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
21
21
22 from unittest.mock import patch, Mock
22 from unittest.mock import patch, Mock
23 from prompt_toolkit.enums import DEFAULT_BUFFER
23
24
24
25
25 def test_deprected():
26 def test_deprected():
26 import IPython.terminal.shortcuts.auto_suggest as iptsa
27 import IPython.terminal.shortcuts.auto_suggest as iptsa
27
28
28 with pytest.warns(DeprecationWarning, match=r"8\.12.+accept_or_jump_to_end"):
29 with pytest.warns(DeprecationWarning, match=r"8\.12.+accept_or_jump_to_end"):
29 iptsa.accept_in_vi_insert_mode
30 iptsa.accept_in_vi_insert_mode
30
31
31
32
32 def make_event(text, cursor, suggestion):
33 def make_event(text, cursor, suggestion):
33 event = Mock()
34 event = Mock()
34 event.current_buffer = Mock()
35 event.current_buffer = Mock()
35 event.current_buffer.suggestion = Mock()
36 event.current_buffer.suggestion = Mock()
36 event.current_buffer.text = text
37 event.current_buffer.text = text
37 event.current_buffer.cursor_position = cursor
38 event.current_buffer.cursor_position = cursor
38 event.current_buffer.suggestion.text = suggestion
39 event.current_buffer.suggestion.text = suggestion
39 event.current_buffer.document = Document(text=text, cursor_position=cursor)
40 event.current_buffer.document = Document(text=text, cursor_position=cursor)
40 return event
41 return event
41
42
42
43
43 @pytest.mark.parametrize(
44 @pytest.mark.parametrize(
44 "text, suggestion, expected",
45 "text, suggestion, expected",
45 [
46 [
46 ("", "def out(tag: str, n=50):", "def out(tag: str, n=50):"),
47 ("", "def out(tag: str, n=50):", "def out(tag: str, n=50):"),
47 ("def ", "out(tag: str, n=50):", "out(tag: str, n=50):"),
48 ("def ", "out(tag: str, n=50):", "out(tag: str, n=50):"),
48 ],
49 ],
49 )
50 )
50 def test_accept(text, suggestion, expected):
51 def test_accept(text, suggestion, expected):
51 event = make_event(text, len(text), suggestion)
52 event = make_event(text, len(text), suggestion)
52 buffer = event.current_buffer
53 buffer = event.current_buffer
53 buffer.insert_text = Mock()
54 buffer.insert_text = Mock()
54 accept(event)
55 accept(event)
55 assert buffer.insert_text.called
56 assert buffer.insert_text.called
56 assert buffer.insert_text.call_args[0] == (expected,)
57 assert buffer.insert_text.call_args[0] == (expected,)
57
58
58
59
59 @pytest.mark.parametrize(
60 @pytest.mark.parametrize(
60 "text, suggestion",
61 "text, suggestion",
61 [
62 [
62 ("", "def out(tag: str, n=50):"),
63 ("", "def out(tag: str, n=50):"),
63 ("def ", "out(tag: str, n=50):"),
64 ("def ", "out(tag: str, n=50):"),
64 ],
65 ],
65 )
66 )
66 def test_discard(text, suggestion):
67 def test_discard(text, suggestion):
67 event = make_event(text, len(text), suggestion)
68 event = make_event(text, len(text), suggestion)
68 buffer = event.current_buffer
69 buffer = event.current_buffer
69 buffer.insert_text = Mock()
70 buffer.insert_text = Mock()
70 discard(event)
71 discard(event)
71 assert not buffer.insert_text.called
72 assert not buffer.insert_text.called
72 assert buffer.suggestion is None
73 assert buffer.suggestion is None
73
74
74
75
75 @pytest.mark.parametrize(
76 @pytest.mark.parametrize(
76 "text, cursor, suggestion, called",
77 "text, cursor, suggestion, called",
77 [
78 [
78 ("123456", 6, "123456789", True),
79 ("123456", 6, "123456789", True),
79 ("123456", 3, "123456789", False),
80 ("123456", 3, "123456789", False),
80 ("123456 \n789", 6, "123456789", True),
81 ("123456 \n789", 6, "123456789", True),
81 ],
82 ],
82 )
83 )
83 def test_autosuggest_at_EOL(text, cursor, suggestion, called):
84 def test_autosuggest_at_EOL(text, cursor, suggestion, called):
84 """
85 """
85 test that autosuggest is only applied at end of line.
86 test that autosuggest is only applied at end of line.
86 """
87 """
87
88
88 event = make_event(text, cursor, suggestion)
89 event = make_event(text, cursor, suggestion)
89 event.current_buffer.insert_text = Mock()
90 event.current_buffer.insert_text = Mock()
90 accept_or_jump_to_end(event)
91 accept_or_jump_to_end(event)
91 if called:
92 if called:
92 event.current_buffer.insert_text.assert_called()
93 event.current_buffer.insert_text.assert_called()
93 else:
94 else:
94 event.current_buffer.insert_text.assert_not_called()
95 event.current_buffer.insert_text.assert_not_called()
95 # event.current_buffer.document.get_end_of_line_position.assert_called()
96 # event.current_buffer.document.get_end_of_line_position.assert_called()
96
97
97
98
98 @pytest.mark.parametrize(
99 @pytest.mark.parametrize(
99 "text, suggestion, expected",
100 "text, suggestion, expected",
100 [
101 [
101 ("", "def out(tag: str, n=50):", "def "),
102 ("", "def out(tag: str, n=50):", "def "),
102 ("d", "ef out(tag: str, n=50):", "ef "),
103 ("d", "ef out(tag: str, n=50):", "ef "),
103 ("de ", "f out(tag: str, n=50):", "f "),
104 ("de ", "f out(tag: str, n=50):", "f "),
104 ("def", " out(tag: str, n=50):", " "),
105 ("def", " out(tag: str, n=50):", " "),
105 ("def ", "out(tag: str, n=50):", "out("),
106 ("def ", "out(tag: str, n=50):", "out("),
106 ("def o", "ut(tag: str, n=50):", "ut("),
107 ("def o", "ut(tag: str, n=50):", "ut("),
107 ("def ou", "t(tag: str, n=50):", "t("),
108 ("def ou", "t(tag: str, n=50):", "t("),
108 ("def out", "(tag: str, n=50):", "("),
109 ("def out", "(tag: str, n=50):", "("),
109 ("def out(", "tag: str, n=50):", "tag: "),
110 ("def out(", "tag: str, n=50):", "tag: "),
110 ("def out(t", "ag: str, n=50):", "ag: "),
111 ("def out(t", "ag: str, n=50):", "ag: "),
111 ("def out(ta", "g: str, n=50):", "g: "),
112 ("def out(ta", "g: str, n=50):", "g: "),
112 ("def out(tag", ": str, n=50):", ": "),
113 ("def out(tag", ": str, n=50):", ": "),
113 ("def out(tag:", " str, n=50):", " "),
114 ("def out(tag:", " str, n=50):", " "),
114 ("def out(tag: ", "str, n=50):", "str, "),
115 ("def out(tag: ", "str, n=50):", "str, "),
115 ("def out(tag: s", "tr, n=50):", "tr, "),
116 ("def out(tag: s", "tr, n=50):", "tr, "),
116 ("def out(tag: st", "r, n=50):", "r, "),
117 ("def out(tag: st", "r, n=50):", "r, "),
117 ("def out(tag: str", ", n=50):", ", n"),
118 ("def out(tag: str", ", n=50):", ", n"),
118 ("def out(tag: str,", " n=50):", " n"),
119 ("def out(tag: str,", " n=50):", " n"),
119 ("def out(tag: str, ", "n=50):", "n="),
120 ("def out(tag: str, ", "n=50):", "n="),
120 ("def out(tag: str, n", "=50):", "="),
121 ("def out(tag: str, n", "=50):", "="),
121 ("def out(tag: str, n=", "50):", "50)"),
122 ("def out(tag: str, n=", "50):", "50)"),
122 ("def out(tag: str, n=5", "0):", "0)"),
123 ("def out(tag: str, n=5", "0):", "0)"),
123 ("def out(tag: str, n=50", "):", "):"),
124 ("def out(tag: str, n=50", "):", "):"),
124 ("def out(tag: str, n=50)", ":", ":"),
125 ("def out(tag: str, n=50)", ":", ":"),
125 ],
126 ],
126 )
127 )
127 def test_autosuggest_token(text, suggestion, expected):
128 def test_autosuggest_token(text, suggestion, expected):
128 event = make_event(text, len(text), suggestion)
129 event = make_event(text, len(text), suggestion)
129 event.current_buffer.insert_text = Mock()
130 event.current_buffer.insert_text = Mock()
130 accept_token(event)
131 accept_token(event)
131 assert event.current_buffer.insert_text.called
132 assert event.current_buffer.insert_text.called
132 assert event.current_buffer.insert_text.call_args[0] == (expected,)
133 assert event.current_buffer.insert_text.call_args[0] == (expected,)
133
134
134
135
135 @pytest.mark.parametrize(
136 @pytest.mark.parametrize(
136 "text, suggestion, expected",
137 "text, suggestion, expected",
137 [
138 [
138 ("", "def out(tag: str, n=50):", "d"),
139 ("", "def out(tag: str, n=50):", "d"),
139 ("d", "ef out(tag: str, n=50):", "e"),
140 ("d", "ef out(tag: str, n=50):", "e"),
140 ("de ", "f out(tag: str, n=50):", "f"),
141 ("de ", "f out(tag: str, n=50):", "f"),
141 ("def", " out(tag: str, n=50):", " "),
142 ("def", " out(tag: str, n=50):", " "),
142 ],
143 ],
143 )
144 )
144 def test_accept_character(text, suggestion, expected):
145 def test_accept_character(text, suggestion, expected):
145 event = make_event(text, len(text), suggestion)
146 event = make_event(text, len(text), suggestion)
146 event.current_buffer.insert_text = Mock()
147 event.current_buffer.insert_text = Mock()
147 accept_character(event)
148 accept_character(event)
148 assert event.current_buffer.insert_text.called
149 assert event.current_buffer.insert_text.called
149 assert event.current_buffer.insert_text.call_args[0] == (expected,)
150 assert event.current_buffer.insert_text.call_args[0] == (expected,)
150
151
151
152
152 @pytest.mark.parametrize(
153 @pytest.mark.parametrize(
153 "text, suggestion, expected",
154 "text, suggestion, expected",
154 [
155 [
155 ("", "def out(tag: str, n=50):", "def "),
156 ("", "def out(tag: str, n=50):", "def "),
156 ("d", "ef out(tag: str, n=50):", "ef "),
157 ("d", "ef out(tag: str, n=50):", "ef "),
157 ("de", "f out(tag: str, n=50):", "f "),
158 ("de", "f out(tag: str, n=50):", "f "),
158 ("def", " out(tag: str, n=50):", " "),
159 ("def", " out(tag: str, n=50):", " "),
159 # (this is why we also have accept_token)
160 # (this is why we also have accept_token)
160 ("def ", "out(tag: str, n=50):", "out(tag: "),
161 ("def ", "out(tag: str, n=50):", "out(tag: "),
161 ],
162 ],
162 )
163 )
163 def test_accept_word(text, suggestion, expected):
164 def test_accept_word(text, suggestion, expected):
164 event = make_event(text, len(text), suggestion)
165 event = make_event(text, len(text), suggestion)
165 event.current_buffer.insert_text = Mock()
166 event.current_buffer.insert_text = Mock()
166 accept_word(event)
167 accept_word(event)
167 assert event.current_buffer.insert_text.called
168 assert event.current_buffer.insert_text.called
168 assert event.current_buffer.insert_text.call_args[0] == (expected,)
169 assert event.current_buffer.insert_text.call_args[0] == (expected,)
169
170
170
171
171 @pytest.mark.parametrize(
172 @pytest.mark.parametrize(
172 "text, suggestion, expected, cursor",
173 "text, suggestion, expected, cursor",
173 [
174 [
174 ("", "def out(tag: str, n=50):", "def out(tag: str, n=50):", 0),
175 ("", "def out(tag: str, n=50):", "def out(tag: str, n=50):", 0),
175 ("def ", "out(tag: str, n=50):", "out(tag: str, n=50):", 4),
176 ("def ", "out(tag: str, n=50):", "out(tag: str, n=50):", 4),
176 ],
177 ],
177 )
178 )
178 def test_accept_and_keep_cursor(text, suggestion, expected, cursor):
179 def test_accept_and_keep_cursor(text, suggestion, expected, cursor):
179 event = make_event(text, cursor, suggestion)
180 event = make_event(text, cursor, suggestion)
180 buffer = event.current_buffer
181 buffer = event.current_buffer
181 buffer.insert_text = Mock()
182 buffer.insert_text = Mock()
182 accept_and_keep_cursor(event)
183 accept_and_keep_cursor(event)
183 assert buffer.insert_text.called
184 assert buffer.insert_text.called
184 assert buffer.insert_text.call_args[0] == (expected,)
185 assert buffer.insert_text.call_args[0] == (expected,)
185 assert buffer.cursor_position == cursor
186 assert buffer.cursor_position == cursor
186
187
187
188
188 def test_autosuggest_token_empty():
189 def test_autosuggest_token_empty():
189 full = "def out(tag: str, n=50):"
190 full = "def out(tag: str, n=50):"
190 event = make_event(full, len(full), "")
191 event = make_event(full, len(full), "")
191 event.current_buffer.insert_text = Mock()
192 event.current_buffer.insert_text = Mock()
192
193
193 with patch(
194 with patch(
194 "prompt_toolkit.key_binding.bindings.named_commands.forward_word"
195 "prompt_toolkit.key_binding.bindings.named_commands.forward_word"
195 ) as forward_word:
196 ) as forward_word:
196 accept_token(event)
197 accept_token(event)
197 assert not event.current_buffer.insert_text.called
198 assert not event.current_buffer.insert_text.called
198 assert forward_word.called
199 assert forward_word.called
199
200
200
201
202 def test_reset_search_buffer():
203 event_with_text = Mock()
204 event_with_text.current_buffer.document.text = "some text"
205 event_with_text.current_buffer.reset = Mock()
206
207 event_empty = Mock()
208 event_empty.current_buffer.document.text = ""
209 event_empty.app.layout.focus = Mock()
210
211 reset_search_buffer(event_with_text)
212 event_with_text.current_buffer.reset.assert_called_once()
213
214 reset_search_buffer(event_empty)
215 event_empty.app.layout.focus.assert_called_once_with(DEFAULT_BUFFER)
216
217
218
219
201 def test_other_providers():
220 def test_other_providers():
202 """Ensure that swapping autosuggestions does not break with other providers"""
221 """Ensure that swapping autosuggestions does not break with other providers"""
203 provider = AutoSuggestFromHistory()
222 provider = AutoSuggestFromHistory()
204 ip = get_ipython()
223 ip = get_ipython()
205 ip.auto_suggest = provider
224 ip.auto_suggest = provider
206 event = Mock()
225 event = Mock()
207 event.current_buffer = Buffer()
226 event.current_buffer = Buffer()
208 assert swap_autosuggestion_up(event) is None
227 assert swap_autosuggestion_up(event) is None
209 assert swap_autosuggestion_down(event) is None
228 assert swap_autosuggestion_down(event) is None
210
229
211
230
212 async def test_navigable_provider():
231 async def test_navigable_provider():
213 provider = NavigableAutoSuggestFromHistory()
232 provider = NavigableAutoSuggestFromHistory()
214 history = InMemoryHistory(history_strings=["very_a", "very", "very_b", "very_c"])
233 history = InMemoryHistory(history_strings=["very_a", "very", "very_b", "very_c"])
215 buffer = Buffer(history=history)
234 buffer = Buffer(history=history)
216 ip = get_ipython()
235 ip = get_ipython()
217 ip.auto_suggest = provider
236 ip.auto_suggest = provider
218
237
219 async for _ in history.load():
238 async for _ in history.load():
220 pass
239 pass
221
240
222 buffer.cursor_position = 5
241 buffer.cursor_position = 5
223 buffer.text = "very"
242 buffer.text = "very"
224
243
225 up = swap_autosuggestion_up
244 up = swap_autosuggestion_up
226 down = swap_autosuggestion_down
245 down = swap_autosuggestion_down
227
246
228 event = Mock()
247 event = Mock()
229 event.current_buffer = buffer
248 event.current_buffer = buffer
230
249
231 def get_suggestion():
250 def get_suggestion():
232 suggestion = provider.get_suggestion(buffer, buffer.document)
251 suggestion = provider.get_suggestion(buffer, buffer.document)
233 buffer.suggestion = suggestion
252 buffer.suggestion = suggestion
234 return suggestion
253 return suggestion
235
254
236 assert get_suggestion().text == "_c"
255 assert get_suggestion().text == "_c"
237
256
238 # should go up
257 # should go up
239 up(event)
258 up(event)
240 assert get_suggestion().text == "_b"
259 assert get_suggestion().text == "_b"
241
260
242 # should skip over 'very' which is identical to buffer content
261 # should skip over 'very' which is identical to buffer content
243 up(event)
262 up(event)
244 assert get_suggestion().text == "_a"
263 assert get_suggestion().text == "_a"
245
264
246 # should cycle back to beginning
265 # should cycle back to beginning
247 up(event)
266 up(event)
248 assert get_suggestion().text == "_c"
267 assert get_suggestion().text == "_c"
249
268
250 # should cycle back through end boundary
269 # should cycle back through end boundary
251 down(event)
270 down(event)
252 assert get_suggestion().text == "_a"
271 assert get_suggestion().text == "_a"
253
272
254 down(event)
273 down(event)
255 assert get_suggestion().text == "_b"
274 assert get_suggestion().text == "_b"
256
275
257 down(event)
276 down(event)
258 assert get_suggestion().text == "_c"
277 assert get_suggestion().text == "_c"
259
278
260 down(event)
279 down(event)
261 assert get_suggestion().text == "_a"
280 assert get_suggestion().text == "_a"
262
281
263
282
264 async def test_navigable_provider_multiline_entries():
283 async def test_navigable_provider_multiline_entries():
265 provider = NavigableAutoSuggestFromHistory()
284 provider = NavigableAutoSuggestFromHistory()
266 history = InMemoryHistory(history_strings=["very_a\nvery_b", "very_c"])
285 history = InMemoryHistory(history_strings=["very_a\nvery_b", "very_c"])
267 buffer = Buffer(history=history)
286 buffer = Buffer(history=history)
268 ip = get_ipython()
287 ip = get_ipython()
269 ip.auto_suggest = provider
288 ip.auto_suggest = provider
270
289
271 async for _ in history.load():
290 async for _ in history.load():
272 pass
291 pass
273
292
274 buffer.cursor_position = 5
293 buffer.cursor_position = 5
275 buffer.text = "very"
294 buffer.text = "very"
276 up = swap_autosuggestion_up
295 up = swap_autosuggestion_up
277 down = swap_autosuggestion_down
296 down = swap_autosuggestion_down
278
297
279 event = Mock()
298 event = Mock()
280 event.current_buffer = buffer
299 event.current_buffer = buffer
281
300
282 def get_suggestion():
301 def get_suggestion():
283 suggestion = provider.get_suggestion(buffer, buffer.document)
302 suggestion = provider.get_suggestion(buffer, buffer.document)
284 buffer.suggestion = suggestion
303 buffer.suggestion = suggestion
285 return suggestion
304 return suggestion
286
305
287 assert get_suggestion().text == "_c"
306 assert get_suggestion().text == "_c"
288
307
289 up(event)
308 up(event)
290 assert get_suggestion().text == "_b"
309 assert get_suggestion().text == "_b"
291
310
292 up(event)
311 up(event)
293 assert get_suggestion().text == "_a"
312 assert get_suggestion().text == "_a"
294
313
295 down(event)
314 down(event)
296 assert get_suggestion().text == "_b"
315 assert get_suggestion().text == "_b"
297
316
298 down(event)
317 down(event)
299 assert get_suggestion().text == "_c"
318 assert get_suggestion().text == "_c"
300
319
301
320
302 def create_session_mock():
321 def create_session_mock():
303 session = Mock()
322 session = Mock()
304 session.default_buffer = Buffer()
323 session.default_buffer = Buffer()
305 return session
324 return session
306
325
307
326
308 def test_navigable_provider_connection():
327 def test_navigable_provider_connection():
309 provider = NavigableAutoSuggestFromHistory()
328 provider = NavigableAutoSuggestFromHistory()
310 provider.skip_lines = 1
329 provider.skip_lines = 1
311
330
312 session_1 = create_session_mock()
331 session_1 = create_session_mock()
313 provider.connect(session_1)
332 provider.connect(session_1)
314
333
315 assert provider.skip_lines == 1
334 assert provider.skip_lines == 1
316 session_1.default_buffer.on_text_insert.fire()
335 session_1.default_buffer.on_text_insert.fire()
317 assert provider.skip_lines == 0
336 assert provider.skip_lines == 0
318
337
319 session_2 = create_session_mock()
338 session_2 = create_session_mock()
320 provider.connect(session_2)
339 provider.connect(session_2)
321 provider.skip_lines = 2
340 provider.skip_lines = 2
322
341
323 assert provider.skip_lines == 2
342 assert provider.skip_lines == 2
324 session_2.default_buffer.on_text_insert.fire()
343 session_2.default_buffer.on_text_insert.fire()
325 assert provider.skip_lines == 0
344 assert provider.skip_lines == 0
326
345
327 provider.skip_lines = 3
346 provider.skip_lines = 3
328 provider.disconnect()
347 provider.disconnect()
329 session_1.default_buffer.on_text_insert.fire()
348 session_1.default_buffer.on_text_insert.fire()
330 session_2.default_buffer.on_text_insert.fire()
349 session_2.default_buffer.on_text_insert.fire()
331 assert provider.skip_lines == 3
350 assert provider.skip_lines == 3
332
351
333
352
334 @pytest.fixture
353 @pytest.fixture
335 def ipython_with_prompt():
354 def ipython_with_prompt():
336 ip = get_ipython()
355 ip = get_ipython()
337 ip.pt_app = Mock()
356 ip.pt_app = Mock()
338 ip.pt_app.key_bindings = create_ipython_shortcuts(ip)
357 ip.pt_app.key_bindings = create_ipython_shortcuts(ip)
339 try:
358 try:
340 yield ip
359 yield ip
341 finally:
360 finally:
342 ip.pt_app = None
361 ip.pt_app = None
343
362
344
363
345 def find_bindings_by_command(command):
364 def find_bindings_by_command(command):
346 ip = get_ipython()
365 ip = get_ipython()
347 return [
366 return [
348 binding
367 binding
349 for binding in ip.pt_app.key_bindings.bindings
368 for binding in ip.pt_app.key_bindings.bindings
350 if binding.handler == command
369 if binding.handler == command
351 ]
370 ]
352
371
353
372
354 def test_modify_unique_shortcut(ipython_with_prompt):
373 def test_modify_unique_shortcut(ipython_with_prompt):
355 original = find_bindings_by_command(accept_token)
374 original = find_bindings_by_command(accept_token)
356 assert len(original) == 1
375 assert len(original) == 1
357
376
358 ipython_with_prompt.shortcuts = [
377 ipython_with_prompt.shortcuts = [
359 {"command": "IPython:auto_suggest.accept_token", "new_keys": ["a", "b", "c"]}
378 {"command": "IPython:auto_suggest.accept_token", "new_keys": ["a", "b", "c"]}
360 ]
379 ]
361 matched = find_bindings_by_command(accept_token)
380 matched = find_bindings_by_command(accept_token)
362 assert len(matched) == 1
381 assert len(matched) == 1
363 assert list(matched[0].keys) == ["a", "b", "c"]
382 assert list(matched[0].keys) == ["a", "b", "c"]
364 assert list(matched[0].keys) != list(original[0].keys)
383 assert list(matched[0].keys) != list(original[0].keys)
365 assert matched[0].filter == original[0].filter
384 assert matched[0].filter == original[0].filter
366
385
367 ipython_with_prompt.shortcuts = [
386 ipython_with_prompt.shortcuts = [
368 {"command": "IPython:auto_suggest.accept_token", "new_filter": "always"}
387 {"command": "IPython:auto_suggest.accept_token", "new_filter": "always"}
369 ]
388 ]
370 matched = find_bindings_by_command(accept_token)
389 matched = find_bindings_by_command(accept_token)
371 assert len(matched) == 1
390 assert len(matched) == 1
372 assert list(matched[0].keys) != ["a", "b", "c"]
391 assert list(matched[0].keys) != ["a", "b", "c"]
373 assert list(matched[0].keys) == list(original[0].keys)
392 assert list(matched[0].keys) == list(original[0].keys)
374 assert matched[0].filter != original[0].filter
393 assert matched[0].filter != original[0].filter
375
394
376
395
377 def test_disable_shortcut(ipython_with_prompt):
396 def test_disable_shortcut(ipython_with_prompt):
378 matched = find_bindings_by_command(accept_token)
397 matched = find_bindings_by_command(accept_token)
379 assert len(matched) == 1
398 assert len(matched) == 1
380
399
381 ipython_with_prompt.shortcuts = [
400 ipython_with_prompt.shortcuts = [
382 {"command": "IPython:auto_suggest.accept_token", "new_keys": []}
401 {"command": "IPython:auto_suggest.accept_token", "new_keys": []}
383 ]
402 ]
384 matched = find_bindings_by_command(accept_token)
403 matched = find_bindings_by_command(accept_token)
385 assert len(matched) == 0
404 assert len(matched) == 0
386
405
387 ipython_with_prompt.shortcuts = []
406 ipython_with_prompt.shortcuts = []
388 matched = find_bindings_by_command(accept_token)
407 matched = find_bindings_by_command(accept_token)
389 assert len(matched) == 1
408 assert len(matched) == 1
390
409
391
410
392 def test_modify_shortcut_with_filters(ipython_with_prompt):
411 def test_modify_shortcut_with_filters(ipython_with_prompt):
393 matched = find_bindings_by_command(skip_over)
412 matched = find_bindings_by_command(skip_over)
394 matched_keys = {m.keys[0] for m in matched}
413 matched_keys = {m.keys[0] for m in matched}
395 assert matched_keys == {")", "]", "}", "'", '"'}
414 assert matched_keys == {")", "]", "}", "'", '"'}
396
415
397 with pytest.raises(ValueError, match="Multiple shortcuts matching"):
416 with pytest.raises(ValueError, match="Multiple shortcuts matching"):
398 ipython_with_prompt.shortcuts = [
417 ipython_with_prompt.shortcuts = [
399 {"command": "IPython:auto_match.skip_over", "new_keys": ["x"]}
418 {"command": "IPython:auto_match.skip_over", "new_keys": ["x"]}
400 ]
419 ]
401
420
402 ipython_with_prompt.shortcuts = [
421 ipython_with_prompt.shortcuts = [
403 {
422 {
404 "command": "IPython:auto_match.skip_over",
423 "command": "IPython:auto_match.skip_over",
405 "new_keys": ["x"],
424 "new_keys": ["x"],
406 "match_filter": "focused_insert & auto_match & followed_by_single_quote",
425 "match_filter": "focused_insert & auto_match & followed_by_single_quote",
407 }
426 }
408 ]
427 ]
409 matched = find_bindings_by_command(skip_over)
428 matched = find_bindings_by_command(skip_over)
410 matched_keys = {m.keys[0] for m in matched}
429 matched_keys = {m.keys[0] for m in matched}
411 assert matched_keys == {")", "]", "}", "x", '"'}
430 assert matched_keys == {")", "]", "}", "x", '"'}
412
431
413
432
414 def example_command():
433 def example_command():
415 pass
434 pass
416
435
417
436
418 def test_add_shortcut_for_new_command(ipython_with_prompt):
437 def test_add_shortcut_for_new_command(ipython_with_prompt):
419 matched = find_bindings_by_command(example_command)
438 matched = find_bindings_by_command(example_command)
420 assert len(matched) == 0
439 assert len(matched) == 0
421
440
422 with pytest.raises(ValueError, match="example_command is not a known"):
441 with pytest.raises(ValueError, match="example_command is not a known"):
423 ipython_with_prompt.shortcuts = [
442 ipython_with_prompt.shortcuts = [
424 {"command": "example_command", "new_keys": ["x"]}
443 {"command": "example_command", "new_keys": ["x"]}
425 ]
444 ]
426 matched = find_bindings_by_command(example_command)
445 matched = find_bindings_by_command(example_command)
427 assert len(matched) == 0
446 assert len(matched) == 0
428
447
429
448
430 def test_modify_shortcut_failure(ipython_with_prompt):
449 def test_modify_shortcut_failure(ipython_with_prompt):
431 with pytest.raises(ValueError, match="No shortcuts matching"):
450 with pytest.raises(ValueError, match="No shortcuts matching"):
432 ipython_with_prompt.shortcuts = [
451 ipython_with_prompt.shortcuts = [
433 {
452 {
434 "command": "IPython:auto_match.skip_over",
453 "command": "IPython:auto_match.skip_over",
435 "match_keys": ["x"],
454 "match_keys": ["x"],
436 "new_keys": ["y"],
455 "new_keys": ["y"],
437 }
456 }
438 ]
457 ]
439
458
440
459
441 def test_add_shortcut_for_existing_command(ipython_with_prompt):
460 def test_add_shortcut_for_existing_command(ipython_with_prompt):
442 matched = find_bindings_by_command(skip_over)
461 matched = find_bindings_by_command(skip_over)
443 assert len(matched) == 5
462 assert len(matched) == 5
444
463
445 with pytest.raises(ValueError, match="Cannot add a shortcut without keys"):
464 with pytest.raises(ValueError, match="Cannot add a shortcut without keys"):
446 ipython_with_prompt.shortcuts = [
465 ipython_with_prompt.shortcuts = [
447 {"command": "IPython:auto_match.skip_over", "new_keys": [], "create": True}
466 {"command": "IPython:auto_match.skip_over", "new_keys": [], "create": True}
448 ]
467 ]
449
468
450 ipython_with_prompt.shortcuts = [
469 ipython_with_prompt.shortcuts = [
451 {"command": "IPython:auto_match.skip_over", "new_keys": ["x"], "create": True}
470 {"command": "IPython:auto_match.skip_over", "new_keys": ["x"], "create": True}
452 ]
471 ]
453 matched = find_bindings_by_command(skip_over)
472 matched = find_bindings_by_command(skip_over)
454 assert len(matched) == 6
473 assert len(matched) == 6
455
474
456 ipython_with_prompt.shortcuts = []
475 ipython_with_prompt.shortcuts = []
457 matched = find_bindings_by_command(skip_over)
476 matched = find_bindings_by_command(skip_over)
458 assert len(matched) == 5
477 assert len(matched) == 5
459
478
460
479
461 def test_setting_shortcuts_before_pt_app_init():
480 def test_setting_shortcuts_before_pt_app_init():
462 ipython = get_ipython()
481 ipython = get_ipython()
463 assert ipython.pt_app is None
482 assert ipython.pt_app is None
464 shortcuts = [
483 shortcuts = [
465 {"command": "IPython:auto_match.skip_over", "new_keys": ["x"], "create": True}
484 {"command": "IPython:auto_match.skip_over", "new_keys": ["x"], "create": True}
466 ]
485 ]
467 ipython.shortcuts = shortcuts
486 ipython.shortcuts = shortcuts
468 assert ipython.shortcuts == shortcuts
487 assert ipython.shortcuts == shortcuts
General Comments 0
You need to be logged in to leave comments. Login now