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