|
1
|
|
|
from curses import ascii |
|
2
|
|
|
from collections import abc |
|
3
|
|
|
from contextlib import ExitStack |
|
4
|
|
|
|
|
5
|
|
|
from datetime import datetime, timedelta |
|
6
|
|
|
from unittest.mock import MagicMock, patch |
|
7
|
|
|
|
|
8
|
|
|
import pytest |
|
9
|
|
|
|
|
10
|
|
|
from neovim_prompt.key import Key |
|
11
|
|
|
from neovim_prompt.keystroke import Keystroke |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
def test_Keystroke_property(nvim): |
|
15
|
|
|
keys = Keystroke.parse(nvim, 'abc') |
|
16
|
|
|
assert isinstance(keys, abc.Hashable) |
|
17
|
|
|
assert isinstance(keys, abc.Container) |
|
18
|
|
|
assert isinstance(keys, abc.Sized) |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
def test_Keystroke_parse_with_sequence(nvim): |
|
22
|
|
|
expr = [ |
|
23
|
|
|
Key.parse(nvim, 'a'), |
|
24
|
|
|
Key.parse(nvim, 'b'), |
|
25
|
|
|
Key.parse(nvim, 'c'), |
|
26
|
|
|
] |
|
27
|
|
|
keys = Keystroke.parse(nvim, expr) |
|
28
|
|
|
assert keys == tuple(expr) |
|
29
|
|
|
|
|
30
|
|
|
keys = Keystroke.parse(nvim, tuple(expr)) |
|
31
|
|
|
assert keys == tuple(expr) |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
def test_Keystroke_parse_with_bytes(nvim): |
|
35
|
|
|
expr = [ |
|
36
|
|
|
Key.parse(nvim, 'a'), |
|
37
|
|
|
Key.parse(nvim, 'b'), |
|
38
|
|
|
Key.parse(nvim, 'c'), |
|
39
|
|
|
] |
|
40
|
|
|
keys = Keystroke.parse(nvim, b'abc') |
|
41
|
|
|
assert keys == tuple(expr) |
|
42
|
|
|
|
|
43
|
|
|
expr = [ |
|
44
|
|
|
Key.parse(nvim, '<C-T>'), |
|
45
|
|
|
Key.parse(nvim, 'b'), |
|
46
|
|
|
Key.parse(nvim, '<Home>'), |
|
47
|
|
|
] |
|
48
|
|
|
keys = Keystroke.parse(nvim, b'<C-T>b<Home>') |
|
49
|
|
|
assert keys == tuple(expr) |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
def test_Keystroke_parse_with_str(nvim): |
|
53
|
|
|
expr = [ |
|
54
|
|
|
Key.parse(nvim, 'a'), |
|
55
|
|
|
Key.parse(nvim, 'b'), |
|
56
|
|
|
Key.parse(nvim, 'c'), |
|
57
|
|
|
] |
|
58
|
|
|
keys = Keystroke.parse(nvim, 'abc') |
|
59
|
|
|
assert keys == tuple(expr) |
|
60
|
|
|
|
|
61
|
|
|
expr = [ |
|
62
|
|
|
Key.parse(nvim, '<C-T>'), |
|
63
|
|
|
Key.parse(nvim, 'b'), |
|
64
|
|
|
Key.parse(nvim, '<Home>'), |
|
65
|
|
|
] |
|
66
|
|
|
keys = Keystroke.parse(nvim, '<C-T>b<Home>') |
|
67
|
|
|
assert keys == tuple(expr) |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
def test_Keystroke_harvest(nvim): |
|
71
|
|
|
now = datetime.now() |
|
72
|
|
|
with patch('neovim_prompt.keystroke.datetime') as m1: |
|
73
|
|
|
def side_effect(*args): |
|
74
|
|
|
yield ord('a') |
|
75
|
|
|
yield ord('\x08') # ^H |
|
76
|
|
|
yield list(b'\x80kI') # Insert |
|
77
|
|
|
m1.now.return_value += timedelta(milliseconds=1000) |
|
78
|
|
|
yield ord('b') |
|
79
|
|
|
m1.now.return_value += timedelta(milliseconds=1001) |
|
80
|
|
|
yield ord('c') |
|
81
|
|
|
m1.now.return_value = now |
|
82
|
|
|
nvim.call = MagicMock() |
|
83
|
|
|
nvim.call.side_effect = side_effect() |
|
84
|
|
|
nvim.options = { |
|
85
|
|
|
'timeout': True, |
|
86
|
|
|
'timeoutlen': 1000, |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
keys = None |
|
90
|
|
|
timeout = None |
|
91
|
|
|
keys, timeout = Keystroke.harvest(nvim, keys, timeout) |
|
92
|
|
|
assert keys == Keystroke.parse(nvim, b'a') |
|
93
|
|
|
|
|
94
|
|
|
keys, timeout = Keystroke.harvest(nvim, keys, timeout) |
|
95
|
|
|
assert keys == Keystroke.parse(nvim, b'a<C-H>') |
|
96
|
|
|
|
|
97
|
|
|
keys, timeout = Keystroke.harvest(nvim, keys, timeout) |
|
98
|
|
|
assert keys == Keystroke.parse(nvim, b'a<C-H><INS>') |
|
99
|
|
|
|
|
100
|
|
|
keys, timeout = Keystroke.harvest(nvim, keys, timeout) |
|
101
|
|
|
assert keys == Keystroke.parse(nvim, b'a<C-H><INS>b') |
|
102
|
|
|
|
|
103
|
|
|
# timeout |
|
104
|
|
|
keys, timeout = Keystroke.harvest(nvim, keys, timeout) |
|
105
|
|
|
assert keys == Keystroke.parse(nvim, b'c') |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
nvim.call.side_effect = side_effect() |
|
109
|
|
|
nvim.options = { |
|
110
|
|
|
'timeout': False, |
|
111
|
|
|
'timeoutlen': 1000, |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
keys = None |
|
115
|
|
|
timeout = None |
|
116
|
|
|
keys, timeout = Keystroke.harvest(nvim, keys, timeout) |
|
117
|
|
|
assert keys == Keystroke.parse(nvim, b'a') |
|
118
|
|
|
|
|
119
|
|
|
keys, timeout = Keystroke.harvest(nvim, keys, timeout) |
|
120
|
|
|
assert keys == Keystroke.parse(nvim, b'a<C-H>') |
|
121
|
|
|
|
|
122
|
|
|
keys, timeout = Keystroke.harvest(nvim, keys, timeout) |
|
123
|
|
|
assert keys == Keystroke.parse(nvim, b'a<C-H><INS>') |
|
124
|
|
|
|
|
125
|
|
|
keys, timeout = Keystroke.harvest(nvim, keys, timeout) |
|
126
|
|
|
assert keys == Keystroke.parse(nvim, b'a<C-H><INS>b') |
|
127
|
|
|
|
|
128
|
|
|
keys, timeout = Keystroke.harvest(nvim, keys, timeout) |
|
129
|
|
|
assert keys == Keystroke.parse(nvim, b'a<C-H><INS>bc') |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
def test_keystroke_startswith(nvim): |
|
133
|
|
|
rhs1 = Keystroke.parse(nvim, '<C-A>') |
|
134
|
|
|
rhs2 = Keystroke.parse(nvim, '<C-A><C-B>') |
|
135
|
|
|
rhs3 = Keystroke.parse(nvim, '<C-A><C-C>') |
|
136
|
|
|
|
|
137
|
|
|
assert rhs1.startswith(Keystroke.parse(nvim, '')) |
|
138
|
|
|
assert rhs2.startswith(Keystroke.parse(nvim, '')) |
|
139
|
|
|
assert rhs3.startswith(Keystroke.parse(nvim, '')) |
|
140
|
|
|
|
|
141
|
|
|
assert rhs1.startswith(Keystroke.parse(nvim, '<C-A>')) |
|
142
|
|
|
assert rhs2.startswith(Keystroke.parse(nvim, '<C-A>')) |
|
143
|
|
|
assert rhs3.startswith(Keystroke.parse(nvim, '<C-A>')) |
|
144
|
|
|
|
|
145
|
|
|
assert not rhs1.startswith(Keystroke.parse(nvim, '<C-A><C-B>')) |
|
146
|
|
|
assert rhs2.startswith(Keystroke.parse(nvim, '<C-A><C-B>')) |
|
147
|
|
|
assert not rhs3.startswith(Keystroke.parse(nvim, '<C-A><C-B>')) |
|
148
|
|
|
|
|
149
|
|
|
assert not rhs1.startswith(Keystroke.parse(nvim, '<C-A><C-B><C-C>')) |
|
150
|
|
|
assert not rhs2.startswith(Keystroke.parse(nvim, '<C-A><C-B><C-C>')) |
|
151
|
|
|
assert not rhs3.startswith(Keystroke.parse(nvim, '<C-A><C-B><C-C>')) |
|
152
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
def test_keystroke_str(nvim): |
|
155
|
|
|
assert str(Keystroke.parse(nvim, 'abc')) == 'abc' |
|
156
|
|
|
assert str(Keystroke.parse(nvim, '<C-H><C-H>')) == '\x08\x08' |
|
157
|
|
|
assert str(Keystroke.parse(nvim, '<Backspace><Delete>')) == '' |
|
158
|
|
|
assert str(Keystroke.parse(nvim, '<prompt:accept>')) == '<prompt:accept>' |
|
159
|
|
|
|