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