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 | c8970b9 | 2020-10-26 20:18:08 +0100 | [diff] [blame] | 3 | " This test also works without the lua feature. |
| 4 | func Test_skip_lua() |
| 5 | if 0 |
| 6 | lua print("Not executed") |
| 7 | endif |
| 8 | endfunc |
| 9 | |
Bram Moolenaar | b46fecd | 2019-06-15 17:58:09 +0200 | [diff] [blame] | 10 | CheckFeature lua |
Bram Moolenaar | d58f03b | 2017-01-29 22:48:45 +0100 | [diff] [blame] | 11 | |
Bram Moolenaar | f65ed86 | 2021-04-03 14:13:33 +0200 | [diff] [blame] | 12 | " Depending on the lua version, the error messages are different. |
Bram Moolenaar | 125ed27 | 2021-04-07 20:11:12 +0200 | [diff] [blame] | 13 | let [s:major, s:minor, s:patch] = luaeval('vim.lua_version')->split('\.')->map({-> str2nr(v:val)}) |
Bram Moolenaar | f65ed86 | 2021-04-03 14:13:33 +0200 | [diff] [blame] | 14 | let s:lua_53_or_later = 0 |
=?UTF-8?q?Jakub=20Kul=C3=ADk?= | 57ff2b7 | 2022-01-28 17:20:03 +0000 | [diff] [blame] | 15 | let s:lua_543 = 0 |
Bram Moolenaar | f65ed86 | 2021-04-03 14:13:33 +0200 | [diff] [blame] | 16 | if (s:major == 5 && s:minor >= 3) || s:major > 5 |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 17 | let s:lua_53_or_later = 1 |
=?UTF-8?q?Jakub=20Kul=C3=ADk?= | 57ff2b7 | 2022-01-28 17:20:03 +0000 | [diff] [blame] | 18 | if s:major == 5 && s:minor == 4 && s:patch == 3 |
| 19 | let s:lua_543 = 1 |
Bram Moolenaar | f65ed86 | 2021-04-03 14:13:33 +0200 | [diff] [blame] | 20 | endif |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 21 | endif |
| 22 | |
Bram Moolenaar | e165f63 | 2019-03-10 09:48:59 +0100 | [diff] [blame] | 23 | func TearDown() |
| 24 | " Run garbage collection after each test to exercise luaV_setref(). |
| 25 | call test_garbagecollect_now() |
| 26 | endfunc |
| 27 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 28 | " Check that switching to another buffer does not trigger ml_get error. |
zeertzjq | e99f068 | 2024-01-29 19:32:39 +0100 | [diff] [blame] | 29 | func Test_lua_luado_change_buffer() |
Bram Moolenaar | d58f03b | 2017-01-29 22:48:45 +0100 | [diff] [blame] | 30 | new |
zeertzjq | e99f068 | 2024-01-29 19:32:39 +0100 | [diff] [blame] | 31 | |
Bram Moolenaar | d58f03b | 2017-01-29 22:48:45 +0100 | [diff] [blame] | 32 | let wincount = winnr('$') |
| 33 | call setline(1, ['one', 'two', 'three']) |
| 34 | luado vim.command("new") |
| 35 | call assert_equal(wincount + 1, winnr('$')) |
zeertzjq | e99f068 | 2024-01-29 19:32:39 +0100 | [diff] [blame] | 36 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 37 | %bwipe! |
| 38 | endfunc |
| 39 | |
zeertzjq | e99f068 | 2024-01-29 19:32:39 +0100 | [diff] [blame] | 40 | " Check that :luado deleting lines does not trigger ml_get error. |
| 41 | func Test_lua_luado_delete_lines() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 42 | new |
zeertzjq | e99f068 | 2024-01-29 19:32:39 +0100 | [diff] [blame] | 43 | |
| 44 | call setline(1, ['one', 'two', 'three']) |
| 45 | luado vim.command("%d_") |
| 46 | call assert_equal([''], getline(1, '$')) |
| 47 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 48 | call setline(1, ['one', 'two', 'three']) |
| 49 | luado vim.command("1,2d_") |
| 50 | call assert_equal(['three'], getline(1, '$')) |
zeertzjq | e99f068 | 2024-01-29 19:32:39 +0100 | [diff] [blame] | 51 | |
| 52 | call setline(1, ['one', 'two', 'three']) |
| 53 | luado vim.command("2,3d_"); return "REPLACED" |
| 54 | call assert_equal(['REPLACED'], getline(1, '$')) |
| 55 | |
| 56 | call setline(1, ['one', 'two', 'three']) |
| 57 | 2,3luado vim.command("1,2d_"); return "REPLACED" |
| 58 | call assert_equal(['three'], getline(1, '$')) |
| 59 | |
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)") |
=?UTF-8?q?Jakub=20Kul=C3=ADk?= | 57ff2b7 | 2022-01-28 17:20:03 +0000 | [diff] [blame] | 73 | if s:lua_543 |
Bram Moolenaar | f65ed86 | 2021-04-03 14:13:33 +0200 | [diff] [blame] | 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') |
zeertzjq | 50732c7 | 2024-11-09 18:30:10 +0100 | [diff] [blame] | 118 | call assert_equal('boolean', luaeval('vim.type(v)')) |
| 119 | call assert_equal(v:true, luaeval('v')) |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 120 | lua v = vim.eval('v:false') |
zeertzjq | 50732c7 | 2024-11-09 18:30:10 +0100 | [diff] [blame] | 121 | call assert_equal('boolean', luaeval('vim.type(v)')) |
| 122 | call assert_equal(v:false, luaeval('v')) |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 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 | |
=?UTF-8?q?Jakub=20Kul=C3=ADk?= | 57ff2b7 | 2022-01-28 17:20:03 +0000 | [diff] [blame] | 165 | if s:lua_543 |
Bram Moolenaar | f65ed86 | 2021-04-03 14:13:33 +0200 | [diff] [blame] | 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()[{}]')) |
=?UTF-8?q?Jakub=20Kul=C3=ADk?= | 57ff2b7 | 2022-01-28 17:20:03 +0000 | [diff] [blame] | 357 | if s:lua_543 |
Bram Moolenaar | f65ed86 | 2021-04-03 14:13:33 +0200 | [diff] [blame] | 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) |
=?UTF-8?q?Jakub=20Kul=C3=ADk?= | 57ff2b7 | 2022-01-28 17:20:03 +0000 | [diff] [blame] | 468 | if s:lua_543 |
Bram Moolenaar | f65ed86 | 2021-04-03 14:13:33 +0200 | [diff] [blame] | 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")')) |
Christian Brabandt | 6efb198 | 2023-08-10 05:44:25 +0200 | [diff] [blame] | 633 | call assert_equal(0z00010203, luaeval('vim.blob("\000\001\002\003")')) |
| 634 | call assert_equal(0z8081FEFF, luaeval('vim.blob("\128\129\254\255")')) |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 635 | |
Christian Brabandt | 6efb198 | 2023-08-10 05:44:25 +0200 | [diff] [blame] | 636 | lua b = vim.blob("\000\000\000\000") |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 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) |
=?UTF-8?q?Jakub=20Kul=C3=ADk?= | 57ff2b7 | 2022-01-28 17:20:03 +0000 | [diff] [blame] | 659 | if s:lua_543 |
Bram Moolenaar | f65ed86 | 2021-04-03 14:13:33 +0200 | [diff] [blame] | 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 | 7d149f8 | 2022-06-17 19:23:34 +0100 | [diff] [blame] | 680 | def Vim9Test(Callback: func()) |
| 681 | Callback() |
| 682 | enddef |
| 683 | |
| 684 | func Test_call_lua_func_from_vim9_func() |
| 685 | " this only tests that Vim doesn't crash |
| 686 | lua << EOF |
| 687 | vim.fn.Vim9Test(function () print('Hello') end) |
| 688 | EOF |
| 689 | endfunc |
| 690 | |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 691 | func Test_lua_funcref() |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 692 | function I(x) |
| 693 | return a:x |
| 694 | endfunction |
| 695 | let R = function('I') |
| 696 | lua i1 = vim.funcref"I" |
| 697 | lua i2 = vim.eval"R" |
| 698 | lua msg = "funcref|test|" .. (#i2(i1) == #i1(i2) and "OK" or "FAIL") |
| 699 | lua msg = vim.funcref"tr"(msg, "|", " ") |
| 700 | call assert_equal("funcref test OK", luaeval('msg')) |
| 701 | |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 702 | " Error cases |
| 703 | call assert_fails('lua f1 = vim.funcref("")', |
| 704 | \ '[string "vim chunk"]:1: invalid function name: ') |
| 705 | call assert_fails('lua f1 = vim.funcref("10")', |
| 706 | \ '[string "vim chunk"]:1: invalid function name: 10') |
| 707 | let fname = test_null_string() |
| 708 | call assert_fails('lua f1 = vim.funcref(fname)', |
| 709 | \ "[string \"vim chunk\"]:1: bad argument #1 to 'funcref' (string expected, got nil)") |
| 710 | call assert_fails('lua vim.funcref("abc")()', |
Bram Moolenaar | ecdd14a | 2020-07-11 22:49:59 +0200 | [diff] [blame] | 711 | \ ['E117:', '\[string "vim chunk"]:1: cannot call funcref']) |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 712 | |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 713 | " dict funcref |
| 714 | function Mylen() dict |
| 715 | return len(self.data) |
| 716 | endfunction |
| 717 | let l = [0, 1, 2, 3] |
| 718 | let mydict = {'data': l} |
| 719 | lua d = vim.eval"mydict" |
| 720 | lua d.len = vim.funcref"Mylen" -- assign d as 'self' |
| 721 | lua res = (d.len() == vim.funcref"len"(vim.eval"l")) and "OK" or "FAIL" |
| 722 | call assert_equal("OK", luaeval('res')) |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 723 | call assert_equal(function('Mylen', {'data': l, 'len': function('Mylen')}), mydict.len) |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 724 | |
| 725 | lua i1, i2, msg, d, res = nil |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 726 | endfunc |
| 727 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 728 | " Test vim.type() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 729 | func Test_lua_type() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 730 | " The following values are identical to Lua's type function. |
| 731 | call assert_equal('string', luaeval('vim.type("foo")')) |
| 732 | call assert_equal('number', luaeval('vim.type(1)')) |
| 733 | call assert_equal('number', luaeval('vim.type(1.2)')) |
| 734 | call assert_equal('function', luaeval('vim.type(print)')) |
| 735 | call assert_equal('table', luaeval('vim.type({})')) |
| 736 | call assert_equal('boolean', luaeval('vim.type(true)')) |
| 737 | call assert_equal('boolean', luaeval('vim.type(false)')) |
| 738 | call assert_equal('nil', luaeval('vim.type(nil)')) |
| 739 | |
| 740 | " The following values are specific to Vim. |
| 741 | call assert_equal('window', luaeval('vim.type(vim.window())')) |
| 742 | call assert_equal('buffer', luaeval('vim.type(vim.buffer())')) |
| 743 | call assert_equal('list', luaeval('vim.type(vim.list())')) |
| 744 | call assert_equal('dict', luaeval('vim.type(vim.dict())')) |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 745 | call assert_equal('funcref', luaeval('vim.type(vim.funcref("Test_type"))')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 746 | endfunc |
| 747 | |
| 748 | " Test vim.open() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 749 | func Test_lua_open() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 750 | call assert_notmatch('XOpen', execute('ls')) |
| 751 | |
| 752 | " Open a buffer XOpen1, but do not jump to it. |
| 753 | lua b = vim.open('XOpen1') |
| 754 | call assert_equal('XOpen1', luaeval('b.name')) |
| 755 | call assert_equal('', bufname('%')) |
| 756 | |
| 757 | call assert_match('XOpen1', execute('ls')) |
| 758 | call assert_notequal('XOpen2', bufname('%')) |
| 759 | |
| 760 | " Open a buffer XOpen2 and jump to it. |
| 761 | lua b = vim.open('XOpen2')() |
| 762 | call assert_equal('XOpen2', luaeval('b.name')) |
| 763 | call assert_equal('XOpen2', bufname('%')) |
| 764 | |
| 765 | lua b = nil |
| 766 | %bwipe! |
| 767 | endfunc |
| 768 | |
Bram Moolenaar | 788fbb4 | 2020-05-31 14:08:12 +0200 | [diff] [blame] | 769 | func Test_update_package_paths() |
| 770 | set runtimepath+=./testluaplugin |
| 771 | call assert_equal("hello from lua", luaeval("require('testluaplugin').hello()")) |
| 772 | endfunc |
| 773 | |
Bram Moolenaar | 801ab06 | 2020-06-25 19:27:56 +0200 | [diff] [blame] | 774 | func Vim_func_call_lua_callback(Concat, Cb) |
| 775 | let l:message = a:Concat("hello", "vim") |
| 776 | call a:Cb(l:message) |
| 777 | endfunc |
| 778 | |
| 779 | func Test_pass_lua_callback_to_vim_from_lua() |
| 780 | lua pass_lua_callback_to_vim_from_lua_result = "" |
| 781 | call assert_equal("", luaeval("pass_lua_callback_to_vim_from_lua_result")) |
| 782 | lua <<EOF |
| 783 | vim.funcref('Vim_func_call_lua_callback')( |
| 784 | function(greeting, message) |
| 785 | return greeting .. " " .. message |
| 786 | end, |
| 787 | function(message) |
| 788 | pass_lua_callback_to_vim_from_lua_result = message |
| 789 | end) |
| 790 | EOF |
| 791 | call assert_equal("hello vim", luaeval("pass_lua_callback_to_vim_from_lua_result")) |
| 792 | endfunc |
| 793 | |
| 794 | func Vim_func_call_metatable_lua_callback(Greet) |
| 795 | return a:Greet("world") |
| 796 | endfunc |
| 797 | |
| 798 | func Test_pass_lua_metatable_callback_to_vim_from_lua() |
| 799 | let result = luaeval("vim.funcref('Vim_func_call_metatable_lua_callback')(setmetatable({ space = ' '}, { __call = function(tbl, msg) return 'hello' .. tbl.space .. msg end }) )") |
| 800 | call assert_equal("hello world", result) |
| 801 | endfunc |
| 802 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 803 | " Test vim.line() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 804 | func Test_lua_line() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 805 | new |
| 806 | call setline(1, ['first line', 'second line']) |
| 807 | 1 |
| 808 | call assert_equal('first line', luaeval('vim.line()')) |
| 809 | 2 |
| 810 | call assert_equal('second line', luaeval('vim.line()')) |
| 811 | bwipe! |
| 812 | endfunc |
| 813 | |
| 814 | " Test vim.beep() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 815 | func Test_lua_beep() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 816 | call assert_beeps('lua vim.beep()') |
| 817 | endfunc |
| 818 | |
| 819 | " Test errors in luaeval() |
| 820 | func Test_luaeval_error() |
| 821 | " Compile error |
| 822 | call assert_fails("call luaeval('-nil')", |
| 823 | \ '[string "luaeval"]:1: attempt to perform arithmetic on a nil value') |
| 824 | call assert_fails("call luaeval(']')", |
| 825 | \ "[string \"luaeval\"]:1: unexpected symbol near ']'") |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 826 | lua co = coroutine.create(function () print("hi") end) |
| 827 | call assert_fails('let i = luaeval("co")', 'luaeval: cannot convert value') |
| 828 | lua co = nil |
| 829 | call assert_fails('let m = luaeval("{}")', 'luaeval: cannot convert value') |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 830 | endfunc |
| 831 | |
| 832 | " Test :luafile foo.lua |
| 833 | func Test_luafile() |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 834 | call writefile(["str = 'hello'", "num = 123" ], 'Xlua_file', 'D') |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 835 | call setfperm('Xlua_file', 'r-xr-xr-x') |
| 836 | |
| 837 | luafile Xlua_file |
| 838 | call assert_equal('hello', luaeval('str')) |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 839 | call assert_equal(123, luaeval('num')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 840 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 841 | lua str, num = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 842 | endfunc |
| 843 | |
| 844 | " Test :luafile % |
| 845 | func Test_luafile_percent() |
| 846 | new Xlua_file |
| 847 | append |
| 848 | str, num = 'foo', 321.0 |
| 849 | print(string.format('str=%s, num=%d', str, num)) |
| 850 | . |
| 851 | w! |
| 852 | luafile % |
| 853 | let msg = split(execute('message'), "\n")[-1] |
| 854 | call assert_equal('str=foo, num=321', msg) |
| 855 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 856 | lua str, num = nil |
| 857 | call delete('Xlua_file') |
| 858 | bwipe! |
| 859 | endfunc |
| 860 | |
| 861 | " Test :luafile with syntax error |
| 862 | func Test_luafile_error() |
| 863 | new Xlua_file |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 864 | call writefile(['nil = 0' ], 'Xlua_file', 'D') |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 865 | call setfperm('Xlua_file', 'r-xr-xr-x') |
| 866 | |
| 867 | call assert_fails('luafile Xlua_file', "Xlua_file:1: unexpected symbol near 'nil'") |
| 868 | |
Bram Moolenaar | d58f03b | 2017-01-29 22:48:45 +0100 | [diff] [blame] | 869 | bwipe! |
| 870 | endfunc |
Bram Moolenaar | 5390144 | 2018-07-25 22:02:36 +0200 | [diff] [blame] | 871 | |
Bram Moolenaar | 78e006b | 2021-07-28 15:07:01 +0200 | [diff] [blame] | 872 | " Test :luafile printing a long string |
| 873 | func Test_luafile_print() |
| 874 | new Xlua_file |
| 875 | let lines =<< trim END |
| 876 | local data = '' |
| 877 | for i = 1, 130 do |
| 878 | data = data .. 'xxxxx asd as as dad sad sad xz cxz czxcxzczxc ad ad asd asd asd asd asd' |
| 879 | end |
| 880 | print(data) |
| 881 | END |
| 882 | call setline(1, lines) |
| 883 | w |
| 884 | luafile % |
| 885 | |
| 886 | call delete('Xlua_file') |
| 887 | bwipe! |
| 888 | endfunc |
| 889 | |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 890 | " Test for dealing with strings containing newlines and null character |
| 891 | func Test_lua_string_with_newline() |
Bram Moolenaar | 2a4bd00 | 2021-07-28 21:48:59 +0200 | [diff] [blame] | 892 | let x = execute('lua print("Hello\nWorld", 2)') |
| 893 | call assert_equal("\nHello\nWorld 2", x) |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 894 | new |
| 895 | lua k = vim.buffer(vim.eval('bufnr()')) |
| 896 | lua k:insert("Hello\0World", 0) |
| 897 | call assert_equal(["Hello\nWorld", ''], getline(1, '$')) |
| 898 | close! |
| 899 | endfunc |
| 900 | |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 901 | func Test_lua_set_cursor() |
Bram Moolenaar | 5390144 | 2018-07-25 22:02:36 +0200 | [diff] [blame] | 902 | " Check that setting the cursor position works. |
| 903 | new |
| 904 | call setline(1, ['first line', 'second line']) |
| 905 | normal gg |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 906 | lua << trim EOF |
| 907 | w = vim.window() |
| 908 | w.line = 1 |
| 909 | w.col = 5 |
| 910 | EOF |
Bram Moolenaar | 5390144 | 2018-07-25 22:02:36 +0200 | [diff] [blame] | 911 | call assert_equal([1, 5], [line('.'), col('.')]) |
| 912 | |
| 913 | " Check that movement after setting cursor position keeps current column. |
| 914 | normal j |
| 915 | call assert_equal([2, 5], [line('.'), col('.')]) |
| 916 | endfunc |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 917 | |
| 918 | " Test for various heredoc syntax |
| 919 | func Test_lua_heredoc() |
| 920 | lua << END |
| 921 | vim.command('let s = "A"') |
| 922 | END |
| 923 | lua << |
| 924 | vim.command('let s ..= "B"') |
| 925 | . |
| 926 | lua << trim END |
| 927 | vim.command('let s ..= "C"') |
| 928 | END |
| 929 | lua << trim |
| 930 | vim.command('let s ..= "D"') |
| 931 | . |
Bram Moolenaar | 6ab0953 | 2020-05-01 14:10:13 +0200 | [diff] [blame] | 932 | lua << trim eof |
| 933 | vim.command('let s ..= "E"') |
| 934 | eof |
zeertzjq | 449c2e5 | 2025-02-03 18:56:16 +0100 | [diff] [blame] | 935 | lua << trimm |
| 936 | vim.command('let s ..= "F"') |
| 937 | trimm |
| 938 | call assert_equal('ABCDEF', s) |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 939 | endfunc |
| 940 | |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 941 | " Test for adding, accessing and removing global variables using the vim.g |
| 942 | " Lua table |
| 943 | func Test_lua_global_var_table() |
| 944 | " Access global variables with different types of values |
| 945 | let g:Var1 = 10 |
| 946 | let g:Var2 = 'Hello' |
| 947 | let g:Var3 = ['a', 'b'] |
| 948 | let g:Var4 = #{x: 'edit', y: 'run'} |
| 949 | let g:Var5 = function('min') |
| 950 | call assert_equal(10, luaeval('vim.g.Var1')) |
| 951 | call assert_equal('Hello', luaeval('vim.g.Var2')) |
| 952 | call assert_equal(['a', 'b'], luaeval('vim.g.Var3')) |
| 953 | call assert_equal(#{x: 'edit', y: 'run'}, luaeval('vim.g.Var4')) |
| 954 | call assert_equal(2, luaeval('vim.g.Var5')([5, 9, 2])) |
| 955 | |
| 956 | " Access list of dictionaries and dictionary of lists |
| 957 | let g:Var1 = [#{a: 10}, #{b: 20}] |
| 958 | let g:Var2 = #{p: [5, 6], q: [1.1, 2.2]} |
| 959 | call assert_equal([#{a: 10}, #{b: 20}], luaeval('vim.g.Var1')) |
| 960 | call assert_equal(#{p: [5, 6], q: [1.1, 2.2]}, luaeval('vim.g.Var2')) |
| 961 | |
| 962 | " Create new global variables with different types of values |
| 963 | unlet g:Var1 g:Var2 g:Var3 g:Var4 g:Var5 |
| 964 | lua << trim END |
| 965 | vim.g.Var1 = 34 |
| 966 | vim.g.Var2 = 'World' |
| 967 | vim.g.Var3 = vim.list({'#', '$'}) |
| 968 | vim.g.Var4 = vim.dict({model='honda', year=2020}) |
| 969 | vim.g.Var5 = vim.funcref('max') |
| 970 | END |
| 971 | call assert_equal(34, g:Var1) |
| 972 | call assert_equal('World', g:Var2) |
| 973 | call assert_equal(['#', '$'], g:Var3) |
| 974 | call assert_equal(#{model: 'honda', year: 2020}, g:Var4) |
| 975 | call assert_equal(10, g:Var5([5, 10, 9])) |
| 976 | |
| 977 | " Create list of dictionaries and dictionary of lists |
| 978 | unlet g:Var1 g:Var2 |
| 979 | lua << trim END |
| 980 | vim.g.Var1 = vim.list({vim.dict({a=10}), vim.dict({b=20})}) |
| 981 | vim.g.Var2 = vim.dict({p=vim.list({5, 6}), q=vim.list({1.1, 2.2})}) |
| 982 | END |
| 983 | call assert_equal([#{a: 10}, #{b: 20}], luaeval('vim.g.Var1')) |
| 984 | call assert_equal(#{p: [5, 6], q: [1.1, 2.2]}, luaeval('vim.g.Var2')) |
| 985 | |
| 986 | " Modify a global variable with a list value or a dictionary value |
| 987 | let g:Var1 = [10, 20] |
| 988 | let g:Var2 = #{one: 'mercury', two: 'mars'} |
| 989 | lua << trim END |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 990 | vim.g.Var1[2] = nil |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 991 | vim.g.Var1[3] = 15 |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 992 | vim.g.Var2['two'] = nil |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 993 | vim.g.Var2['three'] = 'earth' |
| 994 | END |
| 995 | call assert_equal([10, 15], g:Var1) |
| 996 | call assert_equal(#{one: 'mercury', three: 'earth'}, g:Var2) |
| 997 | |
| 998 | " Remove global variables with different types of values |
| 999 | let g:Var1 = 10 |
| 1000 | let g:Var2 = 'Hello' |
| 1001 | let g:Var3 = ['a', 'b'] |
| 1002 | let g:Var4 = #{x: 'edit', y: 'run'} |
| 1003 | let g:Var5 = function('min') |
| 1004 | lua << trim END |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 1005 | vim.g.Var1 = nil |
| 1006 | vim.g.Var2 = nil |
| 1007 | vim.g.Var3 = nil |
| 1008 | vim.g.Var4 = nil |
| 1009 | vim.g.Var5 = nil |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1010 | END |
| 1011 | call assert_false(exists('g:Var1')) |
| 1012 | call assert_false(exists('g:Var2')) |
| 1013 | call assert_false(exists('g:Var3')) |
| 1014 | call assert_false(exists('g:Var4')) |
| 1015 | call assert_false(exists('g:Var5')) |
| 1016 | |
| 1017 | " Try to modify and remove a locked global variable |
| 1018 | let g:Var1 = 10 |
| 1019 | lockvar g:Var1 |
| 1020 | call assert_fails('lua vim.g.Var1 = 20', 'variable is locked') |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 1021 | call assert_fails('lua vim.g.Var1 = nil', 'variable is locked') |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1022 | unlockvar g:Var1 |
| 1023 | let g:Var2 = [7, 14] |
| 1024 | lockvar 0 g:Var2 |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 1025 | lua vim.g.Var2[2] = nil |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1026 | lua vim.g.Var2[3] = 21 |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 1027 | call assert_fails('lua vim.g.Var2 = nil', 'variable is locked') |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1028 | call assert_equal([7, 21], g:Var2) |
| 1029 | lockvar 1 g:Var2 |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 1030 | call assert_fails('lua vim.g.Var2[2] = nil', 'list is locked') |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1031 | call assert_fails('lua vim.g.Var2[3] = 21', 'list is locked') |
| 1032 | unlockvar g:Var2 |
| 1033 | |
Bram Moolenaar | 4a01159 | 2021-08-05 15:11:08 +0200 | [diff] [blame] | 1034 | let g:TestFunc = function('len') |
| 1035 | call assert_fails('lua vim.g.func = vim.g.TestFunc', ['E704:', 'Couldn''t add to dictionary']) |
| 1036 | unlet g:TestFunc |
| 1037 | |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1038 | " Attempt to access a non-existing global variable |
| 1039 | call assert_equal(v:null, luaeval('vim.g.NonExistingVar')) |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 1040 | lua vim.g.NonExisting = nil |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1041 | |
| 1042 | unlet! g:Var1 g:Var2 g:Var3 g:Var4 g:Var5 |
| 1043 | endfunc |
| 1044 | |
| 1045 | " Test for accessing and modifying predefined vim variables using the vim.v |
| 1046 | " Lua table |
| 1047 | func Test_lua_predefined_var_table() |
| 1048 | call assert_equal(v:progpath, luaeval('vim.v.progpath')) |
| 1049 | let v:errmsg = 'SomeError' |
| 1050 | call assert_equal('SomeError', luaeval('vim.v.errmsg')) |
| 1051 | lua vim.v.errmsg = 'OtherError' |
| 1052 | call assert_equal('OtherError', v:errmsg) |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 1053 | lua vim.v.errmsg = 42 |
| 1054 | call assert_equal('42', v:errmsg) |
| 1055 | call assert_fails('lua vim.v.errmsg = nil', 'variable is fixed') |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1056 | let v:oldfiles = ['one', 'two'] |
| 1057 | call assert_equal(['one', 'two'], luaeval('vim.v.oldfiles')) |
| 1058 | lua vim.v.oldfiles = vim.list({}) |
| 1059 | call assert_equal([], v:oldfiles) |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 1060 | call assert_fails('lua vim.v.oldfiles = "a"', |
| 1061 | \ 'Setting v:oldfiles to value with wrong type') |
| 1062 | call assert_equal([], v:oldfiles) |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1063 | call assert_equal(v:null, luaeval('vim.v.null')) |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 1064 | call assert_fails('lua vim.v.argv[1] = nil', 'list is locked') |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1065 | call assert_fails('lua vim.v.newvar = 1', 'Dictionary is locked') |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 1066 | |
| 1067 | new |
| 1068 | call setline(1, ' foo foo foo') |
| 1069 | /foo |
| 1070 | call assert_equal([0, 1, 2, 0, 2], getcurpos()) |
| 1071 | call assert_equal(1, v:searchforward) |
| 1072 | normal! n |
| 1073 | call assert_equal([0, 1, 6, 0, 6], getcurpos()) |
| 1074 | lua vim.v.searchforward = 0 |
| 1075 | call assert_equal(0, v:searchforward) |
| 1076 | normal! n |
| 1077 | call assert_equal([0, 1, 2, 0, 2], getcurpos()) |
| 1078 | lua vim.v.searchforward = 1 |
| 1079 | call assert_equal(1, v:searchforward) |
| 1080 | normal! n |
| 1081 | call assert_equal([0, 1, 6, 0, 6], getcurpos()) |
| 1082 | bwipe! |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1083 | endfunc |
| 1084 | |
| 1085 | " Test for adding, accessing and modifying window-local variables using the |
| 1086 | " vim.w Lua table |
| 1087 | func Test_lua_window_var_table() |
| 1088 | " Access window variables with different types of values |
| 1089 | new |
| 1090 | let w:wvar1 = 10 |
| 1091 | let w:wvar2 = 'edit' |
| 1092 | let w:wvar3 = 3.14 |
| 1093 | let w:wvar4 = 0zdeadbeef |
| 1094 | let w:wvar5 = ['a', 'b'] |
| 1095 | let w:wvar6 = #{action: 'run'} |
| 1096 | call assert_equal(10, luaeval('vim.w.wvar1')) |
| 1097 | call assert_equal('edit', luaeval('vim.w.wvar2')) |
| 1098 | call assert_equal(3.14, luaeval('vim.w.wvar3')) |
| 1099 | call assert_equal(0zdeadbeef, luaeval('vim.w.wvar4')) |
| 1100 | call assert_equal(['a', 'b'], luaeval('vim.w.wvar5')) |
| 1101 | call assert_equal(#{action: 'run'}, luaeval('vim.w.wvar6')) |
| 1102 | call assert_equal(v:null, luaeval('vim.w.NonExisting')) |
| 1103 | |
| 1104 | " modify a window variable |
| 1105 | lua vim.w.wvar2 = 'paste' |
| 1106 | call assert_equal('paste', w:wvar2) |
| 1107 | |
| 1108 | " change the type stored in a variable |
| 1109 | let w:wvar2 = [1, 2] |
| 1110 | lua vim.w.wvar2 = vim.dict({a=10, b=20}) |
| 1111 | call assert_equal(#{a: 10, b: 20}, w:wvar2) |
| 1112 | |
| 1113 | " create a new window variable |
| 1114 | lua vim.w.wvar7 = vim.dict({a=vim.list({1, 2}), b=20}) |
| 1115 | call assert_equal(#{a: [1, 2], b: 20}, w:wvar7) |
| 1116 | |
| 1117 | " delete a window variable |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 1118 | lua vim.w.wvar2 = nil |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1119 | call assert_false(exists('w:wvar2')) |
| 1120 | |
| 1121 | new |
| 1122 | call assert_equal(v:null, luaeval('vim.w.wvar1')) |
| 1123 | call assert_equal(v:null, luaeval('vim.w.wvar2')) |
| 1124 | %bw! |
| 1125 | endfunc |
| 1126 | |
| 1127 | " Test for adding, accessing and modifying buffer-local variables using the |
| 1128 | " vim.b Lua table |
| 1129 | func Test_lua_buffer_var_table() |
| 1130 | " Access buffer variables with different types of values |
| 1131 | let b:bvar1 = 10 |
| 1132 | let b:bvar2 = 'edit' |
| 1133 | let b:bvar3 = 3.14 |
| 1134 | let b:bvar4 = 0zdeadbeef |
| 1135 | let b:bvar5 = ['a', 'b'] |
| 1136 | let b:bvar6 = #{action: 'run'} |
| 1137 | call assert_equal(10, luaeval('vim.b.bvar1')) |
| 1138 | call assert_equal('edit', luaeval('vim.b.bvar2')) |
| 1139 | call assert_equal(3.14, luaeval('vim.b.bvar3')) |
| 1140 | call assert_equal(0zdeadbeef, luaeval('vim.b.bvar4')) |
| 1141 | call assert_equal(['a', 'b'], luaeval('vim.b.bvar5')) |
| 1142 | call assert_equal(#{action: 'run'}, luaeval('vim.b.bvar6')) |
| 1143 | call assert_equal(v:null, luaeval('vim.b.NonExisting')) |
| 1144 | |
| 1145 | " modify a buffer variable |
| 1146 | lua vim.b.bvar2 = 'paste' |
| 1147 | call assert_equal('paste', b:bvar2) |
| 1148 | |
| 1149 | " change the type stored in a variable |
| 1150 | let b:bvar2 = [1, 2] |
| 1151 | lua vim.b.bvar2 = vim.dict({a=10, b=20}) |
| 1152 | call assert_equal(#{a: 10, b: 20}, b:bvar2) |
| 1153 | |
| 1154 | " create a new buffer variable |
| 1155 | lua vim.b.bvar7 = vim.dict({a=vim.list({1, 2}), b=20}) |
| 1156 | call assert_equal(#{a: [1, 2], b: 20}, b:bvar7) |
| 1157 | |
| 1158 | " delete a buffer variable |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 1159 | lua vim.b.bvar2 = nil |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1160 | call assert_false(exists('b:bvar2')) |
| 1161 | |
| 1162 | new |
| 1163 | call assert_equal(v:null, luaeval('vim.b.bvar1')) |
| 1164 | call assert_equal(v:null, luaeval('vim.b.bvar2')) |
| 1165 | %bw! |
| 1166 | endfunc |
| 1167 | |
| 1168 | " Test for adding, accessing and modifying tabpage-local variables using the |
| 1169 | " vim.t Lua table |
| 1170 | func Test_lua_tabpage_var_table() |
| 1171 | " Access tabpage variables with different types of values |
| 1172 | let t:tvar1 = 10 |
| 1173 | let t:tvar2 = 'edit' |
| 1174 | let t:tvar3 = 3.14 |
| 1175 | let t:tvar4 = 0zdeadbeef |
| 1176 | let t:tvar5 = ['a', 'b'] |
| 1177 | let t:tvar6 = #{action: 'run'} |
| 1178 | call assert_equal(10, luaeval('vim.t.tvar1')) |
| 1179 | call assert_equal('edit', luaeval('vim.t.tvar2')) |
| 1180 | call assert_equal(3.14, luaeval('vim.t.tvar3')) |
| 1181 | call assert_equal(0zdeadbeef, luaeval('vim.t.tvar4')) |
| 1182 | call assert_equal(['a', 'b'], luaeval('vim.t.tvar5')) |
| 1183 | call assert_equal(#{action: 'run'}, luaeval('vim.t.tvar6')) |
| 1184 | call assert_equal(v:null, luaeval('vim.t.NonExisting')) |
| 1185 | |
| 1186 | " modify a tabpage variable |
| 1187 | lua vim.t.tvar2 = 'paste' |
| 1188 | call assert_equal('paste', t:tvar2) |
| 1189 | |
| 1190 | " change the type stored in a variable |
| 1191 | let t:tvar2 = [1, 2] |
| 1192 | lua vim.t.tvar2 = vim.dict({a=10, b=20}) |
| 1193 | call assert_equal(#{a: 10, b: 20}, t:tvar2) |
| 1194 | |
| 1195 | " create a new tabpage variable |
| 1196 | lua vim.t.tvar7 = vim.dict({a=vim.list({1, 2}), b=20}) |
| 1197 | call assert_equal(#{a: [1, 2], b: 20}, t:tvar7) |
| 1198 | |
| 1199 | " delete a tabpage variable |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 1200 | lua vim.t.tvar2 = nil |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1201 | call assert_false(exists('t:tvar2')) |
| 1202 | |
| 1203 | tabnew |
| 1204 | call assert_equal(v:null, luaeval('vim.t.tvar1')) |
| 1205 | call assert_equal(v:null, luaeval('vim.t.tvar2')) |
| 1206 | %bw! |
| 1207 | endfunc |
| 1208 | |
Yegappan Lakshmanan | 11328bc | 2021-08-06 21:34:38 +0200 | [diff] [blame] | 1209 | " Test for vim.version() |
| 1210 | func Test_lua_vim_version() |
| 1211 | lua << trim END |
| 1212 | vimver = vim.version() |
| 1213 | vimver_n = vimver.major * 100 + vimver.minor |
| 1214 | END |
| 1215 | call assert_equal(v:version, luaeval('vimver_n')) |
| 1216 | endfunc |
| 1217 | |
| 1218 | " Test for running multiple commands using vim.command() |
| 1219 | func Test_lua_multiple_commands() |
| 1220 | lua << trim END |
| 1221 | vim.command([[ |
| 1222 | let Var1 = [] |
| 1223 | for i in range(3) |
| 1224 | let Var1 += [#{name: 'x'}] |
| 1225 | endfor |
| 1226 | augroup Luagroup |
| 1227 | autocmd! |
| 1228 | autocmd User Luatest echo 'Hello' |
| 1229 | augroup END |
| 1230 | ]]) |
| 1231 | END |
| 1232 | call assert_equal([{'name': 'x'}, {'name': 'x'}, {'name': 'x'}], Var1) |
| 1233 | call assert_true(exists('#Luagroup')) |
| 1234 | call assert_true(exists('#Luagroup#User#Luatest')) |
| 1235 | augroup Luagroup |
| 1236 | autocmd! |
| 1237 | augroup END |
| 1238 | augroup! Luagroup |
| 1239 | endfunc |
| 1240 | |
Dominique Pelle | b6643d1 | 2022-03-20 11:46:01 +0000 | [diff] [blame] | 1241 | func Test_lua_debug() |
| 1242 | CheckRunVimInTerminal |
| 1243 | |
| 1244 | let buf = RunVimInTerminal('', {'rows': 10}) |
| 1245 | call term_sendkeys(buf, ":lua debug.debug()\n") |
| 1246 | call WaitForAssert({-> assert_equal('lua_debug> ', term_getline(buf, 10))}) |
| 1247 | |
| 1248 | call term_sendkeys(buf, "foo = 42\n") |
| 1249 | call WaitForAssert({-> assert_equal('lua_debug> foo = 42', term_getline(buf, 9))}) |
| 1250 | call WaitForAssert({-> assert_equal('lua_debug> ', term_getline(buf, 10))}) |
| 1251 | |
| 1252 | call term_sendkeys(buf, "print(foo)\n") |
| 1253 | call WaitForAssert({-> assert_equal('lua_debug> print(foo)', term_getline(buf, 8))}) |
| 1254 | call WaitForAssert({-> assert_equal('42', term_getline(buf, 9))}) |
| 1255 | call WaitForAssert({-> assert_equal('lua_debug> ', term_getline(buf, 10))}) |
| 1256 | |
Dominique Pelle | 81b573d | 2022-03-22 21:14:55 +0000 | [diff] [blame] | 1257 | call term_sendkeys(buf, "-\n") |
| 1258 | call WaitForAssert({-> assert_equal("(debug command):1: unexpected symbol near '-'", |
| 1259 | \ term_getline(buf, 9))}) |
| 1260 | call WaitForAssert({-> assert_equal('lua_debug> ', term_getline(buf, 10))}) |
| 1261 | |
Dominique Pelle | b6643d1 | 2022-03-20 11:46:01 +0000 | [diff] [blame] | 1262 | call term_sendkeys(buf, "cont\n") |
| 1263 | call WaitForAssert({-> assert_match(' All$', term_getline(buf, 10))}) |
| 1264 | |
Dominique Pelle | 81b573d | 2022-03-22 21:14:55 +0000 | [diff] [blame] | 1265 | " Entering an empty line also exits the debugger. |
| 1266 | call term_sendkeys(buf, ":lua debug.debug()\n") |
| 1267 | call WaitForAssert({-> assert_equal('lua_debug> ', term_getline(buf, 10))}) |
| 1268 | call term_sendkeys(buf, "\n") |
| 1269 | call WaitForAssert({-> assert_match(' All$', term_getline(buf, 10))}) |
| 1270 | |
Dominique Pelle | b6643d1 | 2022-03-20 11:46:01 +0000 | [diff] [blame] | 1271 | call StopVimInTerminal(buf) |
Dominique Pelle | b6643d1 | 2022-03-20 11:46:01 +0000 | [diff] [blame] | 1272 | endfunc |
| 1273 | |
Jesse Pavel | 8a35033 | 2023-08-13 22:05:45 -0400 | [diff] [blame] | 1274 | " Test for a crash when a Lua funcref is invoked with parameters from Vim |
| 1275 | func Test_lua_funcref_with_params() |
| 1276 | let Lua_funcref = luaeval('function(d) local s = "in Lua callback" end') |
| 1277 | call Lua_funcref({'a' : 'b'}) |
| 1278 | call assert_true(v:true) |
| 1279 | endfunc |
| 1280 | |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 1281 | " vim: shiftwidth=2 sts=2 expandtab |