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