test_vim.test_lua()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
import os
3
import sys
4
import tempfile
5
6
import pytest
7
8
9
def source(vim, code):
10
    fd, fname = tempfile.mkstemp()
11
    with os.fdopen(fd, 'w') as f:
12
        f.write(code)
13
    vim.command('source ' + fname)
14
    os.unlink(fname)
15
16
17
def test_clientinfo(vim):
18
    assert 'remote' == vim.api.get_chan_info(vim.channel_id)['client']['type']
19
20
21
def test_command(vim):
22
    fname = tempfile.mkstemp()[1]
23
    vim.command('new')
24
    vim.command('edit {}'.format(fname))
25
    # skip the "press return" state, which does not handle deferred calls
26
    vim.input('\r')
27
    vim.command('normal itesting\npython\napi')
28
    vim.command('w')
29
    assert os.path.isfile(fname)
30
    with open(fname) as f:
31
        assert f.read() == 'testing\npython\napi\n'
32
    os.unlink(fname)
33
34
35
def test_command_output(vim):
36
    assert vim.command_output('echo "test"') == 'test'
37
38
39
def test_command_error(vim):
40
    with pytest.raises(vim.error) as excinfo:
41
        vim.current.window.cursor = -1, -1
42
    assert excinfo.value.args == ('Cursor position outside buffer',)
43
44
45
def test_eval(vim):
46
    vim.command('let g:v1 = "a"')
47
    vim.command('let g:v2 = [1, 2, {"v3": 3}]')
48
    g = vim.eval('g:')
49
    assert g['v1'] == 'a'
50
    assert g['v2'] == [1, 2, {'v3': 3}]
51
52
53
def test_call(vim):
54
    assert vim.funcs.join(['first', 'last'], ', ') == 'first, last'
55
    source(vim, """
56
        function! Testfun(a,b)
57
            return string(a:a).":".a:b
58
        endfunction
59
    """)
60
    assert vim.funcs.Testfun(3, 'alpha') == '3:alpha'
61
62
63
def test_api(vim):
64
    vim.api.command('let g:var = 3')
65
    assert vim.api.eval('g:var') == 3
66
67
68
def test_strwidth(vim):
69
    assert vim.strwidth('abc') == 3
70
    # 6 + (neovim)
71
    # 19 * 2 (each japanese character occupies two cells)
72
    assert vim.strwidth('neovimのデザインかなりまともなのになってる。') == 44
73
74
75
def test_chdir(vim):
76
    pwd = vim.eval('getcwd()')
77
    root = os.path.abspath(os.sep)
78
    # We can chdir to '/' on Windows, but then the pwd will be the root drive
79
    vim.chdir('/')
80
    assert vim.eval('getcwd()') == root
81
    vim.chdir(pwd)
82
    assert vim.eval('getcwd()') == pwd
83
84
85
def test_current_line(vim):
86
    assert vim.current.line == ''
87
    vim.current.line = 'abc'
88
    assert vim.current.line == 'abc'
89
90
91
def test_current_line_delete(vim):
92
    vim.current.buffer[:] = ['one', 'two']
93
    assert len(vim.current.buffer[:]) == 2
94
    del vim.current.line
95
    assert len(vim.current.buffer[:]) == 1 and vim.current.buffer[0] == 'two'
96
    del vim.current.line
97
    assert len(vim.current.buffer[:]) == 1 and not vim.current.buffer[0]
98
99
100 View Code Duplication
def test_vars(vim):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
101
    vim.vars['python'] = [1, 2, {'3': 1}]
102
    assert vim.vars['python'], [1, 2 == {'3': 1}]
103
    assert vim.eval('g:python'), [1, 2 == {'3': 1}]
104
    assert vim.vars.get('python') == [1, 2, {'3': 1}]
105
106
    del vim.vars['python']
107
    with pytest.raises(KeyError):
108
        vim.vars['python']
109
    assert vim.eval('exists("g:python")') == 0
110
111
    with pytest.raises(KeyError):
112
        del vim.vars['python']
113
114
    assert vim.vars.get('python', 'default') == 'default'
115
116
117
def test_options(vim):
118
    assert vim.options['background'] == 'dark'
119
    vim.options['background'] = 'light'
120
    assert vim.options['background'] == 'light'
121
122
123
def test_local_options(vim):
124
    assert vim.windows[0].options['foldmethod'] == 'manual'
125
    vim.windows[0].options['foldmethod'] = 'syntax'
126
    assert vim.windows[0].options['foldmethod'] == 'syntax'
127
128
129
def test_buffers(vim):
130
    buffers = []
131
132
    # Number of elements
133
    assert len(vim.buffers) == 1
134
135
    # Indexing (by buffer number)
136
    assert vim.buffers[vim.current.buffer.number] == vim.current.buffer
137
138
    buffers.append(vim.current.buffer)
139
    vim.command('new')
140
    assert len(vim.buffers) == 2
141
    buffers.append(vim.current.buffer)
142
    assert vim.buffers[vim.current.buffer.number] == vim.current.buffer
143
    vim.current.buffer = buffers[0]
144
    assert vim.buffers[vim.current.buffer.number] == buffers[0]
145
146
    # Membership test
147
    assert buffers[0] in vim.buffers
148
    assert buffers[1] in vim.buffers
149
    assert {} not in vim.buffers
150
151
    # Iteration
152
    assert buffers == list(vim.buffers)
153
154
155
def test_windows(vim):
156
    assert len(vim.windows) == 1
157
    assert vim.windows[0] == vim.current.window
158
    vim.command('vsplit')
159
    vim.command('split')
160
    assert len(vim.windows) == 3
161
    assert vim.windows[0] == vim.current.window
162
    vim.current.window = vim.windows[1]
163
    assert vim.windows[1] == vim.current.window
164
165
166
def test_tabpages(vim):
167
    assert len(vim.tabpages) == 1
168
    assert vim.tabpages[0] == vim.current.tabpage
169
    vim.command('tabnew')
170
    assert len(vim.tabpages) == 2
171
    assert len(vim.windows) == 2
172
    assert vim.windows[1] == vim.current.window
173
    assert vim.tabpages[1] == vim.current.tabpage
174
    vim.current.window = vim.windows[0]
175
    # Switching window also switches tabpages if necessary(this probably
176
    # isn't the current behavior, but compatibility will be handled in the
177
    # python client with an optional parameter)
178
    assert vim.tabpages[0] == vim.current.tabpage
179
    assert vim.windows[0] == vim.current.window
180
    vim.current.tabpage = vim.tabpages[1]
181
    assert vim.tabpages[1] == vim.current.tabpage
182
    assert vim.windows[1] == vim.current.window
183
184
185
def test_hash(vim):
186
    d = {}
187
    d[vim.current.buffer] = "alpha"
188
    assert d[vim.current.buffer] == 'alpha'
189
    vim.command('new')
190
    d[vim.current.buffer] = "beta"
191
    assert d[vim.current.buffer] == 'beta'
192
    vim.command('winc w')
193
    assert d[vim.current.buffer] == 'alpha'
194
    vim.command('winc w')
195
    assert d[vim.current.buffer] == 'beta'
196
197
198
def test_cwd(vim, tmpdir):
199
    pycmd = 'python'
200
    if sys.version_info >= (3, 0):
201
        pycmd = 'python3'
202
203
    vim.command('{} import os'.format(pycmd))
204
    cwd_before = vim.command_output('{} print(os.getcwd())'.format(pycmd))
205
206
    vim.command('cd {}'.format(tmpdir.strpath))
207
    cwd_vim = vim.command_output('pwd')
208
    cwd_python = vim.command_output('{} print(os.getcwd())'.format(pycmd))
209
    assert cwd_python == cwd_vim
210
    assert cwd_python != cwd_before
211
212
213
lua_code = """
214
local a = vim.api
215
local y = ...
216
function pynvimtest_func(x)
217
    return x+y
218
end
219
220
local function setbuf(buf,lines)
221
   a.nvim_buf_set_lines(buf, 0, -1, true, lines)
222
end
223
224
225
local function getbuf(buf)
226
   return a.nvim_buf_line_count(buf)
227
end
228
229
pynvimtest = {setbuf=setbuf,getbuf=getbuf}
230
231
return "eggspam"
232
"""
233
234
235
def test_lua(vim):
236
    assert vim.exec_lua(lua_code, 7) == "eggspam"
237
    assert vim.lua.pynvimtest_func(3) == 10
238
    testmod = vim.lua.pynvimtest
239
    buf = vim.current.buffer
240
    testmod.setbuf(buf, ["a", "b", "c", "d"], async_=True)
241
    assert testmod.getbuf(buf) == 4
242