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