Completed
Push — master ( 9962ed...cbdbd2 )
by Lambda
01:01
created

test_Key_parse_with_bytes()   C

Complexity

Conditions 9

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
dl 0
loc 16
rs 6.4615
c 0
b 0
f 0
1
from curses import ascii
2
from collections import abc
3
4
from datetime import datetime, timedelta
5
6
import pytest
7
8
from neovim_prompt.key import Key
9
10
11
def test_Key_property():
12
    key = Key(0x01, '\x01')
13
    assert isinstance(key, abc.Hashable)
14
    assert isinstance(key, abc.Container)
15
    assert isinstance(key, abc.Sized)
16
17
18
def test_Key_parse_with_int(nvim):
19
    expr = 0x00
20
    assert Key.parse(nvim, expr).code == expr
21
    assert Key.parse(nvim, expr).char == '\x00'
22
23
24
def test_Key_parse_with_bytes(nvim):
25
    expr = b'\x80kb'
26
    assert Key.parse(nvim, expr).code == expr
27
    assert Key.parse(nvim, expr).char == ''
28
29
    expr = b'a'
30
    assert Key.parse(nvim, expr).code == ord(expr)
31
    assert Key.parse(nvim, expr).char == 'a'
32
33
    expr = b'INS'
34
    assert Key.parse(nvim, expr).code == b'INS'
35
    assert Key.parse(nvim, expr).char == 'INS'
36
37
    expr = b'<prompt:accept>'
38
    assert Key.parse(nvim, expr).code == b'<prompt:accept>'
39
    assert Key.parse(nvim, expr).char == '<prompt:accept>'
40
41
42 View Code Duplication
def test_Key_parse_with_bytes_special_key_constant_int(nvim):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
43
    expr = b'<CR>'
44
    assert Key.parse(nvim, expr).code == ascii.CR
45
    assert Key.parse(nvim, expr).char == '\r'
46
47
    expr = b'<BSLASH>'
48
    assert Key.parse(nvim, expr).code == ord('\\')
49
    assert Key.parse(nvim, expr).char == '\\'
50
51
    expr = b'<LT>'
52
    assert Key.parse(nvim, expr).code == ord('<')
53
    assert Key.parse(nvim, expr).char == '<'
54
55
56 View Code Duplication
def test_Key_parse_with_bytes_special_key_constant_bytes(nvim):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
57
    expr = b'<BACKSPACE>'
58
    assert Key.parse(nvim, expr).code == b'\x80kb'
59
    assert Key.parse(nvim, expr).char == ''
60
61
    expr = b'<DELETE>'
62
    assert Key.parse(nvim, expr).code == b'\x80kD'
63
    assert Key.parse(nvim, expr).char == ''
64
65
    expr = b'<INSERT>'
66
    assert Key.parse(nvim, expr).code == b'\x80kI'
67
    assert Key.parse(nvim, expr).char == ''
68
69
70
def test_Key_parse_with_bytes_special_key_ctrl(nvim):
71
    expr = b'<C-A>'
72
    assert Key.parse(nvim, expr).code == ord('\x01')
73
    assert Key.parse(nvim, expr).char == '\x01'
74
75
    expr = b'<C-H>'
76
    assert Key.parse(nvim, expr).code == ord('\x08')
77
    assert Key.parse(nvim, expr).char == '\x08'
78
79
    # NOTE:
80
    # https://github.com/vim/vim/blob/d58b0f982ad758c59abe47627216a15497e9c3c1/src/gui_w32.c#L1956-L1989
81
    # When user type '2', '6', or '-' with 'Ctrl' key, Vim assume they type
82
    # '^@', '^^', or '^_' instead. However, Vim reports "\x90\xfc\x046" for
83
    # :echo "\<C-6>" so <C-6> should not become <C-^> in key mapping level.
84
    expr = b'<C-2>'
85
    assert Key.parse(nvim, expr).code == b'\x80\xfc\x042'
86
    assert Key.parse(nvim, expr).char == ''
87
88
    expr = b'<C-6>'
89
    assert Key.parse(nvim, expr).code == b'\x80\xfc\x046'
90
    assert Key.parse(nvim, expr).char == ''
91
92
    expr = b'<C-->'
93
    assert Key.parse(nvim, expr).code == b'\x80\xfc\x04-'
94
    assert Key.parse(nvim, expr).char == ''
95
96
    expr = b'<C-BS>'
97
    assert Key.parse(nvim, expr).code == b'\x80\xfc\x04\x80kb'
98
    assert Key.parse(nvim, expr).char == ''
99
100
    expr = b'<C-INS>'
101
    assert Key.parse(nvim, expr).code == b'\x80\xfc\x04\x80kI'
102
    assert Key.parse(nvim, expr).char == ''
103
104
105
def test_Key_parse_with_bytes_special_key_meta(nvim):
106
    expr = b'<M-A>'
107
    assert Key.parse(nvim, expr).code == b'\x80\xfc\x08A'
108
    assert Key.parse(nvim, expr).char == ''
109
110
    expr = b'<M-z>'
111
    assert Key.parse(nvim, expr).code == b'\x80\xfc\x08z'
112
    assert Key.parse(nvim, expr).char == ''
113
114
    expr = b'<M-BS>'
115
    assert Key.parse(nvim, expr).code == b'\x80\xfc\x08\x80kb'
116
    assert Key.parse(nvim, expr).char == ''
117
118
    expr = b'<M-INS>'
119
    assert Key.parse(nvim, expr).code == b'\x80\xfc\x08\x80kI'
120
    assert Key.parse(nvim, expr).char == ''
121
122
    expr = b'<A-A>'
123
    assert Key.parse(nvim, expr).code == b'\x80\xfc\x08A'
124
    assert Key.parse(nvim, expr).char == ''
125
126
    expr = b'<A-z>'
127
    assert Key.parse(nvim, expr).code == b'\x80\xfc\x08z'
128
    assert Key.parse(nvim, expr).char == ''
129
130
    expr = b'<A-BS>'
131
    assert Key.parse(nvim, expr).code == b'\x80\xfc\x08\x80kb'
132
    assert Key.parse(nvim, expr).char == ''
133
134
    expr = b'<A-INS>'
135
    assert Key.parse(nvim, expr).code == b'\x80\xfc\x08\x80kI'
136
    assert Key.parse(nvim, expr).char == ''
137
138
139
def test_Key_parse_with_bytes_special_key_leader(nvim):
140
    nvim.vars = {
141
        'mapleader': '\\',
142
        'maplocalleader': ',',
143
    }
144
    expr = b'<Leader>'
145
    assert Key.parse(nvim, expr).code == ord(b'\\')
146
    assert Key.parse(nvim, expr).char == '\\'
147
148
    expr = b'<LocalLeader>'
149
    assert Key.parse(nvim, expr).code == ord(b',')
150
    assert Key.parse(nvim, expr).char == ','
151
152
153
def test_Key_parse_with_bytes_str(nvim):
154
    expr = '<Backspace>'
155
    assert Key.parse(nvim, expr).code == b'\x80kb'
156
    assert Key.parse(nvim, expr).char == ''
157
158
159
def test_Key_parse_reify(nvim):
160
    assert Key.parse(nvim, 0) == Key.parse(nvim, 0)
161
    assert Key.parse(nvim, 0) != Key.parse(nvim, 1)
162
    assert Key.parse(nvim, 0) is Key.parse(nvim, 0)
163
164
    assert Key.parse(nvim, b'\x80kb') == Key.parse(nvim, b'\x80kb')
165
    assert Key.parse(nvim, b'\x80kb') != Key.parse(nvim, b'\x80ku')
166
    assert Key.parse(nvim, b'\x80kb') is Key.parse(nvim, b'\x80kb')
167
168
169
def test_Key_immutability(nvim):
170
    key = Key.parse(nvim, 0)
171
    # Changing attributes is not permitted
172
    with pytest.raises(AttributeError):
173
        key.code = 1
174
    with pytest.raises(AttributeError):
175
        key.char = 1
176
    # Adding a new attribute is not permitted
177
    with pytest.raises(AttributeError):
178
        key.new_attribute = 1
179