FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ipython/tests/test_shortcuts.py at python-3.14-dev · krassowski/ipython · GitHub
krassowski
ipython
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ipython
/
tests
/
test_shortcuts.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
519 lines (419 loc) · 15.6 KB
Breadcrumbs
ipython
/
tests
/
test_shortcuts.py
Copy path
File metadata and controls
519 lines (419 loc) · 15.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
import
pytest
import
time
from
IPython
.
terminal
.
interactiveshell
import
PtkHistoryAdapter
from
IPython
.
terminal
.
shortcuts
.
auto_suggest
import
(
accept
,
accept_or_jump_to_end
,
accept_token
,
accept_character
,
accept_word
,
accept_and_keep_cursor
,
discard
,
llm_autosuggestion
,
NavigableAutoSuggestFromHistory
,
swap_autosuggestion_up
,
swap_autosuggestion_down
,
)
from
IPython
.
terminal
.
shortcuts
.
auto_match
import
skip_over
from
IPython
.
terminal
.
shortcuts
import
create_ipython_shortcuts
,
reset_search_buffer
from
IPython
.
testing
import
decorators
as
dec
from
prompt_toolkit
.
history
import
InMemoryHistory
from
prompt_toolkit
.
buffer
import
Buffer
from
prompt_toolkit
.
document
import
Document
from
prompt_toolkit
.
auto_suggest
import
AutoSuggestFromHistory
from
prompt_toolkit
.
enums
import
DEFAULT_BUFFER
from
unittest
.
mock
import
patch
,
Mock
def
make_event
(
text
,
cursor
,
suggestion
):
event
=
Mock
()
event
.
current_buffer
=
Mock
()
event
.
current_buffer
.
suggestion
=
Mock
()
event
.
current_buffer
.
text
=
text
event
.
current_buffer
.
cursor_position
=
cursor
event
.
current_buffer
.
suggestion
.
text
=
suggestion
event
.
current_buffer
.
document
=
Document
(
text
=
text
,
cursor_position
=
cursor
)
return
event
try
:
from
.
fake_llm
import
FIBONACCI
except
ImportError
:
FIBONACCI
=
""
@
dec
.
skip_without
(
"jupyter_ai"
)
@
pytest
.
mark
.
asyncio
async
def
test_llm_autosuggestion
():
provider
=
NavigableAutoSuggestFromHistory
()
ip
=
get_ipython
()
ip
.
auto_suggest
=
provider
ip
.
llm_provider_class
=
"tests.fake_llm.FibonacciCompletionProvider"
ip
.
history_manager
.
get_range
=
Mock
(
return_value
=
[])
text
=
"def fib"
event
=
Mock
()
event
.
current_buffer
=
Buffer
(
history
=
PtkHistoryAdapter
(
ip
),
)
event
.
current_buffer
.
insert_text
(
text
,
move_cursor
=
True
)
await
llm_autosuggestion
(
event
)
assert
event
.
current_buffer
.
suggestion
.
text
==
FIBONACCI
[
len
(
text
) :]
def
test_slow_llm_provider_should_not_block_init
():
ip
=
get_ipython
()
provider
=
NavigableAutoSuggestFromHistory
()
ip
.
auto_suggest
=
provider
start
=
time
.
perf_counter
()
ip
.
llm_provider_class
=
"tests.fake_llm.SlowStartingCompletionProvider"
end
=
time
.
perf_counter
()
elapsed
=
end
-
start
assert
elapsed
<
0.1
@
pytest
.
mark
.
parametrize
(
"text, suggestion, expected"
,
[
(
""
,
"def out(tag: str, n=50):"
,
"def out(tag: str, n=50):"
),
(
"def "
,
"out(tag: str, n=50):"
,
"out(tag: str, n=50):"
),
],
)
def
test_accept
(
text
,
suggestion
,
expected
):
event
=
make_event
(
text
,
len
(
text
),
suggestion
)
buffer
=
event
.
current_buffer
buffer
.
insert_text
=
Mock
()
accept
(
event
)
assert
buffer
.
insert_text
.
called
assert
buffer
.
insert_text
.
call_args
[
0
]
==
(
expected
,)
@
pytest
.
mark
.
parametrize
(
"text, suggestion"
,
[
(
""
,
"def out(tag: str, n=50):"
),
(
"def "
,
"out(tag: str, n=50):"
),
],
)
def
test_discard
(
text
,
suggestion
):
event
=
make_event
(
text
,
len
(
text
),
suggestion
)
buffer
=
event
.
current_buffer
buffer
.
insert_text
=
Mock
()
discard
(
event
)
assert
not
buffer
.
insert_text
.
called
assert
buffer
.
suggestion
is
None
@
pytest
.
mark
.
parametrize
(
"text, cursor, suggestion, called"
,
[
(
"123456"
,
6
,
"123456789"
,
True
),
(
"123456"
,
3
,
"123456789"
,
False
),
(
"123456
\n
789"
,
6
,
"123456789"
,
True
),
],
)
def
test_autosuggest_at_EOL
(
text
,
cursor
,
suggestion
,
called
):
"""
test that autosuggest is only applied at end of line.
"""
event
=
make_event
(
text
,
cursor
,
suggestion
)
event
.
current_buffer
.
insert_text
=
Mock
()
accept_or_jump_to_end
(
event
)
if
called
:
event
.
current_buffer
.
insert_text
.
assert_called
()
else
:
event
.
current_buffer
.
insert_text
.
assert_not_called
()
# event.current_buffer.document.get_end_of_line_position.assert_called()
@
pytest
.
mark
.
parametrize
(
"text, suggestion, expected"
,
[
(
""
,
"def out(tag: str, n=50):"
,
"def "
),
(
"d"
,
"ef out(tag: str, n=50):"
,
"ef "
),
(
"de "
,
"f out(tag: str, n=50):"
,
"f "
),
(
"def"
,
" out(tag: str, n=50):"
,
" "
),
(
"def "
,
"out(tag: str, n=50):"
,
"out("
),
(
"def o"
,
"ut(tag: str, n=50):"
,
"ut("
),
(
"def ou"
,
"t(tag: str, n=50):"
,
"t("
),
(
"def out"
,
"(tag: str, n=50):"
,
"("
),
(
"def out("
,
"tag: str, n=50):"
,
"tag: "
),
(
"def out(t"
,
"ag: str, n=50):"
,
"ag: "
),
(
"def out(ta"
,
"g: str, n=50):"
,
"g: "
),
(
"def out(tag"
,
": str, n=50):"
,
": "
),
(
"def out(tag:"
,
" str, n=50):"
,
" "
),
(
"def out(tag: "
,
"str, n=50):"
,
"str, "
),
(
"def out(tag: s"
,
"tr, n=50):"
,
"tr, "
),
(
"def out(tag: st"
,
"r, n=50):"
,
"r, "
),
(
"def out(tag: str"
,
", n=50):"
,
", n"
),
(
"def out(tag: str,"
,
" n=50):"
,
" n"
),
(
"def out(tag: str, "
,
"n=50):"
,
"n="
),
(
"def out(tag: str, n"
,
"=50):"
,
"="
),
(
"def out(tag: str, n="
,
"50):"
,
"50)"
),
(
"def out(tag: str, n=5"
,
"0):"
,
"0)"
),
(
"def out(tag: str, n=50"
,
"):"
,
"):"
),
(
"def out(tag: str, n=50)"
,
":"
,
":"
),
],
)
def
test_autosuggest_token
(
text
,
suggestion
,
expected
):
event
=
make_event
(
text
,
len
(
text
),
suggestion
)
event
.
current_buffer
.
insert_text
=
Mock
()
accept_token
(
event
)
assert
event
.
current_buffer
.
insert_text
.
called
assert
event
.
current_buffer
.
insert_text
.
call_args
[
0
]
==
(
expected
,)
@
pytest
.
mark
.
parametrize
(
"text, suggestion, expected"
,
[
(
""
,
"def out(tag: str, n=50):"
,
"d"
),
(
"d"
,
"ef out(tag: str, n=50):"
,
"e"
),
(
"de "
,
"f out(tag: str, n=50):"
,
"f"
),
(
"def"
,
" out(tag: str, n=50):"
,
" "
),
],
)
def
test_accept_character
(
text
,
suggestion
,
expected
):
event
=
make_event
(
text
,
len
(
text
),
suggestion
)
event
.
current_buffer
.
insert_text
=
Mock
()
accept_character
(
event
)
assert
event
.
current_buffer
.
insert_text
.
called
assert
event
.
current_buffer
.
insert_text
.
call_args
[
0
]
==
(
expected
,)
@
pytest
.
mark
.
parametrize
(
"text, suggestion, expected"
,
[
(
""
,
"def out(tag: str, n=50):"
,
"def "
),
(
"d"
,
"ef out(tag: str, n=50):"
,
"ef "
),
(
"de"
,
"f out(tag: str, n=50):"
,
"f "
),
(
"def"
,
" out(tag: str, n=50):"
,
" "
),
# (this is why we also have accept_token)
(
"def "
,
"out(tag: str, n=50):"
,
"out(tag: "
),
],
)
def
test_accept_word
(
text
,
suggestion
,
expected
):
event
=
make_event
(
text
,
len
(
text
),
suggestion
)
event
.
current_buffer
.
insert_text
=
Mock
()
accept_word
(
event
)
assert
event
.
current_buffer
.
insert_text
.
called
assert
event
.
current_buffer
.
insert_text
.
call_args
[
0
]
==
(
expected
,)
@
pytest
.
mark
.
parametrize
(
"text, suggestion, expected, cursor"
,
[
(
""
,
"def out(tag: str, n=50):"
,
"def out(tag: str, n=50):"
,
0
),
(
"def "
,
"out(tag: str, n=50):"
,
"out(tag: str, n=50):"
,
4
),
],
)
def
test_accept_and_keep_cursor
(
text
,
suggestion
,
expected
,
cursor
):
event
=
make_event
(
text
,
cursor
,
suggestion
)
buffer
=
event
.
current_buffer
buffer
.
insert_text
=
Mock
()
accept_and_keep_cursor
(
event
)
assert
buffer
.
insert_text
.
called
assert
buffer
.
insert_text
.
call_args
[
0
]
==
(
expected
,)
assert
buffer
.
cursor_position
==
cursor
def
test_autosuggest_token_empty
():
full
=
"def out(tag: str, n=50):"
event
=
make_event
(
full
,
len
(
full
),
""
)
event
.
current_buffer
.
insert_text
=
Mock
()
with
patch
(
"prompt_toolkit.key_binding.bindings.named_commands.forward_word"
)
as
forward_word
:
accept_token
(
event
)
assert
not
event
.
current_buffer
.
insert_text
.
called
assert
forward_word
.
called
def
test_reset_search_buffer
():
event_with_text
=
Mock
()
event_with_text
.
current_buffer
.
document
.
text
=
"some text"
event_with_text
.
current_buffer
.
reset
=
Mock
()
event_empty
=
Mock
()
event_empty
.
current_buffer
.
document
.
text
=
""
event_empty
.
app
.
layout
.
focus
=
Mock
()
reset_search_buffer
(
event_with_text
)
event_with_text
.
current_buffer
.
reset
.
assert_called_once
()
reset_search_buffer
(
event_empty
)
event_empty
.
app
.
layout
.
focus
.
assert_called_once_with
(
DEFAULT_BUFFER
)
def
test_other_providers
():
"""Ensure that swapping autosuggestions does not break with other providers"""
provider
=
AutoSuggestFromHistory
()
ip
=
get_ipython
()
ip
.
auto_suggest
=
provider
event
=
Mock
()
event
.
current_buffer
=
Buffer
()
assert
swap_autosuggestion_up
(
event
)
is
None
assert
swap_autosuggestion_down
(
event
)
is
None
@
pytest
.
mark
.
asyncio
async
def
test_navigable_provider
():
provider
=
NavigableAutoSuggestFromHistory
()
history
=
InMemoryHistory
(
history_strings
=
[
"very_a"
,
"very"
,
"very_b"
,
"very_c"
])
buffer
=
Buffer
(
history
=
history
)
ip
=
get_ipython
()
ip
.
auto_suggest
=
provider
async
for
_
in
history
.
load
():
pass
buffer
.
cursor_position
=
5
buffer
.
text
=
"very"
up
=
swap_autosuggestion_up
down
=
swap_autosuggestion_down
event
=
Mock
()
event
.
current_buffer
=
buffer
def
get_suggestion
():
suggestion
=
provider
.
get_suggestion
(
buffer
,
buffer
.
document
)
buffer
.
suggestion
=
suggestion
return
suggestion
assert
get_suggestion
().
text
==
"_c"
# should go up
up
(
event
)
assert
get_suggestion
().
text
==
"_b"
# should skip over 'very' which is identical to buffer content
up
(
event
)
assert
get_suggestion
().
text
==
"_a"
# should cycle back to beginning
up
(
event
)
assert
get_suggestion
().
text
==
"_c"
# should cycle back through end boundary
down
(
event
)
assert
get_suggestion
().
text
==
"_a"
down
(
event
)
assert
get_suggestion
().
text
==
"_b"
down
(
event
)
assert
get_suggestion
().
text
==
"_c"
down
(
event
)
assert
get_suggestion
().
text
==
"_a"
@
pytest
.
mark
.
asyncio
async
def
test_navigable_provider_multiline_entries
():
provider
=
NavigableAutoSuggestFromHistory
()
history
=
InMemoryHistory
(
history_strings
=
[
"very_a
\n
very_b"
,
"very_c"
])
buffer
=
Buffer
(
history
=
history
)
ip
=
get_ipython
()
ip
.
auto_suggest
=
provider
async
for
_
in
history
.
load
():
pass
buffer
.
cursor_position
=
5
buffer
.
text
=
"very"
up
=
swap_autosuggestion_up
down
=
swap_autosuggestion_down
event
=
Mock
()
event
.
current_buffer
=
buffer
def
get_suggestion
():
suggestion
=
provider
.
get_suggestion
(
buffer
,
buffer
.
document
)
buffer
.
suggestion
=
suggestion
return
suggestion
assert
get_suggestion
().
text
==
"_c"
up
(
event
)
assert
get_suggestion
().
text
==
"_b"
up
(
event
)
assert
get_suggestion
().
text
==
"_a"
down
(
event
)
assert
get_suggestion
().
text
==
"_b"
down
(
event
)
assert
get_suggestion
().
text
==
"_c"
def
create_session_mock
():
session
=
Mock
()
session
.
default_buffer
=
Buffer
()
return
session
def
test_navigable_provider_connection
():
provider
=
NavigableAutoSuggestFromHistory
()
provider
.
skip_lines
=
1
session_1
=
create_session_mock
()
provider
.
connect
(
session_1
)
assert
provider
.
skip_lines
==
1
session_1
.
default_buffer
.
on_text_insert
.
fire
()
assert
provider
.
skip_lines
==
0
session_2
=
create_session_mock
()
provider
.
connect
(
session_2
)
provider
.
skip_lines
=
2
assert
provider
.
skip_lines
==
2
session_2
.
default_buffer
.
on_text_insert
.
fire
()
assert
provider
.
skip_lines
==
0
provider
.
skip_lines
=
3
provider
.
disconnect
()
session_1
.
default_buffer
.
on_text_insert
.
fire
()
session_2
.
default_buffer
.
on_text_insert
.
fire
()
assert
provider
.
skip_lines
==
3
@
pytest
.
fixture
def
ipython_with_prompt
():
ip
=
get_ipython
()
ip
.
pt_app
=
Mock
()
ip
.
pt_app
.
key_bindings
=
create_ipython_shortcuts
(
ip
)
try
:
yield
ip
finally
:
ip
.
pt_app
=
None
def
find_bindings_by_command
(
command
):
ip
=
get_ipython
()
return
[
binding
for
binding
in
ip
.
pt_app
.
key_bindings
.
bindings
if
binding
.
handler
==
command
]
def
test_modify_unique_shortcut
(
ipython_with_prompt
):
original
=
find_bindings_by_command
(
accept_token
)
assert
len
(
original
)
==
1
ipython_with_prompt
.
shortcuts
=
[
{
"command"
:
"IPython:auto_suggest.accept_token"
,
"new_keys"
: [
"a"
,
"b"
,
"c"
]}
]
matched
=
find_bindings_by_command
(
accept_token
)
assert
len
(
matched
)
==
1
assert
list
(
matched
[
0
].
keys
)
==
[
"a"
,
"b"
,
"c"
]
assert
list
(
matched
[
0
].
keys
)
!=
list
(
original
[
0
].
keys
)
assert
matched
[
0
].
filter
==
original
[
0
].
filter
ipython_with_prompt
.
shortcuts
=
[
{
"command"
:
"IPython:auto_suggest.accept_token"
,
"new_filter"
:
"always"
}
]
matched
=
find_bindings_by_command
(
accept_token
)
assert
len
(
matched
)
==
1
assert
list
(
matched
[
0
].
keys
)
!=
[
"a"
,
"b"
,
"c"
]
assert
list
(
matched
[
0
].
keys
)
==
list
(
original
[
0
].
keys
)
assert
matched
[
0
].
filter
!=
original
[
0
].
filter
def
test_disable_shortcut
(
ipython_with_prompt
):
matched
=
find_bindings_by_command
(
accept_token
)
assert
len
(
matched
)
==
1
ipython_with_prompt
.
shortcuts
=
[
{
"command"
:
"IPython:auto_suggest.accept_token"
,
"new_keys"
: []}
]
matched
=
find_bindings_by_command
(
accept_token
)
assert
len
(
matched
)
==
0
ipython_with_prompt
.
shortcuts
=
[]
matched
=
find_bindings_by_command
(
accept_token
)
assert
len
(
matched
)
==
1
def
test_modify_shortcut_with_filters
(
ipython_with_prompt
):
matched
=
find_bindings_by_command
(
skip_over
)
matched_keys
=
{
m
.
keys
[
0
]
for
m
in
matched
}
assert
matched_keys
==
{
")"
,
"]"
,
"}"
,
"'"
,
'"'
}
with
pytest
.
raises
(
ValueError
,
match
=
"Multiple shortcuts matching"
):
ipython_with_prompt
.
shortcuts
=
[
{
"command"
:
"IPython:auto_match.skip_over"
,
"new_keys"
: [
"x"
]}
]
ipython_with_prompt
.
shortcuts
=
[
{
"command"
:
"IPython:auto_match.skip_over"
,
"new_keys"
: [
"x"
],
"match_filter"
:
"focused_insert & auto_match & followed_by_single_quote"
,
}
]
matched
=
find_bindings_by_command
(
skip_over
)
matched_keys
=
{
m
.
keys
[
0
]
for
m
in
matched
}
assert
matched_keys
==
{
")"
,
"]"
,
"}"
,
"x"
,
'"'
}
def
example_command
():
pass
def
test_add_shortcut_for_new_command
(
ipython_with_prompt
):
matched
=
find_bindings_by_command
(
example_command
)
assert
len
(
matched
)
==
0
with
pytest
.
raises
(
ValueError
,
match
=
"example_command is not a known"
):
ipython_with_prompt
.
shortcuts
=
[
{
"command"
:
"example_command"
,
"new_keys"
: [
"x"
]}
]
matched
=
find_bindings_by_command
(
example_command
)
assert
len
(
matched
)
==
0
def
test_modify_shortcut_failure
(
ipython_with_prompt
):
with
pytest
.
raises
(
ValueError
,
match
=
"No shortcuts matching"
):
ipython_with_prompt
.
shortcuts
=
[
{
"command"
:
"IPython:auto_match.skip_over"
,
"match_keys"
: [
"x"
],
"new_keys"
: [
"y"
],
}
]
def
test_add_shortcut_for_existing_command
(
ipython_with_prompt
):
matched
=
find_bindings_by_command
(
skip_over
)
assert
len
(
matched
)
==
5
with
pytest
.
raises
(
ValueError
,
match
=
"Cannot add a shortcut without keys"
):
ipython_with_prompt
.
shortcuts
=
[
{
"command"
:
"IPython:auto_match.skip_over"
,
"new_keys"
: [],
"create"
:
True
}
]
ipython_with_prompt
.
shortcuts
=
[
{
"command"
:
"IPython:auto_match.skip_over"
,
"new_keys"
: [
"x"
],
"create"
:
True
}
]
matched
=
find_bindings_by_command
(
skip_over
)
assert
len
(
matched
)
==
6
ipython_with_prompt
.
shortcuts
=
[]
matched
=
find_bindings_by_command
(
skip_over
)
assert
len
(
matched
)
==
5
def
test_setting_shortcuts_before_pt_app_init
():
ipython
=
get_ipython
()
assert
ipython
.
pt_app
is
None
shortcuts
=
[
{
"command"
:
"IPython:auto_match.skip_over"
,
"new_keys"
: [
"x"
],
"create"
:
True
}
]
ipython
.
shortcuts
=
shortcuts
assert
ipython
.
shortcuts
==
shortcuts
Back
|
FazBrowse Home
|
New Git URL