1
|
|
|
from unittest.mock import MagicMock |
2
|
|
|
import pytest |
3
|
|
|
|
4
|
|
|
from neovim_prompt.action import Action, DEFAULT_ACTION |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
@pytest.fixture |
8
|
|
|
def action(): |
9
|
|
|
return DEFAULT_ACTION |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
def test_Action(): |
13
|
|
|
action = Action() |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
def test_action_register(): |
17
|
|
|
callback = lambda prompt: None |
18
|
|
|
action = Action() |
19
|
|
|
action.register('prompt:test', callback) |
20
|
|
|
assert 'prompt:test' in action.registry |
21
|
|
|
assert action.registry['prompt:test'] == callback |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
def test_action_register_from_rules(): |
25
|
|
|
callback = lambda prompt: None |
26
|
|
|
action = Action() |
27
|
|
|
action.register_from_rules([ |
28
|
|
|
('prompt:test1', callback), |
29
|
|
|
('prompt:test2', callback), |
30
|
|
|
]) |
31
|
|
|
assert 'prompt:test1' in action.registry |
32
|
|
|
assert 'prompt:test2' in action.registry |
33
|
|
|
assert action.registry['prompt:test1'] == callback |
34
|
|
|
assert action.registry['prompt:test2'] == callback |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
def test_action_call(prompt): |
38
|
|
|
prompt.text = 'foo' |
39
|
|
|
callback = lambda prompt: prompt.text |
40
|
|
|
action = Action() |
41
|
|
|
action.register('prompt:test', callback) |
42
|
|
|
assert action.call(prompt, 'prompt:test') == 'foo' |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
def test_Action_from_rules(): |
46
|
|
|
callback = lambda prompt: None |
47
|
|
|
action = Action.from_rules([ |
48
|
|
|
('prompt:test1', callback), |
49
|
|
|
('prompt:test2', callback), |
50
|
|
|
]) |
51
|
|
|
assert 'prompt:test1' in action.registry |
52
|
|
|
assert 'prompt:test2' in action.registry |
53
|
|
|
assert action.registry['prompt:test1'] == callback |
54
|
|
|
assert action.registry['prompt:test2'] == callback |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
def test_accept(prompt, action): |
58
|
|
|
from neovim_prompt.prompt import Status |
59
|
|
|
assert action.call(prompt, 'prompt:accept') == Status.accept |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
def test_cancel(prompt, action): |
63
|
|
|
from neovim_prompt.prompt import Status |
64
|
|
|
assert action.call(prompt, 'prompt:cancel') == Status.cancel |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
def test_toggle_insert_mode(prompt, action): |
68
|
|
|
from neovim_prompt.prompt import InsertMode |
69
|
|
|
prompt.insert_mode = InsertMode.insert |
70
|
|
|
assert action.call(prompt, 'prompt:toggle_insert_mode') is None |
71
|
|
|
assert prompt.insert_mode == InsertMode.replace |
72
|
|
|
assert action.call(prompt, 'prompt:toggle_insert_mode') is None |
73
|
|
|
assert prompt.insert_mode == InsertMode.insert |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
def test_delete_char_before_caret(prompt, action): |
77
|
|
|
prompt.text = 'Hello Goodbye' |
78
|
|
|
prompt.caret.locus = 5 |
79
|
|
|
assert action.call(prompt, 'prompt:delete_char_before_caret') is None |
80
|
|
|
assert prompt.text == 'Hell Goodbye' |
81
|
|
|
assert prompt.caret.locus == 4 |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
def test_delete_char_under_caret(prompt, action): |
85
|
|
|
prompt.text = 'Hello Goodbye' |
86
|
|
|
prompt.caret.locus = 5 |
87
|
|
|
assert action.call(prompt, 'prompt:delete_char_under_caret') is None |
88
|
|
|
assert prompt.text == 'HelloGoodbye' |
89
|
|
|
assert prompt.caret.locus == 5 |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
def test_delete_text_after_caret(prompt, action): |
93
|
|
|
prompt.text = 'Hello Goodbye' |
94
|
|
|
prompt.caret.locus = 5 |
95
|
|
|
assert action.call(prompt, 'prompt:delete_text_after_caret') is None |
96
|
|
|
assert prompt.text == 'Hello' |
97
|
|
|
assert prompt.caret.locus == 5 |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
def test_delete_entire_text(prompt, action): |
101
|
|
|
prompt.text = 'Hello Goodbye' |
102
|
|
|
prompt.caret.locus = 5 |
103
|
|
|
assert action.call(prompt, 'prompt:delete_entire_text') is None |
104
|
|
|
assert prompt.text == '' |
105
|
|
|
assert prompt.caret.locus == 0 |
106
|
|
|
|
107
|
|
|
def test_move_caret_to_left(prompt, action): |
108
|
|
|
prompt.text = 'Hello Goodbye' |
109
|
|
|
prompt.caret.locus = 5 |
110
|
|
|
assert action.call(prompt, 'prompt:move_caret_to_left') is None |
111
|
|
|
assert prompt.text == 'Hello Goodbye' |
112
|
|
|
assert prompt.caret.locus == 4 |
113
|
|
|
|
114
|
|
|
assert action.call(prompt, 'prompt:move_caret_to_left') is None |
115
|
|
|
assert action.call(prompt, 'prompt:move_caret_to_left') is None |
116
|
|
|
assert action.call(prompt, 'prompt:move_caret_to_left') is None |
117
|
|
|
assert action.call(prompt, 'prompt:move_caret_to_left') is None |
118
|
|
|
assert prompt.caret.locus == 0 |
119
|
|
|
assert action.call(prompt, 'prompt:move_caret_to_left') is None |
120
|
|
|
assert prompt.caret.locus == 0 |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
def test_move_caret_to_right(prompt, action): |
124
|
|
|
prompt.text = 'Hello Goodbye' |
125
|
|
|
prompt.caret.locus = 5 |
126
|
|
|
assert action.call(prompt, 'prompt:move_caret_to_right') is None |
127
|
|
|
assert prompt.text == 'Hello Goodbye' |
128
|
|
|
assert prompt.caret.locus == 6 |
129
|
|
|
|
130
|
|
|
assert action.call(prompt, 'prompt:move_caret_to_right') is None |
131
|
|
|
assert action.call(prompt, 'prompt:move_caret_to_right') is None |
132
|
|
|
assert action.call(prompt, 'prompt:move_caret_to_right') is None |
133
|
|
|
assert action.call(prompt, 'prompt:move_caret_to_right') is None |
134
|
|
|
assert action.call(prompt, 'prompt:move_caret_to_right') is None |
135
|
|
|
assert action.call(prompt, 'prompt:move_caret_to_right') is None |
136
|
|
|
assert action.call(prompt, 'prompt:move_caret_to_right') is None |
137
|
|
|
assert prompt.caret.locus == 13 |
138
|
|
|
assert action.call(prompt, 'prompt:move_caret_to_right') is None |
139
|
|
|
assert prompt.caret.locus == 13 |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
def test_move_caret_to_head(prompt, action): |
143
|
|
|
prompt.text = 'Hello Goodbye' |
144
|
|
|
prompt.caret.locus = 5 |
145
|
|
|
assert action.call(prompt, 'prompt:move_caret_to_head') is None |
146
|
|
|
assert prompt.text == 'Hello Goodbye' |
147
|
|
|
assert prompt.caret.locus == 0 |
148
|
|
|
assert action.call(prompt, 'prompt:move_caret_to_head') is None |
149
|
|
|
assert prompt.caret.locus == 0 |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
def test_move_caret_to_lead(prompt, action): |
153
|
|
|
prompt.text = ' Hello Goodbye' |
154
|
|
|
prompt.caret.locus = 9 |
155
|
|
|
assert action.call(prompt, 'prompt:move_caret_to_lead') is None |
156
|
|
|
assert prompt.text == ' Hello Goodbye' |
157
|
|
|
assert prompt.caret.locus == 4 |
158
|
|
|
assert action.call(prompt, 'prompt:move_caret_to_lead') is None |
159
|
|
|
assert prompt.caret.locus == 4 |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
def test_move_caret_to_tail(prompt, action): |
163
|
|
|
prompt.text = 'Hello Goodbye' |
164
|
|
|
prompt.caret.locus = 5 |
165
|
|
|
assert action.call(prompt, 'prompt:move_caret_to_tail') is None |
166
|
|
|
assert prompt.text == 'Hello Goodbye' |
167
|
|
|
assert prompt.caret.locus == 13 |
168
|
|
|
assert action.call(prompt, 'prompt:move_caret_to_tail') is None |
169
|
|
|
assert prompt.caret.locus == 13 |
170
|
|
|
|
171
|
|
|
|
172
|
|
View Code Duplication |
def test_assign_previous_text(prompt, action): |
|
|
|
|
173
|
|
|
prompt.history = MagicMock() |
174
|
|
|
prompt.history.previous.side_effect = [ |
175
|
|
|
'foo', 'bar', 'hoge', |
176
|
|
|
] |
177
|
|
|
prompt.text = 'Hello Goodbye' |
178
|
|
|
prompt.caret.locus = 5 |
179
|
|
|
assert action.call(prompt, 'prompt:assign_previous_text') is None |
180
|
|
|
assert prompt.text == 'foo' |
181
|
|
|
assert prompt.caret.locus == len(prompt.text) |
182
|
|
|
|
183
|
|
|
assert action.call(prompt, 'prompt:assign_previous_text') is None |
184
|
|
|
assert prompt.text == 'bar' |
185
|
|
|
assert prompt.caret.locus == len(prompt.text) |
186
|
|
|
|
187
|
|
|
assert action.call(prompt, 'prompt:assign_previous_text') is None |
188
|
|
|
assert prompt.text == 'hoge' |
189
|
|
|
assert prompt.caret.locus == len(prompt.text) |
190
|
|
|
|
191
|
|
|
|
192
|
|
View Code Duplication |
def test_assign_next_text(prompt, action): |
|
|
|
|
193
|
|
|
prompt.history = MagicMock() |
194
|
|
|
prompt.history.next.side_effect = [ |
195
|
|
|
'foo', 'bar', 'hoge', |
196
|
|
|
] |
197
|
|
|
prompt.text = 'Hello Goodbye' |
198
|
|
|
prompt.caret.locus = 5 |
199
|
|
|
assert action.call(prompt, 'prompt:assign_next_text') is None |
200
|
|
|
assert prompt.text == 'foo' |
201
|
|
|
assert prompt.caret.locus == len(prompt.text) |
202
|
|
|
|
203
|
|
|
assert action.call(prompt, 'prompt:assign_next_text') is None |
204
|
|
|
assert prompt.text == 'bar' |
205
|
|
|
assert prompt.caret.locus == len(prompt.text) |
206
|
|
|
|
207
|
|
|
assert action.call(prompt, 'prompt:assign_next_text') is None |
208
|
|
|
assert prompt.text == 'hoge' |
209
|
|
|
assert prompt.caret.locus == len(prompt.text) |
210
|
|
|
|
211
|
|
|
|
212
|
|
View Code Duplication |
def test_assign_previous_matched_text(prompt, action): |
|
|
|
|
213
|
|
|
prompt.history = MagicMock() |
214
|
|
|
prompt.history.previous_match.side_effect = [ |
215
|
|
|
'foo', 'bar', 'hoge', |
216
|
|
|
] |
217
|
|
|
prompt.text = 'Hello Goodbye' |
218
|
|
|
prompt.caret.locus = 5 |
219
|
|
|
assert action.call(prompt, 'prompt:assign_previous_matched_text') is None |
220
|
|
|
assert prompt.text == 'foo' |
221
|
|
|
assert prompt.caret.locus == len(prompt.text) |
222
|
|
|
|
223
|
|
|
assert action.call(prompt, 'prompt:assign_previous_matched_text') is None |
224
|
|
|
assert prompt.text == 'bar' |
225
|
|
|
assert prompt.caret.locus == len(prompt.text) |
226
|
|
|
|
227
|
|
|
assert action.call(prompt, 'prompt:assign_previous_matched_text') is None |
228
|
|
|
assert prompt.text == 'hoge' |
229
|
|
|
assert prompt.caret.locus == len(prompt.text) |
230
|
|
|
|
231
|
|
|
|
232
|
|
View Code Duplication |
def test_assign_next_matched_text(prompt, action): |
|
|
|
|
233
|
|
|
prompt.history = MagicMock() |
234
|
|
|
prompt.history.next_match.side_effect = [ |
235
|
|
|
'foo', 'bar', 'hoge', |
236
|
|
|
] |
237
|
|
|
prompt.text = 'Hello Goodbye' |
238
|
|
|
prompt.caret.locus = 5 |
239
|
|
|
assert action.call(prompt, 'prompt:assign_next_matched_text') is None |
240
|
|
|
assert prompt.text == 'foo' |
241
|
|
|
assert prompt.caret.locus == len(prompt.text) |
242
|
|
|
|
243
|
|
|
assert action.call(prompt, 'prompt:assign_next_matched_text') is None |
244
|
|
|
assert prompt.text == 'bar' |
245
|
|
|
assert prompt.caret.locus == len(prompt.text) |
246
|
|
|
|
247
|
|
|
assert action.call(prompt, 'prompt:assign_next_matched_text') is None |
248
|
|
|
assert prompt.text == 'hoge' |
249
|
|
|
assert prompt.caret.locus == len(prompt.text) |
250
|
|
|
|
251
|
|
|
|
252
|
|
|
def test_paste_from_register(prompt, action): |
253
|
|
|
prompt.nvim.eval = MagicMock() |
254
|
|
|
prompt.nvim.call = MagicMock() |
255
|
|
|
prompt.nvim.command = MagicMock() |
256
|
|
|
prompt.nvim.eval.return_value = 'a' |
257
|
|
|
prompt.nvim.call.side_effect = lambda fname, reg: '<%s>' % reg |
258
|
|
|
prompt.text = 'Hello Goodbye' |
259
|
|
|
prompt.caret.locus = 5 |
260
|
|
|
assert action.call(prompt, 'prompt:paste_from_register') is None |
261
|
|
|
assert prompt.text == 'Hello<a> Goodbye' |
262
|
|
|
assert prompt.caret.locus == 8 |
263
|
|
|
|
264
|
|
|
|
265
|
|
View Code Duplication |
def test_paste_from_default_register(prompt, action): |
|
|
|
|
266
|
|
|
prompt.nvim.vvars = { |
267
|
|
|
'register': '*', |
268
|
|
|
} |
269
|
|
|
prompt.nvim.call = MagicMock() |
270
|
|
|
prompt.nvim.command = MagicMock() |
271
|
|
|
prompt.nvim.call.side_effect = lambda fname, reg: '<%s>' % reg |
272
|
|
|
prompt.text = 'Hello Goodbye' |
273
|
|
|
prompt.caret.locus = 5 |
274
|
|
|
assert action.call(prompt, 'prompt:paste_from_default_register') is None |
275
|
|
|
assert prompt.text == 'Hello<*> Goodbye' |
276
|
|
|
assert prompt.caret.locus == 8 |
277
|
|
|
|
278
|
|
|
|
279
|
|
View Code Duplication |
def test_yank_to_register(prompt, action): |
|
|
|
|
280
|
|
|
prompt.nvim.eval = MagicMock() |
281
|
|
|
prompt.nvim.call = MagicMock() |
282
|
|
|
prompt.nvim.command = MagicMock() |
283
|
|
|
prompt.nvim.eval.return_value = 'a' |
284
|
|
|
prompt.text = 'Hello Goodbye' |
285
|
|
|
prompt.caret.locus = 5 |
286
|
|
|
assert action.call(prompt, 'prompt:yank_to_register') is None |
287
|
|
|
assert prompt.text == 'Hello Goodbye' |
288
|
|
|
assert prompt.caret.locus == 5 |
289
|
|
|
prompt.nvim.call.assert_called_with('setreg', 'a', 'Hello Goodbye') |
290
|
|
|
|
291
|
|
|
|
292
|
|
View Code Duplication |
def test_yank_to_default_register(prompt, action): |
|
|
|
|
293
|
|
|
prompt.nvim.vvars = { |
294
|
|
|
'register': '*', |
295
|
|
|
} |
296
|
|
|
prompt.nvim.call = MagicMock() |
297
|
|
|
prompt.nvim.command = MagicMock() |
298
|
|
|
prompt.text = 'Hello Goodbye' |
299
|
|
|
prompt.caret.locus = 5 |
300
|
|
|
assert action.call(prompt, 'prompt:yank_to_default_register') is None |
301
|
|
|
assert prompt.text == 'Hello Goodbye' |
302
|
|
|
assert prompt.caret.locus == 5 |
303
|
|
|
prompt.nvim.call.assert_called_with('setreg', '*', 'Hello Goodbye') |
304
|
|
|
|