File: test_buffer.py

package info (click to toggle)
python-pynvim 0.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 344 kB
  • sloc: python: 2,538; sh: 6; makefile: 4
file content (201 lines) | stat: -rw-r--r-- 6,809 bytes parent folder | download
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
import os

import pytest

from pynvim.api import NvimError
from pynvim.compat import IS_PYTHON3


def test_repr(vim):
    assert repr(vim.current.buffer) == "<Buffer(handle=1)>"


def test_get_length(vim):
    assert len(vim.current.buffer) == 1
    vim.current.buffer.append('line')
    assert len(vim.current.buffer) == 2
    vim.current.buffer.append('line')
    assert len(vim.current.buffer) == 3
    vim.current.buffer[-1] = None
    assert len(vim.current.buffer) == 2
    vim.current.buffer[-1] = None
    vim.current.buffer[-1] = None
    # There's always at least one line
    assert len(vim.current.buffer) == 1


def test_get_set_del_line(vim):
    assert vim.current.buffer[0] == ''
    vim.current.buffer[0] = 'line1'
    assert vim.current.buffer[0] == 'line1'
    vim.current.buffer[0] = 'line2'
    assert vim.current.buffer[0] == 'line2'
    vim.current.buffer[0] = None
    assert vim.current.buffer[0] == ''
    # __delitem__
    vim.current.buffer[:] = ['line1', 'line2', 'line3']
    assert vim.current.buffer[2] == 'line3'
    del vim.current.buffer[0]
    assert vim.current.buffer[0] == 'line2'
    assert vim.current.buffer[1] == 'line3'
    del vim.current.buffer[-1]
    assert vim.current.buffer[0] == 'line2'
    assert len(vim.current.buffer) == 1


def test_get_set_del_slice(vim):
    assert vim.current.buffer[:] == ['']
    # Replace buffer
    vim.current.buffer[:] = ['a', 'b', 'c']
    assert vim.current.buffer[:] == ['a', 'b', 'c']
    assert vim.current.buffer[1:] == ['b', 'c']
    assert vim.current.buffer[1:2] == ['b']
    assert vim.current.buffer[1:1] == []
    assert vim.current.buffer[:-1] == ['a', 'b']
    assert vim.current.buffer[1:-1] == ['b']
    assert vim.current.buffer[-2:] == ['b', 'c']
    vim.current.buffer[1:2] = ['a', 'b', 'c']
    assert vim.current.buffer[:] == ['a', 'a', 'b', 'c', 'c']
    vim.current.buffer[-1:] = ['a', 'b', 'c']
    assert vim.current.buffer[:] == ['a', 'a', 'b', 'c', 'a', 'b', 'c']
    vim.current.buffer[:-3] = None
    assert vim.current.buffer[:] == ['a', 'b', 'c']
    vim.current.buffer[:] = None
    assert vim.current.buffer[:] == ['']
    # __delitem__
    vim.current.buffer[:] = ['a', 'b', 'c']
    del vim.current.buffer[:]
    assert vim.current.buffer[:] == ['']
    vim.current.buffer[:] = ['a', 'b', 'c']
    del vim.current.buffer[:1]
    assert vim.current.buffer[:] == ['b', 'c']
    del vim.current.buffer[:-1]
    assert vim.current.buffer[:] == ['c']


def test_vars(vim):
    vim.current.buffer.vars['python'] = [1, 2, {'3': 1}]
    assert vim.current.buffer.vars['python'] == [1, 2, {'3': 1}]
    assert vim.eval('b:python') == [1, 2, {'3': 1}]
    assert vim.current.buffer.vars.get('python') == [1, 2, {'3': 1}]

    del vim.current.buffer.vars['python']
    with pytest.raises(KeyError):
        vim.current.buffer.vars['python']
    assert vim.eval('exists("b:python")') == 0

    with pytest.raises(KeyError):
        del vim.current.buffer.vars['python']

    assert vim.current.buffer.vars.get('python', 'default') == 'default'


def test_api(vim):
    vim.current.buffer.api.set_var('myvar', 'thetext')
    assert vim.current.buffer.api.get_var('myvar') == 'thetext'
    assert vim.eval('b:myvar') == 'thetext'
    vim.current.buffer.api.set_lines(0, -1, True, ['alpha', 'beta'])
    assert vim.current.buffer.api.get_lines(0, -1, True) == ['alpha', 'beta']
    assert vim.current.buffer[:] == ['alpha', 'beta']


def test_options(vim):
    assert vim.current.buffer.options['shiftwidth'] == 8
    vim.current.buffer.options['shiftwidth'] = 4
    assert vim.current.buffer.options['shiftwidth'] == 4
    # global-local option
    vim.current.buffer.options['define'] = 'test'
    assert vim.current.buffer.options['define'] == 'test'
    # Doesn't change the global value
    assert vim.options['define'] == r'^\s*#\s*define'

    with pytest.raises(KeyError) as excinfo:
        vim.current.buffer.options['doesnotexist']
    assert excinfo.value.args == ("Invalid option name: 'doesnotexist'",)


def test_number(vim):
    curnum = vim.current.buffer.number
    vim.command('new')
    assert vim.current.buffer.number == curnum + 1
    vim.command('new')
    assert vim.current.buffer.number == curnum + 2


def test_name(vim):
    vim.command('new')
    assert vim.current.buffer.name == ''
    new_name = vim.eval('resolve(tempname())')
    vim.current.buffer.name = new_name
    assert vim.current.buffer.name == new_name
    vim.command('silent w!')
    assert os.path.isfile(new_name)
    os.unlink(new_name)


def test_valid(vim):
    vim.command('new')
    buffer = vim.current.buffer
    assert buffer.valid
    vim.command('bw!')
    assert not buffer.valid


def test_append(vim):
    vim.current.buffer.append('a')
    assert vim.current.buffer[:] == ['', 'a']
    vim.current.buffer.append('b', 0)
    assert vim.current.buffer[:] == ['b', '', 'a']
    vim.current.buffer.append(['c', 'd'])
    assert vim.current.buffer[:] == ['b', '', 'a', 'c', 'd']
    vim.current.buffer.append(['c', 'd'], 2)
    assert vim.current.buffer[:] == ['b', '', 'c', 'd', 'a', 'c', 'd']
    vim.current.buffer.append(b'bytes')
    assert vim.current.buffer[:] == ['b', '', 'c', 'd', 'a', 'c', 'd', 'bytes']


def test_mark(vim):
    vim.current.buffer.append(['a', 'bit of', 'text'])
    vim.current.window.cursor = [3, 4]
    vim.command('mark V')
    assert vim.current.buffer.mark('V') == [3, 0]


def test_invalid_utf8(vim):
    vim.command('normal "=printf("%c", 0xFF)\np')
    assert vim.eval("char2nr(getline(1))") == 0xFF
    assert vim.current.buffer[:] == ['\udcff'] if IS_PYTHON3 else ['\xff']

    vim.current.line += 'x'
    assert vim.eval("getline(1)", decode=False) == '\udcffx' if IS_PYTHON3 else '\xffx'
    assert vim.current.buffer[:] == ['\udcffx'] if IS_PYTHON3 else ['\xffx']


def test_get_exceptions(vim):
    with pytest.raises(KeyError) as excinfo:
        vim.current.buffer.options['invalid-option']

    assert not isinstance(excinfo.value, NvimError)
    assert excinfo.value.args == ("Invalid option name: 'invalid-option'",)


def test_set_items_for_range(vim):
    vim.current.buffer[:] = ['a', 'b', 'c', 'd', 'e']
    r = vim.current.buffer.range(1, 3)
    r[1:3] = ['foo'] * 3
    assert vim.current.buffer[:] == ['a', 'foo', 'foo', 'foo', 'd', 'e']


# NB: we can't easily test the effect of this. But at least run the lua
# function sync, so we know it runs without runtime error with simple args.
def test_update_highlights(vim):
    vim.current.buffer[:] = ['a', 'b', 'c']
    src_id = vim.new_highlight_source()
    vim.current.buffer.update_highlights(
        src_id, [["Comment", 0, 0, -1], ("String", 1, 0, 1)], clear=True, async_=False
    )


def test_buffer_inequality(vim):
    b = vim.current.buffer
    assert not (b != vim.current.buffer)