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 | d58f03b | 2017-01-29 22:48:45 +0100 | [diff] [blame] | 13 | |
Bram Moolenaar | f65ed86 | 2021-04-03 14:13:33 +0200 | [diff] [blame] | 14 | " Depending on the lua version, the error messages are different. |
Bram Moolenaar | 125ed27 | 2021-04-07 20:11:12 +0200 | [diff] [blame] | 15 | 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] | 16 | let s:lua_53_or_later = 0 |
=?UTF-8?q?Jakub=20Kul=C3=ADk?= | 57ff2b7 | 2022-01-28 17:20:03 +0000 | [diff] [blame] | 17 | let s:lua_543 = 0 |
Bram Moolenaar | f65ed86 | 2021-04-03 14:13:33 +0200 | [diff] [blame] | 18 | if (s:major == 5 && s:minor >= 3) || s:major > 5 |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 19 | let s:lua_53_or_later = 1 |
=?UTF-8?q?Jakub=20Kul=C3=ADk?= | 57ff2b7 | 2022-01-28 17:20:03 +0000 | [diff] [blame] | 20 | if s:major == 5 && s:minor == 4 && s:patch == 3 |
| 21 | let s:lua_543 = 1 |
Bram Moolenaar | f65ed86 | 2021-04-03 14:13:33 +0200 | [diff] [blame] | 22 | endif |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 23 | endif |
| 24 | |
Bram Moolenaar | e165f63 | 2019-03-10 09:48:59 +0100 | [diff] [blame] | 25 | func TearDown() |
| 26 | " Run garbage collection after each test to exercise luaV_setref(). |
| 27 | call test_garbagecollect_now() |
| 28 | endfunc |
| 29 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 30 | " Check that switching to another buffer does not trigger ml_get error. |
zeertzjq | e99f068 | 2024-01-29 19:32:39 +0100 | [diff] [blame] | 31 | func Test_lua_luado_change_buffer() |
Bram Moolenaar | d58f03b | 2017-01-29 22:48:45 +0100 | [diff] [blame] | 32 | new |
zeertzjq | e99f068 | 2024-01-29 19:32:39 +0100 | [diff] [blame] | 33 | |
Bram Moolenaar | d58f03b | 2017-01-29 22:48:45 +0100 | [diff] [blame] | 34 | let wincount = winnr('$') |
| 35 | call setline(1, ['one', 'two', 'three']) |
| 36 | luado vim.command("new") |
| 37 | call assert_equal(wincount + 1, winnr('$')) |
zeertzjq | e99f068 | 2024-01-29 19:32:39 +0100 | [diff] [blame] | 38 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 39 | %bwipe! |
| 40 | endfunc |
| 41 | |
zeertzjq | e99f068 | 2024-01-29 19:32:39 +0100 | [diff] [blame] | 42 | " Check that :luado deleting lines does not trigger ml_get error. |
| 43 | func Test_lua_luado_delete_lines() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 44 | new |
zeertzjq | e99f068 | 2024-01-29 19:32:39 +0100 | [diff] [blame] | 45 | |
| 46 | call setline(1, ['one', 'two', 'three']) |
| 47 | luado vim.command("%d_") |
| 48 | call assert_equal([''], getline(1, '$')) |
| 49 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 50 | call setline(1, ['one', 'two', 'three']) |
| 51 | luado vim.command("1,2d_") |
| 52 | call assert_equal(['three'], getline(1, '$')) |
zeertzjq | e99f068 | 2024-01-29 19:32:39 +0100 | [diff] [blame] | 53 | |
| 54 | call setline(1, ['one', 'two', 'three']) |
| 55 | luado vim.command("2,3d_"); return "REPLACED" |
| 56 | call assert_equal(['REPLACED'], getline(1, '$')) |
| 57 | |
| 58 | call setline(1, ['one', 'two', 'three']) |
| 59 | 2,3luado vim.command("1,2d_"); return "REPLACED" |
| 60 | call assert_equal(['three'], getline(1, '$')) |
| 61 | |
Bram Moolenaar | d58f03b | 2017-01-29 22:48:45 +0100 | [diff] [blame] | 62 | bwipe! |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 63 | endfunc |
| 64 | |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 65 | func Test_lua_luado() |
| 66 | new |
| 67 | call setline(1, ['one', 'two']) |
| 68 | luado return(linenr) |
| 69 | call assert_equal(['1', '2'], getline(1, '$')) |
| 70 | close! |
| 71 | |
| 72 | " Error cases |
| 73 | call assert_fails('luado string.format()', |
| 74 | \ "[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] | 75 | if s:lua_543 |
Bram Moolenaar | f65ed86 | 2021-04-03 14:13:33 +0200 | [diff] [blame] | 76 | let msg = "[string \"vim chunk\"]:1: global 'func' is not callable (a nil value)" |
| 77 | elseif s:lua_53_or_later |
| 78 | let msg = "[string \"vim chunk\"]:1: attempt to call a nil value (global 'func')" |
| 79 | else |
| 80 | let msg = "[string \"vim chunk\"]:1: attempt to call global 'func' (a nil value)" |
| 81 | endif |
| 82 | call assert_fails('luado func()', msg) |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 83 | call assert_fails('luado error("failed")', "[string \"vim chunk\"]:1: failed") |
| 84 | endfunc |
| 85 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 86 | " Test vim.eval() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 87 | func Test_lua_eval() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 88 | " lua.eval with a number |
| 89 | lua v = vim.eval('123') |
| 90 | call assert_equal('number', luaeval('vim.type(v)')) |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 91 | call assert_equal(123, luaeval('v')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 92 | |
| 93 | " lua.eval with a string |
| 94 | lua v = vim.eval('"abc"') |
Bram Moolenaar | 02b3111 | 2019-08-31 22:16:38 +0200 | [diff] [blame] | 95 | call assert_equal('string', 'vim.type(v)'->luaeval()) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 96 | call assert_equal('abc', luaeval('v')) |
| 97 | |
| 98 | " lua.eval with a list |
| 99 | lua v = vim.eval("['a']") |
| 100 | call assert_equal('list', luaeval('vim.type(v)')) |
| 101 | call assert_equal(['a'], luaeval('v')) |
| 102 | |
| 103 | " lua.eval with a dict |
| 104 | lua v = vim.eval("{'a':'b'}") |
| 105 | call assert_equal('dict', luaeval('vim.type(v)')) |
| 106 | call assert_equal({'a':'b'}, luaeval('v')) |
| 107 | |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 108 | " lua.eval with a blob |
| 109 | lua v = vim.eval("0z00112233.deadbeef") |
| 110 | call assert_equal('blob', luaeval('vim.type(v)')) |
| 111 | call assert_equal(0z00112233.deadbeef, luaeval('v')) |
| 112 | |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 113 | " lua.eval with a float |
| 114 | lua v = vim.eval('3.14') |
| 115 | call assert_equal('number', luaeval('vim.type(v)')) |
| 116 | call assert_equal(3.14, luaeval('v')) |
| 117 | |
| 118 | " lua.eval with a bool |
| 119 | lua v = vim.eval('v:true') |
| 120 | call assert_equal('number', luaeval('vim.type(v)')) |
| 121 | call assert_equal(1, luaeval('v')) |
| 122 | lua v = vim.eval('v:false') |
| 123 | call assert_equal('number', luaeval('vim.type(v)')) |
| 124 | call assert_equal(0, luaeval('v')) |
| 125 | |
| 126 | " lua.eval with a null |
| 127 | lua v = vim.eval('v:null') |
| 128 | call assert_equal('nil', luaeval('vim.type(v)')) |
| 129 | call assert_equal(v:null, luaeval('v')) |
| 130 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 131 | call assert_fails('lua v = vim.eval(nil)', |
| 132 | \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got nil)") |
| 133 | call assert_fails('lua v = vim.eval(true)', |
| 134 | \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got boolean)") |
| 135 | call assert_fails('lua v = vim.eval({})', |
| 136 | \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got table)") |
| 137 | call assert_fails('lua v = vim.eval(print)', |
| 138 | \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got function)") |
| 139 | call assert_fails('lua v = vim.eval(vim.buffer())', |
| 140 | \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got userdata)") |
| 141 | |
| 142 | lua v = nil |
| 143 | endfunc |
| 144 | |
Bram Moolenaar | 86c3a21 | 2021-03-08 19:50:24 +0100 | [diff] [blame] | 145 | " Test luaeval() with lambda |
| 146 | func Test_luaeval_with_lambda() |
| 147 | lua function hello_luaeval_lambda(a, cb) return a .. cb() end |
| 148 | call assert_equal('helloworld', |
| 149 | \ luaeval('hello_luaeval_lambda(_A[1], _A[2])', |
| 150 | \ ['hello', {->'world'}])) |
| 151 | lua hello_luaeval_lambda = nil |
| 152 | endfunc |
| 153 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 154 | " Test vim.window() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 155 | func Test_lua_window() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 156 | e Xfoo2 |
| 157 | new Xfoo1 |
| 158 | |
| 159 | " Window 1 (top window) contains Xfoo1 |
| 160 | " Window 2 (bottom window) contains Xfoo2 |
| 161 | call assert_equal('Xfoo1', luaeval('vim.window(1):buffer().name')) |
| 162 | call assert_equal('Xfoo2', luaeval('vim.window(2):buffer().name')) |
| 163 | |
| 164 | " Window 3 does not exist so vim.window(3) should return nil |
| 165 | call assert_equal('nil', luaeval('tostring(vim.window(3))')) |
| 166 | |
=?UTF-8?q?Jakub=20Kul=C3=ADk?= | 57ff2b7 | 2022-01-28 17:20:03 +0000 | [diff] [blame] | 167 | if s:lua_543 |
Bram Moolenaar | f65ed86 | 2021-04-03 14:13:33 +0200 | [diff] [blame] | 168 | let msg = "[string \"luaeval\"]:1: field 'xyz' is not callable (a nil value)" |
| 169 | elseif s:lua_53_or_later |
| 170 | let msg = "[string \"luaeval\"]:1: attempt to call a nil value (field 'xyz')" |
| 171 | else |
| 172 | let msg = "[string \"luaeval\"]:1: attempt to call field 'xyz' (a nil value)" |
| 173 | endif |
| 174 | call assert_fails("let n = luaeval('vim.window().xyz()')", msg) |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 175 | call assert_fails('lua vim.window().xyz = 1', |
| 176 | \ "[string \"vim chunk\"]:1: invalid window property: `xyz'") |
| 177 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 178 | %bwipe! |
| 179 | endfunc |
| 180 | |
| 181 | " Test vim.window().height |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 182 | func Test_lua_window_height() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 183 | new |
| 184 | lua vim.window().height = 2 |
| 185 | call assert_equal(2, winheight(0)) |
| 186 | lua vim.window().height = vim.window().height + 1 |
| 187 | call assert_equal(3, winheight(0)) |
| 188 | bwipe! |
| 189 | endfunc |
| 190 | |
| 191 | " Test vim.window().width |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 192 | func Test_lua_window_width() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 193 | vert new |
| 194 | lua vim.window().width = 2 |
| 195 | call assert_equal(2, winwidth(0)) |
| 196 | lua vim.window().width = vim.window().width + 1 |
| 197 | call assert_equal(3, winwidth(0)) |
| 198 | bwipe! |
| 199 | endfunc |
| 200 | |
| 201 | " Test vim.window().line and vim.window.col |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 202 | func Test_lua_window_line_col() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 203 | new |
| 204 | call setline(1, ['line1', 'line2', 'line3']) |
| 205 | lua vim.window().line = 2 |
| 206 | lua vim.window().col = 4 |
| 207 | call assert_equal([0, 2, 4, 0], getpos('.')) |
| 208 | lua vim.window().line = vim.window().line + 1 |
| 209 | lua vim.window().col = vim.window().col - 1 |
| 210 | call assert_equal([0, 3, 3, 0], getpos('.')) |
| 211 | |
| 212 | call assert_fails('lua vim.window().line = 10', |
| 213 | \ '[string "vim chunk"]:1: line out of range') |
| 214 | bwipe! |
| 215 | endfunc |
| 216 | |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 217 | " Test vim.call |
| 218 | func Test_lua_call() |
| 219 | call assert_equal(has('lua'), luaeval('vim.call("has", "lua")')) |
| 220 | 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] | 221 | |
| 222 | " Error cases |
| 223 | 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] | 224 | \ s:lua_53_or_later |
| 225 | \ ? '[string "luaeval"]:1: Function called with too many arguments' |
| 226 | \ : 'Function called with too many arguments') |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 227 | lua co = coroutine.create(function () print("hi") end) |
| 228 | call assert_fails("call luaeval('vim.call(\"type\", co)')", |
Bram Moolenaar | b898a02 | 2020-07-12 18:33:53 +0200 | [diff] [blame] | 229 | \ s:lua_53_or_later |
| 230 | \ ? '[string "luaeval"]:1: lua: cannot convert value' |
| 231 | \ : 'lua: cannot convert value') |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 232 | lua co = nil |
Bram Moolenaar | b898a02 | 2020-07-12 18:33:53 +0200 | [diff] [blame] | 233 | call assert_fails("call luaeval('vim.call(\"abc\")')", |
| 234 | \ ['E117:', s:lua_53_or_later ? '\[string "luaeval"]:1: lua: call_vim_function failed' |
| 235 | \ : 'lua: call_vim_function failed']) |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 236 | endfunc |
| 237 | |
| 238 | " Test vim.fn.* |
| 239 | func Test_lua_fn() |
| 240 | call assert_equal(has('lua'), luaeval('vim.fn.has("lua")')) |
| 241 | call assert_equal(printf("Hello %s", "vim"), luaeval('vim.fn.printf("Hello %s", "vim")')) |
| 242 | endfunc |
| 243 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 244 | " Test setting the current window |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 245 | func Test_lua_window_set_current() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 246 | new Xfoo1 |
| 247 | lua w1 = vim.window() |
| 248 | new Xfoo2 |
| 249 | lua w2 = vim.window() |
| 250 | |
| 251 | call assert_equal('Xfoo2', bufname('%')) |
| 252 | lua w1() |
| 253 | call assert_equal('Xfoo1', bufname('%')) |
| 254 | lua w2() |
| 255 | call assert_equal('Xfoo2', bufname('%')) |
| 256 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 257 | lua w1, w2 = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 258 | %bwipe! |
| 259 | endfunc |
| 260 | |
| 261 | " Test vim.window().buffer |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 262 | func Test_lua_window_buffer() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 263 | new Xfoo1 |
| 264 | lua w1 = vim.window() |
| 265 | lua b1 = w1.buffer() |
| 266 | new Xfoo2 |
| 267 | lua w2 = vim.window() |
| 268 | lua b2 = w2.buffer() |
| 269 | |
| 270 | lua b1() |
| 271 | call assert_equal('Xfoo1', bufname('%')) |
| 272 | lua b2() |
| 273 | call assert_equal('Xfoo2', bufname('%')) |
| 274 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 275 | lua b1, b2, w1, w2 = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 276 | %bwipe! |
| 277 | endfunc |
| 278 | |
| 279 | " Test vim.window():previous() and vim.window():next() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 280 | func Test_lua_window_next_previous() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 281 | new Xfoo1 |
| 282 | new Xfoo2 |
| 283 | new Xfoo3 |
| 284 | wincmd j |
| 285 | |
| 286 | call assert_equal('Xfoo2', luaeval('vim.window().buffer().name')) |
| 287 | call assert_equal('Xfoo1', luaeval('vim.window():next():buffer().name')) |
| 288 | call assert_equal('Xfoo3', luaeval('vim.window():previous():buffer().name')) |
| 289 | |
| 290 | %bwipe! |
| 291 | endfunc |
| 292 | |
| 293 | " Test vim.window():isvalid() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 294 | func Test_lua_window_isvalid() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 295 | new Xfoo |
| 296 | lua w = vim.window() |
| 297 | call assert_true(luaeval('w:isvalid()')) |
| 298 | |
| 299 | " FIXME: how to test the case when isvalid() returns v:false? |
| 300 | " isvalid() gives errors when the window is deleted. Is it a bug? |
| 301 | |
| 302 | lua w = nil |
| 303 | bwipe! |
| 304 | endfunc |
| 305 | |
| 306 | " Test vim.buffer() with and without argument |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 307 | func Test_lua_buffer() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 308 | new Xfoo1 |
| 309 | let bn1 = bufnr('%') |
| 310 | new Xfoo2 |
| 311 | let bn2 = bufnr('%') |
| 312 | |
| 313 | " Test vim.buffer() without argument. |
| 314 | call assert_equal('Xfoo2', luaeval("vim.buffer().name")) |
| 315 | |
| 316 | " Test vim.buffer() with string argument. |
| 317 | call assert_equal('Xfoo1', luaeval("vim.buffer('Xfoo1').name")) |
| 318 | call assert_equal('Xfoo2', luaeval("vim.buffer('Xfoo2').name")) |
| 319 | |
| 320 | " Test vim.buffer() with integer argument. |
| 321 | call assert_equal('Xfoo1', luaeval("vim.buffer(" . bn1 . ").name")) |
| 322 | call assert_equal('Xfoo2', luaeval("vim.buffer(" . bn2 . ").name")) |
| 323 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 324 | lua bn1, bn2 = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 325 | %bwipe! |
| 326 | endfunc |
| 327 | |
| 328 | " Test vim.buffer().name and vim.buffer().fname |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 329 | func Test_lua_buffer_name() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 330 | new |
Bram Moolenaar | fe08df4 | 2018-07-07 23:07:41 +0200 | [diff] [blame] | 331 | call assert_equal('', luaeval('vim.buffer().name')) |
| 332 | call assert_equal('', luaeval('vim.buffer().fname')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 333 | bwipe! |
| 334 | |
| 335 | new Xfoo |
| 336 | call assert_equal('Xfoo', luaeval('vim.buffer().name')) |
| 337 | call assert_equal(expand('%:p'), luaeval('vim.buffer().fname')) |
| 338 | bwipe! |
| 339 | endfunc |
| 340 | |
| 341 | " Test vim.buffer().number |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 342 | func Test_lua_buffer_number() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 343 | " All numbers in Lua are floating points number (no integers). |
| 344 | call assert_equal(bufnr('%'), float2nr(luaeval('vim.buffer().number'))) |
| 345 | endfunc |
| 346 | |
| 347 | " Test inserting lines in buffer. |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 348 | func Test_lua_buffer_insert() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 349 | new |
| 350 | lua vim.buffer()[1] = '3' |
| 351 | lua vim.buffer():insert('1', 0) |
| 352 | lua vim.buffer():insert('2', 1) |
| 353 | lua vim.buffer():insert('4', 10) |
| 354 | |
| 355 | call assert_equal(['1', '2', '3', '4'], getline(1, '$')) |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 356 | call assert_equal('4', luaeval('vim.buffer()[4]')) |
| 357 | call assert_equal(v:null, luaeval('vim.buffer()[5]')) |
| 358 | 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] | 359 | if s:lua_543 |
Bram Moolenaar | f65ed86 | 2021-04-03 14:13:33 +0200 | [diff] [blame] | 360 | let msg = "[string \"vim chunk\"]:1: method 'xyz' is not callable (a nil value)" |
| 361 | elseif s:lua_53_or_later |
| 362 | let msg = "[string \"vim chunk\"]:1: attempt to call a nil value (method 'xyz')" |
| 363 | else |
| 364 | let msg = "[string \"vim chunk\"]:1: attempt to call method 'xyz' (a nil value)" |
| 365 | endif |
| 366 | call assert_fails('lua vim.buffer():xyz()', msg) |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 367 | call assert_fails('lua vim.buffer()[1] = {}', |
| 368 | \ '[string "vim chunk"]:1: wrong argument to change') |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 369 | bwipe! |
| 370 | endfunc |
| 371 | |
| 372 | " Test deleting line in buffer |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 373 | func Test_lua_buffer_delete() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 374 | new |
| 375 | call setline(1, ['1', '2', '3']) |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 376 | call cursor(3, 1) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 377 | lua vim.buffer()[2] = nil |
| 378 | call assert_equal(['1', '3'], getline(1, '$')) |
| 379 | |
| 380 | call assert_fails('lua vim.buffer()[3] = nil', |
| 381 | \ '[string "vim chunk"]:1: invalid line number') |
| 382 | bwipe! |
| 383 | endfunc |
| 384 | |
| 385 | " Test #vim.buffer() i.e. number of lines in buffer |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 386 | func Test_lua_buffer_number_lines() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 387 | new |
| 388 | call setline(1, ['a', 'b', 'c']) |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 389 | call assert_equal(3, luaeval('#vim.buffer()')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 390 | bwipe! |
| 391 | endfunc |
| 392 | |
| 393 | " Test vim.buffer():next() and vim.buffer():previous() |
| 394 | " Note that these functions get the next or previous buffers |
| 395 | " but do not switch buffer. |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 396 | func Test_lua_buffer_next_previous() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 397 | new Xfoo1 |
| 398 | new Xfoo2 |
| 399 | new Xfoo3 |
| 400 | b Xfoo2 |
| 401 | |
| 402 | lua bn = vim.buffer():next() |
| 403 | lua bp = vim.buffer():previous() |
| 404 | |
| 405 | call assert_equal('Xfoo2', luaeval('vim.buffer().name')) |
| 406 | call assert_equal('Xfoo1', luaeval('bp.name')) |
| 407 | call assert_equal('Xfoo3', luaeval('bn.name')) |
| 408 | |
| 409 | call assert_equal('Xfoo2', bufname('%')) |
| 410 | |
| 411 | lua bn() |
| 412 | call assert_equal('Xfoo3', luaeval('vim.buffer().name')) |
| 413 | call assert_equal('Xfoo3', bufname('%')) |
| 414 | |
| 415 | lua bp() |
| 416 | call assert_equal('Xfoo1', luaeval('vim.buffer().name')) |
| 417 | call assert_equal('Xfoo1', bufname('%')) |
| 418 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 419 | lua bn, bp = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 420 | %bwipe! |
| 421 | endfunc |
| 422 | |
| 423 | " Test vim.buffer():isvalid() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 424 | func Test_lua_buffer_isvalid() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 425 | new Xfoo |
| 426 | lua b = vim.buffer() |
| 427 | call assert_true(luaeval('b:isvalid()')) |
| 428 | |
| 429 | " FIXME: how to test the case when isvalid() returns v:false? |
| 430 | " isvalid() gives errors when the buffer is wiped. Is it a bug? |
| 431 | |
| 432 | lua b = nil |
| 433 | bwipe! |
| 434 | endfunc |
| 435 | |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 436 | func Test_lua_list() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 437 | call assert_equal([], luaeval('vim.list()')) |
| 438 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 439 | let l = [] |
| 440 | lua l = vim.eval('l') |
| 441 | lua l:add(123) |
| 442 | lua l:add('abc') |
| 443 | lua l:add(true) |
| 444 | lua l:add(false) |
Bram Moolenaar | 9067cd6 | 2019-01-01 00:41:54 +0100 | [diff] [blame] | 445 | lua l:add(nil) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 446 | lua l:add(vim.eval("[1, 2, 3]")) |
| 447 | lua l:add(vim.eval("{'a':1, 'b':2, 'c':3}")) |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 448 | call assert_equal([123, 'abc', v:true, v:false, v:null, [1, 2, 3], {'a': 1, 'b': 2, 'c': 3}], l) |
| 449 | call assert_equal(7, luaeval('#l')) |
Bram Moolenaar | a8a60d0 | 2018-07-02 22:54:36 +0200 | [diff] [blame] | 450 | call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 451 | |
Bram Moolenaar | bd84617 | 2020-06-27 12:32:57 +0200 | [diff] [blame] | 452 | lua l[1] = 124 |
| 453 | lua l[6] = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 454 | lua l:insert('first') |
| 455 | lua l:insert('xx', 3) |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 456 | call assert_fails('lua l:insert("xx", -20)', |
| 457 | \ '[string "vim chunk"]:1: invalid position') |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 458 | 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] | 459 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 460 | lockvar 1 l |
| 461 | 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] | 462 | call assert_fails('lua l:insert(2)', '[string "vim chunk"]:1: list is locked') |
| 463 | call assert_fails('lua l[9] = 1', '[string "vim chunk"]:1: list is locked') |
| 464 | |
| 465 | unlockvar l |
| 466 | let l = [1, 2] |
| 467 | lua ll = vim.eval('l') |
| 468 | let x = luaeval("ll[3]") |
| 469 | call assert_equal(v:null, x) |
=?UTF-8?q?Jakub=20Kul=C3=ADk?= | 57ff2b7 | 2022-01-28 17:20:03 +0000 | [diff] [blame] | 470 | if s:lua_543 |
Bram Moolenaar | f65ed86 | 2021-04-03 14:13:33 +0200 | [diff] [blame] | 471 | let msg = "[string \"luaeval\"]:1: method 'xyz' is not callable (a nil value)" |
| 472 | elseif s:lua_53_or_later |
| 473 | let msg = "[string \"luaeval\"]:1: attempt to call a nil value (method 'xyz')" |
| 474 | else |
| 475 | let msg = "[string \"luaeval\"]:1: attempt to call method 'xyz' (a nil value)" |
| 476 | endif |
| 477 | call assert_fails('let x = luaeval("ll:xyz(3)")', msg) |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 478 | let y = luaeval("ll[{}]") |
| 479 | call assert_equal(v:null, y) |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 480 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 481 | lua l = nil |
| 482 | endfunc |
| 483 | |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 484 | func Test_lua_list_table() |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 485 | " See :help lua-vim |
| 486 | " Non-numeric keys should not be used to initialize the list |
| 487 | " so say = 'hi' should be ignored. |
| 488 | lua t = {3.14, 'hello', false, true, say = 'hi'} |
| 489 | call assert_equal([3.14, 'hello', v:false, v:true], luaeval('vim.list(t)')) |
| 490 | lua t = nil |
| 491 | |
| 492 | call assert_fails('lua vim.list(1)', '[string "vim chunk"]:1: table expected, got number') |
| 493 | call assert_fails('lua vim.list("x")', '[string "vim chunk"]:1: table expected, got string') |
| 494 | call assert_fails('lua vim.list(print)', '[string "vim chunk"]:1: table expected, got function') |
| 495 | call assert_fails('lua vim.list(true)', '[string "vim chunk"]:1: table expected, got boolean') |
| 496 | endfunc |
| 497 | |
Bram Moolenaar | a1f9f86 | 2020-06-28 22:41:26 +0200 | [diff] [blame] | 498 | func Test_lua_list_table_insert_remove() |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 499 | if !s:lua_53_or_later |
Bram Moolenaar | a1f9f86 | 2020-06-28 22:41:26 +0200 | [diff] [blame] | 500 | throw 'Skipped: Lua version < 5.3' |
| 501 | endif |
| 502 | |
Bram Moolenaar | f65ed86 | 2021-04-03 14:13:33 +0200 | [diff] [blame] | 503 | let l = [1, 2] |
Bram Moolenaar | a1f9f86 | 2020-06-28 22:41:26 +0200 | [diff] [blame] | 504 | lua t = vim.eval('l') |
| 505 | lua table.insert(t, 10) |
| 506 | lua t[#t + 1] = 20 |
| 507 | lua table.insert(t, 2, 30) |
| 508 | call assert_equal(l, [1, 30, 2, 10, 20]) |
| 509 | lua table.remove(t, 2) |
| 510 | call assert_equal(l, [1, 2, 10, 20]) |
| 511 | lua t[3] = nil |
| 512 | call assert_equal(l, [1, 2, 20]) |
| 513 | lua removed_value = table.remove(t, 3) |
| 514 | call assert_equal(luaeval('removed_value'), 20) |
| 515 | lua t = nil |
| 516 | lua removed_value = nil |
| 517 | unlet l |
| 518 | endfunc |
| 519 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 520 | " Test l() i.e. iterator on list |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 521 | func Test_lua_list_iter() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 522 | lua l = vim.list():add('foo'):add('bar') |
| 523 | lua str = '' |
| 524 | lua for v in l() do str = str .. v end |
| 525 | call assert_equal('foobar', luaeval('str')) |
| 526 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 527 | lua str, l = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 528 | endfunc |
| 529 | |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 530 | func Test_lua_recursive_list() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 531 | lua l = vim.list():add(1):add(2) |
| 532 | lua l = l:add(l) |
| 533 | |
Bram Moolenaar | bd84617 | 2020-06-27 12:32:57 +0200 | [diff] [blame] | 534 | call assert_equal(1, luaeval('l[1]')) |
| 535 | call assert_equal(2, luaeval('l[2]')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 536 | |
Bram Moolenaar | bd84617 | 2020-06-27 12:32:57 +0200 | [diff] [blame] | 537 | call assert_equal(1, luaeval('l[3][1]')) |
| 538 | call assert_equal(2, luaeval('l[3][2]')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 539 | |
Bram Moolenaar | bd84617 | 2020-06-27 12:32:57 +0200 | [diff] [blame] | 540 | call assert_equal(1, luaeval('l[3][3][1]')) |
| 541 | call assert_equal(2, luaeval('l[3][3][2]')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 542 | |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 543 | call assert_equal('[1, 2, [...]]', string(luaeval('l'))) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 544 | |
Bram Moolenaar | a8a60d0 | 2018-07-02 22:54:36 +0200 | [diff] [blame] | 545 | call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)')) |
Bram Moolenaar | bd84617 | 2020-06-27 12:32:57 +0200 | [diff] [blame] | 546 | call assert_equal(luaeval('tostring(l)'), luaeval('tostring(l[3])')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 547 | |
Bram Moolenaar | bd84617 | 2020-06-27 12:32:57 +0200 | [diff] [blame] | 548 | call assert_equal(luaeval('l'), luaeval('l[3]')) |
| 549 | call assert_equal(luaeval('l'), luaeval('l[3][3]')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 550 | |
| 551 | lua l = nil |
| 552 | endfunc |
| 553 | |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 554 | func Test_lua_dict() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 555 | call assert_equal({}, luaeval('vim.dict()')) |
| 556 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 557 | let d = {} |
| 558 | lua d = vim.eval('d') |
| 559 | lua d[0] = 123 |
| 560 | lua d[1] = "abc" |
| 561 | lua d[2] = true |
| 562 | lua d[3] = false |
| 563 | lua d[4] = vim.eval("[1, 2, 3]") |
| 564 | lua d[5] = vim.eval("{'a':1, 'b':2, 'c':3}") |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 565 | 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) |
| 566 | call assert_equal(6, luaeval('#d')) |
Bram Moolenaar | a8a60d0 | 2018-07-02 22:54:36 +0200 | [diff] [blame] | 567 | call assert_match('^dict: \%(0x\)\?\x\+$', luaeval('tostring(d)')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 568 | |
| 569 | call assert_equal('abc', luaeval('d[1]')) |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 570 | call assert_equal(v:null, luaeval('d[22]')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 571 | |
| 572 | lua d[0] = 124 |
| 573 | lua d[4] = nil |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 574 | 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] | 575 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 576 | lockvar 1 d |
| 577 | 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] | 578 | unlockvar d |
| 579 | |
| 580 | " Error case |
| 581 | lua d = {} |
| 582 | lua d[''] = 10 |
| 583 | call assert_fails("let t = luaeval('vim.dict(d)')", |
Bram Moolenaar | b898a02 | 2020-07-12 18:33:53 +0200 | [diff] [blame] | 584 | \ s:lua_53_or_later |
| 585 | \ ? '[string "luaeval"]:1: table has empty key' |
| 586 | \ : 'table has empty key') |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 587 | let d = {} |
| 588 | lua x = vim.eval('d') |
| 589 | call assert_fails("lua x[''] = 10", '[string "vim chunk"]:1: empty key') |
| 590 | lua x['a'] = nil |
| 591 | call assert_equal({}, d) |
| 592 | |
| 593 | " cannot assign funcrefs in the global scope |
| 594 | lua x = vim.eval('g:') |
| 595 | call assert_fails("lua x['min'] = vim.funcref('max')", |
| 596 | \ '[string "vim chunk"]:1: cannot assign funcref to builtin scope') |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 597 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 598 | lua d = nil |
| 599 | endfunc |
| 600 | |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 601 | func Test_lua_dict_table() |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 602 | lua t = {key1 = 'x', key2 = 3.14, key3 = true, key4 = false} |
| 603 | call assert_equal({'key1': 'x', 'key2': 3.14, 'key3': v:true, 'key4': v:false}, |
| 604 | \ luaeval('vim.dict(t)')) |
| 605 | |
| 606 | " Same example as in :help lua-vim. |
| 607 | lua t = {math.pi, false, say = 'hi'} |
| 608 | " FIXME: commented out as it currently does not work as documented: |
| 609 | " Expected {'say': 'hi'} |
| 610 | " but got {'1': 3.141593, '2': v:false, 'say': 'hi'} |
| 611 | " Is the documentation or the code wrong? |
| 612 | "call assert_equal({'say' : 'hi'}, luaeval('vim.dict(t)')) |
| 613 | lua t = nil |
| 614 | |
| 615 | call assert_fails('lua vim.dict(1)', '[string "vim chunk"]:1: table expected, got number') |
| 616 | call assert_fails('lua vim.dict("x")', '[string "vim chunk"]:1: table expected, got string') |
| 617 | call assert_fails('lua vim.dict(print)', '[string "vim chunk"]:1: table expected, got function') |
| 618 | call assert_fails('lua vim.dict(true)', '[string "vim chunk"]:1: table expected, got boolean') |
| 619 | endfunc |
| 620 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 621 | " Test d() i.e. iterator on dictionary |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 622 | func Test_lua_dict_iter() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 623 | let d = {'a': 1, 'b':2} |
| 624 | lua d = vim.eval('d') |
| 625 | lua str = '' |
| 626 | lua for k,v in d() do str = str .. k ..':' .. v .. ',' end |
| 627 | call assert_equal('a:1,b:2,', luaeval('str')) |
| 628 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 629 | lua str, d = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 630 | endfunc |
| 631 | |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 632 | func Test_lua_blob() |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 633 | call assert_equal(0z, luaeval('vim.blob("")')) |
| 634 | call assert_equal(0z31326162, luaeval('vim.blob("12ab")')) |
Christian Brabandt | 6efb198 | 2023-08-10 05:44:25 +0200 | [diff] [blame] | 635 | call assert_equal(0z00010203, luaeval('vim.blob("\000\001\002\003")')) |
| 636 | call assert_equal(0z8081FEFF, luaeval('vim.blob("\128\129\254\255")')) |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 637 | |
Christian Brabandt | 6efb198 | 2023-08-10 05:44:25 +0200 | [diff] [blame] | 638 | lua b = vim.blob("\000\000\000\000") |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 639 | call assert_equal(0z00000000, luaeval('b')) |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 640 | call assert_equal(4, luaeval('#b')) |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 641 | lua b[0], b[1], b[2], b[3] = 1, 32, 256, 0xff |
| 642 | call assert_equal(0z012000ff, luaeval('b')) |
| 643 | lua b[4] = string.byte("z", 1) |
| 644 | call assert_equal(0z012000ff.7a, luaeval('b')) |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 645 | call assert_equal(5, luaeval('#b')) |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 646 | call assert_fails('lua b[#b+1] = 0x80', '[string "vim chunk"]:1: index out of range') |
| 647 | lua b:add("12ab") |
| 648 | call assert_equal(0z012000ff.7a313261.62, luaeval('b')) |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 649 | call assert_equal(9, luaeval('#b')) |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 650 | call assert_fails('lua b:add(nil)', '[string "vim chunk"]:1: string expected, got nil') |
| 651 | call assert_fails('lua b:add(true)', '[string "vim chunk"]:1: string expected, got boolean') |
| 652 | call assert_fails('lua b:add({})', '[string "vim chunk"]:1: string expected, got table') |
| 653 | lua b = nil |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 654 | |
| 655 | let b = 0z0102 |
| 656 | lua lb = vim.eval('b') |
| 657 | let n = luaeval('lb[1]') |
| 658 | call assert_equal(2, n) |
| 659 | let n = luaeval('lb[6]') |
| 660 | call assert_equal(v:null, n) |
=?UTF-8?q?Jakub=20Kul=C3=ADk?= | 57ff2b7 | 2022-01-28 17:20:03 +0000 | [diff] [blame] | 661 | if s:lua_543 |
Bram Moolenaar | f65ed86 | 2021-04-03 14:13:33 +0200 | [diff] [blame] | 662 | let msg = "[string \"luaeval\"]:1: method 'xyz' is not callable (a nil value)" |
| 663 | elseif s:lua_53_or_later |
| 664 | let msg = "[string \"luaeval\"]:1: attempt to call a nil value (method 'xyz')" |
| 665 | else |
| 666 | let msg = "[string \"luaeval\"]:1: attempt to call method 'xyz' (a nil value)" |
| 667 | endif |
| 668 | call assert_fails('let x = luaeval("lb:xyz(3)")', msg) |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 669 | let y = luaeval("lb[{}]") |
| 670 | call assert_equal(v:null, y) |
| 671 | |
| 672 | lockvar b |
| 673 | call assert_fails('lua lb[1] = 2', '[string "vim chunk"]:1: blob is locked') |
| 674 | call assert_fails('lua lb:add("12")', '[string "vim chunk"]:1: blob is locked') |
| 675 | |
| 676 | " Error cases |
| 677 | lua t = {} |
| 678 | call assert_fails('lua b = vim.blob(t)', |
| 679 | \ '[string "vim chunk"]:1: string expected, got table') |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 680 | endfunc |
| 681 | |
Bram Moolenaar | 7d149f8 | 2022-06-17 19:23:34 +0100 | [diff] [blame] | 682 | def Vim9Test(Callback: func()) |
| 683 | Callback() |
| 684 | enddef |
| 685 | |
| 686 | func Test_call_lua_func_from_vim9_func() |
| 687 | " this only tests that Vim doesn't crash |
| 688 | lua << EOF |
| 689 | vim.fn.Vim9Test(function () print('Hello') end) |
| 690 | EOF |
| 691 | endfunc |
| 692 | |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 693 | func Test_lua_funcref() |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 694 | function I(x) |
| 695 | return a:x |
| 696 | endfunction |
| 697 | let R = function('I') |
| 698 | lua i1 = vim.funcref"I" |
| 699 | lua i2 = vim.eval"R" |
| 700 | lua msg = "funcref|test|" .. (#i2(i1) == #i1(i2) and "OK" or "FAIL") |
| 701 | lua msg = vim.funcref"tr"(msg, "|", " ") |
| 702 | call assert_equal("funcref test OK", luaeval('msg')) |
| 703 | |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 704 | " Error cases |
| 705 | call assert_fails('lua f1 = vim.funcref("")', |
| 706 | \ '[string "vim chunk"]:1: invalid function name: ') |
| 707 | call assert_fails('lua f1 = vim.funcref("10")', |
| 708 | \ '[string "vim chunk"]:1: invalid function name: 10') |
| 709 | let fname = test_null_string() |
| 710 | call assert_fails('lua f1 = vim.funcref(fname)', |
| 711 | \ "[string \"vim chunk\"]:1: bad argument #1 to 'funcref' (string expected, got nil)") |
| 712 | call assert_fails('lua vim.funcref("abc")()', |
Bram Moolenaar | ecdd14a | 2020-07-11 22:49:59 +0200 | [diff] [blame] | 713 | \ ['E117:', '\[string "vim chunk"]:1: cannot call funcref']) |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 714 | |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 715 | " dict funcref |
| 716 | function Mylen() dict |
| 717 | return len(self.data) |
| 718 | endfunction |
| 719 | let l = [0, 1, 2, 3] |
| 720 | let mydict = {'data': l} |
| 721 | lua d = vim.eval"mydict" |
| 722 | lua d.len = vim.funcref"Mylen" -- assign d as 'self' |
| 723 | lua res = (d.len() == vim.funcref"len"(vim.eval"l")) and "OK" or "FAIL" |
| 724 | call assert_equal("OK", luaeval('res')) |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 725 | call assert_equal(function('Mylen', {'data': l, 'len': function('Mylen')}), mydict.len) |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 726 | |
| 727 | lua i1, i2, msg, d, res = nil |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 728 | endfunc |
| 729 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 730 | " Test vim.type() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 731 | func Test_lua_type() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 732 | " The following values are identical to Lua's type function. |
| 733 | call assert_equal('string', luaeval('vim.type("foo")')) |
| 734 | call assert_equal('number', luaeval('vim.type(1)')) |
| 735 | call assert_equal('number', luaeval('vim.type(1.2)')) |
| 736 | call assert_equal('function', luaeval('vim.type(print)')) |
| 737 | call assert_equal('table', luaeval('vim.type({})')) |
| 738 | call assert_equal('boolean', luaeval('vim.type(true)')) |
| 739 | call assert_equal('boolean', luaeval('vim.type(false)')) |
| 740 | call assert_equal('nil', luaeval('vim.type(nil)')) |
| 741 | |
| 742 | " The following values are specific to Vim. |
| 743 | call assert_equal('window', luaeval('vim.type(vim.window())')) |
| 744 | call assert_equal('buffer', luaeval('vim.type(vim.buffer())')) |
| 745 | call assert_equal('list', luaeval('vim.type(vim.list())')) |
| 746 | call assert_equal('dict', luaeval('vim.type(vim.dict())')) |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 747 | call assert_equal('funcref', luaeval('vim.type(vim.funcref("Test_type"))')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 748 | endfunc |
| 749 | |
| 750 | " Test vim.open() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 751 | func Test_lua_open() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 752 | call assert_notmatch('XOpen', execute('ls')) |
| 753 | |
| 754 | " Open a buffer XOpen1, but do not jump to it. |
| 755 | lua b = vim.open('XOpen1') |
| 756 | call assert_equal('XOpen1', luaeval('b.name')) |
| 757 | call assert_equal('', bufname('%')) |
| 758 | |
| 759 | call assert_match('XOpen1', execute('ls')) |
| 760 | call assert_notequal('XOpen2', bufname('%')) |
| 761 | |
| 762 | " Open a buffer XOpen2 and jump to it. |
| 763 | lua b = vim.open('XOpen2')() |
| 764 | call assert_equal('XOpen2', luaeval('b.name')) |
| 765 | call assert_equal('XOpen2', bufname('%')) |
| 766 | |
| 767 | lua b = nil |
| 768 | %bwipe! |
| 769 | endfunc |
| 770 | |
Bram Moolenaar | 788fbb4 | 2020-05-31 14:08:12 +0200 | [diff] [blame] | 771 | func Test_update_package_paths() |
| 772 | set runtimepath+=./testluaplugin |
| 773 | call assert_equal("hello from lua", luaeval("require('testluaplugin').hello()")) |
| 774 | endfunc |
| 775 | |
Bram Moolenaar | 801ab06 | 2020-06-25 19:27:56 +0200 | [diff] [blame] | 776 | func Vim_func_call_lua_callback(Concat, Cb) |
| 777 | let l:message = a:Concat("hello", "vim") |
| 778 | call a:Cb(l:message) |
| 779 | endfunc |
| 780 | |
| 781 | func Test_pass_lua_callback_to_vim_from_lua() |
| 782 | lua pass_lua_callback_to_vim_from_lua_result = "" |
| 783 | call assert_equal("", luaeval("pass_lua_callback_to_vim_from_lua_result")) |
| 784 | lua <<EOF |
| 785 | vim.funcref('Vim_func_call_lua_callback')( |
| 786 | function(greeting, message) |
| 787 | return greeting .. " " .. message |
| 788 | end, |
| 789 | function(message) |
| 790 | pass_lua_callback_to_vim_from_lua_result = message |
| 791 | end) |
| 792 | EOF |
| 793 | call assert_equal("hello vim", luaeval("pass_lua_callback_to_vim_from_lua_result")) |
| 794 | endfunc |
| 795 | |
| 796 | func Vim_func_call_metatable_lua_callback(Greet) |
| 797 | return a:Greet("world") |
| 798 | endfunc |
| 799 | |
| 800 | func Test_pass_lua_metatable_callback_to_vim_from_lua() |
| 801 | let result = luaeval("vim.funcref('Vim_func_call_metatable_lua_callback')(setmetatable({ space = ' '}, { __call = function(tbl, msg) return 'hello' .. tbl.space .. msg end }) )") |
| 802 | call assert_equal("hello world", result) |
| 803 | endfunc |
| 804 | |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 805 | " Test vim.line() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 806 | func Test_lua_line() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 807 | new |
| 808 | call setline(1, ['first line', 'second line']) |
| 809 | 1 |
| 810 | call assert_equal('first line', luaeval('vim.line()')) |
| 811 | 2 |
| 812 | call assert_equal('second line', luaeval('vim.line()')) |
| 813 | bwipe! |
| 814 | endfunc |
| 815 | |
| 816 | " Test vim.beep() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 817 | func Test_lua_beep() |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 818 | call assert_beeps('lua vim.beep()') |
| 819 | endfunc |
| 820 | |
| 821 | " Test errors in luaeval() |
| 822 | func Test_luaeval_error() |
| 823 | " Compile error |
| 824 | call assert_fails("call luaeval('-nil')", |
| 825 | \ '[string "luaeval"]:1: attempt to perform arithmetic on a nil value') |
| 826 | call assert_fails("call luaeval(']')", |
| 827 | \ "[string \"luaeval\"]:1: unexpected symbol near ']'") |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 828 | lua co = coroutine.create(function () print("hi") end) |
| 829 | call assert_fails('let i = luaeval("co")', 'luaeval: cannot convert value') |
| 830 | lua co = nil |
| 831 | call assert_fails('let m = luaeval("{}")', 'luaeval: cannot convert value') |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 832 | endfunc |
| 833 | |
| 834 | " Test :luafile foo.lua |
| 835 | func Test_luafile() |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 836 | call writefile(["str = 'hello'", "num = 123" ], 'Xlua_file', 'D') |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 837 | call setfperm('Xlua_file', 'r-xr-xr-x') |
| 838 | |
| 839 | luafile Xlua_file |
| 840 | call assert_equal('hello', luaeval('str')) |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 841 | call assert_equal(123, luaeval('num')) |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 842 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 843 | lua str, num = nil |
Bram Moolenaar | 4ff4814 | 2018-06-30 21:50:25 +0200 | [diff] [blame] | 844 | endfunc |
| 845 | |
| 846 | " Test :luafile % |
| 847 | func Test_luafile_percent() |
| 848 | new Xlua_file |
| 849 | append |
| 850 | str, num = 'foo', 321.0 |
| 851 | print(string.format('str=%s, num=%d', str, num)) |
| 852 | . |
| 853 | w! |
| 854 | luafile % |
| 855 | let msg = split(execute('message'), "\n")[-1] |
| 856 | call assert_equal('str=foo, num=321', msg) |
| 857 | |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 858 | lua str, num = nil |
| 859 | call delete('Xlua_file') |
| 860 | bwipe! |
| 861 | endfunc |
| 862 | |
| 863 | " Test :luafile with syntax error |
| 864 | func Test_luafile_error() |
| 865 | new Xlua_file |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 866 | call writefile(['nil = 0' ], 'Xlua_file', 'D') |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 867 | call setfperm('Xlua_file', 'r-xr-xr-x') |
| 868 | |
| 869 | call assert_fails('luafile Xlua_file', "Xlua_file:1: unexpected symbol near 'nil'") |
| 870 | |
Bram Moolenaar | d58f03b | 2017-01-29 22:48:45 +0100 | [diff] [blame] | 871 | bwipe! |
| 872 | endfunc |
Bram Moolenaar | 5390144 | 2018-07-25 22:02:36 +0200 | [diff] [blame] | 873 | |
Bram Moolenaar | 78e006b | 2021-07-28 15:07:01 +0200 | [diff] [blame] | 874 | " Test :luafile printing a long string |
| 875 | func Test_luafile_print() |
| 876 | new Xlua_file |
| 877 | let lines =<< trim END |
| 878 | local data = '' |
| 879 | for i = 1, 130 do |
| 880 | data = data .. 'xxxxx asd as as dad sad sad xz cxz czxcxzczxc ad ad asd asd asd asd asd' |
| 881 | end |
| 882 | print(data) |
| 883 | END |
| 884 | call setline(1, lines) |
| 885 | w |
| 886 | luafile % |
| 887 | |
| 888 | call delete('Xlua_file') |
| 889 | bwipe! |
| 890 | endfunc |
| 891 | |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 892 | " Test for dealing with strings containing newlines and null character |
| 893 | func Test_lua_string_with_newline() |
Bram Moolenaar | 2a4bd00 | 2021-07-28 21:48:59 +0200 | [diff] [blame] | 894 | let x = execute('lua print("Hello\nWorld", 2)') |
| 895 | call assert_equal("\nHello\nWorld 2", x) |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 896 | new |
| 897 | lua k = vim.buffer(vim.eval('bufnr()')) |
| 898 | lua k:insert("Hello\0World", 0) |
| 899 | call assert_equal(["Hello\nWorld", ''], getline(1, '$')) |
| 900 | close! |
| 901 | endfunc |
| 902 | |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 903 | func Test_lua_set_cursor() |
Bram Moolenaar | 5390144 | 2018-07-25 22:02:36 +0200 | [diff] [blame] | 904 | " Check that setting the cursor position works. |
| 905 | new |
| 906 | call setline(1, ['first line', 'second line']) |
| 907 | normal gg |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 908 | lua << trim EOF |
| 909 | w = vim.window() |
| 910 | w.line = 1 |
| 911 | w.col = 5 |
| 912 | EOF |
Bram Moolenaar | 5390144 | 2018-07-25 22:02:36 +0200 | [diff] [blame] | 913 | call assert_equal([1, 5], [line('.'), col('.')]) |
| 914 | |
| 915 | " Check that movement after setting cursor position keeps current column. |
| 916 | normal j |
| 917 | call assert_equal([2, 5], [line('.'), col('.')]) |
| 918 | endfunc |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 919 | |
| 920 | " Test for various heredoc syntax |
| 921 | func Test_lua_heredoc() |
| 922 | lua << END |
| 923 | vim.command('let s = "A"') |
| 924 | END |
| 925 | lua << |
| 926 | vim.command('let s ..= "B"') |
| 927 | . |
| 928 | lua << trim END |
| 929 | vim.command('let s ..= "C"') |
| 930 | END |
| 931 | lua << trim |
| 932 | vim.command('let s ..= "D"') |
| 933 | . |
Bram Moolenaar | 6ab0953 | 2020-05-01 14:10:13 +0200 | [diff] [blame] | 934 | lua << trim eof |
| 935 | vim.command('let s ..= "E"') |
| 936 | eof |
| 937 | call assert_equal('ABCDE', s) |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 938 | endfunc |
| 939 | |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 940 | " Test for adding, accessing and removing global variables using the vim.g |
| 941 | " Lua table |
| 942 | func Test_lua_global_var_table() |
| 943 | " Access global variables with different types of values |
| 944 | let g:Var1 = 10 |
| 945 | let g:Var2 = 'Hello' |
| 946 | let g:Var3 = ['a', 'b'] |
| 947 | let g:Var4 = #{x: 'edit', y: 'run'} |
| 948 | let g:Var5 = function('min') |
| 949 | call assert_equal(10, luaeval('vim.g.Var1')) |
| 950 | call assert_equal('Hello', luaeval('vim.g.Var2')) |
| 951 | call assert_equal(['a', 'b'], luaeval('vim.g.Var3')) |
| 952 | call assert_equal(#{x: 'edit', y: 'run'}, luaeval('vim.g.Var4')) |
| 953 | call assert_equal(2, luaeval('vim.g.Var5')([5, 9, 2])) |
| 954 | |
| 955 | " Access list of dictionaries and dictionary of lists |
| 956 | let g:Var1 = [#{a: 10}, #{b: 20}] |
| 957 | let g:Var2 = #{p: [5, 6], q: [1.1, 2.2]} |
| 958 | call assert_equal([#{a: 10}, #{b: 20}], luaeval('vim.g.Var1')) |
| 959 | call assert_equal(#{p: [5, 6], q: [1.1, 2.2]}, luaeval('vim.g.Var2')) |
| 960 | |
| 961 | " Create new global variables with different types of values |
| 962 | unlet g:Var1 g:Var2 g:Var3 g:Var4 g:Var5 |
| 963 | lua << trim END |
| 964 | vim.g.Var1 = 34 |
| 965 | vim.g.Var2 = 'World' |
| 966 | vim.g.Var3 = vim.list({'#', '$'}) |
| 967 | vim.g.Var4 = vim.dict({model='honda', year=2020}) |
| 968 | vim.g.Var5 = vim.funcref('max') |
| 969 | END |
| 970 | call assert_equal(34, g:Var1) |
| 971 | call assert_equal('World', g:Var2) |
| 972 | call assert_equal(['#', '$'], g:Var3) |
| 973 | call assert_equal(#{model: 'honda', year: 2020}, g:Var4) |
| 974 | call assert_equal(10, g:Var5([5, 10, 9])) |
| 975 | |
| 976 | " Create list of dictionaries and dictionary of lists |
| 977 | unlet g:Var1 g:Var2 |
| 978 | lua << trim END |
| 979 | vim.g.Var1 = vim.list({vim.dict({a=10}), vim.dict({b=20})}) |
| 980 | vim.g.Var2 = vim.dict({p=vim.list({5, 6}), q=vim.list({1.1, 2.2})}) |
| 981 | END |
| 982 | call assert_equal([#{a: 10}, #{b: 20}], luaeval('vim.g.Var1')) |
| 983 | call assert_equal(#{p: [5, 6], q: [1.1, 2.2]}, luaeval('vim.g.Var2')) |
| 984 | |
| 985 | " Modify a global variable with a list value or a dictionary value |
| 986 | let g:Var1 = [10, 20] |
| 987 | let g:Var2 = #{one: 'mercury', two: 'mars'} |
| 988 | lua << trim END |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 989 | vim.g.Var1[2] = nil |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 990 | vim.g.Var1[3] = 15 |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 991 | vim.g.Var2['two'] = nil |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 992 | vim.g.Var2['three'] = 'earth' |
| 993 | END |
| 994 | call assert_equal([10, 15], g:Var1) |
| 995 | call assert_equal(#{one: 'mercury', three: 'earth'}, g:Var2) |
| 996 | |
| 997 | " Remove global variables with different types of values |
| 998 | let g:Var1 = 10 |
| 999 | let g:Var2 = 'Hello' |
| 1000 | let g:Var3 = ['a', 'b'] |
| 1001 | let g:Var4 = #{x: 'edit', y: 'run'} |
| 1002 | let g:Var5 = function('min') |
| 1003 | lua << trim END |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 1004 | vim.g.Var1 = nil |
| 1005 | vim.g.Var2 = nil |
| 1006 | vim.g.Var3 = nil |
| 1007 | vim.g.Var4 = nil |
| 1008 | vim.g.Var5 = nil |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1009 | END |
| 1010 | call assert_false(exists('g:Var1')) |
| 1011 | call assert_false(exists('g:Var2')) |
| 1012 | call assert_false(exists('g:Var3')) |
| 1013 | call assert_false(exists('g:Var4')) |
| 1014 | call assert_false(exists('g:Var5')) |
| 1015 | |
| 1016 | " Try to modify and remove a locked global variable |
| 1017 | let g:Var1 = 10 |
| 1018 | lockvar g:Var1 |
| 1019 | call assert_fails('lua vim.g.Var1 = 20', 'variable is locked') |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 1020 | call assert_fails('lua vim.g.Var1 = nil', 'variable is locked') |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1021 | unlockvar g:Var1 |
| 1022 | let g:Var2 = [7, 14] |
| 1023 | lockvar 0 g:Var2 |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 1024 | lua vim.g.Var2[2] = nil |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1025 | lua vim.g.Var2[3] = 21 |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 1026 | call assert_fails('lua vim.g.Var2 = nil', 'variable is locked') |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1027 | call assert_equal([7, 21], g:Var2) |
| 1028 | lockvar 1 g:Var2 |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 1029 | call assert_fails('lua vim.g.Var2[2] = nil', 'list is locked') |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1030 | call assert_fails('lua vim.g.Var2[3] = 21', 'list is locked') |
| 1031 | unlockvar g:Var2 |
| 1032 | |
Bram Moolenaar | 4a01159 | 2021-08-05 15:11:08 +0200 | [diff] [blame] | 1033 | let g:TestFunc = function('len') |
| 1034 | call assert_fails('lua vim.g.func = vim.g.TestFunc', ['E704:', 'Couldn''t add to dictionary']) |
| 1035 | unlet g:TestFunc |
| 1036 | |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1037 | " Attempt to access a non-existing global variable |
| 1038 | call assert_equal(v:null, luaeval('vim.g.NonExistingVar')) |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 1039 | lua vim.g.NonExisting = nil |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1040 | |
| 1041 | unlet! g:Var1 g:Var2 g:Var3 g:Var4 g:Var5 |
| 1042 | endfunc |
| 1043 | |
| 1044 | " Test for accessing and modifying predefined vim variables using the vim.v |
| 1045 | " Lua table |
| 1046 | func Test_lua_predefined_var_table() |
| 1047 | call assert_equal(v:progpath, luaeval('vim.v.progpath')) |
| 1048 | let v:errmsg = 'SomeError' |
| 1049 | call assert_equal('SomeError', luaeval('vim.v.errmsg')) |
| 1050 | lua vim.v.errmsg = 'OtherError' |
| 1051 | call assert_equal('OtherError', v:errmsg) |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 1052 | lua vim.v.errmsg = 42 |
| 1053 | call assert_equal('42', v:errmsg) |
| 1054 | call assert_fails('lua vim.v.errmsg = nil', 'variable is fixed') |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1055 | let v:oldfiles = ['one', 'two'] |
| 1056 | call assert_equal(['one', 'two'], luaeval('vim.v.oldfiles')) |
| 1057 | lua vim.v.oldfiles = vim.list({}) |
| 1058 | call assert_equal([], v:oldfiles) |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 1059 | call assert_fails('lua vim.v.oldfiles = "a"', |
| 1060 | \ 'Setting v:oldfiles to value with wrong type') |
| 1061 | call assert_equal([], v:oldfiles) |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1062 | call assert_equal(v:null, luaeval('vim.v.null')) |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 1063 | call assert_fails('lua vim.v.argv[1] = nil', 'list is locked') |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1064 | call assert_fails('lua vim.v.newvar = 1', 'Dictionary is locked') |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 1065 | |
| 1066 | new |
| 1067 | call setline(1, ' foo foo foo') |
| 1068 | /foo |
| 1069 | call assert_equal([0, 1, 2, 0, 2], getcurpos()) |
| 1070 | call assert_equal(1, v:searchforward) |
| 1071 | normal! n |
| 1072 | call assert_equal([0, 1, 6, 0, 6], getcurpos()) |
| 1073 | lua vim.v.searchforward = 0 |
| 1074 | call assert_equal(0, v:searchforward) |
| 1075 | normal! n |
| 1076 | call assert_equal([0, 1, 2, 0, 2], getcurpos()) |
| 1077 | lua vim.v.searchforward = 1 |
| 1078 | call assert_equal(1, v:searchforward) |
| 1079 | normal! n |
| 1080 | call assert_equal([0, 1, 6, 0, 6], getcurpos()) |
| 1081 | bwipe! |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1082 | endfunc |
| 1083 | |
| 1084 | " Test for adding, accessing and modifying window-local variables using the |
| 1085 | " vim.w Lua table |
| 1086 | func Test_lua_window_var_table() |
| 1087 | " Access window variables with different types of values |
| 1088 | new |
| 1089 | let w:wvar1 = 10 |
| 1090 | let w:wvar2 = 'edit' |
| 1091 | let w:wvar3 = 3.14 |
| 1092 | let w:wvar4 = 0zdeadbeef |
| 1093 | let w:wvar5 = ['a', 'b'] |
| 1094 | let w:wvar6 = #{action: 'run'} |
| 1095 | call assert_equal(10, luaeval('vim.w.wvar1')) |
| 1096 | call assert_equal('edit', luaeval('vim.w.wvar2')) |
| 1097 | call assert_equal(3.14, luaeval('vim.w.wvar3')) |
| 1098 | call assert_equal(0zdeadbeef, luaeval('vim.w.wvar4')) |
| 1099 | call assert_equal(['a', 'b'], luaeval('vim.w.wvar5')) |
| 1100 | call assert_equal(#{action: 'run'}, luaeval('vim.w.wvar6')) |
| 1101 | call assert_equal(v:null, luaeval('vim.w.NonExisting')) |
| 1102 | |
| 1103 | " modify a window variable |
| 1104 | lua vim.w.wvar2 = 'paste' |
| 1105 | call assert_equal('paste', w:wvar2) |
| 1106 | |
| 1107 | " change the type stored in a variable |
| 1108 | let w:wvar2 = [1, 2] |
| 1109 | lua vim.w.wvar2 = vim.dict({a=10, b=20}) |
| 1110 | call assert_equal(#{a: 10, b: 20}, w:wvar2) |
| 1111 | |
| 1112 | " create a new window variable |
| 1113 | lua vim.w.wvar7 = vim.dict({a=vim.list({1, 2}), b=20}) |
| 1114 | call assert_equal(#{a: [1, 2], b: 20}, w:wvar7) |
| 1115 | |
| 1116 | " delete a window variable |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 1117 | lua vim.w.wvar2 = nil |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1118 | call assert_false(exists('w:wvar2')) |
| 1119 | |
| 1120 | new |
| 1121 | call assert_equal(v:null, luaeval('vim.w.wvar1')) |
| 1122 | call assert_equal(v:null, luaeval('vim.w.wvar2')) |
| 1123 | %bw! |
| 1124 | endfunc |
| 1125 | |
| 1126 | " Test for adding, accessing and modifying buffer-local variables using the |
| 1127 | " vim.b Lua table |
| 1128 | func Test_lua_buffer_var_table() |
| 1129 | " Access buffer variables with different types of values |
| 1130 | let b:bvar1 = 10 |
| 1131 | let b:bvar2 = 'edit' |
| 1132 | let b:bvar3 = 3.14 |
| 1133 | let b:bvar4 = 0zdeadbeef |
| 1134 | let b:bvar5 = ['a', 'b'] |
| 1135 | let b:bvar6 = #{action: 'run'} |
| 1136 | call assert_equal(10, luaeval('vim.b.bvar1')) |
| 1137 | call assert_equal('edit', luaeval('vim.b.bvar2')) |
| 1138 | call assert_equal(3.14, luaeval('vim.b.bvar3')) |
| 1139 | call assert_equal(0zdeadbeef, luaeval('vim.b.bvar4')) |
| 1140 | call assert_equal(['a', 'b'], luaeval('vim.b.bvar5')) |
| 1141 | call assert_equal(#{action: 'run'}, luaeval('vim.b.bvar6')) |
| 1142 | call assert_equal(v:null, luaeval('vim.b.NonExisting')) |
| 1143 | |
| 1144 | " modify a buffer variable |
| 1145 | lua vim.b.bvar2 = 'paste' |
| 1146 | call assert_equal('paste', b:bvar2) |
| 1147 | |
| 1148 | " change the type stored in a variable |
| 1149 | let b:bvar2 = [1, 2] |
| 1150 | lua vim.b.bvar2 = vim.dict({a=10, b=20}) |
| 1151 | call assert_equal(#{a: 10, b: 20}, b:bvar2) |
| 1152 | |
| 1153 | " create a new buffer variable |
| 1154 | lua vim.b.bvar7 = vim.dict({a=vim.list({1, 2}), b=20}) |
| 1155 | call assert_equal(#{a: [1, 2], b: 20}, b:bvar7) |
| 1156 | |
| 1157 | " delete a buffer variable |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 1158 | lua vim.b.bvar2 = nil |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1159 | call assert_false(exists('b:bvar2')) |
| 1160 | |
| 1161 | new |
| 1162 | call assert_equal(v:null, luaeval('vim.b.bvar1')) |
| 1163 | call assert_equal(v:null, luaeval('vim.b.bvar2')) |
| 1164 | %bw! |
| 1165 | endfunc |
| 1166 | |
| 1167 | " Test for adding, accessing and modifying tabpage-local variables using the |
| 1168 | " vim.t Lua table |
| 1169 | func Test_lua_tabpage_var_table() |
| 1170 | " Access tabpage variables with different types of values |
| 1171 | let t:tvar1 = 10 |
| 1172 | let t:tvar2 = 'edit' |
| 1173 | let t:tvar3 = 3.14 |
| 1174 | let t:tvar4 = 0zdeadbeef |
| 1175 | let t:tvar5 = ['a', 'b'] |
| 1176 | let t:tvar6 = #{action: 'run'} |
| 1177 | call assert_equal(10, luaeval('vim.t.tvar1')) |
| 1178 | call assert_equal('edit', luaeval('vim.t.tvar2')) |
| 1179 | call assert_equal(3.14, luaeval('vim.t.tvar3')) |
| 1180 | call assert_equal(0zdeadbeef, luaeval('vim.t.tvar4')) |
| 1181 | call assert_equal(['a', 'b'], luaeval('vim.t.tvar5')) |
| 1182 | call assert_equal(#{action: 'run'}, luaeval('vim.t.tvar6')) |
| 1183 | call assert_equal(v:null, luaeval('vim.t.NonExisting')) |
| 1184 | |
| 1185 | " modify a tabpage variable |
| 1186 | lua vim.t.tvar2 = 'paste' |
| 1187 | call assert_equal('paste', t:tvar2) |
| 1188 | |
| 1189 | " change the type stored in a variable |
| 1190 | let t:tvar2 = [1, 2] |
| 1191 | lua vim.t.tvar2 = vim.dict({a=10, b=20}) |
| 1192 | call assert_equal(#{a: 10, b: 20}, t:tvar2) |
| 1193 | |
| 1194 | " create a new tabpage variable |
| 1195 | lua vim.t.tvar7 = vim.dict({a=vim.list({1, 2}), b=20}) |
| 1196 | call assert_equal(#{a: [1, 2], b: 20}, t:tvar7) |
| 1197 | |
| 1198 | " delete a tabpage variable |
zeertzjq | edcba96 | 2023-09-24 23:13:51 +0200 | [diff] [blame] | 1199 | lua vim.t.tvar2 = nil |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1200 | call assert_false(exists('t:tvar2')) |
| 1201 | |
| 1202 | tabnew |
| 1203 | call assert_equal(v:null, luaeval('vim.t.tvar1')) |
| 1204 | call assert_equal(v:null, luaeval('vim.t.tvar2')) |
| 1205 | %bw! |
| 1206 | endfunc |
| 1207 | |
Yegappan Lakshmanan | 11328bc | 2021-08-06 21:34:38 +0200 | [diff] [blame] | 1208 | " Test for vim.version() |
| 1209 | func Test_lua_vim_version() |
| 1210 | lua << trim END |
| 1211 | vimver = vim.version() |
| 1212 | vimver_n = vimver.major * 100 + vimver.minor |
| 1213 | END |
| 1214 | call assert_equal(v:version, luaeval('vimver_n')) |
| 1215 | endfunc |
| 1216 | |
| 1217 | " Test for running multiple commands using vim.command() |
| 1218 | func Test_lua_multiple_commands() |
| 1219 | lua << trim END |
| 1220 | vim.command([[ |
| 1221 | let Var1 = [] |
| 1222 | for i in range(3) |
| 1223 | let Var1 += [#{name: 'x'}] |
| 1224 | endfor |
| 1225 | augroup Luagroup |
| 1226 | autocmd! |
| 1227 | autocmd User Luatest echo 'Hello' |
| 1228 | augroup END |
| 1229 | ]]) |
| 1230 | END |
| 1231 | call assert_equal([{'name': 'x'}, {'name': 'x'}, {'name': 'x'}], Var1) |
| 1232 | call assert_true(exists('#Luagroup')) |
| 1233 | call assert_true(exists('#Luagroup#User#Luatest')) |
| 1234 | augroup Luagroup |
| 1235 | autocmd! |
| 1236 | augroup END |
| 1237 | augroup! Luagroup |
| 1238 | endfunc |
| 1239 | |
Dominique Pelle | b6643d1 | 2022-03-20 11:46:01 +0000 | [diff] [blame] | 1240 | func Test_lua_debug() |
| 1241 | CheckRunVimInTerminal |
| 1242 | |
| 1243 | let buf = RunVimInTerminal('', {'rows': 10}) |
| 1244 | call term_sendkeys(buf, ":lua debug.debug()\n") |
| 1245 | call WaitForAssert({-> assert_equal('lua_debug> ', term_getline(buf, 10))}) |
| 1246 | |
| 1247 | call term_sendkeys(buf, "foo = 42\n") |
| 1248 | call WaitForAssert({-> assert_equal('lua_debug> foo = 42', term_getline(buf, 9))}) |
| 1249 | call WaitForAssert({-> assert_equal('lua_debug> ', term_getline(buf, 10))}) |
| 1250 | |
| 1251 | call term_sendkeys(buf, "print(foo)\n") |
| 1252 | call WaitForAssert({-> assert_equal('lua_debug> print(foo)', term_getline(buf, 8))}) |
| 1253 | call WaitForAssert({-> assert_equal('42', term_getline(buf, 9))}) |
| 1254 | call WaitForAssert({-> assert_equal('lua_debug> ', term_getline(buf, 10))}) |
| 1255 | |
Dominique Pelle | 81b573d | 2022-03-22 21:14:55 +0000 | [diff] [blame] | 1256 | call term_sendkeys(buf, "-\n") |
| 1257 | call WaitForAssert({-> assert_equal("(debug command):1: unexpected symbol near '-'", |
| 1258 | \ term_getline(buf, 9))}) |
| 1259 | call WaitForAssert({-> assert_equal('lua_debug> ', term_getline(buf, 10))}) |
| 1260 | |
Dominique Pelle | b6643d1 | 2022-03-20 11:46:01 +0000 | [diff] [blame] | 1261 | call term_sendkeys(buf, "cont\n") |
| 1262 | call WaitForAssert({-> assert_match(' All$', term_getline(buf, 10))}) |
| 1263 | |
Dominique Pelle | 81b573d | 2022-03-22 21:14:55 +0000 | [diff] [blame] | 1264 | " Entering an empty line also exits the debugger. |
| 1265 | call term_sendkeys(buf, ":lua debug.debug()\n") |
| 1266 | call WaitForAssert({-> assert_equal('lua_debug> ', term_getline(buf, 10))}) |
| 1267 | call term_sendkeys(buf, "\n") |
| 1268 | call WaitForAssert({-> assert_match(' All$', term_getline(buf, 10))}) |
| 1269 | |
Dominique Pelle | b6643d1 | 2022-03-20 11:46:01 +0000 | [diff] [blame] | 1270 | call StopVimInTerminal(buf) |
Dominique Pelle | b6643d1 | 2022-03-20 11:46:01 +0000 | [diff] [blame] | 1271 | endfunc |
| 1272 | |
Jesse Pavel | 8a35033 | 2023-08-13 22:05:45 -0400 | [diff] [blame] | 1273 | " Test for a crash when a Lua funcref is invoked with parameters from Vim |
| 1274 | func Test_lua_funcref_with_params() |
| 1275 | let Lua_funcref = luaeval('function(d) local s = "in Lua callback" end') |
| 1276 | call Lua_funcref({'a' : 'b'}) |
| 1277 | call assert_true(v:true) |
| 1278 | endfunc |
| 1279 | |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 1280 | " vim: shiftwidth=2 sts=2 expandtab |