Bram Moolenaar | d58f03b | 2017-01-29 22:48:45 +0100 | [diff] [blame] | 1 | " Tests for Lua. |
Bram Moolenaar | d58f03b | 2017-01-29 22:48:45 +0100 | [diff] [blame] | 2 | |
Bram Moolenaar | b46fecd | 2019-06-15 17:58:09 +0200 | [diff] [blame] | 3 | source check.vim |
| 4 | CheckFeature lua |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 5 | CheckFeature float |
Bram Moolenaar | d58f03b | 2017-01-29 22:48:45 +0100 | [diff] [blame] | 6 | |
Bram Moolenaar | e165f63 | 2019-03-10 09:48:59 +0100 | [diff] [blame] | 7 | func TearDown() |
| 8 | " Run garbage collection after each test to exercise luaV_setref(). |
| 9 | call test_garbagecollect_now() |
| 10 | endfunc |
| 11 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 12 | " Check that switching to another buffer does not trigger ml_get error. |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 13 | func Test_lua_command_new_no_ml_get_error() |
Bram Moolenaar | d58f03b | 2017-01-29 22:48:45 +0100 | [diff] [blame] | 14 | new |
| 15 | let wincount = winnr('$') |
| 16 | call setline(1, ['one', 'two', 'three']) |
| 17 | luado vim.command("new") |
| 18 | call assert_equal(wincount + 1, winnr('$')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 19 | %bwipe! |
| 20 | endfunc |
| 21 | |
| 22 | " Test vim.command() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 23 | func Test_lua_command() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 24 | new |
| 25 | call setline(1, ['one', 'two', 'three']) |
| 26 | luado vim.command("1,2d_") |
| 27 | call assert_equal(['three'], getline(1, '$')) |
Bram Moolenaar | d58f03b | 2017-01-29 22:48:45 +0100 | [diff] [blame] | 28 | bwipe! |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 29 | endfunc |
| 30 | |
| 31 | " Test vim.eval() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 32 | func Test_lua_eval() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 33 | " lua.eval with a number |
| 34 | lua v = vim.eval('123') |
| 35 | call assert_equal('number', luaeval('vim.type(v)')) |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 36 | call assert_equal(123, luaeval('v')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 37 | |
| 38 | " lua.eval with a string |
| 39 | lua v = vim.eval('"abc"') |
Bram Moolenaar | 02b3111 | 2019-08-31 22:16:38 +0200 | [diff] [blame] | 40 | call assert_equal('string', 'vim.type(v)'->luaeval()) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 41 | call assert_equal('abc', luaeval('v')) |
| 42 | |
| 43 | " lua.eval with a list |
| 44 | lua v = vim.eval("['a']") |
| 45 | call assert_equal('list', luaeval('vim.type(v)')) |
| 46 | call assert_equal(['a'], luaeval('v')) |
| 47 | |
| 48 | " lua.eval with a dict |
| 49 | lua v = vim.eval("{'a':'b'}") |
| 50 | call assert_equal('dict', luaeval('vim.type(v)')) |
| 51 | call assert_equal({'a':'b'}, luaeval('v')) |
| 52 | |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 53 | " lua.eval with a blob |
| 54 | lua v = vim.eval("0z00112233.deadbeef") |
| 55 | call assert_equal('blob', luaeval('vim.type(v)')) |
| 56 | call assert_equal(0z00112233.deadbeef, luaeval('v')) |
| 57 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 58 | call assert_fails('lua v = vim.eval(nil)', |
| 59 | \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got nil)") |
| 60 | call assert_fails('lua v = vim.eval(true)', |
| 61 | \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got boolean)") |
| 62 | call assert_fails('lua v = vim.eval({})', |
| 63 | \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got table)") |
| 64 | call assert_fails('lua v = vim.eval(print)', |
| 65 | \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got function)") |
| 66 | call assert_fails('lua v = vim.eval(vim.buffer())', |
| 67 | \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got userdata)") |
| 68 | |
| 69 | lua v = nil |
| 70 | endfunc |
| 71 | |
| 72 | " Test vim.window() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 73 | func Test_lua_window() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 74 | e Xfoo2 |
| 75 | new Xfoo1 |
| 76 | |
| 77 | " Window 1 (top window) contains Xfoo1 |
| 78 | " Window 2 (bottom window) contains Xfoo2 |
| 79 | call assert_equal('Xfoo1', luaeval('vim.window(1):buffer().name')) |
| 80 | call assert_equal('Xfoo2', luaeval('vim.window(2):buffer().name')) |
| 81 | |
| 82 | " Window 3 does not exist so vim.window(3) should return nil |
| 83 | call assert_equal('nil', luaeval('tostring(vim.window(3))')) |
| 84 | |
| 85 | %bwipe! |
| 86 | endfunc |
| 87 | |
| 88 | " Test vim.window().height |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 89 | func Test_lua_window_height() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 90 | new |
| 91 | lua vim.window().height = 2 |
| 92 | call assert_equal(2, winheight(0)) |
| 93 | lua vim.window().height = vim.window().height + 1 |
| 94 | call assert_equal(3, winheight(0)) |
| 95 | bwipe! |
| 96 | endfunc |
| 97 | |
| 98 | " Test vim.window().width |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 99 | func Test_lua_window_width() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 100 | vert new |
| 101 | lua vim.window().width = 2 |
| 102 | call assert_equal(2, winwidth(0)) |
| 103 | lua vim.window().width = vim.window().width + 1 |
| 104 | call assert_equal(3, winwidth(0)) |
| 105 | bwipe! |
| 106 | endfunc |
| 107 | |
| 108 | " Test vim.window().line and vim.window.col |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 109 | func Test_lua_window_line_col() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 110 | new |
| 111 | call setline(1, ['line1', 'line2', 'line3']) |
| 112 | lua vim.window().line = 2 |
| 113 | lua vim.window().col = 4 |
| 114 | call assert_equal([0, 2, 4, 0], getpos('.')) |
| 115 | lua vim.window().line = vim.window().line + 1 |
| 116 | lua vim.window().col = vim.window().col - 1 |
| 117 | call assert_equal([0, 3, 3, 0], getpos('.')) |
| 118 | |
| 119 | call assert_fails('lua vim.window().line = 10', |
| 120 | \ '[string "vim chunk"]:1: line out of range') |
| 121 | bwipe! |
| 122 | endfunc |
| 123 | |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 124 | " Test vim.call |
| 125 | func Test_lua_call() |
| 126 | call assert_equal(has('lua'), luaeval('vim.call("has", "lua")')) |
| 127 | call assert_equal(printf("Hello %s", "vim"), luaeval('vim.call("printf", "Hello %s", "vim")')) |
| 128 | endfunc |
| 129 | |
| 130 | " Test vim.fn.* |
| 131 | func Test_lua_fn() |
| 132 | call assert_equal(has('lua'), luaeval('vim.fn.has("lua")')) |
| 133 | call assert_equal(printf("Hello %s", "vim"), luaeval('vim.fn.printf("Hello %s", "vim")')) |
| 134 | endfunc |
| 135 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 136 | " Test setting the current window |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 137 | func Test_lua_window_set_current() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 138 | new Xfoo1 |
| 139 | lua w1 = vim.window() |
| 140 | new Xfoo2 |
| 141 | lua w2 = vim.window() |
| 142 | |
| 143 | call assert_equal('Xfoo2', bufname('%')) |
| 144 | lua w1() |
| 145 | call assert_equal('Xfoo1', bufname('%')) |
| 146 | lua w2() |
| 147 | call assert_equal('Xfoo2', bufname('%')) |
| 148 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 149 | lua w1, w2 = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 150 | %bwipe! |
| 151 | endfunc |
| 152 | |
| 153 | " Test vim.window().buffer |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 154 | func Test_lua_window_buffer() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 155 | new Xfoo1 |
| 156 | lua w1 = vim.window() |
| 157 | lua b1 = w1.buffer() |
| 158 | new Xfoo2 |
| 159 | lua w2 = vim.window() |
| 160 | lua b2 = w2.buffer() |
| 161 | |
| 162 | lua b1() |
| 163 | call assert_equal('Xfoo1', bufname('%')) |
| 164 | lua b2() |
| 165 | call assert_equal('Xfoo2', bufname('%')) |
| 166 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 167 | lua b1, b2, w1, w2 = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 168 | %bwipe! |
| 169 | endfunc |
| 170 | |
| 171 | " Test vim.window():previous() and vim.window():next() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 172 | func Test_lua_window_next_previous() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 173 | new Xfoo1 |
| 174 | new Xfoo2 |
| 175 | new Xfoo3 |
| 176 | wincmd j |
| 177 | |
| 178 | call assert_equal('Xfoo2', luaeval('vim.window().buffer().name')) |
| 179 | call assert_equal('Xfoo1', luaeval('vim.window():next():buffer().name')) |
| 180 | call assert_equal('Xfoo3', luaeval('vim.window():previous():buffer().name')) |
| 181 | |
| 182 | %bwipe! |
| 183 | endfunc |
| 184 | |
| 185 | " Test vim.window():isvalid() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 186 | func Test_lua_window_isvalid() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 187 | new Xfoo |
| 188 | lua w = vim.window() |
| 189 | call assert_true(luaeval('w:isvalid()')) |
| 190 | |
| 191 | " FIXME: how to test the case when isvalid() returns v:false? |
| 192 | " isvalid() gives errors when the window is deleted. Is it a bug? |
| 193 | |
| 194 | lua w = nil |
| 195 | bwipe! |
| 196 | endfunc |
| 197 | |
| 198 | " Test vim.buffer() with and without argument |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 199 | func Test_lua_buffer() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 200 | new Xfoo1 |
| 201 | let bn1 = bufnr('%') |
| 202 | new Xfoo2 |
| 203 | let bn2 = bufnr('%') |
| 204 | |
| 205 | " Test vim.buffer() without argument. |
| 206 | call assert_equal('Xfoo2', luaeval("vim.buffer().name")) |
| 207 | |
| 208 | " Test vim.buffer() with string argument. |
| 209 | call assert_equal('Xfoo1', luaeval("vim.buffer('Xfoo1').name")) |
| 210 | call assert_equal('Xfoo2', luaeval("vim.buffer('Xfoo2').name")) |
| 211 | |
| 212 | " Test vim.buffer() with integer argument. |
| 213 | call assert_equal('Xfoo1', luaeval("vim.buffer(" . bn1 . ").name")) |
| 214 | call assert_equal('Xfoo2', luaeval("vim.buffer(" . bn2 . ").name")) |
| 215 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 216 | lua bn1, bn2 = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 217 | %bwipe! |
| 218 | endfunc |
| 219 | |
| 220 | " Test vim.buffer().name and vim.buffer().fname |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 221 | func Test_lua_buffer_name() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 222 | new |
Bram Moolenaar | fe08df4 | 2018-07-07 23:07:41 +0200 | [diff] [blame] | 223 | call assert_equal('', luaeval('vim.buffer().name')) |
| 224 | call assert_equal('', luaeval('vim.buffer().fname')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 225 | bwipe! |
| 226 | |
| 227 | new Xfoo |
| 228 | call assert_equal('Xfoo', luaeval('vim.buffer().name')) |
| 229 | call assert_equal(expand('%:p'), luaeval('vim.buffer().fname')) |
| 230 | bwipe! |
| 231 | endfunc |
| 232 | |
| 233 | " Test vim.buffer().number |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 234 | func Test_lua_buffer_number() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 235 | " All numbers in Lua are floating points number (no integers). |
| 236 | call assert_equal(bufnr('%'), float2nr(luaeval('vim.buffer().number'))) |
| 237 | endfunc |
| 238 | |
| 239 | " Test inserting lines in buffer. |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 240 | func Test_lua_buffer_insert() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 241 | new |
| 242 | lua vim.buffer()[1] = '3' |
| 243 | lua vim.buffer():insert('1', 0) |
| 244 | lua vim.buffer():insert('2', 1) |
| 245 | lua vim.buffer():insert('4', 10) |
| 246 | |
| 247 | call assert_equal(['1', '2', '3', '4'], getline(1, '$')) |
| 248 | bwipe! |
| 249 | endfunc |
| 250 | |
| 251 | " Test deleting line in buffer |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 252 | func Test_lua_buffer_delete() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 253 | new |
| 254 | call setline(1, ['1', '2', '3']) |
| 255 | lua vim.buffer()[2] = nil |
| 256 | call assert_equal(['1', '3'], getline(1, '$')) |
| 257 | |
| 258 | call assert_fails('lua vim.buffer()[3] = nil', |
| 259 | \ '[string "vim chunk"]:1: invalid line number') |
| 260 | bwipe! |
| 261 | endfunc |
| 262 | |
| 263 | " Test #vim.buffer() i.e. number of lines in buffer |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 264 | func Test_lua_buffer_number_lines() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 265 | new |
| 266 | call setline(1, ['a', 'b', 'c']) |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 267 | call assert_equal(3, luaeval('#vim.buffer()')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 268 | bwipe! |
| 269 | endfunc |
| 270 | |
| 271 | " Test vim.buffer():next() and vim.buffer():previous() |
| 272 | " Note that these functions get the next or previous buffers |
| 273 | " but do not switch buffer. |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 274 | func Test_lua_buffer_next_previous() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 275 | new Xfoo1 |
| 276 | new Xfoo2 |
| 277 | new Xfoo3 |
| 278 | b Xfoo2 |
| 279 | |
| 280 | lua bn = vim.buffer():next() |
| 281 | lua bp = vim.buffer():previous() |
| 282 | |
| 283 | call assert_equal('Xfoo2', luaeval('vim.buffer().name')) |
| 284 | call assert_equal('Xfoo1', luaeval('bp.name')) |
| 285 | call assert_equal('Xfoo3', luaeval('bn.name')) |
| 286 | |
| 287 | call assert_equal('Xfoo2', bufname('%')) |
| 288 | |
| 289 | lua bn() |
| 290 | call assert_equal('Xfoo3', luaeval('vim.buffer().name')) |
| 291 | call assert_equal('Xfoo3', bufname('%')) |
| 292 | |
| 293 | lua bp() |
| 294 | call assert_equal('Xfoo1', luaeval('vim.buffer().name')) |
| 295 | call assert_equal('Xfoo1', bufname('%')) |
| 296 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 297 | lua bn, bp = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 298 | %bwipe! |
| 299 | endfunc |
| 300 | |
| 301 | " Test vim.buffer():isvalid() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 302 | func Test_lua_buffer_isvalid() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 303 | new Xfoo |
| 304 | lua b = vim.buffer() |
| 305 | call assert_true(luaeval('b:isvalid()')) |
| 306 | |
| 307 | " FIXME: how to test the case when isvalid() returns v:false? |
| 308 | " isvalid() gives errors when the buffer is wiped. Is it a bug? |
| 309 | |
| 310 | lua b = nil |
| 311 | bwipe! |
| 312 | endfunc |
| 313 | |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 314 | func Test_lua_list() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 315 | call assert_equal([], luaeval('vim.list()')) |
| 316 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 317 | let l = [] |
| 318 | lua l = vim.eval('l') |
| 319 | lua l:add(123) |
| 320 | lua l:add('abc') |
| 321 | lua l:add(true) |
| 322 | lua l:add(false) |
Bram Moolenaar | 9067cd6 | 2019-01-01 00:41:54 +0100 | [diff] [blame] | 323 | lua l:add(nil) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 324 | lua l:add(vim.eval("[1, 2, 3]")) |
| 325 | lua l:add(vim.eval("{'a':1, 'b':2, 'c':3}")) |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 326 | call assert_equal([123, 'abc', v:true, v:false, v:null, [1, 2, 3], {'a': 1, 'b': 2, 'c': 3}], l) |
| 327 | call assert_equal(7, luaeval('#l')) |
Bram Moolenaar | a8a60d0 | 2018-07-02 22:54:36 +0200 | [diff] [blame] | 328 | call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 329 | |
| 330 | lua l[0] = 124 |
Bram Moolenaar | 9067cd6 | 2019-01-01 00:41:54 +0100 | [diff] [blame] | 331 | lua l[5] = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 332 | lua l:insert('first') |
| 333 | lua l:insert('xx', 3) |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 334 | call assert_equal(['first', 124, 'abc', 'xx', v:true, v:false, v:null, {'a': 1, 'b': 2, 'c': 3}], l) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 335 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 336 | lockvar 1 l |
| 337 | call assert_fails('lua l:add("x")', '[string "vim chunk"]:1: list is locked') |
| 338 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 339 | lua l = nil |
| 340 | endfunc |
| 341 | |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 342 | func Test_lua_list_table() |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 343 | " See :help lua-vim |
| 344 | " Non-numeric keys should not be used to initialize the list |
| 345 | " so say = 'hi' should be ignored. |
| 346 | lua t = {3.14, 'hello', false, true, say = 'hi'} |
| 347 | call assert_equal([3.14, 'hello', v:false, v:true], luaeval('vim.list(t)')) |
| 348 | lua t = nil |
| 349 | |
| 350 | call assert_fails('lua vim.list(1)', '[string "vim chunk"]:1: table expected, got number') |
| 351 | call assert_fails('lua vim.list("x")', '[string "vim chunk"]:1: table expected, got string') |
| 352 | call assert_fails('lua vim.list(print)', '[string "vim chunk"]:1: table expected, got function') |
| 353 | call assert_fails('lua vim.list(true)', '[string "vim chunk"]:1: table expected, got boolean') |
| 354 | endfunc |
| 355 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 356 | " Test l() i.e. iterator on list |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 357 | func Test_lua_list_iter() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 358 | lua l = vim.list():add('foo'):add('bar') |
| 359 | lua str = '' |
| 360 | lua for v in l() do str = str .. v end |
| 361 | call assert_equal('foobar', luaeval('str')) |
| 362 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 363 | lua str, l = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 364 | endfunc |
| 365 | |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 366 | func Test_lua_recursive_list() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 367 | lua l = vim.list():add(1):add(2) |
| 368 | lua l = l:add(l) |
| 369 | |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 370 | call assert_equal(1, luaeval('l[0]')) |
| 371 | call assert_equal(2, luaeval('l[1]')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 372 | |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 373 | call assert_equal(1, luaeval('l[2][0]')) |
| 374 | call assert_equal(2, luaeval('l[2][1]')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 375 | |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 376 | call assert_equal(1, luaeval('l[2][2][0]')) |
| 377 | call assert_equal(2, luaeval('l[2][2][1]')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 378 | |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 379 | call assert_equal('[1, 2, [...]]', string(luaeval('l'))) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 380 | |
Bram Moolenaar | a8a60d0 | 2018-07-02 22:54:36 +0200 | [diff] [blame] | 381 | call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 382 | call assert_equal(luaeval('tostring(l)'), luaeval('tostring(l[2])')) |
| 383 | |
| 384 | call assert_equal(luaeval('l'), luaeval('l[2]')) |
| 385 | call assert_equal(luaeval('l'), luaeval('l[2][2]')) |
| 386 | |
| 387 | lua l = nil |
| 388 | endfunc |
| 389 | |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 390 | func Test_lua_dict() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 391 | call assert_equal({}, luaeval('vim.dict()')) |
| 392 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 393 | let d = {} |
| 394 | lua d = vim.eval('d') |
| 395 | lua d[0] = 123 |
| 396 | lua d[1] = "abc" |
| 397 | lua d[2] = true |
| 398 | lua d[3] = false |
| 399 | lua d[4] = vim.eval("[1, 2, 3]") |
| 400 | lua d[5] = vim.eval("{'a':1, 'b':2, 'c':3}") |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 401 | call assert_equal({'0':123, '1':'abc', '2':v:true, '3':v:false, '4': [1, 2, 3], '5': {'a':1, 'b':2, 'c':3}}, d) |
| 402 | call assert_equal(6, luaeval('#d')) |
Bram Moolenaar | a8a60d0 | 2018-07-02 22:54:36 +0200 | [diff] [blame] | 403 | call assert_match('^dict: \%(0x\)\?\x\+$', luaeval('tostring(d)')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 404 | |
| 405 | call assert_equal('abc', luaeval('d[1]')) |
| 406 | |
| 407 | lua d[0] = 124 |
| 408 | lua d[4] = nil |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 409 | call assert_equal({'0':124, '1':'abc', '2':v:true, '3':v:false, '5': {'a':1, 'b':2, 'c':3}}, d) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 410 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 411 | lockvar 1 d |
| 412 | call assert_fails('lua d[6] = 1', '[string "vim chunk"]:1: dict is locked') |
| 413 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 414 | lua d = nil |
| 415 | endfunc |
| 416 | |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 417 | func Test_lua_dict_table() |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 418 | lua t = {key1 = 'x', key2 = 3.14, key3 = true, key4 = false} |
| 419 | call assert_equal({'key1': 'x', 'key2': 3.14, 'key3': v:true, 'key4': v:false}, |
| 420 | \ luaeval('vim.dict(t)')) |
| 421 | |
| 422 | " Same example as in :help lua-vim. |
| 423 | lua t = {math.pi, false, say = 'hi'} |
| 424 | " FIXME: commented out as it currently does not work as documented: |
| 425 | " Expected {'say': 'hi'} |
| 426 | " but got {'1': 3.141593, '2': v:false, 'say': 'hi'} |
| 427 | " Is the documentation or the code wrong? |
| 428 | "call assert_equal({'say' : 'hi'}, luaeval('vim.dict(t)')) |
| 429 | lua t = nil |
| 430 | |
| 431 | call assert_fails('lua vim.dict(1)', '[string "vim chunk"]:1: table expected, got number') |
| 432 | call assert_fails('lua vim.dict("x")', '[string "vim chunk"]:1: table expected, got string') |
| 433 | call assert_fails('lua vim.dict(print)', '[string "vim chunk"]:1: table expected, got function') |
| 434 | call assert_fails('lua vim.dict(true)', '[string "vim chunk"]:1: table expected, got boolean') |
| 435 | endfunc |
| 436 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 437 | " Test d() i.e. iterator on dictionary |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 438 | func Test_lua_dict_iter() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 439 | let d = {'a': 1, 'b':2} |
| 440 | lua d = vim.eval('d') |
| 441 | lua str = '' |
| 442 | lua for k,v in d() do str = str .. k ..':' .. v .. ',' end |
| 443 | call assert_equal('a:1,b:2,', luaeval('str')) |
| 444 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 445 | lua str, d = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 446 | endfunc |
| 447 | |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 448 | func Test_lua_blob() |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 449 | call assert_equal(0z, luaeval('vim.blob("")')) |
| 450 | call assert_equal(0z31326162, luaeval('vim.blob("12ab")')) |
| 451 | call assert_equal(0z00010203, luaeval('vim.blob("\x00\x01\x02\x03")')) |
| 452 | call assert_equal(0z8081FEFF, luaeval('vim.blob("\x80\x81\xfe\xff")')) |
| 453 | |
| 454 | lua b = vim.blob("\x00\x00\x00\x00") |
| 455 | call assert_equal(0z00000000, luaeval('b')) |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 456 | call assert_equal(4, luaeval('#b')) |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 457 | lua b[0], b[1], b[2], b[3] = 1, 32, 256, 0xff |
| 458 | call assert_equal(0z012000ff, luaeval('b')) |
| 459 | lua b[4] = string.byte("z", 1) |
| 460 | call assert_equal(0z012000ff.7a, luaeval('b')) |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 461 | call assert_equal(5, luaeval('#b')) |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 462 | call assert_fails('lua b[#b+1] = 0x80', '[string "vim chunk"]:1: index out of range') |
| 463 | lua b:add("12ab") |
| 464 | call assert_equal(0z012000ff.7a313261.62, luaeval('b')) |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 465 | call assert_equal(9, luaeval('#b')) |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 466 | call assert_fails('lua b:add(nil)', '[string "vim chunk"]:1: string expected, got nil') |
| 467 | call assert_fails('lua b:add(true)', '[string "vim chunk"]:1: string expected, got boolean') |
| 468 | call assert_fails('lua b:add({})', '[string "vim chunk"]:1: string expected, got table') |
| 469 | lua b = nil |
| 470 | endfunc |
| 471 | |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 472 | func Test_lua_funcref() |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 473 | function I(x) |
| 474 | return a:x |
| 475 | endfunction |
| 476 | let R = function('I') |
| 477 | lua i1 = vim.funcref"I" |
| 478 | lua i2 = vim.eval"R" |
| 479 | lua msg = "funcref|test|" .. (#i2(i1) == #i1(i2) and "OK" or "FAIL") |
| 480 | lua msg = vim.funcref"tr"(msg, "|", " ") |
| 481 | call assert_equal("funcref test OK", luaeval('msg')) |
| 482 | |
| 483 | " dict funcref |
| 484 | function Mylen() dict |
| 485 | return len(self.data) |
| 486 | endfunction |
| 487 | let l = [0, 1, 2, 3] |
| 488 | let mydict = {'data': l} |
| 489 | lua d = vim.eval"mydict" |
| 490 | lua d.len = vim.funcref"Mylen" -- assign d as 'self' |
| 491 | lua res = (d.len() == vim.funcref"len"(vim.eval"l")) and "OK" or "FAIL" |
| 492 | call assert_equal("OK", luaeval('res')) |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 493 | call assert_equal(function('Mylen', {'data': l, 'len': function('Mylen')}), mydict.len) |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 494 | |
| 495 | lua i1, i2, msg, d, res = nil |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 496 | endfunc |
| 497 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 498 | " Test vim.type() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 499 | func Test_lua_type() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 500 | " The following values are identical to Lua's type function. |
| 501 | call assert_equal('string', luaeval('vim.type("foo")')) |
| 502 | call assert_equal('number', luaeval('vim.type(1)')) |
| 503 | call assert_equal('number', luaeval('vim.type(1.2)')) |
| 504 | call assert_equal('function', luaeval('vim.type(print)')) |
| 505 | call assert_equal('table', luaeval('vim.type({})')) |
| 506 | call assert_equal('boolean', luaeval('vim.type(true)')) |
| 507 | call assert_equal('boolean', luaeval('vim.type(false)')) |
| 508 | call assert_equal('nil', luaeval('vim.type(nil)')) |
| 509 | |
| 510 | " The following values are specific to Vim. |
| 511 | call assert_equal('window', luaeval('vim.type(vim.window())')) |
| 512 | call assert_equal('buffer', luaeval('vim.type(vim.buffer())')) |
| 513 | call assert_equal('list', luaeval('vim.type(vim.list())')) |
| 514 | call assert_equal('dict', luaeval('vim.type(vim.dict())')) |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 515 | call assert_equal('funcref', luaeval('vim.type(vim.funcref("Test_type"))')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 516 | endfunc |
| 517 | |
| 518 | " Test vim.open() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 519 | func Test_lua_open() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 520 | call assert_notmatch('XOpen', execute('ls')) |
| 521 | |
| 522 | " Open a buffer XOpen1, but do not jump to it. |
| 523 | lua b = vim.open('XOpen1') |
| 524 | call assert_equal('XOpen1', luaeval('b.name')) |
| 525 | call assert_equal('', bufname('%')) |
| 526 | |
| 527 | call assert_match('XOpen1', execute('ls')) |
| 528 | call assert_notequal('XOpen2', bufname('%')) |
| 529 | |
| 530 | " Open a buffer XOpen2 and jump to it. |
| 531 | lua b = vim.open('XOpen2')() |
| 532 | call assert_equal('XOpen2', luaeval('b.name')) |
| 533 | call assert_equal('XOpen2', bufname('%')) |
| 534 | |
| 535 | lua b = nil |
| 536 | %bwipe! |
| 537 | endfunc |
| 538 | |
Bram Moolenaar | 788fbb4 | 2020-05-31 14:08:12 +0200 | [diff] [blame] | 539 | func Test_update_package_paths() |
| 540 | set runtimepath+=./testluaplugin |
| 541 | call assert_equal("hello from lua", luaeval("require('testluaplugin').hello()")) |
| 542 | endfunc |
| 543 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 544 | " Test vim.line() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 545 | func Test_lua_line() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 546 | new |
| 547 | call setline(1, ['first line', 'second line']) |
| 548 | 1 |
| 549 | call assert_equal('first line', luaeval('vim.line()')) |
| 550 | 2 |
| 551 | call assert_equal('second line', luaeval('vim.line()')) |
| 552 | bwipe! |
| 553 | endfunc |
| 554 | |
| 555 | " Test vim.beep() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 556 | func Test_lua_beep() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 557 | call assert_beeps('lua vim.beep()') |
| 558 | endfunc |
| 559 | |
| 560 | " Test errors in luaeval() |
| 561 | func Test_luaeval_error() |
| 562 | " Compile error |
| 563 | call assert_fails("call luaeval('-nil')", |
| 564 | \ '[string "luaeval"]:1: attempt to perform arithmetic on a nil value') |
| 565 | call assert_fails("call luaeval(']')", |
| 566 | \ "[string \"luaeval\"]:1: unexpected symbol near ']'") |
| 567 | endfunc |
| 568 | |
| 569 | " Test :luafile foo.lua |
| 570 | func Test_luafile() |
| 571 | call delete('Xlua_file') |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 572 | call writefile(["str = 'hello'", "num = 123" ], 'Xlua_file') |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 573 | call setfperm('Xlua_file', 'r-xr-xr-x') |
| 574 | |
| 575 | luafile Xlua_file |
| 576 | call assert_equal('hello', luaeval('str')) |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 577 | call assert_equal(123, luaeval('num')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 578 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 579 | lua str, num = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 580 | call delete('Xlua_file') |
| 581 | endfunc |
| 582 | |
| 583 | " Test :luafile % |
| 584 | func Test_luafile_percent() |
| 585 | new Xlua_file |
| 586 | append |
| 587 | str, num = 'foo', 321.0 |
| 588 | print(string.format('str=%s, num=%d', str, num)) |
| 589 | . |
| 590 | w! |
| 591 | luafile % |
| 592 | let msg = split(execute('message'), "\n")[-1] |
| 593 | call assert_equal('str=foo, num=321', msg) |
| 594 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 595 | lua str, num = nil |
| 596 | call delete('Xlua_file') |
| 597 | bwipe! |
| 598 | endfunc |
| 599 | |
| 600 | " Test :luafile with syntax error |
| 601 | func Test_luafile_error() |
| 602 | new Xlua_file |
| 603 | call writefile(['nil = 0' ], 'Xlua_file') |
| 604 | call setfperm('Xlua_file', 'r-xr-xr-x') |
| 605 | |
| 606 | call assert_fails('luafile Xlua_file', "Xlua_file:1: unexpected symbol near 'nil'") |
| 607 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 608 | call delete('Xlua_file') |
Bram Moolenaar | d58f03b | 2017-01-29 22:48:45 +0100 | [diff] [blame] | 609 | bwipe! |
| 610 | endfunc |
Bram Moolenaar | 5390144 | 2018-07-25 22:02:36 +0200 | [diff] [blame] | 611 | |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 612 | func Test_lua_set_cursor() |
Bram Moolenaar | 5390144 | 2018-07-25 22:02:36 +0200 | [diff] [blame] | 613 | " Check that setting the cursor position works. |
| 614 | new |
| 615 | call setline(1, ['first line', 'second line']) |
| 616 | normal gg |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 617 | lua << trim EOF |
| 618 | w = vim.window() |
| 619 | w.line = 1 |
| 620 | w.col = 5 |
| 621 | EOF |
Bram Moolenaar | 5390144 | 2018-07-25 22:02:36 +0200 | [diff] [blame] | 622 | call assert_equal([1, 5], [line('.'), col('.')]) |
| 623 | |
| 624 | " Check that movement after setting cursor position keeps current column. |
| 625 | normal j |
| 626 | call assert_equal([2, 5], [line('.'), col('.')]) |
| 627 | endfunc |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 628 | |
| 629 | " Test for various heredoc syntax |
| 630 | func Test_lua_heredoc() |
| 631 | lua << END |
| 632 | vim.command('let s = "A"') |
| 633 | END |
| 634 | lua << |
| 635 | vim.command('let s ..= "B"') |
| 636 | . |
| 637 | lua << trim END |
| 638 | vim.command('let s ..= "C"') |
| 639 | END |
| 640 | lua << trim |
| 641 | vim.command('let s ..= "D"') |
| 642 | . |
Bram Moolenaar | 6ab0953 | 2020-05-01 14:10:13 +0200 | [diff] [blame] | 643 | lua << trim eof |
| 644 | vim.command('let s ..= "E"') |
| 645 | eof |
| 646 | call assert_equal('ABCDE', s) |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 647 | endfunc |
| 648 | |
| 649 | " vim: shiftwidth=2 sts=2 expandtab |