|
1
|
|
|
from unittest.mock import MagicMock |
|
2
|
|
|
import pytest |
|
3
|
|
|
from neovim_prompt.keystroke import Keystroke |
|
4
|
|
|
from neovim_prompt.prompt import Prompt, Status, InsertMode |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
def test_Prompt_constructor(nvim, context): |
|
8
|
|
|
prompt = Prompt(nvim, context) |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
def test_prompt_text(prompt): |
|
12
|
|
|
assert prompt.text == '' |
|
13
|
|
|
assert prompt.caret.locus == 0 |
|
14
|
|
|
prompt.text = 'foo' |
|
15
|
|
|
assert prompt.text == 'foo' |
|
16
|
|
|
assert prompt.caret.locus == 3 |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
def test_prompt_apply_custom_mappings_from_vim_variable(prompt): |
|
20
|
|
|
nvim = prompt.nvim |
|
21
|
|
|
nvim.vars = { |
|
22
|
|
|
'prompt#custom_mappings': [ |
|
23
|
|
|
('<C-H>', '<BS>'), |
|
24
|
|
|
('<C-D>', '<DEL>'), |
|
25
|
|
|
] |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
# Fail silently |
|
29
|
|
|
prompt.apply_custom_mappings_from_vim_variable('does_not_exist') |
|
30
|
|
|
assert Keystroke.parse(nvim, '<C-H>') not in prompt.keymap.registry |
|
31
|
|
|
assert Keystroke.parse(nvim, '<C-D>') not in prompt.keymap.registry |
|
32
|
|
|
|
|
33
|
|
|
prompt.apply_custom_mappings_from_vim_variable('prompt#custom_mappings') |
|
34
|
|
|
assert Keystroke.parse(nvim, '<C-H>') in prompt.keymap.registry |
|
35
|
|
|
assert Keystroke.parse(nvim, '<C-D>') in prompt.keymap.registry |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
def test_insert_text(prompt): |
|
39
|
|
|
# docstring |
|
40
|
|
|
pass |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
def test_replace_text(prompt): |
|
44
|
|
|
# docstring |
|
45
|
|
|
pass |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
def test_update_text(prompt): |
|
49
|
|
|
# docstring |
|
50
|
|
|
pass |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
def test_start(prompt): |
|
54
|
|
|
nvim = prompt.nvim |
|
55
|
|
|
nvim.error = Exception |
|
56
|
|
|
nvim.call = MagicMock() |
|
57
|
|
|
nvim.eval = MagicMock() |
|
58
|
|
|
nvim.command = MagicMock() |
|
59
|
|
|
prompt.keymap = MagicMock() |
|
60
|
|
|
prompt.keymap.harvest.side_effect = [ |
|
61
|
|
|
Keystroke.parse(nvim, 'a'), |
|
62
|
|
|
Keystroke.parse(nvim, 'b'), |
|
63
|
|
|
Keystroke.parse(nvim, 'c'), |
|
64
|
|
|
Keystroke.parse(nvim, '<prompt:accept>'), |
|
65
|
|
|
] |
|
66
|
|
|
assert prompt.start() == 'abc' |
|
67
|
|
|
|
|
68
|
|
|
prompt.keymap.harvest.side_effect = [ |
|
69
|
|
|
Keystroke.parse(nvim, 'a'), |
|
70
|
|
|
Keystroke.parse(nvim, 'b'), |
|
71
|
|
|
Keystroke.parse(nvim, 'c'), |
|
72
|
|
|
Keystroke.parse(nvim, '<prompt:accept>'), |
|
73
|
|
|
] |
|
74
|
|
|
assert prompt.start('default') == 'defaultabc' |
|
75
|
|
|
|
|
76
|
|
|
prompt.keymap.harvest.side_effect = [ |
|
77
|
|
|
Keystroke.parse(nvim, 'a'), |
|
78
|
|
|
Keystroke.parse(nvim, 'b'), |
|
79
|
|
|
Keystroke.parse(nvim, 'c'), |
|
80
|
|
|
Keystroke.parse(nvim, '<prompt:cancel>'), |
|
81
|
|
|
] |
|
82
|
|
|
assert prompt.start() is None |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
def test_on_init(prompt): |
|
86
|
|
|
prompt.nvim.call = MagicMock() |
|
87
|
|
|
prompt.text = 'Hello Goodbye' |
|
88
|
|
|
assert prompt.on_init(None) == None |
|
89
|
|
|
prompt.nvim.call.assert_called_with('inputsave') |
|
90
|
|
|
assert prompt.text == 'Hello Goodbye' |
|
91
|
|
|
|
|
92
|
|
|
assert prompt.on_init('default') == None |
|
93
|
|
|
prompt.nvim.call.assert_called_with('inputsave') |
|
94
|
|
|
assert prompt.text == 'default' |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
def test_on_update(prompt): |
|
98
|
|
|
assert prompt.on_update(Status.accept) == Status.accept |
|
99
|
|
|
assert prompt.on_update(Status.cancel) == Status.cancel |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
def test_on_redraw(prompt): |
|
103
|
|
|
prompt.nvim.command = MagicMock() |
|
104
|
|
|
prompt.prefix = '# ' |
|
105
|
|
|
prompt.text = 'Hello Goodbye' |
|
106
|
|
|
prompt.caret.locus = 5 |
|
107
|
|
|
assert prompt.on_redraw() == None |
|
108
|
|
|
prompt.nvim.command.assert_called_with('|'.join([ |
|
109
|
|
|
'redraw', |
|
110
|
|
|
'echohl Question', |
|
111
|
|
|
'echon "# "', |
|
112
|
|
|
'echohl None', |
|
113
|
|
|
'echon "Hello"', |
|
114
|
|
|
'echohl Cursor', |
|
115
|
|
|
'echon " "', |
|
116
|
|
|
'echohl None', |
|
117
|
|
|
'echon "Goodbye"', |
|
118
|
|
|
])) |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
def on_keypress(prompt): |
|
122
|
|
|
nvim = prompt.nvim |
|
123
|
|
|
prompt.action = MagicMock() |
|
124
|
|
|
prompt.action.call.return_value = Status.accept |
|
125
|
|
|
prompt.update_text = MagicMock() |
|
126
|
|
|
|
|
127
|
|
|
assert prompt.on_keypress(Keystroke.parse(nvim, '<prompt:accept>')) \ |
|
128
|
|
|
== Status.accept |
|
129
|
|
|
prompt.action.call.assert_called_with(prompt, 'prompt:accept') |
|
130
|
|
|
|
|
131
|
|
|
assert prompt.on_keypress(Keystroke.parse(nvim, 'a')) == None |
|
132
|
|
|
prompt.update_text.assert_called_with('a') |
|
133
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
def test_on_term(prompt): |
|
136
|
|
|
prompt.nvim.call = MagicMock() |
|
137
|
|
|
assert prompt.on_term(Status.accept, 'Hello Goodbye') == None |
|
138
|
|
|
prompt.nvim.call.assert_called_with('inputrestore') |
|
139
|
|
|
|