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 |
Bram Moolenaar | c8970b9 | 2020-10-26 20:18:08 +0100 | [diff] [blame] | 4 | |
| 5 | " This test also works without the lua feature. |
| 6 | func Test_skip_lua() |
| 7 | if 0 |
| 8 | lua print("Not executed") |
| 9 | endif |
| 10 | endfunc |
| 11 | |
Bram Moolenaar | b46fecd | 2019-06-15 17:58:09 +0200 | [diff] [blame] | 12 | CheckFeature lua |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 13 | CheckFeature float |
Bram Moolenaar | d58f03b | 2017-01-29 22:48:45 +0100 | [diff] [blame] | 14 | |
Bram Moolenaar | f65ed86 | 2021-04-03 14:13:33 +0200 | [diff] [blame] | 15 | " Depending on the lua version, the error messages are different. |
| 16 | let s:luaver = split(split(systemlist('lua -v')[0], ' ')[1], '\.') |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 17 | let s:major = str2nr(s:luaver[0]) |
| 18 | let s:minor = str2nr(s:luaver[1]) |
Bram Moolenaar | f65ed86 | 2021-04-03 14:13:33 +0200 | [diff] [blame] | 19 | let s:patch = str2nr(s:luaver[2]) |
| 20 | let s:lua_53_or_later = 0 |
| 21 | let s:lua_543_or_later = 0 |
| 22 | if (s:major == 5 && s:minor >= 3) || s:major > 5 |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 23 | let s:lua_53_or_later = 1 |
Bram Moolenaar | f65ed86 | 2021-04-03 14:13:33 +0200 | [diff] [blame] | 24 | if (s:major == 5 |
| 25 | \ && ((s:minor == 4 && s:patch >= 3) || s:minor > 4)) |
| 26 | \ || s:major > 5 |
| 27 | let s:lua_543_or_later = 1 |
| 28 | endif |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 29 | endif |
| 30 | |
Bram Moolenaar | e165f63 | 2019-03-10 09:48:59 +0100 | [diff] [blame] | 31 | func TearDown() |
| 32 | " Run garbage collection after each test to exercise luaV_setref(). |
| 33 | call test_garbagecollect_now() |
| 34 | endfunc |
| 35 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 36 | " Check that switching to another buffer does not trigger ml_get error. |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 37 | func Test_lua_command_new_no_ml_get_error() |
Bram Moolenaar | d58f03b | 2017-01-29 22:48:45 +0100 | [diff] [blame] | 38 | new |
| 39 | let wincount = winnr('$') |
| 40 | call setline(1, ['one', 'two', 'three']) |
| 41 | luado vim.command("new") |
| 42 | call assert_equal(wincount + 1, winnr('$')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 43 | %bwipe! |
| 44 | endfunc |
| 45 | |
| 46 | " Test vim.command() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 47 | func Test_lua_command() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 48 | new |
| 49 | call setline(1, ['one', 'two', 'three']) |
| 50 | luado vim.command("1,2d_") |
| 51 | call assert_equal(['three'], getline(1, '$')) |
Bram Moolenaar | d58f03b | 2017-01-29 22:48:45 +0100 | [diff] [blame] | 52 | bwipe! |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 53 | endfunc |
| 54 | |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 55 | func Test_lua_luado() |
| 56 | new |
| 57 | call setline(1, ['one', 'two']) |
| 58 | luado return(linenr) |
| 59 | call assert_equal(['1', '2'], getline(1, '$')) |
| 60 | close! |
| 61 | |
| 62 | " Error cases |
| 63 | call assert_fails('luado string.format()', |
| 64 | \ "[string \"vim chunk\"]:1: bad argument #1 to 'format' (string expected, got no value)") |
Bram Moolenaar | f65ed86 | 2021-04-03 14:13:33 +0200 | [diff] [blame] | 65 | if s:lua_543_or_later |
| 66 | let msg = "[string \"vim chunk\"]:1: global 'func' is not callable (a nil value)" |
| 67 | elseif s:lua_53_or_later |
| 68 | let msg = "[string \"vim chunk\"]:1: attempt to call a nil value (global 'func')" |
| 69 | else |
| 70 | let msg = "[string \"vim chunk\"]:1: attempt to call global 'func' (a nil value)" |
| 71 | endif |
| 72 | call assert_fails('luado func()', msg) |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 73 | call assert_fails('luado error("failed")', "[string \"vim chunk\"]:1: failed") |
| 74 | endfunc |
| 75 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 76 | " Test vim.eval() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 77 | func Test_lua_eval() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 78 | " lua.eval with a number |
| 79 | lua v = vim.eval('123') |
| 80 | call assert_equal('number', luaeval('vim.type(v)')) |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 81 | call assert_equal(123, luaeval('v')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 82 | |
| 83 | " lua.eval with a string |
| 84 | lua v = vim.eval('"abc"') |
Bram Moolenaar | 02b3111 | 2019-08-31 22:16:38 +0200 | [diff] [blame] | 85 | call assert_equal('string', 'vim.type(v)'->luaeval()) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 86 | call assert_equal('abc', luaeval('v')) |
| 87 | |
| 88 | " lua.eval with a list |
| 89 | lua v = vim.eval("['a']") |
| 90 | call assert_equal('list', luaeval('vim.type(v)')) |
| 91 | call assert_equal(['a'], luaeval('v')) |
| 92 | |
| 93 | " lua.eval with a dict |
| 94 | lua v = vim.eval("{'a':'b'}") |
| 95 | call assert_equal('dict', luaeval('vim.type(v)')) |
| 96 | call assert_equal({'a':'b'}, luaeval('v')) |
| 97 | |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 98 | " lua.eval with a blob |
| 99 | lua v = vim.eval("0z00112233.deadbeef") |
| 100 | call assert_equal('blob', luaeval('vim.type(v)')) |
| 101 | call assert_equal(0z00112233.deadbeef, luaeval('v')) |
| 102 | |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 103 | " lua.eval with a float |
| 104 | lua v = vim.eval('3.14') |
| 105 | call assert_equal('number', luaeval('vim.type(v)')) |
| 106 | call assert_equal(3.14, luaeval('v')) |
| 107 | |
| 108 | " lua.eval with a bool |
| 109 | lua v = vim.eval('v:true') |
| 110 | call assert_equal('number', luaeval('vim.type(v)')) |
| 111 | call assert_equal(1, luaeval('v')) |
| 112 | lua v = vim.eval('v:false') |
| 113 | call assert_equal('number', luaeval('vim.type(v)')) |
| 114 | call assert_equal(0, luaeval('v')) |
| 115 | |
| 116 | " lua.eval with a null |
| 117 | lua v = vim.eval('v:null') |
| 118 | call assert_equal('nil', luaeval('vim.type(v)')) |
| 119 | call assert_equal(v:null, luaeval('v')) |
| 120 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 121 | call assert_fails('lua v = vim.eval(nil)', |
| 122 | \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got nil)") |
| 123 | call assert_fails('lua v = vim.eval(true)', |
| 124 | \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got boolean)") |
| 125 | call assert_fails('lua v = vim.eval({})', |
| 126 | \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got table)") |
| 127 | call assert_fails('lua v = vim.eval(print)', |
| 128 | \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got function)") |
| 129 | call assert_fails('lua v = vim.eval(vim.buffer())', |
| 130 | \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got userdata)") |
| 131 | |
| 132 | lua v = nil |
| 133 | endfunc |
| 134 | |
Bram Moolenaar | 86c3a21 | 2021-03-08 19:50:24 +0100 | [diff] [blame] | 135 | " Test luaeval() with lambda |
| 136 | func Test_luaeval_with_lambda() |
| 137 | lua function hello_luaeval_lambda(a, cb) return a .. cb() end |
| 138 | call assert_equal('helloworld', |
| 139 | \ luaeval('hello_luaeval_lambda(_A[1], _A[2])', |
| 140 | \ ['hello', {->'world'}])) |
| 141 | lua hello_luaeval_lambda = nil |
| 142 | endfunc |
| 143 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 144 | " Test vim.window() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 145 | func Test_lua_window() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 146 | e Xfoo2 |
| 147 | new Xfoo1 |
| 148 | |
| 149 | " Window 1 (top window) contains Xfoo1 |
| 150 | " Window 2 (bottom window) contains Xfoo2 |
| 151 | call assert_equal('Xfoo1', luaeval('vim.window(1):buffer().name')) |
| 152 | call assert_equal('Xfoo2', luaeval('vim.window(2):buffer().name')) |
| 153 | |
| 154 | " Window 3 does not exist so vim.window(3) should return nil |
| 155 | call assert_equal('nil', luaeval('tostring(vim.window(3))')) |
| 156 | |
Bram Moolenaar | f65ed86 | 2021-04-03 14:13:33 +0200 | [diff] [blame] | 157 | if s:lua_543_or_later |
| 158 | let msg = "[string \"luaeval\"]:1: field 'xyz' is not callable (a nil value)" |
| 159 | elseif s:lua_53_or_later |
| 160 | let msg = "[string \"luaeval\"]:1: attempt to call a nil value (field 'xyz')" |
| 161 | else |
| 162 | let msg = "[string \"luaeval\"]:1: attempt to call field 'xyz' (a nil value)" |
| 163 | endif |
| 164 | call assert_fails("let n = luaeval('vim.window().xyz()')", msg) |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 165 | call assert_fails('lua vim.window().xyz = 1', |
| 166 | \ "[string \"vim chunk\"]:1: invalid window property: `xyz'") |
| 167 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 168 | %bwipe! |
| 169 | endfunc |
| 170 | |
| 171 | " Test vim.window().height |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 172 | func Test_lua_window_height() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 173 | new |
| 174 | lua vim.window().height = 2 |
| 175 | call assert_equal(2, winheight(0)) |
| 176 | lua vim.window().height = vim.window().height + 1 |
| 177 | call assert_equal(3, winheight(0)) |
| 178 | bwipe! |
| 179 | endfunc |
| 180 | |
| 181 | " Test vim.window().width |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 182 | func Test_lua_window_width() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 183 | vert new |
| 184 | lua vim.window().width = 2 |
| 185 | call assert_equal(2, winwidth(0)) |
| 186 | lua vim.window().width = vim.window().width + 1 |
| 187 | call assert_equal(3, winwidth(0)) |
| 188 | bwipe! |
| 189 | endfunc |
| 190 | |
| 191 | " Test vim.window().line and vim.window.col |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 192 | func Test_lua_window_line_col() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 193 | new |
| 194 | call setline(1, ['line1', 'line2', 'line3']) |
| 195 | lua vim.window().line = 2 |
| 196 | lua vim.window().col = 4 |
| 197 | call assert_equal([0, 2, 4, 0], getpos('.')) |
| 198 | lua vim.window().line = vim.window().line + 1 |
| 199 | lua vim.window().col = vim.window().col - 1 |
| 200 | call assert_equal([0, 3, 3, 0], getpos('.')) |
| 201 | |
| 202 | call assert_fails('lua vim.window().line = 10', |
| 203 | \ '[string "vim chunk"]:1: line out of range') |
| 204 | bwipe! |
| 205 | endfunc |
| 206 | |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 207 | " Test vim.call |
| 208 | func Test_lua_call() |
| 209 | call assert_equal(has('lua'), luaeval('vim.call("has", "lua")')) |
| 210 | call assert_equal(printf("Hello %s", "vim"), luaeval('vim.call("printf", "Hello %s", "vim")')) |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 211 | |
| 212 | " Error cases |
| 213 | call assert_fails("call luaeval('vim.call(\"min\", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21)')", |
Bram Moolenaar | b898a02 | 2020-07-12 18:33:53 +0200 | [diff] [blame] | 214 | \ s:lua_53_or_later |
| 215 | \ ? '[string "luaeval"]:1: Function called with too many arguments' |
| 216 | \ : 'Function called with too many arguments') |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 217 | lua co = coroutine.create(function () print("hi") end) |
| 218 | call assert_fails("call luaeval('vim.call(\"type\", co)')", |
Bram Moolenaar | b898a02 | 2020-07-12 18:33:53 +0200 | [diff] [blame] | 219 | \ s:lua_53_or_later |
| 220 | \ ? '[string "luaeval"]:1: lua: cannot convert value' |
| 221 | \ : 'lua: cannot convert value') |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 222 | lua co = nil |
Bram Moolenaar | b898a02 | 2020-07-12 18:33:53 +0200 | [diff] [blame] | 223 | call assert_fails("call luaeval('vim.call(\"abc\")')", |
| 224 | \ ['E117:', s:lua_53_or_later ? '\[string "luaeval"]:1: lua: call_vim_function failed' |
| 225 | \ : 'lua: call_vim_function failed']) |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 226 | endfunc |
| 227 | |
| 228 | " Test vim.fn.* |
| 229 | func Test_lua_fn() |
| 230 | call assert_equal(has('lua'), luaeval('vim.fn.has("lua")')) |
| 231 | call assert_equal(printf("Hello %s", "vim"), luaeval('vim.fn.printf("Hello %s", "vim")')) |
| 232 | endfunc |
| 233 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 234 | " Test setting the current window |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 235 | func Test_lua_window_set_current() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 236 | new Xfoo1 |
| 237 | lua w1 = vim.window() |
| 238 | new Xfoo2 |
| 239 | lua w2 = vim.window() |
| 240 | |
| 241 | call assert_equal('Xfoo2', bufname('%')) |
| 242 | lua w1() |
| 243 | call assert_equal('Xfoo1', bufname('%')) |
| 244 | lua w2() |
| 245 | call assert_equal('Xfoo2', bufname('%')) |
| 246 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 247 | lua w1, w2 = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 248 | %bwipe! |
| 249 | endfunc |
| 250 | |
| 251 | " Test vim.window().buffer |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 252 | func Test_lua_window_buffer() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 253 | new Xfoo1 |
| 254 | lua w1 = vim.window() |
| 255 | lua b1 = w1.buffer() |
| 256 | new Xfoo2 |
| 257 | lua w2 = vim.window() |
| 258 | lua b2 = w2.buffer() |
| 259 | |
| 260 | lua b1() |
| 261 | call assert_equal('Xfoo1', bufname('%')) |
| 262 | lua b2() |
| 263 | call assert_equal('Xfoo2', bufname('%')) |
| 264 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 265 | lua b1, b2, w1, w2 = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 266 | %bwipe! |
| 267 | endfunc |
| 268 | |
| 269 | " Test vim.window():previous() and vim.window():next() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 270 | func Test_lua_window_next_previous() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 271 | new Xfoo1 |
| 272 | new Xfoo2 |
| 273 | new Xfoo3 |
| 274 | wincmd j |
| 275 | |
| 276 | call assert_equal('Xfoo2', luaeval('vim.window().buffer().name')) |
| 277 | call assert_equal('Xfoo1', luaeval('vim.window():next():buffer().name')) |
| 278 | call assert_equal('Xfoo3', luaeval('vim.window():previous():buffer().name')) |
| 279 | |
| 280 | %bwipe! |
| 281 | endfunc |
| 282 | |
| 283 | " Test vim.window():isvalid() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 284 | func Test_lua_window_isvalid() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 285 | new Xfoo |
| 286 | lua w = vim.window() |
| 287 | call assert_true(luaeval('w:isvalid()')) |
| 288 | |
| 289 | " FIXME: how to test the case when isvalid() returns v:false? |
| 290 | " isvalid() gives errors when the window is deleted. Is it a bug? |
| 291 | |
| 292 | lua w = nil |
| 293 | bwipe! |
| 294 | endfunc |
| 295 | |
| 296 | " Test vim.buffer() with and without argument |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 297 | func Test_lua_buffer() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 298 | new Xfoo1 |
| 299 | let bn1 = bufnr('%') |
| 300 | new Xfoo2 |
| 301 | let bn2 = bufnr('%') |
| 302 | |
| 303 | " Test vim.buffer() without argument. |
| 304 | call assert_equal('Xfoo2', luaeval("vim.buffer().name")) |
| 305 | |
| 306 | " Test vim.buffer() with string argument. |
| 307 | call assert_equal('Xfoo1', luaeval("vim.buffer('Xfoo1').name")) |
| 308 | call assert_equal('Xfoo2', luaeval("vim.buffer('Xfoo2').name")) |
| 309 | |
| 310 | " Test vim.buffer() with integer argument. |
| 311 | call assert_equal('Xfoo1', luaeval("vim.buffer(" . bn1 . ").name")) |
| 312 | call assert_equal('Xfoo2', luaeval("vim.buffer(" . bn2 . ").name")) |
| 313 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 314 | lua bn1, bn2 = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 315 | %bwipe! |
| 316 | endfunc |
| 317 | |
| 318 | " Test vim.buffer().name and vim.buffer().fname |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 319 | func Test_lua_buffer_name() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 320 | new |
Bram Moolenaar | fe08df4 | 2018-07-07 23:07:41 +0200 | [diff] [blame] | 321 | call assert_equal('', luaeval('vim.buffer().name')) |
| 322 | call assert_equal('', luaeval('vim.buffer().fname')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 323 | bwipe! |
| 324 | |
| 325 | new Xfoo |
| 326 | call assert_equal('Xfoo', luaeval('vim.buffer().name')) |
| 327 | call assert_equal(expand('%:p'), luaeval('vim.buffer().fname')) |
| 328 | bwipe! |
| 329 | endfunc |
| 330 | |
| 331 | " Test vim.buffer().number |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 332 | func Test_lua_buffer_number() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 333 | " All numbers in Lua are floating points number (no integers). |
| 334 | call assert_equal(bufnr('%'), float2nr(luaeval('vim.buffer().number'))) |
| 335 | endfunc |
| 336 | |
| 337 | " Test inserting lines in buffer. |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 338 | func Test_lua_buffer_insert() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 339 | new |
| 340 | lua vim.buffer()[1] = '3' |
| 341 | lua vim.buffer():insert('1', 0) |
| 342 | lua vim.buffer():insert('2', 1) |
| 343 | lua vim.buffer():insert('4', 10) |
| 344 | |
| 345 | call assert_equal(['1', '2', '3', '4'], getline(1, '$')) |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 346 | call assert_equal('4', luaeval('vim.buffer()[4]')) |
| 347 | call assert_equal(v:null, luaeval('vim.buffer()[5]')) |
| 348 | call assert_equal(v:null, luaeval('vim.buffer()[{}]')) |
Bram Moolenaar | f65ed86 | 2021-04-03 14:13:33 +0200 | [diff] [blame] | 349 | if s:lua_543_or_later |
| 350 | let msg = "[string \"vim chunk\"]:1: method 'xyz' is not callable (a nil value)" |
| 351 | elseif s:lua_53_or_later |
| 352 | let msg = "[string \"vim chunk\"]:1: attempt to call a nil value (method 'xyz')" |
| 353 | else |
| 354 | let msg = "[string \"vim chunk\"]:1: attempt to call method 'xyz' (a nil value)" |
| 355 | endif |
| 356 | call assert_fails('lua vim.buffer():xyz()', msg) |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 357 | call assert_fails('lua vim.buffer()[1] = {}', |
| 358 | \ '[string "vim chunk"]:1: wrong argument to change') |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 359 | bwipe! |
| 360 | endfunc |
| 361 | |
| 362 | " Test deleting line in buffer |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 363 | func Test_lua_buffer_delete() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 364 | new |
| 365 | call setline(1, ['1', '2', '3']) |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 366 | call cursor(3, 1) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 367 | lua vim.buffer()[2] = nil |
| 368 | call assert_equal(['1', '3'], getline(1, '$')) |
| 369 | |
| 370 | call assert_fails('lua vim.buffer()[3] = nil', |
| 371 | \ '[string "vim chunk"]:1: invalid line number') |
| 372 | bwipe! |
| 373 | endfunc |
| 374 | |
| 375 | " Test #vim.buffer() i.e. number of lines in buffer |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 376 | func Test_lua_buffer_number_lines() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 377 | new |
| 378 | call setline(1, ['a', 'b', 'c']) |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 379 | call assert_equal(3, luaeval('#vim.buffer()')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 380 | bwipe! |
| 381 | endfunc |
| 382 | |
| 383 | " Test vim.buffer():next() and vim.buffer():previous() |
| 384 | " Note that these functions get the next or previous buffers |
| 385 | " but do not switch buffer. |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 386 | func Test_lua_buffer_next_previous() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 387 | new Xfoo1 |
| 388 | new Xfoo2 |
| 389 | new Xfoo3 |
| 390 | b Xfoo2 |
| 391 | |
| 392 | lua bn = vim.buffer():next() |
| 393 | lua bp = vim.buffer():previous() |
| 394 | |
| 395 | call assert_equal('Xfoo2', luaeval('vim.buffer().name')) |
| 396 | call assert_equal('Xfoo1', luaeval('bp.name')) |
| 397 | call assert_equal('Xfoo3', luaeval('bn.name')) |
| 398 | |
| 399 | call assert_equal('Xfoo2', bufname('%')) |
| 400 | |
| 401 | lua bn() |
| 402 | call assert_equal('Xfoo3', luaeval('vim.buffer().name')) |
| 403 | call assert_equal('Xfoo3', bufname('%')) |
| 404 | |
| 405 | lua bp() |
| 406 | call assert_equal('Xfoo1', luaeval('vim.buffer().name')) |
| 407 | call assert_equal('Xfoo1', bufname('%')) |
| 408 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 409 | lua bn, bp = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 410 | %bwipe! |
| 411 | endfunc |
| 412 | |
| 413 | " Test vim.buffer():isvalid() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 414 | func Test_lua_buffer_isvalid() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 415 | new Xfoo |
| 416 | lua b = vim.buffer() |
| 417 | call assert_true(luaeval('b:isvalid()')) |
| 418 | |
| 419 | " FIXME: how to test the case when isvalid() returns v:false? |
| 420 | " isvalid() gives errors when the buffer is wiped. Is it a bug? |
| 421 | |
| 422 | lua b = nil |
| 423 | bwipe! |
| 424 | endfunc |
| 425 | |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 426 | func Test_lua_list() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 427 | call assert_equal([], luaeval('vim.list()')) |
| 428 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 429 | let l = [] |
| 430 | lua l = vim.eval('l') |
| 431 | lua l:add(123) |
| 432 | lua l:add('abc') |
| 433 | lua l:add(true) |
| 434 | lua l:add(false) |
Bram Moolenaar | 9067cd6 | 2019-01-01 00:41:54 +0100 | [diff] [blame] | 435 | lua l:add(nil) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 436 | lua l:add(vim.eval("[1, 2, 3]")) |
| 437 | lua l:add(vim.eval("{'a':1, 'b':2, 'c':3}")) |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 438 | call assert_equal([123, 'abc', v:true, v:false, v:null, [1, 2, 3], {'a': 1, 'b': 2, 'c': 3}], l) |
| 439 | call assert_equal(7, luaeval('#l')) |
Bram Moolenaar | a8a60d0 | 2018-07-02 22:54:36 +0200 | [diff] [blame] | 440 | call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 441 | |
Bram Moolenaar | bd84617 | 2020-06-27 12:32:57 +0200 | [diff] [blame] | 442 | lua l[1] = 124 |
| 443 | lua l[6] = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 444 | lua l:insert('first') |
| 445 | lua l:insert('xx', 3) |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 446 | call assert_fails('lua l:insert("xx", -20)', |
| 447 | \ '[string "vim chunk"]:1: invalid position') |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 448 | 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] | 449 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 450 | lockvar 1 l |
| 451 | call assert_fails('lua l:add("x")', '[string "vim chunk"]:1: list is locked') |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 452 | call assert_fails('lua l:insert(2)', '[string "vim chunk"]:1: list is locked') |
| 453 | call assert_fails('lua l[9] = 1', '[string "vim chunk"]:1: list is locked') |
| 454 | |
| 455 | unlockvar l |
| 456 | let l = [1, 2] |
| 457 | lua ll = vim.eval('l') |
| 458 | let x = luaeval("ll[3]") |
| 459 | call assert_equal(v:null, x) |
Bram Moolenaar | f65ed86 | 2021-04-03 14:13:33 +0200 | [diff] [blame] | 460 | if s:lua_543_or_later |
| 461 | let msg = "[string \"luaeval\"]:1: method 'xyz' is not callable (a nil value)" |
| 462 | elseif s:lua_53_or_later |
| 463 | let msg = "[string \"luaeval\"]:1: attempt to call a nil value (method 'xyz')" |
| 464 | else |
| 465 | let msg = "[string \"luaeval\"]:1: attempt to call method 'xyz' (a nil value)" |
| 466 | endif |
| 467 | call assert_fails('let x = luaeval("ll:xyz(3)")', msg) |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 468 | let y = luaeval("ll[{}]") |
| 469 | call assert_equal(v:null, y) |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 470 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 471 | lua l = nil |
| 472 | endfunc |
| 473 | |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 474 | func Test_lua_list_table() |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 475 | " See :help lua-vim |
| 476 | " Non-numeric keys should not be used to initialize the list |
| 477 | " so say = 'hi' should be ignored. |
| 478 | lua t = {3.14, 'hello', false, true, say = 'hi'} |
| 479 | call assert_equal([3.14, 'hello', v:false, v:true], luaeval('vim.list(t)')) |
| 480 | lua t = nil |
| 481 | |
| 482 | call assert_fails('lua vim.list(1)', '[string "vim chunk"]:1: table expected, got number') |
| 483 | call assert_fails('lua vim.list("x")', '[string "vim chunk"]:1: table expected, got string') |
| 484 | call assert_fails('lua vim.list(print)', '[string "vim chunk"]:1: table expected, got function') |
| 485 | call assert_fails('lua vim.list(true)', '[string "vim chunk"]:1: table expected, got boolean') |
| 486 | endfunc |
| 487 | |
Bram Moolenaar | a1f9f86 | 2020-06-28 22:41:26 +0200 | [diff] [blame] | 488 | func Test_lua_list_table_insert_remove() |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 489 | if !s:lua_53_or_later |
Bram Moolenaar | a1f9f86 | 2020-06-28 22:41:26 +0200 | [diff] [blame] | 490 | throw 'Skipped: Lua version < 5.3' |
| 491 | endif |
| 492 | |
Bram Moolenaar | f65ed86 | 2021-04-03 14:13:33 +0200 | [diff] [blame] | 493 | let l = [1, 2] |
Bram Moolenaar | a1f9f86 | 2020-06-28 22:41:26 +0200 | [diff] [blame] | 494 | lua t = vim.eval('l') |
| 495 | lua table.insert(t, 10) |
| 496 | lua t[#t + 1] = 20 |
| 497 | lua table.insert(t, 2, 30) |
| 498 | call assert_equal(l, [1, 30, 2, 10, 20]) |
| 499 | lua table.remove(t, 2) |
| 500 | call assert_equal(l, [1, 2, 10, 20]) |
| 501 | lua t[3] = nil |
| 502 | call assert_equal(l, [1, 2, 20]) |
| 503 | lua removed_value = table.remove(t, 3) |
| 504 | call assert_equal(luaeval('removed_value'), 20) |
| 505 | lua t = nil |
| 506 | lua removed_value = nil |
| 507 | unlet l |
| 508 | endfunc |
| 509 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 510 | " Test l() i.e. iterator on list |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 511 | func Test_lua_list_iter() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 512 | lua l = vim.list():add('foo'):add('bar') |
| 513 | lua str = '' |
| 514 | lua for v in l() do str = str .. v end |
| 515 | call assert_equal('foobar', luaeval('str')) |
| 516 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 517 | lua str, l = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 518 | endfunc |
| 519 | |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 520 | func Test_lua_recursive_list() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 521 | lua l = vim.list():add(1):add(2) |
| 522 | lua l = l:add(l) |
| 523 | |
Bram Moolenaar | bd84617 | 2020-06-27 12:32:57 +0200 | [diff] [blame] | 524 | call assert_equal(1, luaeval('l[1]')) |
| 525 | call assert_equal(2, luaeval('l[2]')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 526 | |
Bram Moolenaar | bd84617 | 2020-06-27 12:32:57 +0200 | [diff] [blame] | 527 | call assert_equal(1, luaeval('l[3][1]')) |
| 528 | call assert_equal(2, luaeval('l[3][2]')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 529 | |
Bram Moolenaar | bd84617 | 2020-06-27 12:32:57 +0200 | [diff] [blame] | 530 | call assert_equal(1, luaeval('l[3][3][1]')) |
| 531 | call assert_equal(2, luaeval('l[3][3][2]')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 532 | |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 533 | call assert_equal('[1, 2, [...]]', string(luaeval('l'))) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 534 | |
Bram Moolenaar | a8a60d0 | 2018-07-02 22:54:36 +0200 | [diff] [blame] | 535 | call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)')) |
Bram Moolenaar | bd84617 | 2020-06-27 12:32:57 +0200 | [diff] [blame] | 536 | call assert_equal(luaeval('tostring(l)'), luaeval('tostring(l[3])')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 537 | |
Bram Moolenaar | bd84617 | 2020-06-27 12:32:57 +0200 | [diff] [blame] | 538 | call assert_equal(luaeval('l'), luaeval('l[3]')) |
| 539 | call assert_equal(luaeval('l'), luaeval('l[3][3]')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 540 | |
| 541 | lua l = nil |
| 542 | endfunc |
| 543 | |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 544 | func Test_lua_dict() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 545 | call assert_equal({}, luaeval('vim.dict()')) |
| 546 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 547 | let d = {} |
| 548 | lua d = vim.eval('d') |
| 549 | lua d[0] = 123 |
| 550 | lua d[1] = "abc" |
| 551 | lua d[2] = true |
| 552 | lua d[3] = false |
| 553 | lua d[4] = vim.eval("[1, 2, 3]") |
| 554 | lua d[5] = vim.eval("{'a':1, 'b':2, 'c':3}") |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 555 | 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) |
| 556 | call assert_equal(6, luaeval('#d')) |
Bram Moolenaar | a8a60d0 | 2018-07-02 22:54:36 +0200 | [diff] [blame] | 557 | call assert_match('^dict: \%(0x\)\?\x\+$', luaeval('tostring(d)')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 558 | |
| 559 | call assert_equal('abc', luaeval('d[1]')) |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 560 | call assert_equal(v:null, luaeval('d[22]')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 561 | |
| 562 | lua d[0] = 124 |
| 563 | lua d[4] = nil |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 564 | 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] | 565 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 566 | lockvar 1 d |
| 567 | call assert_fails('lua d[6] = 1', '[string "vim chunk"]:1: dict is locked') |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 568 | unlockvar d |
| 569 | |
| 570 | " Error case |
| 571 | lua d = {} |
| 572 | lua d[''] = 10 |
| 573 | call assert_fails("let t = luaeval('vim.dict(d)')", |
Bram Moolenaar | b898a02 | 2020-07-12 18:33:53 +0200 | [diff] [blame] | 574 | \ s:lua_53_or_later |
| 575 | \ ? '[string "luaeval"]:1: table has empty key' |
| 576 | \ : 'table has empty key') |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 577 | let d = {} |
| 578 | lua x = vim.eval('d') |
| 579 | call assert_fails("lua x[''] = 10", '[string "vim chunk"]:1: empty key') |
| 580 | lua x['a'] = nil |
| 581 | call assert_equal({}, d) |
| 582 | |
| 583 | " cannot assign funcrefs in the global scope |
| 584 | lua x = vim.eval('g:') |
| 585 | call assert_fails("lua x['min'] = vim.funcref('max')", |
| 586 | \ '[string "vim chunk"]:1: cannot assign funcref to builtin scope') |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 587 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 588 | lua d = nil |
| 589 | endfunc |
| 590 | |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 591 | func Test_lua_dict_table() |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 592 | lua t = {key1 = 'x', key2 = 3.14, key3 = true, key4 = false} |
| 593 | call assert_equal({'key1': 'x', 'key2': 3.14, 'key3': v:true, 'key4': v:false}, |
| 594 | \ luaeval('vim.dict(t)')) |
| 595 | |
| 596 | " Same example as in :help lua-vim. |
| 597 | lua t = {math.pi, false, say = 'hi'} |
| 598 | " FIXME: commented out as it currently does not work as documented: |
| 599 | " Expected {'say': 'hi'} |
| 600 | " but got {'1': 3.141593, '2': v:false, 'say': 'hi'} |
| 601 | " Is the documentation or the code wrong? |
| 602 | "call assert_equal({'say' : 'hi'}, luaeval('vim.dict(t)')) |
| 603 | lua t = nil |
| 604 | |
| 605 | call assert_fails('lua vim.dict(1)', '[string "vim chunk"]:1: table expected, got number') |
| 606 | call assert_fails('lua vim.dict("x")', '[string "vim chunk"]:1: table expected, got string') |
| 607 | call assert_fails('lua vim.dict(print)', '[string "vim chunk"]:1: table expected, got function') |
| 608 | call assert_fails('lua vim.dict(true)', '[string "vim chunk"]:1: table expected, got boolean') |
| 609 | endfunc |
| 610 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 611 | " Test d() i.e. iterator on dictionary |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 612 | func Test_lua_dict_iter() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 613 | let d = {'a': 1, 'b':2} |
| 614 | lua d = vim.eval('d') |
| 615 | lua str = '' |
| 616 | lua for k,v in d() do str = str .. k ..':' .. v .. ',' end |
| 617 | call assert_equal('a:1,b:2,', luaeval('str')) |
| 618 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 619 | lua str, d = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 620 | endfunc |
| 621 | |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 622 | func Test_lua_blob() |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 623 | call assert_equal(0z, luaeval('vim.blob("")')) |
| 624 | call assert_equal(0z31326162, luaeval('vim.blob("12ab")')) |
| 625 | call assert_equal(0z00010203, luaeval('vim.blob("\x00\x01\x02\x03")')) |
| 626 | call assert_equal(0z8081FEFF, luaeval('vim.blob("\x80\x81\xfe\xff")')) |
| 627 | |
| 628 | lua b = vim.blob("\x00\x00\x00\x00") |
| 629 | call assert_equal(0z00000000, luaeval('b')) |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 630 | call assert_equal(4, luaeval('#b')) |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 631 | lua b[0], b[1], b[2], b[3] = 1, 32, 256, 0xff |
| 632 | call assert_equal(0z012000ff, luaeval('b')) |
| 633 | lua b[4] = string.byte("z", 1) |
| 634 | call assert_equal(0z012000ff.7a, luaeval('b')) |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 635 | call assert_equal(5, luaeval('#b')) |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 636 | call assert_fails('lua b[#b+1] = 0x80', '[string "vim chunk"]:1: index out of range') |
| 637 | lua b:add("12ab") |
| 638 | call assert_equal(0z012000ff.7a313261.62, luaeval('b')) |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 639 | call assert_equal(9, luaeval('#b')) |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 640 | call assert_fails('lua b:add(nil)', '[string "vim chunk"]:1: string expected, got nil') |
| 641 | call assert_fails('lua b:add(true)', '[string "vim chunk"]:1: string expected, got boolean') |
| 642 | call assert_fails('lua b:add({})', '[string "vim chunk"]:1: string expected, got table') |
| 643 | lua b = nil |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 644 | |
| 645 | let b = 0z0102 |
| 646 | lua lb = vim.eval('b') |
| 647 | let n = luaeval('lb[1]') |
| 648 | call assert_equal(2, n) |
| 649 | let n = luaeval('lb[6]') |
| 650 | call assert_equal(v:null, n) |
Bram Moolenaar | f65ed86 | 2021-04-03 14:13:33 +0200 | [diff] [blame] | 651 | if s:lua_543_or_later |
| 652 | let msg = "[string \"luaeval\"]:1: method 'xyz' is not callable (a nil value)" |
| 653 | elseif s:lua_53_or_later |
| 654 | let msg = "[string \"luaeval\"]:1: attempt to call a nil value (method 'xyz')" |
| 655 | else |
| 656 | let msg = "[string \"luaeval\"]:1: attempt to call method 'xyz' (a nil value)" |
| 657 | endif |
| 658 | call assert_fails('let x = luaeval("lb:xyz(3)")', msg) |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 659 | let y = luaeval("lb[{}]") |
| 660 | call assert_equal(v:null, y) |
| 661 | |
| 662 | lockvar b |
| 663 | call assert_fails('lua lb[1] = 2', '[string "vim chunk"]:1: blob is locked') |
| 664 | call assert_fails('lua lb:add("12")', '[string "vim chunk"]:1: blob is locked') |
| 665 | |
| 666 | " Error cases |
| 667 | lua t = {} |
| 668 | call assert_fails('lua b = vim.blob(t)', |
| 669 | \ '[string "vim chunk"]:1: string expected, got table') |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 670 | endfunc |
| 671 | |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 672 | func Test_lua_funcref() |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 673 | function I(x) |
| 674 | return a:x |
| 675 | endfunction |
| 676 | let R = function('I') |
| 677 | lua i1 = vim.funcref"I" |
| 678 | lua i2 = vim.eval"R" |
| 679 | lua msg = "funcref|test|" .. (#i2(i1) == #i1(i2) and "OK" or "FAIL") |
| 680 | lua msg = vim.funcref"tr"(msg, "|", " ") |
| 681 | call assert_equal("funcref test OK", luaeval('msg')) |
| 682 | |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 683 | " Error cases |
| 684 | call assert_fails('lua f1 = vim.funcref("")', |
| 685 | \ '[string "vim chunk"]:1: invalid function name: ') |
| 686 | call assert_fails('lua f1 = vim.funcref("10")', |
| 687 | \ '[string "vim chunk"]:1: invalid function name: 10') |
| 688 | let fname = test_null_string() |
| 689 | call assert_fails('lua f1 = vim.funcref(fname)', |
| 690 | \ "[string \"vim chunk\"]:1: bad argument #1 to 'funcref' (string expected, got nil)") |
| 691 | call assert_fails('lua vim.funcref("abc")()', |
Bram Moolenaar | ecdd14a | 2020-07-11 22:49:59 +0200 | [diff] [blame] | 692 | \ ['E117:', '\[string "vim chunk"]:1: cannot call funcref']) |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 693 | |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 694 | " dict funcref |
| 695 | function Mylen() dict |
| 696 | return len(self.data) |
| 697 | endfunction |
| 698 | let l = [0, 1, 2, 3] |
| 699 | let mydict = {'data': l} |
| 700 | lua d = vim.eval"mydict" |
| 701 | lua d.len = vim.funcref"Mylen" -- assign d as 'self' |
| 702 | lua res = (d.len() == vim.funcref"len"(vim.eval"l")) and "OK" or "FAIL" |
| 703 | call assert_equal("OK", luaeval('res')) |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 704 | call assert_equal(function('Mylen', {'data': l, 'len': function('Mylen')}), mydict.len) |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 705 | |
| 706 | lua i1, i2, msg, d, res = nil |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 707 | endfunc |
| 708 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 709 | " Test vim.type() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 710 | func Test_lua_type() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 711 | " The following values are identical to Lua's type function. |
| 712 | call assert_equal('string', luaeval('vim.type("foo")')) |
| 713 | call assert_equal('number', luaeval('vim.type(1)')) |
| 714 | call assert_equal('number', luaeval('vim.type(1.2)')) |
| 715 | call assert_equal('function', luaeval('vim.type(print)')) |
| 716 | call assert_equal('table', luaeval('vim.type({})')) |
| 717 | call assert_equal('boolean', luaeval('vim.type(true)')) |
| 718 | call assert_equal('boolean', luaeval('vim.type(false)')) |
| 719 | call assert_equal('nil', luaeval('vim.type(nil)')) |
| 720 | |
| 721 | " The following values are specific to Vim. |
| 722 | call assert_equal('window', luaeval('vim.type(vim.window())')) |
| 723 | call assert_equal('buffer', luaeval('vim.type(vim.buffer())')) |
| 724 | call assert_equal('list', luaeval('vim.type(vim.list())')) |
| 725 | call assert_equal('dict', luaeval('vim.type(vim.dict())')) |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 726 | call assert_equal('funcref', luaeval('vim.type(vim.funcref("Test_type"))')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 727 | endfunc |
| 728 | |
| 729 | " Test vim.open() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 730 | func Test_lua_open() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 731 | call assert_notmatch('XOpen', execute('ls')) |
| 732 | |
| 733 | " Open a buffer XOpen1, but do not jump to it. |
| 734 | lua b = vim.open('XOpen1') |
| 735 | call assert_equal('XOpen1', luaeval('b.name')) |
| 736 | call assert_equal('', bufname('%')) |
| 737 | |
| 738 | call assert_match('XOpen1', execute('ls')) |
| 739 | call assert_notequal('XOpen2', bufname('%')) |
| 740 | |
| 741 | " Open a buffer XOpen2 and jump to it. |
| 742 | lua b = vim.open('XOpen2')() |
| 743 | call assert_equal('XOpen2', luaeval('b.name')) |
| 744 | call assert_equal('XOpen2', bufname('%')) |
| 745 | |
| 746 | lua b = nil |
| 747 | %bwipe! |
| 748 | endfunc |
| 749 | |
Bram Moolenaar | 788fbb4 | 2020-05-31 14:08:12 +0200 | [diff] [blame] | 750 | func Test_update_package_paths() |
| 751 | set runtimepath+=./testluaplugin |
| 752 | call assert_equal("hello from lua", luaeval("require('testluaplugin').hello()")) |
| 753 | endfunc |
| 754 | |
Bram Moolenaar | 801ab06 | 2020-06-25 19:27:56 +0200 | [diff] [blame] | 755 | func Vim_func_call_lua_callback(Concat, Cb) |
| 756 | let l:message = a:Concat("hello", "vim") |
| 757 | call a:Cb(l:message) |
| 758 | endfunc |
| 759 | |
| 760 | func Test_pass_lua_callback_to_vim_from_lua() |
| 761 | lua pass_lua_callback_to_vim_from_lua_result = "" |
| 762 | call assert_equal("", luaeval("pass_lua_callback_to_vim_from_lua_result")) |
| 763 | lua <<EOF |
| 764 | vim.funcref('Vim_func_call_lua_callback')( |
| 765 | function(greeting, message) |
| 766 | return greeting .. " " .. message |
| 767 | end, |
| 768 | function(message) |
| 769 | pass_lua_callback_to_vim_from_lua_result = message |
| 770 | end) |
| 771 | EOF |
| 772 | call assert_equal("hello vim", luaeval("pass_lua_callback_to_vim_from_lua_result")) |
| 773 | endfunc |
| 774 | |
| 775 | func Vim_func_call_metatable_lua_callback(Greet) |
| 776 | return a:Greet("world") |
| 777 | endfunc |
| 778 | |
| 779 | func Test_pass_lua_metatable_callback_to_vim_from_lua() |
| 780 | let result = luaeval("vim.funcref('Vim_func_call_metatable_lua_callback')(setmetatable({ space = ' '}, { __call = function(tbl, msg) return 'hello' .. tbl.space .. msg end }) )") |
| 781 | call assert_equal("hello world", result) |
| 782 | endfunc |
| 783 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 784 | " Test vim.line() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 785 | func Test_lua_line() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 786 | new |
| 787 | call setline(1, ['first line', 'second line']) |
| 788 | 1 |
| 789 | call assert_equal('first line', luaeval('vim.line()')) |
| 790 | 2 |
| 791 | call assert_equal('second line', luaeval('vim.line()')) |
| 792 | bwipe! |
| 793 | endfunc |
| 794 | |
| 795 | " Test vim.beep() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 796 | func Test_lua_beep() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 797 | call assert_beeps('lua vim.beep()') |
| 798 | endfunc |
| 799 | |
| 800 | " Test errors in luaeval() |
| 801 | func Test_luaeval_error() |
| 802 | " Compile error |
| 803 | call assert_fails("call luaeval('-nil')", |
| 804 | \ '[string "luaeval"]:1: attempt to perform arithmetic on a nil value') |
| 805 | call assert_fails("call luaeval(']')", |
| 806 | \ "[string \"luaeval\"]:1: unexpected symbol near ']'") |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 807 | lua co = coroutine.create(function () print("hi") end) |
| 808 | call assert_fails('let i = luaeval("co")', 'luaeval: cannot convert value') |
| 809 | lua co = nil |
| 810 | call assert_fails('let m = luaeval("{}")', 'luaeval: cannot convert value') |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 811 | endfunc |
| 812 | |
| 813 | " Test :luafile foo.lua |
| 814 | func Test_luafile() |
| 815 | call delete('Xlua_file') |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 816 | call writefile(["str = 'hello'", "num = 123" ], 'Xlua_file') |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 817 | call setfperm('Xlua_file', 'r-xr-xr-x') |
| 818 | |
| 819 | luafile Xlua_file |
| 820 | call assert_equal('hello', luaeval('str')) |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 821 | call assert_equal(123, luaeval('num')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 822 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 823 | lua str, num = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 824 | call delete('Xlua_file') |
| 825 | endfunc |
| 826 | |
| 827 | " Test :luafile % |
| 828 | func Test_luafile_percent() |
| 829 | new Xlua_file |
| 830 | append |
| 831 | str, num = 'foo', 321.0 |
| 832 | print(string.format('str=%s, num=%d', str, num)) |
| 833 | . |
| 834 | w! |
| 835 | luafile % |
| 836 | let msg = split(execute('message'), "\n")[-1] |
| 837 | call assert_equal('str=foo, num=321', msg) |
| 838 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 839 | lua str, num = nil |
| 840 | call delete('Xlua_file') |
| 841 | bwipe! |
| 842 | endfunc |
| 843 | |
| 844 | " Test :luafile with syntax error |
| 845 | func Test_luafile_error() |
| 846 | new Xlua_file |
| 847 | call writefile(['nil = 0' ], 'Xlua_file') |
| 848 | call setfperm('Xlua_file', 'r-xr-xr-x') |
| 849 | |
| 850 | call assert_fails('luafile Xlua_file', "Xlua_file:1: unexpected symbol near 'nil'") |
| 851 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 852 | call delete('Xlua_file') |
Bram Moolenaar | d58f03b | 2017-01-29 22:48:45 +0100 | [diff] [blame] | 853 | bwipe! |
| 854 | endfunc |
Bram Moolenaar | 5390144 | 2018-07-25 22:02:36 +0200 | [diff] [blame] | 855 | |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 856 | " Test for dealing with strings containing newlines and null character |
| 857 | func Test_lua_string_with_newline() |
| 858 | let x = execute('lua print("Hello\nWorld")') |
| 859 | call assert_equal("\nHello\nWorld", x) |
| 860 | new |
| 861 | lua k = vim.buffer(vim.eval('bufnr()')) |
| 862 | lua k:insert("Hello\0World", 0) |
| 863 | call assert_equal(["Hello\nWorld", ''], getline(1, '$')) |
| 864 | close! |
| 865 | endfunc |
| 866 | |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 867 | func Test_lua_set_cursor() |
Bram Moolenaar | 5390144 | 2018-07-25 22:02:36 +0200 | [diff] [blame] | 868 | " Check that setting the cursor position works. |
| 869 | new |
| 870 | call setline(1, ['first line', 'second line']) |
| 871 | normal gg |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 872 | lua << trim EOF |
| 873 | w = vim.window() |
| 874 | w.line = 1 |
| 875 | w.col = 5 |
| 876 | EOF |
Bram Moolenaar | 5390144 | 2018-07-25 22:02:36 +0200 | [diff] [blame] | 877 | call assert_equal([1, 5], [line('.'), col('.')]) |
| 878 | |
| 879 | " Check that movement after setting cursor position keeps current column. |
| 880 | normal j |
| 881 | call assert_equal([2, 5], [line('.'), col('.')]) |
| 882 | endfunc |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 883 | |
| 884 | " Test for various heredoc syntax |
| 885 | func Test_lua_heredoc() |
| 886 | lua << END |
| 887 | vim.command('let s = "A"') |
| 888 | END |
| 889 | lua << |
| 890 | vim.command('let s ..= "B"') |
| 891 | . |
| 892 | lua << trim END |
| 893 | vim.command('let s ..= "C"') |
| 894 | END |
| 895 | lua << trim |
| 896 | vim.command('let s ..= "D"') |
| 897 | . |
Bram Moolenaar | 6ab0953 | 2020-05-01 14:10:13 +0200 | [diff] [blame] | 898 | lua << trim eof |
| 899 | vim.command('let s ..= "E"') |
| 900 | eof |
| 901 | call assert_equal('ABCDE', s) |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 902 | endfunc |
| 903 | |
| 904 | " vim: shiftwidth=2 sts=2 expandtab |