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