robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1-- Test suite for testing interactions with API bindings 2local t =require ( 'test.testutil' ) 3local n =require ( 'test.functional.testnvim' )() 4local Screen =require ( 'test.functional.ui.screen' ) 5 6local describe , it , before_each =t . describe , t . it , t . before_each 7local nvim_prog =n . nvim_prog 8local fn =n . fn 9local api =n . api 10local command =n . command 11local dedent =t . dedent 12local insert =n . insert 13local clear =n . clear 14local eq =t . eq 15local neq =t . neq 16local ok =t . ok 17local pesc =vim . pesc 18local eval =n . eval 19local feed =n . feed 20local pcall_err =t . pcall_err 21local exec_lua =n . exec_lua 22local matches =t . matches 23local exec =n . exec 24local NIL =vim . NIL 25local retry =t . retry 26local next_msg =n . next_msg 27local remove_trace =t . remove_trace 28local poke_eventloop =n . poke_eventloop 29local assert_alive =n . assert_alive 30local expect =n . expect 31 32describe ( 'lua stdlib' , function () 33before_each ( clear ) 34-- İ: `tolower("İ")` is `i` which has length 1 while `İ` itself has 35-- length 2 (in bytes). 36-- Ⱥ: `tolower("Ⱥ")` is `ⱥ` which has length 2 while `Ⱥ` itself has 37-- length 3 (in bytes). 38-- 39-- Note: 'i' !=? 'İ' and 'ⱥ' !=? 'Ⱥ' on some systems. 40-- Note: Built-in Nvim comparison (on systems lacking `strcasecmp`) works 41-- only on ASCII characters. 42it ( 'vim.stricmp' , function () 43eq ( 0 , fn . luaeval ( 'vim.stricmp("a", "A")' )) 44eq ( 0 , fn . luaeval ( 'vim.stricmp("A", "a")' )) 45eq ( 0 , fn . luaeval ( 'vim.stricmp("a", "a")' )) 46eq ( 0 , fn . luaeval ( 'vim.stricmp("A", "A")' )) 47 48eq ( 0 , fn . luaeval ( 'vim.stricmp("", "")' )) 49eq ( 0 , fn . luaeval ( 'vim.stricmp("\\0", "\\0")' )) 50eq ( 0 , fn . luaeval ( 'vim.stricmp("\\0\\0", "\\0\\0")' )) 51eq ( 0 , fn . luaeval ( 'vim.stricmp("\\0\\0\\0", "\\0\\0\\0")' )) 52eq ( 0 , fn . luaeval ( 'vim.stricmp("\\0\\0\\0A", "\\0\\0\\0a")' )) 53eq ( 0 , fn . luaeval ( 'vim.stricmp("\\0\\0\\0a", "\\0\\0\\0A")' )) 54eq ( 0 , fn . luaeval ( 'vim.stricmp("\\0\\0\\0a", "\\0\\0\\0a")' )) 55 56eq ( 0 , fn . luaeval ( 'vim.stricmp("a\\0", "A\\0")' )) 57eq ( 0 , fn . luaeval ( 'vim.stricmp("A\\0", "a\\0")' )) 58eq ( 0 , fn . luaeval ( 'vim.stricmp("a\\0", "a\\0")' )) 59eq ( 0 , fn . luaeval ( 'vim.stricmp("A\\0", "A\\0")' )) 60 61eq ( 0 , fn . luaeval ( 'vim.stricmp("\\0a", "\\0A")' )) 62eq ( 0 , fn . luaeval ( 'vim.stricmp("\\0A", "\\0a")' )) 63eq ( 0 , fn . luaeval ( 'vim.stricmp("\\0a", "\\0a")' )) 64eq ( 0 , fn . luaeval ( 'vim.stricmp("\\0A", "\\0A")' )) 65 66eq ( 0 , fn . luaeval ( 'vim.stricmp("\\0a\\0", "\\0A\\0")' )) 67eq ( 0 , fn . luaeval ( 'vim.stricmp("\\0A\\0", "\\0a\\0")' )) 68eq ( 0 , fn . luaeval ( 'vim.stricmp("\\0a\\0", "\\0a\\0")' )) 69eq ( 0 , fn . luaeval ( 'vim.stricmp("\\0A\\0", "\\0A\\0")' )) 70 71eq ( - 1 , fn . luaeval ( 'vim.stricmp("a", "B")' )) 72eq ( - 1 , fn . luaeval ( 'vim.stricmp("A", "b")' )) 73eq ( - 1 , fn . luaeval ( 'vim.stricmp("a", "b")' )) 74eq ( - 1 , fn . luaeval ( 'vim.stricmp("A", "B")' )) 75 76eq ( - 1 , fn . luaeval ( 'vim.stricmp("", "\\0")' )) 77eq ( - 1 , fn . luaeval ( 'vim.stricmp("\\0", "\\0\\0")' )) 78eq ( - 1 , fn . luaeval ( 'vim.stricmp("\\0\\0", "\\0\\0\\0")' )) 79eq ( - 1 , fn . luaeval ( 'vim.stricmp("\\0\\0\\0A", "\\0\\0\\0b")' )) 80eq ( - 1 , fn . luaeval ( 'vim.stricmp("\\0\\0\\0a", "\\0\\0\\0B")' )) 81eq ( - 1 , fn . luaeval ( 'vim.stricmp("\\0\\0\\0a", "\\0\\0\\0b")' )) 82 83eq ( - 1 , fn . luaeval ( 'vim.stricmp("a\\0", "B\\0")' )) 84eq ( - 1 , fn . luaeval ( 'vim.stricmp("A\\0", "b\\0")' )) 85eq ( - 1 , fn . luaeval ( 'vim.stricmp("a\\0", "b\\0")' )) 86eq ( - 1 , fn . luaeval ( 'vim.stricmp("A\\0", "B\\0")' )) 87 88eq ( - 1 , fn . luaeval ( 'vim.stricmp("\\0a", "\\0B")' )) 89eq ( - 1 , fn . luaeval ( 'vim.stricmp("\\0A", "\\0b")' )) 90eq ( - 1 , fn . luaeval ( 'vim.stricmp("\\0a", "\\0b")' )) 91eq ( - 1 , fn . luaeval ( 'vim.stricmp("\\0A", "\\0B")' )) 92 93eq ( - 1 , fn . luaeval ( 'vim.stricmp("\\0a\\0", "\\0B\\0")' )) 94eq ( - 1 , fn . luaeval ( 'vim.stricmp("\\0A\\0", "\\0b\\0")' )) 95eq ( - 1 , fn . luaeval ( 'vim.stricmp("\\0a\\0", "\\0b\\0")' )) 96eq ( - 1 , fn . luaeval ( 'vim.stricmp("\\0A\\0", "\\0B\\0")' )) 97 98eq ( 1 , fn . luaeval ( 'vim.stricmp("c", "B")' )) 99eq ( 1 , fn . luaeval ( 'vim.stricmp("C", "b")' )) 100eq ( 1 , fn . luaeval ( 'vim.stricmp("c", "b")' )) 101eq ( 1 , fn . luaeval ( 'vim.stricmp("C", "B")' )) 102 103eq ( 1 , fn . luaeval ( 'vim.stricmp("\\0", "")' )) 104eq ( 1 , fn . luaeval ( 'vim.stricmp("\\0\\0", "\\0")' )) 105eq ( 1 , fn . luaeval ( 'vim.stricmp("\\0\\0\\0", "\\0\\0")' )) 106eq ( 1 , fn . luaeval ( 'vim.stricmp("\\0\\0\\0\\0", "\\0\\0\\0")' )) 107eq ( 1 , fn . luaeval ( 'vim.stricmp("\\0\\0\\0C", "\\0\\0\\0b")' )) 108eq ( 1 , fn . luaeval ( 'vim.stricmp("\\0\\0\\0c", "\\0\\0\\0B")' )) 109eq ( 1 , fn . luaeval ( 'vim.stricmp("\\0\\0\\0c", "\\0\\0\\0b")' )) 110 111eq ( 1 , fn . luaeval ( 'vim.stricmp("c\\0", "B\\0")' )) 112eq ( 1 , fn . luaeval ( 'vim.stricmp("C\\0", "b\\0")' )) 113eq ( 1 , fn . luaeval ( 'vim.stricmp("c\\0", "b\\0")' )) 114eq ( 1 , fn . luaeval ( 'vim.stricmp("C\\0", "B\\0")' )) 115 116eq ( 1 , fn . luaeval ( 'vim.stricmp("c\\0", "B")' )) 117eq ( 1 , fn . luaeval ( 'vim.stricmp("C\\0", "b")' )) 118eq ( 1 , fn . luaeval ( 'vim.stricmp("c\\0", "b")' )) 119eq ( 1 , fn . luaeval ( 'vim.stricmp("C\\0", "B")' )) 120 121eq ( 1 , fn . luaeval ( 'vim.stricmp("\\0c", "\\0B")' )) 122eq ( 1 , fn . luaeval ( 'vim.stricmp("\\0C", "\\0b")' )) 123eq ( 1 , fn . luaeval ( 'vim.stricmp("\\0c", "\\0b")' )) 124eq ( 1 , fn . luaeval ( 'vim.stricmp("\\0C", "\\0B")' )) 125 126eq ( 1 , fn . luaeval ( 'vim.stricmp("\\0c\\0", "\\0B\\0")' )) 127eq ( 1 , fn . luaeval ( 'vim.stricmp("\\0C\\0", "\\0b\\0")' )) 128eq ( 1 , fn . luaeval ( 'vim.stricmp("\\0c\\0", "\\0b\\0")' )) 129eq ( 1 , fn . luaeval ( 'vim.stricmp("\\0C\\0", "\\0B\\0")' )) 130end ) 131 132--- @param prerel string | nil 133local function test_vim_deprecate ( prerel ) 134-- vim.deprecate(name, alternative, version, plugin, backtrace) 135-- See MAINTAIN.md for the soft/hard deprecation policy 136 137describe (( 'vim.deprecate prerel=%s,' ): format ( prerel or 'nil' ), function () 138local curver --- @type {major:number, minor:number} 139local curstr --- @type string 140local nextver --- @type string 141 142before_each ( function () 143curver =exec_lua ( 'return vim.version()' ) 144-- "0.10" or "0.10-dev+xxx" 145curstr =( '%s.%s%s' ): format ( curver . major , curver . minor , prerel or '' ) 146-- "0.10" or "0.11" 147nextver =( '%s.%s' ): format ( curver . major , curver . minor + ( prerel and 0 or 1 )) 148end ) 149 150it ( 'plugin=nil, same message skipped' , function () 151eq ( 152[[foo.bar() is deprecated. Run ":checkhealth vim.deprecated" for more information]] , 153exec_lua ( 'return vim.deprecate(...)' , 'foo.bar()' , 'zub.wooo{ok=yay}' , curstr ) 154) 155-- Same message as above; skipped this time. 156eq ( vim . NIL , exec_lua ( 'return vim.deprecate(...)' , 'foo.bar()' , 'zub.wooo{ok=yay}' , curstr )) 157end ) 158 159it ( 'plugin=nil, no error if soft-deprecated' , function () 160eq ( vim . NIL , exec_lua [[return vim.deprecate('old1', 'new1', '0.99.0')]] ) 161-- Major version > current Nvim major is always "soft-deprecated". 162-- XXX: This is also a reminder to update the hardcoded `nvim_major`, when Nvim reaches 1.0. 163eq ( vim . NIL , exec_lua [[return vim.deprecate('old2', 'new2', '1.0.0')]] ) 164end ) 165 166it ( 'plugin=nil, show error if hard-deprecated' , function () 167eq ( 168[[foo.hard_dep() is deprecated. Run ":checkhealth vim.deprecated" for more information]] , 169exec_lua ( 'return vim.deprecate(...)' , 'foo.hard_dep()' , 'vim.new_api()' , nextver ) 170) 171end ) 172 173it ( 'plugin=nil, message is only truncated in display #38841' , function () 174local screen =Screen . new ( 50 , 10 ) 175exec_lua ( 'vim.deprecate(...)' , 'foo.bar()' , 'zub.wooo{ok=yay}' , curstr ) 176screen : expect ( [[ 177^ | 178{1:~ }|*8 179{19:foo.bar() is deprecated. Run ":checkhealth vim.dep}| 180]] ) 181eq ( 182[[foo.bar() is deprecated. Run ":checkhealth vim.deprecated" for more information]] , 183n . exec_capture ( 'messages' ) 184) 185end ) 186 187it ( 'plugin specified' , function () 188-- When `plugin` is specified, don't show ":help deprecated". #22235 189eq ( 190dedent [[ 191foo.bar() is deprecated, use zub.wooo{ok=yay} instead. 192Feature will be removed in my-plugin.nvim 0.3.0]] , 193exec_lua ( 194'return vim.deprecate(...)' , 195'foo.bar()' , 196'zub.wooo{ok=yay}' , 197'0.3.0' , 198'my-plugin.nvim' , 199false 200) 201) 202 203-- plugins: no soft deprecation period 204eq ( 205dedent [[ 206foo.bar() is deprecated, use zub.wooo{ok=yay} instead. 207Feature will be removed in my-plugin.nvim 0.11.0]] , 208exec_lua ( 209'return vim.deprecate(...)' , 210'foo.bar()' , 211'zub.wooo{ok=yay}' , 212'0.11.0' , 213'my-plugin.nvim' , 214false 215) 216) 217end ) 218end ) 219end 220 221test_vim_deprecate () 222test_vim_deprecate ( '-dev+g0000000' ) 223 224it ( 'vim.startswith' , function () 225eq ( true , fn . luaeval ( 'vim.startswith("123", "1")' )) 226eq ( true , fn . luaeval ( 'vim.startswith("123", "")' )) 227eq ( true , fn . luaeval ( 'vim.startswith("123", "123")' )) 228eq ( true , fn . luaeval ( 'vim.startswith("", "")' )) 229 230eq ( false , fn . luaeval ( 'vim.startswith("123", " ")' )) 231eq ( false , fn . luaeval ( 'vim.startswith("123", "2")' )) 232eq ( false , fn . luaeval ( 'vim.startswith("123", "1234")' )) 233 234matches ( 235'prefix: expected string, got nil' , 236pcall_err ( exec_lua , 'return vim.startswith("123", nil)' ) 237) 238matches ( 's: expected string, got nil' , pcall_err ( exec_lua , 'return vim.startswith(nil, "123")' )) 239end ) 240 241it ( 'vim.endswith' , function () 242eq ( true , fn . luaeval ( 'vim.endswith("123", "3")' )) 243eq ( true , fn . luaeval ( 'vim.endswith("123", "")' )) 244eq ( true , fn . luaeval ( 'vim.endswith("123", "123")' )) 245eq ( true , fn . luaeval ( 'vim.endswith("", "")' )) 246 247eq ( false , fn . luaeval ( 'vim.endswith("123", " ")' )) 248eq ( false , fn . luaeval ( 'vim.endswith("123", "2")' )) 249eq ( false , fn . luaeval ( 'vim.endswith("123", "1234")' )) 250 251matches ( 252'suffix: expected string, got nil' , 253pcall_err ( exec_lua , 'return vim.endswith("123", nil)' ) 254) 255matches ( 's: expected string, got nil' , pcall_err ( exec_lua , 'return vim.endswith(nil, "123")' )) 256end ) 257 258it ( 'vim.str_utfindex/str_byteindex' , function () 259exec_lua ( [[_G.test_text = "xy åäö ɧ 汉语 ↥ 🤦x🦄 å بِيَّ\000ъ"]] ) 260local indices32 ={ 261[ 0 ] =0 , 2621 , 2632 , 2643 , 2655 , 2667 , 2679 , 26810 , 26912 , 27013 , 27116 , 27219 , 27320 , 27423 , 27524 , 27628 , 27729 , 27833 , 27934 , 28035 , 28137 , 28238 , 28340 , 28442 , 28544 , 28646 , 28748 , 28849 , 28951 , 290} 291local indices16 ={ 292[ 0 ] =0 , 2931 , 2942 , 2953 , 2965 , 2977 , 2989 , 29910 , 30012 , 30113 , 30216 , 30319 , 30420 , 30523 , 30624 , 30728 , 30828 , 30929 , 31033 , 31133 , 31234 , 31335 , 31437 , 31538 , 31640 , 31742 , 31844 , 31946 , 32048 , 32149 , 32251 , 323} 324local indices8 ={ 325[ 0 ] =0 , 3261 , 3272 , 3283 , 3294 , 3305 , 3316 , 3327 , 3338 , 3349 , 33510 , 33611 , 33712 , 33813 , 33914 , 34015 , 34116 , 34217 , 34318 , 34419 , 34520 , 34621 , 34722 , 34823 , 34924 , 35025 , 35126 , 35227 , 35328 , 35429 , 35530 , 35631 , 35732 , 35833 , 35934 , 36035 , 36136 , 36237 , 36338 , 36439 , 36540 , 36641 , 36742 , 36843 , 36944 , 37045 , 37146 , 37247 , 37348 , 37449 , 37550 , 37651 , 377} 378for i , k in pairs ( indices32 ) do 379eq ( k , exec_lua ( 'return vim.str_byteindex(_G.test_text, ...)' , i ), i ) 380eq ( k , exec_lua ( 'return vim.str_byteindex(_G.test_text, ..., false)' , i ), i ) 381eq ( k , exec_lua ( 'return vim.str_byteindex(_G.test_text, "utf-32", ...)' , i ), i ) 382end 383for i , k in pairs ( indices16 ) do 384eq ( k , exec_lua ( 'return vim.str_byteindex(_G.test_text, ..., true)' , i ), i ) 385eq ( k , exec_lua ( 'return vim.str_byteindex(_G.test_text, "utf-16", ...)' , i ), i ) 386end 387for i , k in pairs ( indices8 ) do 388eq ( k , exec_lua ( 'return vim.str_byteindex(_G.test_text, "utf-8", ...)' , i ), i ) 389end 390matches ( 391'index out of range' , 392pcall_err ( exec_lua , 'return vim.str_byteindex(_G.test_text, ...)' , # indices32 + 1 ) 393) 394matches ( 395'index out of range' , 396pcall_err ( exec_lua , 'return vim.str_byteindex(_G.test_text, ..., true)' , # indices16 + 1 ) 397) 398matches ( 399'index out of range' , 400pcall_err ( exec_lua , 'return vim.str_byteindex(_G.test_text, "utf-16", ...)' , # indices16 + 1 ) 401) 402matches ( 403'index out of range' , 404pcall_err ( exec_lua , 'return vim.str_byteindex(_G.test_text, "utf-32", ...)' , # indices32 + 1 ) 405) 406matches ( 407'invalid encoding' , 408pcall_err ( exec_lua , 'return vim.str_byteindex("hello", "madeupencoding", 1)' ) 409) 410eq ( 411indices32 [ # indices32 ], 412exec_lua ( 'return vim.str_byteindex(_G.test_text, "utf-32", 99999, false)' ) 413) 414eq ( 415indices16 [ # indices16 ], 416exec_lua ( 'return vim.str_byteindex(_G.test_text, "utf-16", 99999, false)' ) 417) 418eq ( 419indices8 [ # indices8 ], 420exec_lua ( 'return vim.str_byteindex(_G.test_text, "utf-8", 99999, false)' ) 421) 422eq ( 2 , exec_lua ( 'return vim.str_byteindex("é", "utf-16", 2, false)' )) 423local i32 , i16 , i8 =0 , 0 , 0 424local len =51 425for k =0 , len do 426if indices32 [ i32 ] < k then 427i32 =i32 + 1 428end 429if indices16 [ i16 ] < k then 430i16 =i16 + 1 431if indices16 [ i16 + 1 ] == indices16 [ i16 ] then 432i16 =i16 + 1 433end 434end 435if indices8 [ i8 ] < k then 436i8 =i8 + 1 437end 438eq ( { i32 , i16 } , exec_lua ( 'return {vim.str_utfindex(_G.test_text, ...)}' , k ), k ) 439eq ( { i32 } , exec_lua ( 'return {vim.str_utfindex(_G.test_text, "utf-32", ...)}' , k ), k ) 440eq ( { i16 } , exec_lua ( 'return {vim.str_utfindex(_G.test_text, "utf-16", ...)}' , k ), k ) 441eq ( { i8 } , exec_lua ( 'return {vim.str_utfindex(_G.test_text, "utf-8", ...)}' , k ), k ) 442end 443 444eq ( { # indices32 , # indices16 } , exec_lua ( 'return {vim.str_utfindex(_G.test_text)}' )) 445 446eq ( # indices32 , exec_lua ( 'return vim.str_utfindex(_G.test_text, "utf-32", math.huge, false)' )) 447eq ( # indices16 , exec_lua ( 'return vim.str_utfindex(_G.test_text, "utf-16", math.huge, false)' )) 448eq ( # indices8 , exec_lua ( 'return vim.str_utfindex(_G.test_text, "utf-8", math.huge, false)' )) 449 450eq ( # indices32 , exec_lua ( 'return vim.str_utfindex(_G.test_text, "utf-32")' )) 451eq ( # indices16 , exec_lua ( 'return vim.str_utfindex(_G.test_text, "utf-16")' )) 452eq ( # indices8 , exec_lua ( 'return vim.str_utfindex(_G.test_text, "utf-8")' )) 453matches ( 454'invalid encoding' , 455pcall_err ( exec_lua , 'return vim.str_utfindex(_G.test_text, "madeupencoding", ...)' , 1 ) 456) 457matches ( 458'index out of range' , 459pcall_err ( exec_lua , 'return vim.str_utfindex(_G.test_text, ...)' , len + 1 ) 460) 461end ) 462 463it ( 'vim.str_byteindex stays within the string for truncated sequences' , function () 464for _ , s in ipairs ( { '\xc3' , '\xe4\xb8' , '\xf0\x9f' , '\xf0\x9f\x98' , 'abc\xc3' } ) do 465for _ , encoding in ipairs ( { 'utf-8' , 'utf-16' , 'utf-32' } ) do 466for index =0 , # s + 2 do 467local byteindex =exec_lua ( 'return vim.str_byteindex(...)' , s , encoding , index , false ) 468ok ( 469byteindex <= # s , 470( 'at most %d' ): format ( # s ), 471( '%d (encoding=%s, index=%d)' ): format ( byteindex , encoding , index ) 472) 473end 474end 475end 476end ) 477 478it ( 'vim.str_utf_start' , function () 479exec_lua ( [[_G.test_text = "xy åäö ɧ 汉语 ↥ 🤦x🦄 å بِيَّ"]] ) 480local expected_positions ={ 4810 , 4820 , 4830 , 4840 , 485- 1 , 4860 , 487- 1 , 4880 , 489- 1 , 4900 , 4910 , 492- 1 , 4930 , 4940 , 495- 1 , 496- 2 , 4970 , 498- 1 , 499- 2 , 5000 , 5010 , 502- 1 , 503- 2 , 5040 , 5050 , 506- 1 , 507- 2 , 508- 3 , 5090 , 5100 , 511- 1 , 512- 2 , 513- 3 , 5140 , 5150 , 5160 , 517- 1 , 5180 , 5190 , 520- 1 , 5210 , 522- 1 , 5230 , 524- 1 , 5250 , 526- 1 , 5270 , 528- 1 , 529} 530eq ( 531expected_positions , 532exec_lua ( [[ 533local start_codepoint_positions = {} 534for idx = 1, #_G.test_text do 535table.insert(start_codepoint_positions, vim.str_utf_start(_G.test_text, idx)) 536end 537return start_codepoint_positions 538]] ) 539) 540end ) 541 542it ( 'vim.str_utf_end' , function () 543exec_lua ( [[_G.test_text = "xy åäö ɧ 汉语 ↥ 🤦x🦄 å بِيَّ"]] ) 544local expected_positions ={ 5450 , 5460 , 5470 , 5481 , 5490 , 5501 , 5510 , 5521 , 5530 , 5540 , 5551 , 5560 , 5570 , 5582 , 5591 , 5600 , 5612 , 5621 , 5630 , 5640 , 5652 , 5661 , 5670 , 5680 , 5693 , 5702 , 5711 , 5720 , 5730 , 5743 , 5752 , 5761 , 5770 , 5780 , 5790 , 5801 , 5810 , 5820 , 5831 , 5840 , 5851 , 5860 , 5871 , 5880 , 5891 , 5900 , 5911 , 5920 , 593} 594eq ( 595expected_positions , 596exec_lua ( [[ 597local end_codepoint_positions = {} 598for idx = 1, #_G.test_text do 599table.insert(end_codepoint_positions, vim.str_utf_end(_G.test_text, idx)) 600end 601return end_codepoint_positions 602]] ) 603) 604end ) 605 606it ( 'vim.str_utf_pos' , function () 607exec_lua ( [[_G.test_text = "xy åäö ɧ 汉语 ↥ 🤦x🦄 å بِيَّ"]] ) 608local expected_positions ={ 6091 , 6102 , 6113 , 6124 , 6136 , 6148 , 61510 , 61611 , 61713 , 61814 , 61917 , 62020 , 62121 , 62224 , 62325 , 62429 , 62530 , 62634 , 62735 , 62836 , 62938 , 63039 , 63141 , 63243 , 63345 , 63447 , 635} 636eq ( expected_positions , exec_lua ( 'return vim.str_utf_pos(_G.test_text)' )) 637end ) 638 639it ( 'vim.schedule' , function () 640exec_lua ( [[ 641test_table = {} 642vim.schedule(function() 643table.insert(test_table, "xx") 644end) 645table.insert(test_table, "yy") 646]] ) 647eq ( { 'yy' , 'xx' } , exec_lua ( 'return test_table' )) 648 649-- Validates args. 650matches ( 'vim.schedule: expected function' , pcall_err ( exec_lua , "vim.schedule('stringly')" )) 651matches ( 'vim.schedule: expected function' , pcall_err ( exec_lua , 'vim.schedule()' )) 652 653exec_lua ( [[ 654vim.schedule(function() 655error("big failure\nvery async") 656end) 657]] ) 658 659feed ( '<cr>' ) 660matches ( 'big failure\nvery async' , remove_trace ( eval ( 'v:errmsg' ))) 661 662local screen =Screen . new ( 60 , 5 ) 663screen : expect { 664grid =[[ 665^ | 666{1:~ }|*3 667| 668]] , 669} 670 671-- nvim_command causes a Vimscript exception, check that it is properly caught 672-- and propagated as an error message in async contexts.. #10809 673exec_lua ( [[ 674vim.schedule(function() 675vim.api.nvim_command(":echo 'err") 676end) 677]] ) 678screen : expect { 679grid =[[ 680{9:stack traceback:} | 681{9: [C]: in function 'nvim_command'} | 682{9: [string "<nvim>"]:2: in function <[string "<nvim>"]:}| 683{9:1>} | 684{6:Press ENTER or type command to continue}^ | 685]] , 686} 687end ) 688 689it ( 'vim.gsplit, vim.split' , function () 690local tests ={ 691-- plain trimempty 692{ 'a,b' , ',' , false , false , { 'a' , 'b' } } , 693{ ':aa::::bb:' , ':' , false , false , { '' , 'aa' , '' , '' , '' , 'bb' , '' } } , 694{ ':aa::::bb:' , ':' , false , true , { 'aa' , '' , '' , '' , 'bb' } } , 695{ 'aa::::bb:' , ':' , false , true , { 'aa' , '' , '' , '' , 'bb' } } , 696{ ':aa::bb:' , ':' , false , true , { 'aa' , '' , 'bb' } } , 697{ '/a/b:/b/\n' , '[:\n]' , false , true , { '/a/b' , '/b/' } } , 698{ '::ee::ff:' , ':' , false , false , { '' , '' , 'ee' , '' , 'ff' , '' } } , 699{ '::ee::ff::' , ':' , false , true , { 'ee' , '' , 'ff' } } , 700{ 'ab' , '.' , false , false , { '' , '' , '' } } , 701{ 'a1b2c' , '[0-9]' , false , false , { 'a' , 'b' , 'c' } } , 702{ 'xy' , '' , false , false , { 'x' , 'y' } } , 703{ 'here be dragons' , ' ' , false , false , { 'here' , 'be' , 'dragons' } } , 704{ 'axaby' , 'ab?' , false , false , { '' , 'x' , 'y' } } , 705{ 'f v2v v3v w2w ' , '([vw])2%1' , false , false , { 'f ' , ' v3v ' , ' ' } } , 706{ '' , '' , false , false , {} } , 707{ '' , '' , false , true , {} } , 708{ '\n' , '[:\n]' , false , true , {} } , 709{ '' , 'a' , false , false , { '' } } , 710{ 'x*yz*oo*l' , '*' , true , false , { 'x' , 'yz' , 'oo' , 'l' } } , 711} 712 713for _ , q in ipairs ( tests ) do 714eq ( q [ 5 ], vim . split ( q [ 1 ], q [ 2 ], { plain =q [ 3 ], trimempty =q [ 4 ] } ), q [ 1 ]) 715end 716 717-- Test old signature 718eq ( { 'x' , 'yz' , 'oo' , 'l' } , vim . split ( 'x*yz*oo*l' , '*' , true )) 719 720local loops ={ 721{ 'abc' , '.-' } , 722} 723 724for _ , q in ipairs ( loops ) do 725matches ( 'Infinite loop detected' , pcall_err ( vim . split , q [ 1 ], q [ 2 ])) 726end 727 728-- Validates args. 729eq ( true , pcall ( vim . split , 'string' , 'string' )) 730matches ( 's: expected string, got number' , pcall_err ( vim . split , 1 , 'string' )) 731matches ( 'sep: expected string, got number' , pcall_err ( vim . split , 'string' , 1 )) 732matches ( 'opts: expected table, got number' , pcall_err ( vim . split , 'string' , 'string' , 1 )) 733end ) 734 735it ( 'vim.trim' , function () 736local trim =function ( s ) 737return exec_lua ( 'return vim.trim(...)' , s ) 738end 739 740local trims ={ 741{ ' a' , 'a' } , 742{ ' b ' , 'b' } , 743{ '\tc' , 'c' } , 744{ 'r\n' , 'r' } , 745{ '' , '' } , 746{ ' \t \n' , '' } , 747} 748 749for _ , q in ipairs ( trims ) do 750eq ( q [ 2 ], trim ( q [ 1 ])) 751end 752 753-- Validates args. 754matches ( 's: expected string, got number' , pcall_err ( trim , 2 )) 755end ) 756 757it ( 'vim.inspect' , function () 758-- just make sure it basically works, it has its own test suite 759local inspect =function ( q , opts ) 760return exec_lua ( 'return vim.inspect(...)' , q , opts ) 761end 762 763eq ( '2' , inspect ( 2 )) 764eq ( '{+a = {+b = 1+}+}' , inspect ( { a ={ b =1 } } , { newline ='+' , indent ='' } )) 765 766-- special value vim.inspect.KEY works 767eq ( 768'{ KEY_a = "x", KEY_b = "y"}' , 769exec_lua ( [[ 770return vim.inspect({a="x", b="y"}, {newline = '', process = function(item, path) 771if path[#path] == vim.inspect.KEY then 772return 'KEY_'..item 773end 774return item 775end}) 776]] ) 777) 778end ) 779 780it ( 'vim.deepcopy' , function () 781ok ( exec_lua ( [[ 782local a = { x = { 1, 2 }, y = 5} 783local b = vim.deepcopy(a) 784 785return b.x[1] == 1 and b.x[2] == 2 and b.y == 5 and vim.tbl_count(b) == 2 786and tostring(a) ~= tostring(b) 787]] )) 788 789ok ( exec_lua ( [[ 790local a = {} 791local b = vim.deepcopy(a) 792 793return vim.islist(b) and vim.tbl_count(b) == 0 and tostring(a) ~= tostring(b) 794]] )) 795 796ok ( exec_lua ( [[ 797local a = vim.empty_dict() 798local b = vim.deepcopy(a) 799 800return not vim.islist(b) and vim.tbl_count(b) == 0 801]] )) 802 803ok ( exec_lua ( [[ 804local a = {x = vim.empty_dict(), y = {}} 805local b = vim.deepcopy(a) 806 807return not vim.islist(b.x) and vim.islist(b.y) 808and vim.tbl_count(b) == 2 809and tostring(a) ~= tostring(b) 810]] )) 811 812ok ( exec_lua ( [[ 813local f1 = function() return 1 end 814local f2 = function() return 2 end 815local t1 = {f = f1} 816local t2 = vim.deepcopy(t1) 817t1.f = f2 818return t1.f() ~= t2.f() 819]] )) 820 821ok ( exec_lua ( [[ 822local t1 = {a = 5} 823t1.self = t1 824local t2 = vim.deepcopy(t1) 825return t2.self == t2 and t2.self ~= t1 826]] )) 827 828ok ( exec_lua ( [[ 829local mt = {mt=true} 830local t1 = setmetatable({a = 5}, mt) 831local t2 = vim.deepcopy(t1) 832return getmetatable(t2) == mt 833]] )) 834 835ok ( exec_lua ( [[ 836local t1 = {a = vim.NIL} 837local t2 = vim.deepcopy(t1) 838return t2.a == vim.NIL 839]] )) 840 841matches ( 842'Cannot deepcopy object of type thread' , 843pcall_err ( 844exec_lua , 845[[ 846local thread = coroutine.create(function () return 0 end) 847local t = {thr = thread} 848vim.deepcopy(t) 849]] 850) 851) 852end ) 853 854it ( 'vim._copy' , function () 855ok ( exec_lua ( [[ 856local inner = { x = 1 } 857local mt = { tag = true } 858local a = setmetatable({ inner = inner }, mt) 859local b = vim._copy(a) 860 861local c = vim.empty_dict() 862c.inner = inner 863local d = vim._copy(c) 864 865return b ~= a 866and b.inner == inner 867and getmetatable(b) == mt 868and d ~= c 869and d.inner == inner 870and not vim.islist(d) 871]] )) 872end ) 873 874it ( 'vim.pesc' , function () 875eq ( 'foo%-bar' , exec_lua ( [[return vim.pesc('foo-bar')]] )) 876eq ( 'foo%%%-bar' , exec_lua ( [[return vim.pesc(vim.pesc('foo-bar'))]] )) 877-- pesc() returns one result. #20751 878eq ( { 'x' } , exec_lua ( [[return {vim.pesc('x')}]] )) 879 880-- Validates args. 881matches ( 's: expected string, got number' , pcall_err ( exec_lua , [[return vim.pesc(2)]] )) 882end ) 883 884it ( 'vim.list_contains' , function () 885eq ( true , exec_lua ( "return vim.list_contains({'a','b','c'}, 'c')" )) 886eq ( false , exec_lua ( "return vim.list_contains({'a','b','c'}, 'd')" )) 887end ) 888 889it ( 'vim.tbl_contains' , function () 890eq ( true , exec_lua ( "return vim.tbl_contains({'a','b','c'}, 'c')" )) 891eq ( false , exec_lua ( "return vim.tbl_contains({'a','b','c'}, 'd')" )) 892eq ( true , exec_lua ( "return vim.tbl_contains({[2]='a',foo='b',[5] = 'c'}, 'c')" )) 893eq ( 894true , 895exec_lua ( [[ 896return vim.tbl_contains({ 'a', { 'b', 'c' } }, function(v) 897return vim.deep_equal(v, { 'b', 'c' }) 898end, { predicate = true }) 899]] ) 900) 901end ) 902 903it ( 'vim.tbl_keys' , function () 904eq ( {} , exec_lua ( 'return vim.tbl_keys({})' )) 905for _ , v in pairs ( exec_lua ( "return vim.tbl_keys({'a', 'b', 'c'})" )) do 906eq ( true , exec_lua ( 'return vim.tbl_contains({ 1, 2, 3 }, ...)' , v )) 907end 908for _ , v in pairs ( exec_lua ( 'return vim.tbl_keys({a=1, b=2, c=3})' )) do 909eq ( true , exec_lua ( "return vim.tbl_contains({ 'a', 'b', 'c' }, ...)" , v )) 910end 911end ) 912 913it ( 'vim.tbl_values' , function () 914eq ( {} , exec_lua ( 'return vim.tbl_values({})' )) 915for _ , v in pairs ( exec_lua ( "return vim.tbl_values({'a', 'b', 'c'})" )) do 916eq ( true , exec_lua ( "return vim.tbl_contains({ 'a', 'b', 'c' }, ...)" , v )) 917end 918for _ , v in pairs ( exec_lua ( 'return vim.tbl_values({a=1, b=2, c=3})' )) do 919eq ( true , exec_lua ( 'return vim.tbl_contains({ 1, 2, 3 }, ...)' , v )) 920end 921end ) 922 923it ( 'vim.tbl_map' , function () 924eq ( 925{} , 926exec_lua ( [[ 927return vim.tbl_map(function(v) return v * 2 end, {}) 928]] ) 929) 930eq ( 931{ 2 , 4 , 6 } , 932exec_lua ( [[ 933return vim.tbl_map(function(v) return v * 2 end, {1, 2, 3}) 934]] ) 935) 936eq ( 937{ { i =2 } , { i =4 } , { i =6 } } , 938exec_lua ( [[ 939return vim.tbl_map(function(v) return { i = v.i * 2 } end, {{i=1}, {i=2}, {i=3}}) 940]] ) 941) 942end ) 943 944it ( 'vim.tbl_filter' , function () 945eq ( 946{} , 947exec_lua ( [[ 948return vim.tbl_filter(function(v) return (v % 2) == 0 end, {}) 949]] ) 950) 951eq ( 952{ 2 } , 953exec_lua ( [[ 954return vim.tbl_filter(function(v) return (v % 2) == 0 end, {1, 2, 3}) 955]] ) 956) 957eq ( 958{ { i =2 } } , 959exec_lua ( [[ 960return vim.tbl_filter(function(v) return (v.i % 2) == 0 end, {{i=1}, {i=2}, {i=3}}) 961]] ) 962) 963end ) 964 965it ( 'vim.isarray' , function () 966eq ( true , exec_lua ( 'return vim.isarray({})' )) 967eq ( false , exec_lua ( 'return vim.isarray(vim.empty_dict())' )) 968eq ( true , exec_lua ( "return vim.isarray({'a', 'b', 'c'})" )) 969eq ( false , exec_lua ( "return vim.isarray({'a', '32', a='hello', b='baz'})" )) 970eq ( false , exec_lua ( "return vim.isarray({1, a='hello', b='baz'})" )) 971eq ( false , exec_lua ( "return vim.isarray({a='hello', b='baz', 1})" )) 972eq ( false , exec_lua ( "return vim.isarray({1, 2, nil, a='hello'})" )) 973eq ( true , exec_lua ( 'return vim.isarray({1, 2, nil, 4})' )) 974eq ( true , exec_lua ( 'return vim.isarray({nil, 2, 3, 4})' )) 975eq ( false , exec_lua ( 'return vim.isarray({1, [1.5]=2, [3]=3})' )) 976end ) 977 978it ( 'vim.islist' , function () 979eq ( true , exec_lua ( 'return vim.islist({})' )) 980eq ( false , exec_lua ( 'return vim.islist(vim.empty_dict())' )) 981eq ( true , exec_lua ( "return vim.islist({'a', 'b', 'c'})" )) 982eq ( false , exec_lua ( "return vim.islist({'a', '32', a='hello', b='baz'})" )) 983eq ( false , exec_lua ( "return vim.islist({1, a='hello', b='baz'})" )) 984eq ( false , exec_lua ( "return vim.islist({a='hello', b='baz', 1})" )) 985eq ( false , exec_lua ( "return vim.islist({1, 2, nil, a='hello'})" )) 986eq ( false , exec_lua ( 'return vim.islist({1, 2, nil, 4})' )) 987eq ( false , exec_lua ( 'return vim.islist({nil, 2, 3, 4})' )) 988eq ( false , exec_lua ( 'return vim.islist({1, [1.5]=2, [3]=3})' )) 989eq ( 990false , 991exec_lua ( [[ 992local t = setmetatable({ 1, [3] = 3 }, { 993__index = function() 994return 2 995end, 996}) 997return vim.islist(t) 998]] ) 999) 1000end ) 1001 1002it ( 'vim.isnil' , function () 1003eq ( true , exec_lua ( 'return vim.isnil(nil)' )) 1004eq ( true , exec_lua ( 'return vim.isnil(vim.NIL)' )) 1005eq ( false , exec_lua ( 'return vim.isnil(true)' )) 1006eq ( false , exec_lua ( 'return vim.isnil(false)' )) 1007eq ( false , exec_lua ( 'return vim.isnil({})' )) 1008end ) 1009 1010it ( 'vim.tbl_isempty' , function () 1011eq ( true , exec_lua ( 'return vim.tbl_isempty({})' )) 1012eq ( false , exec_lua ( 'return vim.tbl_isempty({ 1, 2, 3 })' )) 1013eq ( false , exec_lua ( 'return vim.tbl_isempty({a=1, b=2, c=3})' )) 1014end ) 1015 1016it ( 'vim.tbl_get' , function () 1017eq ( 1018true , 1019exec_lua ( "return vim.tbl_get({ test = { nested_test = true }}, 'test', 'nested_test')" ) 1020) 1021eq ( NIL , exec_lua ( "return vim.tbl_get({ unindexable = true }, 'unindexable', 'missing_key')" )) 1022eq ( NIL , exec_lua ( "return vim.tbl_get({ unindexable = 1 }, 'unindexable', 'missing_key')" )) 1023eq ( 1024NIL , 1025exec_lua ( 1026"return vim.tbl_get({ unindexable = coroutine.create(function () end) }, 'unindexable', 'missing_key')" 1027) 1028) 1029eq ( 1030NIL , 1031exec_lua ( 1032"return vim.tbl_get({ unindexable = function () end }, 'unindexable', 'missing_key')" 1033) 1034) 1035eq ( NIL , exec_lua ( "return vim.tbl_get({}, 'missing_key')" )) 1036eq ( NIL , exec_lua ( 'return vim.tbl_get({})' )) 1037eq ( NIL , exec_lua ( "return vim.tbl_get({}, nil, 'key')" )) 1038eq ( 1 , exec_lua ( "return select('#', vim.tbl_get({}))" )) 1039eq ( 1 , exec_lua ( "return select('#', vim.tbl_get({ nested = {} }, 'nested', 'missing_key'))" )) 1040end ) 1041 1042it ( 'vim.tbl_extend' , function () 1043ok ( exec_lua ( [[ 1044local a = {x = 1} 1045local b = {y = 2} 1046local c = vim.tbl_extend("keep", a, b) 1047 1048return c.x == 1 and b.y == 2 and vim.tbl_count(c) == 2 1049]] )) 1050 1051ok ( exec_lua ( [[ 1052local a = {x = 1} 1053local b = {y = 2} 1054local c = {z = 3} 1055local d = vim.tbl_extend("keep", a, b, c) 1056 1057return d.x == 1 and d.y == 2 and d.z == 3 and vim.tbl_count(d) == 3 1058]] )) 1059 1060ok ( exec_lua ( [[ 1061local a = {x = 1} 1062local b = {x = 3} 1063local c = vim.tbl_extend("keep", a, b) 1064 1065return c.x == 1 and vim.tbl_count(c) == 1 1066]] )) 1067 1068ok ( exec_lua ( [[ 1069local a = {x = 1} 1070local b = {x = 3} 1071local c = vim.tbl_extend("force", a, b) 1072 1073return c.x == 3 and vim.tbl_count(c) == 1 1074]] )) 1075 1076ok ( exec_lua ( [[ 1077local a = vim.empty_dict() 1078local b = {} 1079local c = vim.tbl_extend("keep", a, b) 1080 1081return not vim.islist(c) and vim.tbl_count(c) == 0 1082]] )) 1083 1084ok ( exec_lua ( [[ 1085local a = {} 1086local b = vim.empty_dict() 1087local c = vim.tbl_extend("keep", a, b) 1088 1089return vim.islist(c) and vim.tbl_count(c) == 0 1090]] )) 1091 1092ok ( exec_lua ( [[ 1093local a = {x = {a = 1, b = 2}} 1094local b = {x = {a = 2, c = {y = 3}}} 1095local c = vim.tbl_extend("keep", a, b) 1096 1097local count = 0 1098for _ in pairs(c) do count = count + 1 end 1099 1100return c.x.a == 1 and c.x.b == 2 and c.x.c == nil and count == 1 1101]] )) 1102 1103ok ( exec_lua ( [[ 1104local a = { a = 1, b = 2, c = 1 } 1105local b = { a = -1, b = 5, c = 3, d = 4 } 1106-- Return the maximum value for each key. 1107local c = vim.tbl_extend(function(k, prev_v, v) 1108if prev_v then 1109return v > prev_v and v or prev_v 1110else 1111return v 1112end 1113end, a, b) 1114return vim.deep_equal(c, { a = 1, b = 5, c = 3, d = 4 }) 1115]] )) 1116 1117matches ( 1118'invalid "behavior": nil' , 1119pcall_err ( 1120exec_lua , 1121[[ 1122return vim.tbl_extend() 1123]] 1124) 1125) 1126 1127matches ( 1128'wrong number of arguments %(given 1, expected at least 3%)' , 1129pcall_err ( 1130exec_lua , 1131[[ 1132return vim.tbl_extend("keep") 1133]] 1134) 1135) 1136 1137matches ( 1138'wrong number of arguments %(given 2, expected at least 3%)' , 1139pcall_err ( 1140exec_lua , 1141[[ 1142return vim.tbl_extend("keep", {}) 1143]] 1144) 1145) 1146end ) 1147 1148it ( 'vim.tbl_deep_extend' , function () 1149ok ( exec_lua ( [[ 1150local a = {x = {a = 1, b = 2}} 1151local b = {x = {a = 2, c = {y = 3}}} 1152local c = vim.tbl_deep_extend("keep", a, b) 1153 1154local count = 0 1155for _ in pairs(c) do count = count + 1 end 1156 1157return c.x.a == 1 and c.x.b == 2 and c.x.c.y == 3 and count == 1 1158]] )) 1159 1160ok ( exec_lua ( [[ 1161local a = {x = {a = 1, b = 2}} 1162local b = {x = {a = 2, c = {y = 3}}} 1163local c = vim.tbl_deep_extend("force", a, b) 1164 1165local count = 0 1166for _ in pairs(c) do count = count + 1 end 1167 1168return c.x.a == 2 and c.x.b == 2 and c.x.c.y == 3 and count == 1 1169]] )) 1170 1171ok ( exec_lua ( [[ 1172local a = {x = {a = 1, b = 2}} 1173local b = {x = {a = 2, c = {y = 3}}} 1174local c = {x = {c = 4, d = {y = 4}}} 1175local d = vim.tbl_deep_extend("keep", a, b, c) 1176 1177local count = 0 1178for _ in pairs(c) do count = count + 1 end 1179 1180return d.x.a == 1 and d.x.b == 2 and d.x.c.y == 3 and d.x.d.y == 4 and count == 1 1181]] )) 1182 1183ok ( exec_lua ( [[ 1184local a = {x = {a = 1, b = 2}} 1185local b = {x = {a = 2, c = {y = 3}}} 1186local c = {x = {c = 4, d = {y = 4}}} 1187local d = vim.tbl_deep_extend("force", a, b, c) 1188 1189local count = 0 1190for _ in pairs(c) do count = count + 1 end 1191 1192return d.x.a == 2 and d.x.b == 2 and d.x.c == 4 and d.x.d.y == 4 and count == 1 1193]] )) 1194 1195ok ( exec_lua ( [[ 1196local a = vim.empty_dict() 1197local b = {} 1198local c = vim.tbl_deep_extend("keep", a, b) 1199 1200local count = 0 1201for _ in pairs(c) do count = count + 1 end 1202 1203return not vim.islist(c) and count == 0 1204]] )) 1205 1206ok ( exec_lua ( [[ 1207local a = {} 1208local b = vim.empty_dict() 1209local c = vim.tbl_deep_extend("keep", a, b) 1210 1211local count = 0 1212for _ in pairs(c) do count = count + 1 end 1213 1214return vim.islist(c) and count == 0 1215]] )) 1216 1217eq ( 1218{ a ={ b =1 } } , 1219exec_lua ( [[ 1220local a = { a = { b = 1 } } 1221local b = { a = {} } 1222return vim.tbl_deep_extend("force", a, b) 1223]] ) 1224) 1225 1226eq ( 1227{ a ={ b =1 } } , 1228exec_lua ( [[ 1229local a = { a = 123 } 1230local b = { a = { b = 1} } 1231return vim.tbl_deep_extend("force", a, b) 1232]] ) 1233) 1234 1235ok ( exec_lua ( [[ 1236local a = { a = {[2] = 3} } 1237local b = { a = {[3] = 3} } 1238local c = vim.tbl_deep_extend("force", a, b) 1239return vim.deep_equal(c, {a = {[2] = 3, [3] = 3}}) 1240]] )) 1241 1242eq ( 1243{ a =123 } , 1244exec_lua ( [[ 1245local a = { a = { b = 1} } 1246local b = { a = 123 } 1247return vim.tbl_deep_extend("force", a, b) 1248]] ) 1249) 1250 1251ok ( exec_lua ( [[ 1252local a = { sub = { 'a', 'b' } } 1253local b = { sub = { 'b', 'c' } } 1254local c = vim.tbl_deep_extend('force', a, b) 1255return vim.deep_equal(c, { sub = { 'b', 'c' } }) 1256]] )) 1257 1258ok ( exec_lua ( [[ 1259local a = { a = 1, b = 2, c = { d = 1, e = -2} } 1260local b = { a = -1, b = 5, c = { d = 6 } } 1261-- Return the maximum value for each key. 1262local c = vim.tbl_deep_extend(function(k, prev_v, v) 1263if prev_v then 1264return v > prev_v and v or prev_v 1265else 1266return v 1267end 1268end, a, b) 1269return vim.deep_equal(c, { a = 1, b = 5, c = { d = 6, e = -2 } }) 1270]] )) 1271 1272matches ( 'invalid "behavior": nil' , pcall_err ( exec_lua , [[return vim.tbl_deep_extend()]] )) 1273 1274matches ( 1275'wrong number of arguments %(given 1, expected at least 3%)' , 1276pcall_err ( exec_lua , [[return vim.tbl_deep_extend("keep")]] ) 1277) 1278 1279matches ( 1280'wrong number of arguments %(given 2, expected at least 3%)' , 1281pcall_err ( exec_lua , [[return vim.tbl_deep_extend("keep", {})]] ) 1282) 1283 1284matches ( 1285'after the second argument%: expected table, got number' , 1286pcall_err ( exec_lua , [[return vim.tbl_deep_extend("keep", {}, 42)]] ) 1287) 1288end ) 1289 1290it ( 'vim.tbl_count' , function () 1291eq ( 0 , exec_lua [[ return vim.tbl_count({}) ]] ) 1292eq ( 0 , exec_lua [[ return vim.tbl_count(vim.empty_dict()) ]] ) 1293eq ( 0 , exec_lua [[ return vim.tbl_count({nil}) ]] ) 1294eq ( 0 , exec_lua [[ return vim.tbl_count({a=nil}) ]] ) 1295eq ( 1 , exec_lua [[ return vim.tbl_count({1}) ]] ) 1296eq ( 2 , exec_lua [[ return vim.tbl_count({1, 2}) ]] ) 1297eq ( 2 , exec_lua [[ return vim.tbl_count({1, nil, 3}) ]] ) 1298eq ( 1 , exec_lua [[ return vim.tbl_count({a=1}) ]] ) 1299eq ( 2 , exec_lua [[ return vim.tbl_count({a=1, b=2}) ]] ) 1300eq ( 2 , exec_lua [[ return vim.tbl_count({a=1, b=nil, c=3}) ]] ) 1301end ) 1302 1303it ( 'vim.deep_equal' , function () 1304eq ( true , exec_lua [[ return vim.deep_equal({a=1}, {a=1}) ]] ) 1305eq ( true , exec_lua [[ return vim.deep_equal({a={b=1}}, {a={b=1}}) ]] ) 1306eq ( true , exec_lua [[ return vim.deep_equal({a={b={nil}}}, {a={b={}}}) ]] ) 1307eq ( true , exec_lua [[ return vim.deep_equal({a=1, [5]=5}, {nil,nil,nil,nil,5,a=1}) ]] ) 1308eq ( 1309true , 1310exec_lua [[ local shared = {}; return vim.deep_equal({ 1, shared, 1, shared }, { 1, {}, 1, {} }) ]] 1311) 1312-- cyclic table 1313eq ( true , exec_lua [[ local a,b={},{}; a[1]=a; b[1]=b; return vim.deep_equal(a, b) ]] ) 1314eq ( false , exec_lua [[ return vim.deep_equal(1, {nil,nil,nil,nil,5,a=1}) ]] ) 1315eq ( false , exec_lua [[ return vim.deep_equal(1, 3) ]] ) 1316eq ( false , exec_lua [[ return vim.deep_equal(nil, 3) ]] ) 1317eq ( false , exec_lua [[ return vim.deep_equal({a=1}, {a=2}) ]] ) 1318eq ( false , exec_lua [[ local a,b={},{}; a[1]=a; b[1]={}; return vim.deep_equal(a, b) ]] ) 1319end ) 1320 1321it ( 'vim.list_extend' , function () 1322eq ( { 1 , 2 , 3 } , exec_lua [[ return vim.list_extend({1}, {2,3}) ]] ) 1323matches ( 1324'src: expected table, got nil' , 1325pcall_err ( exec_lua , [[ return vim.list_extend({1}, nil) ]] ) 1326) 1327eq ( { 1 , 2 } , exec_lua [[ return vim.list_extend({1}, {2;a=1}) ]] ) 1328eq ( true , exec_lua [[ local a = {1} return vim.list_extend(a, {2;a=1}) == a ]] ) 1329eq ( { 2 } , exec_lua [[ return vim.list_extend({}, {2;a=1}, 1) ]] ) 1330eq ( {} , exec_lua [[ return vim.list_extend({}, {2;a=1}, 2) ]] ) 1331eq ( {} , exec_lua [[ return vim.list_extend({}, {2;a=1}, 1, -1) ]] ) 1332eq ( { 2 } , exec_lua [[ return vim.list_extend({}, {2;a=1}, -1, 2) ]] ) 1333end ) 1334 1335it ( 'vim.spairs' , function () 1336local res ='' 1337local table ={ 1338ccc =1 , 1339bbb =2 , 1340ddd =3 , 1341aaa =4 , 1342} 1343for key , _ in vim . spairs ( table ) do 1344res =res .. key 1345end 1346matches ( 'aaabbbcccddd' , res ) 1347end ) 1348 1349it ( 'vim.call, vim.fn' , function () 1350eq ( true , exec_lua ( [[return vim.call('sin', 0.0) == 0.0 ]] )) 1351eq ( true , exec_lua ( [[return vim.fn.sin(0.0) == 0.0 ]] )) 1352-- compat: nvim_call_function uses "special" value for Vimscript float 1353eq ( false , exec_lua ( [[return vim.api.nvim_call_function('sin', {0.0}) == 0.0 ]] )) 1354 1355exec ( [[ 1356func! FooFunc(test) 1357let g:test = a:test 1358return {} 1359endfunc 1360func! VarArg(...) 1361return a:000 1362endfunc 1363func! Nilly() 1364return [v:null, v:null] 1365endfunc 1366]] ) 1367eq ( true , exec_lua ( [[return next(vim.fn.FooFunc(3)) == nil ]] )) 1368eq ( 3 , eval ( 'g:test' )) 1369eq ( true , exec_lua ( [[return vim.tbl_isempty(vim.api.nvim_call_function("FooFunc", {5}))]] )) 1370eq ( 5 , eval ( 'g:test' )) 1371 1372eq ( { 2 , 'foo' , true } , exec_lua ( [[return vim.fn.VarArg(2, "foo", true)]] )) 1373 1374eq ( 1375true , 1376exec_lua ( [[ 1377local x = vim.fn.Nilly() 1378return #x == 2 and x[1] == vim.NIL and x[2] == vim.NIL 1379]] ) 1380) 1381eq ( { NIL , NIL } , exec_lua ( [[return vim.fn.Nilly()]] )) 1382 1383-- error handling 1384eq ( 1385{ false , 'Vim:E897: List or Blob required' } , 1386exec_lua ( [[return {pcall(vim.fn.add, "aa", "bb")}]] ) 1387) 1388 1389-- conversion between LuaRef and Vim Funcref 1390eq ( 1391true , 1392exec_lua ( [[ 1393local x = vim.fn.VarArg(function() return 'foo' end, function() return 'bar' end) 1394return #x == 2 and x[1]() == 'foo' and x[2]() == 'bar' 1395]] ) 1396) 1397 1398-- Test for #20211 1399eq ( 1400'a (b) c' , 1401exec_lua ( [[ 1402return vim.fn.substitute('a b c', 'b', function(m) return '(' .. m[1] .. ')' end, 'g') 1403]] ) 1404) 1405end ) 1406 1407it ( 'vim.fn `func_lua` (fast path for Lua-implemented builtins)' , function () 1408-- hostname() is implemented via func_lua, calling Lua directly when invoked from Lua. 1409local lua_result =exec_lua ( [[return vim.fn.hostname()]] ) 1410eq ( 'string' , type ( lua_result )) 1411assert ( # lua_result > 0 , 'hostname() should return a non-empty string' ) 1412-- VimScript path (lua_wrapper) should return the same result. 1413eq ( lua_result , eval ( 'hostname()' )) 1414end ) 1415 1416it ( 'vim.call fails in fast context' , function () 1417local screen =Screen . new ( 120 , 10 ) 1418exec_lua ( [[ 1419local timer = vim.uv.new_timer() 1420timer:start(0, 0, function() 1421timer:close() 1422vim.call('sin', 0.0) 1423end) 1424]] ) 1425screen : expect ( { 1426any =pesc ( 'E5560: Vimscript function "sin" must not be called in a fast event context' ), 1427} ) 1428feed ( '<CR>' ) 1429assert_alive () 1430end ) 1431 1432it ( 'vim.fn errors when calling API function' , function () 1433matches ( 1434'Tried to call API function with vim.fn: use vim.api.nvim_get_current_line instead' , 1435pcall_err ( exec_lua , 'vim.fn.nvim_get_current_line()' ) 1436) 1437end ) 1438 1439it ( 'vim.fn is allowed in "fast" context by some functions #18306' , function () 1440exec_lua ( [[ 1441local timer = vim.uv.new_timer() 1442timer:start(0, 0, function() 1443timer:close() 1444assert(vim.in_fast_event()) 1445vim.g.fnres = vim.fn.iconv('hello', 'utf-8', 'utf-8') 1446end) 1447]] ) 1448 1449poke_eventloop () 1450eq ( 'hello' , exec_lua [[return vim.g.fnres]] ) 1451end ) 1452 1453it ( 'vim.rpcrequest and vim.rpcnotify' , function () 1454exec_lua ( [[ 1455chan = vim.fn.jobstart({'cat'}, {rpc=true}) 1456vim.rpcrequest(chan, 'nvim_set_current_line', 'meow') 1457]] ) 1458eq ( 'meow' , api . nvim_get_current_line ()) 1459command ( "let x = [3, 'aa', v:true, v:null]" ) 1460eq ( 1461true , 1462exec_lua ( [[ 1463ret = vim.rpcrequest(chan, 'nvim_get_var', 'x') 1464return #ret == 4 and ret[1] == 3 and ret[2] == 'aa' and ret[3] == true and ret[4] == vim.NIL 1465]] ) 1466) 1467eq ( { 3 , 'aa' , true , NIL } , exec_lua ( [[return ret]] )) 1468 1469eq ( 1470{ {} , {} , false , true } , 1471exec_lua ( [[ 1472vim.rpcrequest(chan, 'nvim_exec', 'let xx = {}\nlet yy = []', false) 1473local dict = vim.rpcrequest(chan, 'nvim_eval', 'xx') 1474local list = vim.rpcrequest(chan, 'nvim_eval', 'yy') 1475return {dict, list, vim.islist(dict), vim.islist(list)} 1476]] ) 1477) 1478 1479exec_lua ( [[ 1480vim.rpcrequest(chan, 'nvim_set_var', 'aa', {}) 1481vim.rpcrequest(chan, 'nvim_set_var', 'bb', vim.empty_dict()) 1482]] ) 1483eq ( { 1 , 1 } , eval ( '[type(g:aa) == type([]), type(g:bb) == type({})]' )) 1484 1485-- error handling 1486eq ( { false , 'Invalid channel: 23' } , exec_lua ( [[return {pcall(vim.rpcrequest, 23, 'foo')}]] )) 1487eq ( { false , 'Invalid channel: 23' } , exec_lua ( [[return {pcall(vim.rpcnotify, 23, 'foo')}]] )) 1488 1489eq ( 1490{ false , 'Vim:E121: Undefined variable: foobar' } , 1491exec_lua ( [[return {pcall(vim.rpcrequest, chan, 'nvim_eval', "foobar")}]] ) 1492) 1493 1494-- rpcnotify doesn't wait on request 1495eq ( 1496'meow' , 1497exec_lua ( [[ 1498vim.rpcnotify(chan, 'nvim_set_current_line', 'foo') 1499return vim.api.nvim_get_current_line() 1500]] ) 1501) 1502retry ( 10 , nil , function () 1503eq ( 'foo' , api . nvim_get_current_line ()) 1504end ) 1505 1506local screen =Screen . new ( 50 , 7 ) 1507exec_lua ( [[ 1508timer = vim.uv.new_timer() 1509timer:start(20, 0, function () 1510-- notify ok (executed later when safe) 1511vim.rpcnotify(chan, 'nvim_set_var', 'yy', {3, vim.NIL}) 1512-- rpcrequest an error 1513vim.rpcrequest(chan, 'nvim_set_current_line', 'bork') 1514end) 1515]] ) 1516screen : expect { 1517grid =[[ 1518{9:[string "<nvim>"]:6: E5560: rpcrequest must not be}| 1519{9: called in a fast event context} | 1520{9:stack traceback:} | 1521{9: [C]: in function 'rpcrequest'} | 1522{9: [string "<nvim>"]:6: in function <[string }| 1523{9:"<nvim>"]:2>} | 1524{6:Press ENTER or type command to continue}^ | 1525]] , 1526} 1527feed ( '<cr>' ) 1528retry ( 10 , nil , function () 1529eq ( { 3 , NIL } , api . nvim_get_var ( 'yy' )) 1530end ) 1531 1532exec_lua ( [[timer:close()]] ) 1533end ) 1534 1535it ( 'vim.empty_dict()' , function () 1536eq ( 1537{ true , false , true , true } , 1538exec_lua ( [[ 1539vim.api.nvim_set_var('listy', {}) 1540vim.api.nvim_set_var('dicty', vim.empty_dict()) 1541local listy = vim.fn.eval("listy") 1542local dicty = vim.fn.eval("dicty") 1543return {vim.islist(listy), vim.islist(dicty), next(listy) == nil, next(dicty) == nil} 1544]] ) 1545) 1546 1547-- vim.empty_dict() gives new value each time 1548-- equality is not overridden (still by ref) 1549-- non-empty table uses the usual heuristics (ignores the tag) 1550eq ( 1551{ false , { 'foo' } , { namey ='bar' } } , 1552exec_lua ( [[ 1553local aa = vim.empty_dict() 1554local bb = vim.empty_dict() 1555local equally = (aa == bb) 1556aa[1] = "foo" 1557bb["namey"] = "bar" 1558return {equally, aa, bb} 1559]] ) 1560) 1561 1562eq ( '{ {}, vim.empty_dict() }' , exec_lua ( 'return vim.inspect({{}, vim.empty_dict()})' )) 1563eq ( '{}' , exec_lua ( [[ return vim.fn.json_encode(vim.empty_dict()) ]] )) 1564eq ( '{"a": {}, "b": []}' , exec_lua ( [[ return vim.fn.json_encode({a=vim.empty_dict(), b={}}) ]] )) 1565end ) 1566 1567it ( 'vim.validate (fast form)' , function () 1568exec_lua ( "vim.validate('arg1', {}, 'table')" ) 1569exec_lua ( "vim.validate('arg1', nil, 'table', true)" ) 1570exec_lua ( "vim.validate('arg1', { foo='foo' }, 'table')" ) 1571exec_lua ( "vim.validate('arg1', { 'foo' }, 'table')" ) 1572exec_lua ( "vim.validate('arg1', 'foo', 'string')" ) 1573exec_lua ( "vim.validate('arg1', nil, 'string', true)" ) 1574exec_lua ( "vim.validate('arg1', 1, 'number')" ) 1575exec_lua ( "vim.validate('arg1', 0, 'number')" ) 1576exec_lua ( "vim.validate('arg1', 0.1, 'number')" ) 1577exec_lua ( "vim.validate('arg1', nil, 'number', true)" ) 1578exec_lua ( "vim.validate('arg1', true, 'boolean')" ) 1579exec_lua ( "vim.validate('arg1', false, 'boolean')" ) 1580exec_lua ( "vim.validate('arg1', nil, 'boolean', true)" ) 1581exec_lua ( "vim.validate('arg1', function()end, 'function')" ) 1582exec_lua ( "vim.validate('arg1', nil, 'function', true)" ) 1583exec_lua ( "vim.validate('arg1', nil, 'nil')" ) 1584exec_lua ( "vim.validate('arg1', nil, 'nil', true)" ) 1585exec_lua ( "vim.validate('arg1', coroutine.create(function()end), 'thread')" ) 1586exec_lua ( "vim.validate('arg1', nil, 'thread', true)" ) 1587exec_lua ( "vim.validate('arg1', 2, function(a) return (a % 2) == 0 end, 'even number')" ) 1588exec_lua ( "vim.validate('arg1', 5, {'number', 'string'})" ) 1589exec_lua ( "vim.validate('arg2', 'foo', {'number', 'string'})" ) 1590 1591matches ( 'arg1: expected number, got nil' , pcall_err ( vim . validate , 'arg1' , nil , 'number' )) 1592matches ( 'arg1: expected string, got nil' , pcall_err ( vim . validate , 'arg1' , nil , 'string' )) 1593matches ( 'arg1: expected table, got nil' , pcall_err ( vim . validate , 'arg1' , nil , 'table' )) 1594matches ( 'arg1: expected function, got nil' , pcall_err ( vim . validate , 'arg1' , nil , 'function' )) 1595matches ( 'arg1: expected string, got number' , pcall_err ( vim . validate , 'arg1' , 5 , 'string' )) 1596matches ( 'arg1: expected table, got number' , pcall_err ( vim . validate , 'arg1' , 5 , 'table' )) 1597matches ( 'arg1: expected function, got number' , pcall_err ( vim . validate , 'arg1' , 5 , 'function' )) 1598matches ( 'arg1: expected number, got string' , pcall_err ( vim . validate , 'arg1' , '5' , 'number' )) 1599matches ( 'arg1: expected x, got number' , pcall_err ( exec_lua , "vim.validate('arg1', 1, 'x')" )) 1600matches ( 'invalid validator: 1' , pcall_err ( exec_lua , "vim.validate('arg1', 1, 1)" )) 1601matches ( 'invalid arguments' , pcall_err ( exec_lua , "vim.validate('arg1', { 1 })" )) 1602 1603-- Validated parameters are required by default. 1604matches ( 1605'arg1: expected string, got nil' , 1606pcall_err ( exec_lua , "vim.validate('arg1', nil, 'string')" ) 1607) 1608-- Explicitly required. 1609matches ( 1610'arg1: expected string, got nil' , 1611pcall_err ( exec_lua , "vim.validate('arg1', nil, 'string', false)" ) 1612) 1613 1614matches ( 1615'arg1: expected table, got number' , 1616pcall_err ( exec_lua , "vim.validate('arg1', 1, 'table')" ) 1617) 1618 1619matches ( 1620'arg1: expected even number, got 3' , 1621pcall_err ( exec_lua , "vim.validate('arg1', 3, function(a) return a == 1 end, 'even number')" ) 1622) 1623matches ( 1624'arg1: expected %?, got 3' , 1625pcall_err ( exec_lua , "vim.validate('arg1', 3, function(a) return a == 1 end)" ) 1626) 1627matches ( 1628'arg1: expected number|string, got nil' , 1629pcall_err ( exec_lua , "vim.validate('arg1', nil, {'number', 'string'})" ) 1630) 1631 1632-- Validator func can return an extra "Info" message. 1633matches ( 1634'arg1: expected %?, got 3. Info: TEST_MSG' , 1635pcall_err ( exec_lua , "vim.validate('arg1', 3, function(a) return a == 1, 'TEST_MSG' end)" ) 1636) 1637-- Caller can override the "expected" message. 1638eq ( 1639'arg1: expected TEST_MSG, got nil' , 1640pcall_err ( exec_lua , "vim.validate('arg1', nil, 'table', 'TEST_MSG')" ) 1641) 1642end ) 1643 1644it ( 'vim.validate (spec form)' , function () 1645exec_lua ( "vim.validate{arg1={{}, 'table' }}" ) 1646exec_lua ( "vim.validate{arg1={{}, 't' }}" ) 1647exec_lua ( "vim.validate{arg1={nil, 't', true }}" ) 1648exec_lua ( "vim.validate{arg1={{ foo='foo' }, 't' }}" ) 1649exec_lua ( "vim.validate{arg1={{ 'foo' }, 't' }}" ) 1650exec_lua ( "vim.validate{arg1={'foo', 'string' }}" ) 1651exec_lua ( "vim.validate{arg1={'foo', 's' }}" ) 1652exec_lua ( "vim.validate{arg1={'', 's' }}" ) 1653exec_lua ( "vim.validate{arg1={nil, 's', true }}" ) 1654exec_lua ( "vim.validate{arg1={1, 'number' }}" ) 1655exec_lua ( "vim.validate{arg1={1, 'n' }}" ) 1656exec_lua ( "vim.validate{arg1={0, 'n' }}" ) 1657exec_lua ( "vim.validate{arg1={0.1, 'n' }}" ) 1658exec_lua ( "vim.validate{arg1={nil, 'n', true }}" ) 1659exec_lua ( "vim.validate{arg1={true, 'boolean' }}" ) 1660exec_lua ( "vim.validate{arg1={true, 'b' }}" ) 1661exec_lua ( "vim.validate{arg1={false, 'b' }}" ) 1662exec_lua ( "vim.validate{arg1={nil, 'b', true }}" ) 1663exec_lua ( "vim.validate{arg1={function()end, 'function' }}" ) 1664exec_lua ( "vim.validate{arg1={function()end, 'f' }}" ) 1665exec_lua ( "vim.validate{arg1={nil, 'f', true }}" ) 1666exec_lua ( "vim.validate{arg1={nil, 'nil' }}" ) 1667exec_lua ( "vim.validate{arg1={nil, 'nil', true }}" ) 1668exec_lua ( "vim.validate{arg1={coroutine.create(function()end), 'thread' }}" ) 1669exec_lua ( "vim.validate{arg1={nil, 'thread', true }}" ) 1670exec_lua ( "vim.validate{arg1={{}, 't' }, arg2={ 'foo', 's' }}" ) 1671exec_lua ( "vim.validate{arg1={2, function(a) return (a % 2) == 0 end, 'even number' }}" ) 1672exec_lua ( "vim.validate{arg1={5, {'n', 's'} }, arg2={ 'foo', {'n', 's'} }}" ) 1673 1674matches ( 'expected table, got number' , pcall_err ( exec_lua , "vim.validate{ 1, 'x' }" )) 1675matches ( 'arg1: expected x, got number' , pcall_err ( exec_lua , "vim.validate{ arg1={ 1, 'x' }}" )) 1676matches ( 'invalid validator: 1' , pcall_err ( exec_lua , 'vim.validate{ arg1={ 1, 1 }}' )) 1677matches ( 'invalid validator: nil' , pcall_err ( exec_lua , 'vim.validate{ arg1={ 1 }}' )) 1678 1679-- Validated parameters are required by default. 1680matches ( 1681'arg1: expected string, got nil' , 1682pcall_err ( exec_lua , "vim.validate{ arg1={ nil, 's' }}" ) 1683) 1684-- Explicitly required. 1685matches ( 1686'arg1: expected string, got nil' , 1687pcall_err ( exec_lua , "vim.validate{ arg1={ nil, 's', false }}" ) 1688) 1689 1690matches ( 'arg1: expected table, got number' , pcall_err ( exec_lua , "vim.validate{arg1={1, 't'}}" )) 1691matches ( 1692'arg2: expected string, got number' , 1693pcall_err ( exec_lua , "vim.validate{arg1={{}, 't'}, arg2={1, 's'}}" ) 1694) 1695matches ( 1696'arg2: expected string, got nil' , 1697pcall_err ( exec_lua , "vim.validate{arg1={{}, 't'}, arg2={nil, 's'}}" ) 1698) 1699matches ( 1700'arg2: expected string, got nil' , 1701pcall_err ( exec_lua , "vim.validate{arg1={{}, 't'}, arg2={nil, 's'}}" ) 1702) 1703matches ( 1704'arg1: expected even number, got 3' , 1705pcall_err ( exec_lua , "vim.validate{arg1={3, function(a) return a == 1 end, 'even number'}}" ) 1706) 1707matches ( 1708'arg1: expected %?, got 3' , 1709pcall_err ( exec_lua , 'vim.validate{arg1={3, function(a) return a == 1 end}}' ) 1710) 1711matches ( 1712'arg1: expected number|string, got nil' , 1713pcall_err ( exec_lua , "vim.validate{ arg1={ nil, {'n', 's'} }}" ) 1714) 1715 1716-- Pass an additional message back. 1717matches ( 1718'arg1: expected %?, got 3. Info: TEST_MSG' , 1719pcall_err ( exec_lua , "vim.validate{arg1={3, function(a) return a == 1, 'TEST_MSG' end}}" ) 1720) 1721end ) 1722 1723it ( 'vim.is_callable' , function () 1724eq ( true , exec_lua ( 'return vim.is_callable(function()end)' )) 1725eq ( 1726true , 1727exec_lua ( [[ 1728local meta = { __call = function()end } 1729local function new_callable() 1730return setmetatable({}, meta) 1731end 1732local callable = new_callable() 1733return vim.is_callable(callable) 1734]] ) 1735) 1736 1737eq ( 1738{ false , false } , 1739exec_lua ( [[ 1740local meta = { __call = {} } 1741assert(meta.__call) 1742local function new() 1743return setmetatable({}, meta) 1744end 1745local not_callable = new() 1746return { pcall(function() not_callable() end), vim.is_callable(not_callable) } 1747]] ) 1748) 1749eq ( 1750{ false , false } , 1751exec_lua ( [[ 1752local function new() 1753return { __call = function()end } 1754end 1755local not_callable = new() 1756assert(not_callable.__call) 1757return { pcall(function() not_callable() end), vim.is_callable(not_callable) } 1758]] ) 1759) 1760eq ( 1761{ false , false } , 1762exec_lua ( [[ 1763local meta = setmetatable( 1764{ __index = { __call = function() end } }, 1765{ __index = { __call = function() end } } 1766) 1767assert(meta.__call) 1768local not_callable = setmetatable({}, meta) 1769assert(not_callable.__call) 1770return { pcall(function() not_callable() end), vim.is_callable(not_callable) } 1771]] ) 1772) 1773eq ( 1774{ false , false } , 1775exec_lua ( [[ 1776local meta = setmetatable({ 1777__index = function() 1778return function() end 1779end, 1780}, { 1781__index = function() 1782return function() end 1783end, 1784}) 1785assert(meta.__call) 1786local not_callable = setmetatable({}, meta) 1787assert(not_callable.__call) 1788return { pcall(function() not_callable() end), vim.is_callable(not_callable) } 1789]] ) 1790) 1791eq ( false , exec_lua ( 'return vim.is_callable(1)' )) 1792eq ( false , exec_lua ( "return vim.is_callable('foo')" )) 1793eq ( false , exec_lua ( 'return vim.is_callable({})' )) 1794end ) 1795 1796it ( 'vim.cmd' , function () 1797exec_lua [[ 1798vim.cmd "autocmd BufNew * ++once lua BUF = vim.fn.expand('<abuf>')" 1799vim.cmd "new" 1800]] 1801eq ( '2' , fn . luaeval 'BUF' ) 1802eq ( 2 , fn . luaeval '#vim.api.nvim_list_bufs()' ) 1803 1804-- vim.cmd can be indexed with a command name 1805exec_lua [[ 1806vim.cmd.let 'g:var = 2' 1807]] 1808 1809eq ( 2 , fn . luaeval 'vim.g.var' ) 1810 1811-- Passing many arguments to indexed vim.cmd (#41314) 1812local props =exec_lua [[ 1813local props = {} 1814for i = 1, 300 do 1815props[i] = ('custom_property_%03d'):format(i) 1816end 1817vim.cmd.syntax { 'keyword', 'editorconfigProperty', unpack(props) } 1818return props 1819]] 1820 1821local output =n . exec_capture ( 'syntax list editorconfigProperty' ) 1822output =output : match ( '^%-%-%- Syntax items %-%-%-\neditorconfigProperty xxx (.*)' ) 1823local output_list =vim . split ( output , ' ' ) 1824table . sort ( output_list ) 1825eq ( props , output_list ) 1826end ) 1827 1828it ( 'vim.regex' , function () 1829exec_lua [[ 1830re1 = vim.regex"ab\\+c" 1831vim.cmd "set nomagic ignorecase" 1832re2 = vim.regex"xYz" 1833]] 1834eq ( {} , exec_lua [[return {re1:match_str("x ac")}]] ) 1835eq ( { 3 , 7 } , exec_lua [[return {re1:match_str("ac abbc")}]] ) 1836 1837api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'yy' , 'abc abbc' } ) 1838eq ( {} , exec_lua [[return {re1:match_line(0, 0)}]] ) 1839eq ( { 0 , 3 } , exec_lua [[return {re1:match_line(0, 1)}]] ) 1840eq ( { 3 , 7 } , exec_lua [[return {re1:match_line(0, 1, 1)}]] ) 1841eq ( { 3 , 7 } , exec_lua [[return {re1:match_line(0, 1, 1, 8)}]] ) 1842eq ( {} , exec_lua [[return {re1:match_line(0, 1, 1, 7)}]] ) 1843eq ( { 0 , 3 } , exec_lua [[return {re1:match_line(0, 1, 0, 7)}]] ) 1844 1845-- vim.regex() error inside :silent! should not crash. #20546 1846command ( [[silent! lua vim.regex('\\z')]] ) 1847assert_alive () 1848 1849-- match_str() in a fast (luv) callback does not abort. #18111 1850eq ( 1851{ true , true } , 1852exec_lua ( function () 1853local re =vim . regex ( '^.*\\.go$' ) 1854local res 1855local timer =assert ( vim . uv . new_timer ()) 1856timer : start ( 10 , 0 , function () 1857-- Enough matches to reach BREAKCHECK_SKIP, which would abort (reentrant event loop). 1858local ok =true 1859for _ =1 , 3000 do 1860ok =ok and re : match_str ( 'internal/config/config.go' ) ~= nil 1861end 1862res ={ ok , vim . in_fast_event () } 1863timer : close () 1864end ) 1865vim . wait ( 1000 , function () 1866return res ~= nil 1867end ) 1868return res 1869end ) 1870) 1871end ) 1872 1873it ( 'vim.defer_fn' , function () 1874eq ( 1875false , 1876exec_lua [[ 1877vim.g.test = false 1878vim.defer_fn(function() vim.g.test = true end, 150) 1879return vim.g.test 1880]] 1881) 1882exec_lua [[vim.wait(1000, function() return vim.g.test end)]] 1883eq ( true , exec_lua [[return vim.g.test]] ) 1884end ) 1885 1886it ( 'nested vim.defer_fn does not leak handles on exit #19727' , function () 1887n . expect_exit ( exec_lua , function () 1888vim . defer_fn ( function () 1889vim . defer_fn ( function () 1890vim . defer_fn ( function () end , 0 ) 1891end , 0 ) 1892end , 0 ) 1893vim . cmd ( 'qall' ) 1894end ) 1895end ) 1896 1897it ( 'vim.defer_fn with timeout does not leak handles on exit' , function () 1898n . expect_exit ( exec_lua , function () 1899vim . defer_fn ( function () end , 50 ) 1900vim . cmd ( 'qall' ) 1901end ) 1902end ) 1903 1904describe ( 'vim.region' , function () 1905it ( 'charwise' , function () 1906insert ( dedent ( [[ 1907text tααt tααt text 1908text tαxt txtα tex 1909text tαxt tαxt 1910]] )) 1911eq ( { 5 , 13 } , exec_lua [[ return vim.region(0,{0,5},{0,13},'v',false)[0] ]] ) 1912eq ( { 5 , 15 } , exec_lua [[ return vim.region(0,{0,5},{0,13},'v',true)[0] ]] ) 1913eq ( { 5 , 15 } , exec_lua [[ return vim.region(0,{0,5},{0,14},'v',true)[0] ]] ) 1914eq ( { 5 , 15 } , exec_lua [[ return vim.region(0,{0,5},{0,15},'v',false)[0] ]] ) 1915eq ( { 5 , 17 } , exec_lua [[ return vim.region(0,{0,5},{0,15},'v',true)[0] ]] ) 1916eq ( { 5 , 17 } , exec_lua [[ return vim.region(0,{0,5},{0,16},'v',true)[0] ]] ) 1917eq ( { 5 , 17 } , exec_lua [[ return vim.region(0,{0,5},{0,17},'v',false)[0] ]] ) 1918eq ( { 5 , 18 } , exec_lua [[ return vim.region(0,{0,5},{0,17},'v',true)[0] ]] ) 1919end ) 1920it ( 'blockwise' , function () 1921insert ( [[αα]] ) 1922eq ( { 0 , 5 } , exec_lua [[ return vim.region(0,{0,0},{0,4},'3',true)[0] ]] ) 1923end ) 1924it ( 'linewise' , function () 1925insert ( dedent ( [[ 1926text tααt tααt text 1927text tαxt txtα tex 1928text tαxt tαxt 1929]] )) 1930eq ( { 0 , - 1 } , exec_lua [[ return vim.region(0,{1,5},{1,14},'V',true)[1] ]] ) 1931end ) 1932it ( 'getpos() input' , function () 1933insert ( 'getpos' ) 1934eq ( { 0 , 6 } , exec_lua [[ return vim.region(0,{0,0},'.','v',true)[0] ]] ) 1935end ) 1936end ) 1937 1938describe ( 'vim.on_key' , function () 1939it ( 'tracks Unicode input' , function () 1940insert ( [[hello world ]] ) 1941 1942exec_lua [[ 1943keys = {} 1944typed = {} 1945 1946vim.on_key(function(buf, typed_buf) 1947if buf:byte() == 27 then 1948buf = "<ESC>" 1949end 1950if typed_buf:byte() == 27 then 1951typed_buf = "<ESC>" 1952end 1953 1954table.insert(keys, buf) 1955table.insert(typed, typed_buf) 1956end) 1957]] 1958 1959insert ( [[next 🤦 lines å …]] ) 1960 1961-- It has escape in the keys pressed 1962eq ( 'inext 🤦 lines å …<ESC>' , exec_lua [[return table.concat(keys, '')]] ) 1963eq ( 'inext 🤦 lines å …<ESC>' , exec_lua [[return table.concat(typed, '')]] ) 1964end ) 1965 1966it ( 'tracks input with modifiers' , function () 1967exec_lua [[ 1968keys = {} 1969typed = {} 1970 1971vim.on_key(function(buf, typed_buf) 1972table.insert(keys, vim.fn.keytrans(buf)) 1973table.insert(typed, vim.fn.keytrans(typed_buf)) 1974end) 1975]] 1976 1977feed ( [[i<C-V><C-;><C-V><C-…><Esc>]] ) 1978 1979eq ( 'i<C-V><C-;><C-V><C-…><Esc>' , exec_lua [[return table.concat(keys, '')]] ) 1980eq ( 'i<C-V><C-;><C-V><C-…><Esc>' , exec_lua [[return table.concat(typed, '')]] ) 1981end ) 1982 1983it ( 'works with character find and Select mode' , function () 1984insert ( '12345' ) 1985 1986exec_lua [[ 1987typed = {} 1988 1989vim.cmd('snoremap # @') 1990 1991vim.on_key(function(buf, typed_buf) 1992table.insert(typed, vim.fn.keytrans(typed_buf)) 1993end) 1994]] 1995 1996feed ( 'F3gHβγδεζ<Esc>gH…<Esc>gH#$%^' ) 1997eq ( 'F3gHβγδεζ<Esc>gH…<Esc>gH#$%^' , exec_lua [[return table.concat(typed, '')]] ) 1998end ) 1999 2000it ( 'allows removing on_key listeners' , function () 2001-- Create some unused namespaces 2002api . nvim_create_namespace ( 'unused1' ) 2003api . nvim_create_namespace ( 'unused2' ) 2004api . nvim_create_namespace ( 'unused3' ) 2005api . nvim_create_namespace ( 'unused4' ) 2006 2007insert ( [[hello world]] ) 2008 2009exec_lua [[ 2010keys = {} 2011 2012return vim.on_key(function(buf) 2013if buf:byte() == 27 then 2014buf = "<ESC>" 2015end 2016 2017table.insert(keys, buf) 2018end, vim.api.nvim_create_namespace("logger")) 2019]] 2020 2021insert ( [[next lines]] ) 2022 2023eq ( 1 , exec_lua ( 'return vim.on_key()' )) 2024exec_lua ( "vim.on_key(nil, vim.api.nvim_create_namespace('logger'))" ) 2025eq ( 0 , exec_lua ( 'return vim.on_key()' )) 2026 2027insert ( [[more lines]] ) 2028 2029-- It has escape in the keys pressed 2030eq ( 'inext lines<ESC>' , exec_lua [[return table.concat(keys, '')]] ) 2031end ) 2032 2033it ( 'skips any function that caused an error and shows stacktrace' , function () 2034insert ( [[hello world]] ) 2035 2036exec_lua [[ 2037local function ErrF2() 2038error("Dumb Error") 2039end 2040local function ErrF1() 2041ErrF2() 2042end 2043 2044keys = {} 2045 2046return vim.on_key(function(buf) 2047if buf:byte() == 27 then 2048buf = "<ESC>" 2049end 2050 2051table.insert(keys, buf) 2052 2053if buf == 'l' then 2054ErrF1() 2055end 2056end) 2057]] 2058 2059insert ( [[next lines]] ) 2060insert ( [[more lines]] ) 2061 2062-- Only the first letter gets added. After that we remove the callback 2063eq ( 'inext l' , exec_lua [[ return table.concat(keys, '') ]] ) 2064 2065local errmsg =api . nvim_get_vvar ( 'errmsg' ) 2066matches ( 2067t . dedent [[ 2068^vim%.on%_key%(%) callbacks:.* 2069With ns%_id %d+: .*: Dumb Error 2070stack traceback: 2071.*: in function 'error' 2072.*: in function 'ErrF2' 2073.*: in function 'ErrF1' 2074.*]] , 2075errmsg 2076) 2077end ) 2078 2079it ( 'argument 1 is keys after mapping, argument 2 is typed keys' , function () 2080exec_lua [[ 2081keys = {} 2082typed = {} 2083 2084vim.cmd("inoremap hello world") 2085 2086vim.on_key(function(buf, typed_buf) 2087if buf:byte() == 27 then 2088buf = "<ESC>" 2089end 2090if typed_buf:byte() == 27 then 2091typed_buf = "<ESC>" 2092end 2093 2094table.insert(keys, buf) 2095table.insert(typed, typed_buf) 2096end) 2097]] 2098insert ( 'hello' ) 2099 2100eq ( 'iworld<ESC>' , exec_lua [[return table.concat(keys, '')]] ) 2101eq ( 'ihello<ESC>' , exec_lua [[return table.concat(typed, '')]] ) 2102end ) 2103 2104it ( 'can call vim.fn functions on Ctrl-C #17273' , function () 2105exec_lua ( [[ 2106_G.ctrl_c_cmdtype = '' 2107 2108vim.on_key(function(c) 2109if c == '\3' then 2110_G.ctrl_c_cmdtype = vim.fn.getcmdtype() 2111end 2112end) 2113]] ) 2114feed ( '/' ) 2115poke_eventloop () -- This is needed because Ctrl-C flushes input 2116feed ( '<C-C>' ) 2117eq ( '/' , exec_lua ( [[return _G.ctrl_c_cmdtype]] )) 2118end ) 2119 2120it ( 'not invoked recursively #30752' , function () 2121local screen =Screen . new ( 60 , 10 ) 2122exec_lua ( [[ 2123vim.on_key(function(key, typed) 2124vim.api.nvim_echo({ 2125{ 'key_cb\n' }, 2126{ ("KEYCB: key '%s', typed '%s'\n"):format(key, typed) }, 2127}, false, {}) 2128end) 2129]] ) 2130feed ( '^' ) 2131screen : expect ( [[ 2132| 2133{1:~ }|*5 2134{3: }| 2135key_cb | 2136KEYCB: key '^', typed '^' | 2137{6:Press ENTER or type command to continue}^ | 2138]] ) 2139feed ( '<C-C>' ) 2140screen : expect ( [[ 2141| 2142{1:~ }|*3 2143{3: }| 2144key_cb | 2145KEYCB: key '^', typed '^' | 2146key_cb | 2147KEYCB: key '{18:^C}', typed '{18:^C}' | 2148{6:Press ENTER or type command to continue}^ | 2149]] ) 2150feed ( '<C-C>' ) 2151screen : expect ( [[ 2152^ | 2153{1:~ }|*8 2154| 2155]] ) 2156end ) 2157 2158it ( 'not invoked for keys consumed by getchar() #40010' , function () 2159exec_lua ( function () 2160_G . typed ={} 2161vim . on_key ( function ( _ , typed ) 2162table . insert ( _G . typed , vim . fn . keytrans ( typed )) 2163end ) 2164vim . keymap . set ( 'n' , '<M-m>' , function () 2165_G . got =vim . fn . getchar () 2166end ) 2167end ) 2168feed ( '<M-m><CR>' ) 2169eq ( 13 , exec_lua ( 'return _G.got' )) 2170eq ( { '<M-m>' } , exec_lua ( 'return _G.typed' )) 2171end ) 2172 2173it ( 'can discard input' , function () 2174-- discard the first key produced by every other 'x' key typed 2175exec_lua [[ 2176n_key = 0 2177 2178vim.on_key(function(buf, typed_buf) 2179if typed_buf == 'x' then 2180n_key = n_key + 1 2181return (n_key % 2 == 0) and '' or nil 2182end 2183end) 2184]] 2185 2186api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { '54321' } ) 2187 2188feed ( 'x' ) -- 'x' not discarded 2189expect ( '4321' ) 2190feed ( 'x' ) -- 'x' discarded 2191expect ( '4321' ) 2192feed ( 'x' ) -- 'x' not discarded 2193expect ( '321' ) 2194feed ( 'x' ) -- 'x' discarded 2195expect ( '321' ) 2196 2197api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { '54321' } ) 2198 2199-- only the first key from the mapping is discarded 2200command ( 'nnoremap x $x' ) 2201feed ( '0x' ) -- '$' not discarded 2202expect ( '5432' ) 2203feed ( '0x' ) -- '$' discarded 2204expect ( '432' ) 2205feed ( '0x' ) -- '$' not discarded 2206expect ( '43' ) 2207feed ( '0x' ) -- '$' discarded 2208expect ( '3' ) 2209 2210feed ( 'i' ) 2211-- when discarding <Cmd>, the following command is also discarded. 2212command ( [[inoremap x <Cmd>call append('$', 'foo')<CR>]] ) 2213feed ( 'x' ) -- not discarded 2214expect ( '3\nfoo' ) 2215feed ( 'x' ) -- discarded 2216expect ( '3\nfoo' ) 2217feed ( 'x' ) -- not discarded 2218expect ( '3\nfoo\nfoo' ) 2219feed ( 'x' ) -- discarded 2220expect ( '3\nfoo\nfoo' ) 2221 2222-- K_LUA is handled similarly to <Cmd> 2223exec_lua ( [[vim.keymap.set('i', 'x', function() vim.fn.append('$', 'bar') end)]] ) 2224feed ( 'x' ) -- not discarded 2225expect ( '3\nfoo\nfoo\nbar' ) 2226feed ( 'x' ) -- discarded 2227expect ( '3\nfoo\nfoo\nbar' ) 2228feed ( 'x' ) -- not discarded 2229expect ( '3\nfoo\nfoo\nbar\nbar' ) 2230feed ( 'x' ) -- discarded 2231expect ( '3\nfoo\nfoo\nbar\nbar' ) 2232end ) 2233 2234it ( 'behaves consistently with <Cmd>, K_LUA, nvim_paste' , function () 2235exec_lua ( [[ 2236vim.keymap.set('i', '<F2>', "<Cmd>call append('$', 'FOO')<CR>") 2237vim.keymap.set('i', '<F3>', function() vim.fn.append('$', 'BAR') end) 2238]] ) 2239 2240feed ( 'qrafoo<F2><F3>' ) 2241api . nvim_paste ( 'bar' , false , - 1 ) 2242feed ( '<Esc>q' ) 2243expect ( 'foobar\nFOO\nBAR' ) 2244 2245exec_lua ( [[ 2246keys = {} 2247typed = {} 2248 2249vim.on_key(function(buf, typed_buf) 2250table.insert(keys, buf) 2251table.insert(typed, typed_buf) 2252end) 2253]] ) 2254 2255feed ( '@r' ) 2256local keys =exec_lua ( 'return keys' ) 2257eq ( '@r' , exec_lua ( [[return table.concat(typed, '')]] )) 2258expect ( 'foobarfoobar\nFOO\nBAR\nFOO\nBAR' ) 2259 2260-- Add a new callback that discards most special keys as well as 'f'. 2261-- The old callback is still active. 2262exec_lua ( [[ 2263vim.on_key(function(buf, _) 2264if not buf:find('^[@rao\27]$') then 2265return '' 2266end 2267end) 2268 2269keys = {} 2270typed = {} 2271]] ) 2272 2273feed ( '@r' ) 2274eq ( keys , exec_lua ( 'return keys' )) 2275eq ( '@r' , exec_lua ( [[return table.concat(typed, '')]] )) 2276-- The "bar" paste is discarded as a whole. 2277expect ( 'foobarfoobaroo\nFOO\nBAR\nFOO\nBAR' ) 2278end ) 2279 2280it ( 'callback invalid return' , function () 2281-- second key produces an error which removes the callback 2282exec_lua [[ 2283n_call = 0 2284 2285vim.on_key(function(buf, typed_buf) 2286if typed_buf == 'x' then 2287n_call = n_call + 1 2288end 2289return n_call >= 2 and '!' or nil 2290end) 2291]] 2292 2293api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { '54321' } ) 2294 2295feed ( 'x' ) 2296eq ( 1 , exec_lua [[ return n_call ]] ) 2297eq ( 1 , exec_lua [[ return vim.on_key(nil, nil) ]] ) 2298eq ( '' , eval ( 'v:errmsg' )) 2299feed ( 'x' ) 2300eq ( 2 , exec_lua [[ return n_call ]] ) 2301matches ( 'return string must be empty' , eval ( 'v:errmsg' )) 2302command ( 'let v:errmsg = ""' ) 2303 2304eq ( 0 , exec_lua [[ return vim.on_key(nil, nil) ]] ) 2305 2306feed ( 'x' ) 2307eq ( 2 , exec_lua [[ return n_call ]] ) 2308expect ( '21' ) 2309eq ( '' , eval ( 'v:errmsg' )) 2310end ) 2311end ) 2312 2313describe ( 'vim.wait' , function () 2314before_each ( function () 2315exec_lua [[ 2316-- high precision timer 2317get_time = function() 2318return vim.fn.reltimefloat(vim.fn.reltime()) 2319end 2320]] 2321end ) 2322 2323it ( 'runs from lua' , function () 2324exec_lua [[vim.wait(100, function() return true end)]] 2325end ) 2326 2327it ( 'returns all (multiple) callback results' , function () 2328eq ( { true , false } , exec_lua [[return { vim.wait(200, function() return true, false end) }]] ) 2329eq ( 2330{ true , 'a' , 42 , { ok ={ 'yes' } } } , 2331exec_lua [[ 2332local ok, rv1, rv2, rv3 = vim.wait(200, function() 2333return true, 'a', 42, { ok = { 'yes' } } 2334end) 2335 2336return { ok, rv1, rv2, rv3 } 2337]] 2338) 2339end ) 2340 2341it ( 'does not return callback results on timeout' , function () 2342eq ( 2343{ false , - 1 } , 2344exec_lua [[ 2345return { vim.wait(1, function() 2346return false, 'a', 42, { ok = { 'yes' } } 2347end) } 2348]] 2349) 2350end ) 2351 2352it ( 'waits the expected time if false' , function () 2353eq ( 2354{ time =true , wait_result ={ false , - 1 } } , 2355exec_lua [[ 2356start_time = get_time() 2357wait_succeed, wait_fail_val = vim.wait(200, function() return false end) 2358 2359return { 2360-- 150ms waiting or more results in true. Flaky tests are bad. 2361time = (start_time + 0.15) < get_time(), 2362wait_result = {wait_succeed, wait_fail_val} 2363} 2364]] 2365) 2366end ) 2367 2368it ( 'does not block other events' , function () 2369eq ( 2370{ time =true , wait_result =true } , 2371exec_lua [[ 2372start_time = get_time() 2373 2374vim.g.timer_result = false 2375timer = vim.uv.new_timer() 2376timer:start(100, 0, vim.schedule_wrap(function() 2377vim.g.timer_result = true 2378end)) 2379 2380-- Would wait ten seconds if results blocked. 2381wait_result = vim.wait(10000, function() return vim.g.timer_result end) 2382 2383timer:close() 2384 2385return { 2386time = (start_time + 5) > get_time(), 2387wait_result = wait_result, 2388} 2389]] 2390) 2391end ) 2392 2393it ( 'does not process non-fast events when commanded' , function () 2394eq ( 2395{ wait_result =false } , 2396exec_lua [[ 2397start_time = get_time() 2398 2399vim.g.timer_result = false 2400timer = vim.uv.new_timer() 2401timer:start(100, 0, vim.schedule_wrap(function() 2402vim.g.timer_result = true 2403end)) 2404 2405wait_result = vim.wait(300, function() return vim.g.timer_result end, nil, true) 2406 2407timer:close() 2408 2409return { 2410wait_result = wait_result, 2411} 2412]] 2413) 2414end ) 2415 2416it ( 'works with vim.defer_fn' , function () 2417eq ( 2418{ time =true , wait_result =true } , 2419exec_lua [[ 2420start_time = get_time() 2421 2422vim.defer_fn(function() vim.g.timer_result = true end, 100) 2423wait_result = vim.wait(10000, function() return vim.g.timer_result end) 2424 2425return { 2426time = (start_time + 5) > get_time(), 2427wait_result = wait_result, 2428} 2429]] 2430) 2431end ) 2432 2433it ( 'does not crash when callback errors' , function () 2434local result =exec_lua [[ 2435return {pcall(function() vim.wait(1000, function() error("As Expected") end) end)} 2436]] 2437eq ( { false , '[string "<nvim>"]:1: As Expected' } , { result [ 1 ], remove_trace ( result [ 2 ]) } ) 2438end ) 2439 2440it ( 'callback must be a function' , function () 2441local result =exec_lua [[return {pcall(function() vim.wait(1000, 13) end)}]] 2442eq ( false , result [ 1 ]) 2443matches ( 'callback: expected callable, got number$' , remove_trace ( result [ 2 ])) 2444end ) 2445 2446it ( 'waits if callback arg is nil' , function () 2447eq ( 2448true , 2449exec_lua [[ 2450local start_time = vim.uv.hrtime() 2451vim.wait(50.1, nil) -- select('#', ...) == 1 2452return vim.uv.hrtime() - start_time > 25000 2453]] 2454) 2455end ) 2456 2457it ( 'waits if callback arg is omitted' , function () 2458eq ( 2459true , 2460exec_lua [[ 2461local start_time = vim.uv.hrtime() 2462vim.wait(50) -- select('#', ...) == 0 2463return vim.uv.hrtime() - start_time > 25000 2464]] 2465) 2466end ) 2467 2468it ( 'invokes callback exactly once if it returns true immediately' , function () 2469eq ( 2470true , 2471exec_lua [[ 2472vim.g.wait_count = 0 2473vim.wait(1000, function() 2474vim.g.wait_count = vim.g.wait_count + 1 2475return true 2476end, 20) 2477return vim.g.wait_count == 1 2478]] 2479) 2480end ) 2481 2482it ( 'calls callbacks few times with large `interval`' , function () 2483eq ( 2484true , 2485exec_lua [[ 2486vim.g.wait_count = 0 2487vim.wait(50, function() vim.g.wait_count = vim.g.wait_count + 1 end, 200) 2488return vim.g.wait_count < 5 2489]] 2490) 2491end ) 2492 2493it ( 'does not leak when Nvim exits while waiting' , function () 2494n . expect_exit ( 500 , exec_lua , function () 2495vim . defer_fn ( function () 2496vim . cmd ( 'qall!' ) 2497end , 10 ) 2498vim . wait ( 10000 ) 2499end ) 2500end ) 2501 2502it ( 'plays nice with `not` when fails' , function () 2503eq ( 2504true , 2505exec_lua [[ 2506if not vim.wait(50, function() end) then 2507return true 2508end 2509 2510return false 2511]] 2512) 2513end ) 2514 2515it ( 'plays nice with `if` when success' , function () 2516eq ( 2517true , 2518exec_lua [[ 2519if vim.wait(50, function() return true end) then 2520return true 2521end 2522 2523return false 2524]] 2525) 2526end ) 2527 2528it ( 'returns immediately with false if timeout is 0' , function () 2529eq ( 2530{ false , - 1 } , 2531exec_lua [[ 2532return { 2533vim.wait(0, function() return false end) 2534} 2535]] 2536) 2537end ) 2538 2539it ( 'works with tables with __call' , function () 2540eq ( 2541true , 2542exec_lua [[ 2543local t = setmetatable({}, {__call = function(...) return true end}) 2544return vim.wait(100, t, 10) 2545]] 2546) 2547end ) 2548 2549it ( 'works with tables with __call that change' , function () 2550eq ( 2551true , 2552exec_lua [[ 2553local t = {count = 0} 2554setmetatable(t, { 2555__call = function() 2556t.count = t.count + 1 2557return t.count > 3 2558end 2559}) 2560 2561return vim.wait(1000, t, 10) 2562]] 2563) 2564end ) 2565 2566it ( 'fails with negative intervals' , function () 2567local pcall_result =exec_lua [[ 2568return pcall(function() vim.wait(1000, function() return false end, -1) end) 2569]] 2570 2571eq ( false , pcall_result ) 2572end ) 2573 2574it ( 'fails with weird intervals' , function () 2575local pcall_result =exec_lua [[ 2576return pcall(function() vim.wait(1000, function() return false end, 'a string value') end) 2577]] 2578 2579eq ( false , pcall_result ) 2580end ) 2581 2582describe ( 'returns -2 when interrupted' , function () 2583before_each ( function () 2584local channel =api . nvim_get_chan_info ( 0 ). id 2585api . nvim_set_var ( 'channel' , channel ) 2586end ) 2587 2588it ( 'without callback' , function () 2589exec_lua ( [[ 2590function _G.Wait() 2591vim.rpcnotify(vim.g.channel, 'ready') 2592-- handles math.huge #36854 2593local _, interrupted = vim.wait(math.huge) 2594vim.rpcnotify(vim.g.channel, 'wait', interrupted) 2595end 2596]] ) 2597feed ( ':lua _G.Wait()<CR>' ) 2598eq ( { 'notification' , 'ready' , {} } , next_msg ( 500 )) 2599feed ( '<C-C>' ) 2600eq ( { 'notification' , 'wait' , { - 2 } } , next_msg ( 500 )) 2601end ) 2602 2603it ( 'with callback' , function () 2604exec_lua ( [[ 2605function _G.Wait() 2606vim.rpcnotify(vim.g.channel, 'ready') 2607local _, interrupted = vim.wait(math.huge, function() end) 2608vim.rpcnotify(vim.g.channel, 'wait', interrupted) 2609end 2610]] ) 2611feed ( ':lua _G.Wait()<CR>' ) 2612eq ( { 'notification' , 'ready' , {} } , next_msg ( 500 )) 2613feed ( '<C-C>' ) 2614eq ( { 'notification' , 'wait' , { - 2 } } , next_msg ( 500 )) 2615end ) 2616end ) 2617 2618it ( 'lets CTRL-C interrupt a Lua loop' , function () 2619api . nvim_set_var ( 'channel' , api . nvim_get_chan_info ( 0 ). id ) 2620exec_lua ( [[ 2621function _G.Loop() 2622vim.rpcnotify(vim.g.channel, 'ready') 2623while true do 2624local _, code = vim.wait(0, nil, 0) 2625if code == -2 then 2626vim.rpcnotify(vim.g.channel, 'wait', code) 2627return 2628end 2629end 2630end 2631]] ) 2632feed ( ':lua _G.Loop()<CR>' ) 2633eq ( { 'notification' , 'ready' , {} } , next_msg ( 500 )) 2634feed ( '<C-C>' ) 2635eq ( { 'notification' , 'wait' , { - 2 } } , next_msg ( 500 )) 2636end ) 2637 2638it ( 'fails in fast callbacks #26122' , function () 2639local screen =Screen . new ( 80 , 10 ) 2640exec_lua ( [[ 2641local timer = vim.uv.new_timer() 2642timer:start(0, 0, function() 2643timer:close() 2644vim.wait(100, function() end) 2645end) 2646]] ) 2647screen : expect ( { 2648any =pesc ( 'E5560: vim.wait must not be called in a fast event context' ), 2649} ) 2650feed ( '<CR>' ) 2651assert_alive () 2652end ) 2653end ) 2654 2655it ( 'vim.notify_once' , function () 2656local screen =Screen . new ( 60 , 5 ) 2657screen : expect { 2658grid =[[ 2659^ | 2660{1:~ }|*3 2661| 2662]] , 2663} 2664exec_lua [[vim.notify_once("I'll only tell you this once...", vim.log.levels.WARN)]] 2665screen : expect { 2666grid =[[ 2667^ | 2668{1:~ }|*3 2669{19:I'll only tell you this once...} | 2670]] , 2671} 2672feed ( '<C-l>' ) 2673screen : expect { 2674grid =[[ 2675^ | 2676{1:~ }|*3 2677| 2678]] , 2679} 2680exec_lua [[vim.notify_once("I'll only tell you this once...")]] 2681screen : expect_unchanged () 2682end ) 2683 2684describe ( 'vim.schedule_wrap' , function () 2685it ( 'preserves argument lists' , function () 2686exec_lua [[ 2687local fun = vim.schedule_wrap(function(kling, klang, klonk) 2688vim.rpcnotify(1, 'mayday_mayday', {a=kling, b=klang, c=klonk}) 2689end) 2690fun("BOB", nil, "MIKE") 2691]] 2692eq ( { 'notification' , 'mayday_mayday' , { { a ='BOB' , c ='MIKE' } } } , next_msg ()) 2693 2694-- let's gooooo 2695exec_lua [[ 2696vim.schedule_wrap(function(...) vim.rpcnotify(1, 'boogalo', select('#', ...)) end)(nil,nil,nil,nil) 2697]] 2698eq ( { 'notification' , 'boogalo' , { 4 } } , next_msg ()) 2699end ) 2700end ) 2701 2702describe ( 'vim.iconv' , function () 2703it ( 'can convert strings' , function () 2704eq ( 2705'hello' , 2706exec_lua [[ 2707return vim.iconv('hello', 'latin1', 'utf-8') 2708]] 2709) 2710end ) 2711 2712it ( 'can validate arguments' , function () 2713eq ( 2714{ false , 'Expected at least 3 arguments' } , 2715exec_lua [[ 2716return {pcall(vim.iconv, 'hello')} 2717]] 2718) 2719 2720eq ( 2721{ false , "bad argument #3 to '?' (expected string)" } , 2722exec_lua [[ 2723return {pcall(vim.iconv, 'hello', 'utf-8', true)} 2724]] 2725) 2726end ) 2727 2728it ( 'can handle bad encodings' , function () 2729eq ( 2730NIL , 2731exec_lua [[ 2732return vim.iconv('hello', 'foo', 'bar') 2733]] 2734) 2735end ) 2736 2737it ( 'can handle strings with NUL bytes' , function () 2738eq ( 27397 , 2740exec_lua [[ 2741local a = string.char(97, 98, 99, 0, 100, 101, 102) -- abc\0def 2742return string.len(vim.iconv(a, 'latin1', 'utf-8')) 2743]] 2744) 2745end ) 2746 2747it ( 'utf-16le should not be equal to utf-16be' , function () 2748neq ( 2749exec_lua [[ 2750return vim.iconv('hello', 'utf-8', 'utf-16le') 2751]] , 2752exec_lua [[ 2753return vim.iconv('hello', 'utf-8', 'utf-16be') 2754]] 2755) 2756end ) 2757end ) 2758 2759describe ( 'vim.defaulttable' , function () 2760it ( 'creates nested table by default' , function () 2761eq ( 2762{ b ={ c =1 } } , 2763exec_lua [[ 2764local a = vim.defaulttable() 2765a.b.c = 1 2766return a 2767]] 2768) 2769end ) 2770 2771it ( 'allows to create default objects' , function () 2772eq ( 2773{ b =1 } , 2774exec_lua [[ 2775local a = vim.defaulttable(function() return 0 end) 2776a.b = a.b + 1 2777return a 2778]] 2779) 2780end ) 2781 2782it ( 'accepts the key name' , function () 2783eq ( 2784{ b ='b' , c ='c' } , 2785exec_lua [[ 2786local a = vim.defaulttable(function(k) return k end) 2787local _ = a.b 2788local _ = a.c 2789return a 2790]] 2791) 2792end ) 2793end ) 2794 2795it ( 'vim.lua_omnifunc' , function () 2796local screen =Screen . new ( 60 , 5 ) 2797command [[ set omnifunc=v:lua.vim.lua_omnifunc ]] 2798 2799-- Note: the implementation is shared with lua command line completion. 2800-- More tests for completion in lua/command_line_completion_spec.lua 2801feed [[ivim.insp<c-x><c-o>]] 2802screen : expect { 2803grid =[[ 2804vim.inspect^ | 2805{1:~ }{12: inspect }{1: }| 2806{1:~ }{4: inspect_pos }{1: }| 2807{1:~ }| 2808{5:-- Omni completion (^O^N^P) }{6:match 1 of 2} | 2809]] , 2810} 2811end ) 2812 2813it ( 'vim.print' , function () 2814-- vim.print() returns its args. 2815eq ( 2816{ 42 , 'abc' , { a ={ b =77 } } } , 2817exec_lua [[return {vim.print(42, 'abc', { a = { b = 77 }})}]] 2818) 2819 2820-- vim.print() pretty-prints the args. 2821eq ( 2822dedent [[ 2823 282442 2825abc 2826{ 2827a = { 2828b = 77 2829} 2830}]] , 2831eval [[execute('lua vim.print(42, "abc", { a = { b = 77 }})')]] 2832) 2833end ) 2834 2835it ( 'vim.nonnil' , function () 2836local function nonnil ( ... ) 2837return exec_lua ( 2838[[ 2839local args = {...} 2840local nargs = select('#', ...) 2841for i = 1, nargs do 2842if args[i] == vim.NIL then 2843args[i] = nil 2844end 2845end 2846return vim.nonnil(unpack(args, 1, nargs)) 2847]] , 2848... 2849) 2850end 2851 2852local a =NIL 2853local b =NIL 2854local c =42 2855local d =false 2856eq ( 42 , nonnil ( a , c )) 2857eq ( false , nonnil ( d , b )) 2858eq ( 42 , nonnil ( a , b , c , d )) 2859eq ( false , nonnil ( d )) 2860eq ( false , nonnil ( d , c )) 2861eq ( NIL , nonnil ( a )) 2862end ) 2863 2864it ( 'vim.npcall' , function () 2865-- No error 2866eq ( 2867{ '123' , 'test' } , 2868exec_lua ( function () 2869local function swap_args ( a , b ) 2870return b , a 2871end 2872return { vim . npcall ( swap_args , 'test' , '123' ) } 2873end ) 2874) 2875 2876-- Error 2877eq ( 2878nil , 2879exec_lua ( function () 2880return vim . npcall ( error , 'error' ) 2881end ) 2882) 2883end ) 2884 2885it ( 'lpeg' , function () 2886eq ( 28875 , 2888exec_lua [[ 2889local m = vim.lpeg 2890return m.match(m.R'09'^1, '4504ab') 2891]] 2892) 2893 2894eq ( 4 , exec_lua [[ return vim.re.match("abcde", '[a-c]+') ]] ) 2895end ) 2896 2897it ( 'vim.ringbuf' , function () 2898local results =exec_lua ( [[ 2899local ringbuf = vim.ringbuf(3) 2900ringbuf:push("a") -- idx: 0 2901local peeka1 = ringbuf:peek() 2902local peeka2 = ringbuf:peek() 2903local popa = ringbuf:pop() 2904local popnil = ringbuf:pop() 2905ringbuf:push("a") -- idx: 1 2906ringbuf:push("b") -- idx: 2 2907 2908-- doesn't read last added item, but uses separate read index 2909local pop_after_add_b = ringbuf:pop() 2910 2911ringbuf:push("c") -- idx: 3 wraps around, overrides idx: 0 "a" 2912ringbuf:push("d") -- idx: 4 wraps around, overrides idx: 1 "a" 2913return { 2914peeka1 = peeka1, 2915peeka2 = peeka2, 2916pop1 = popa, 2917pop2 = popnil, 2918pop3 = ringbuf:pop(), 2919pop4 = ringbuf:pop(), 2920pop5 = ringbuf:pop(), 2921pop_after_add_b = pop_after_add_b, 2922} 2923]] ) 2924local expected ={ 2925peeka1 ='a' , 2926peeka2 ='a' , 2927pop1 ='a' , 2928pop2 =nil , 2929pop3 ='b' , 2930pop4 ='c' , 2931pop5 ='d' , 2932pop_after_add_b ='a' , 2933} 2934eq ( expected , results ) 2935end ) 2936end ) 2937 2938describe ( 'lua: builtin modules' , function () 2939local function do_tests () 2940eq ( 2 , exec_lua [[return vim.tbl_count {x=1,y=2}]] ) 2941eq ( '{ 10, "spam" }' , exec_lua [[return vim.inspect {10, 'spam'}]] ) 2942end 2943 2944it ( 'works' , function () 2945clear () 2946do_tests () 2947end ) 2948 2949it ( 'works when disabled' , function () 2950clear ( '--luamod-dev' ) 2951do_tests () 2952end ) 2953 2954it ( 'works without runtime' , function () 2955clear { env ={ VIMRUNTIME ='fixtures/a' } } 2956do_tests () 2957end ) 2958 2959it ( 'fails when disabled without runtime' , function () 2960clear () 2961command ( "let $VIMRUNTIME='fixtures/a'" ) 2962-- Use system([nvim,…]) instead of clear() to avoid stderr noise. #21844 2963local out =fn . system ( { 2964nvim_prog , 2965'--clean' , 2966'--luamod-dev' , 2967[[+call nvim_exec_lua('return vim.tbl_count {x=1,y=2}')]] , 2968'+qa!' , 2969} ): gsub ( '\r\n' , '\n' ) 2970eq ( 1 , eval ( 'v:shell_error' )) 2971matches ( "'vim%._core.shared' not found" , out ) 2972end ) 2973end ) 2974 2975describe ( 'lua: require("mod") from packages' , function () 2976before_each ( function () 2977clear ( '--cmd' , 'set rtp+=test/functional/fixtures pp+=test/functional/fixtures' ) 2978end ) 2979 2980it ( 'propagates syntax error' , function () 2981local syntax_error_msg =exec_lua [[ 2982local _, err = pcall(require, "syntax_error") 2983return err 2984]] 2985 2986matches ( 'unexpected symbol' , syntax_error_msg ) 2987end ) 2988 2989it ( 'uses the right order of mod.lua vs mod/init.lua' , function () 2990-- lua/fancy_x.lua takes precedence over lua/fancy_x/init.lua 2991eq ( 'I am fancy_x.lua' , exec_lua [[ return require'fancy_x' ]] ) 2992-- but lua/fancy_y/init.lua takes precedence over after/lua/fancy_y.lua 2993eq ( 'I am init.lua of fancy_y!' , exec_lua [[ return require'fancy_y' ]] ) 2994-- safety check: after/lua/fancy_z.lua is still loaded 2995eq ( 'I am fancy_z.lua' , exec_lua [[ return require'fancy_z' ]] ) 2996end ) 2997end ) 2998 2999describe ( 'vim.keymap' , function () 3000before_each ( clear ) 3001 3002it ( 'validates' , function () 3003matches ( 3004'modes: expected string|table, got number' , 3005pcall_err ( exec_lua , [[vim.keymap.set(42, 'x', print)]] ) 3006) 3007 3008matches ( 3009'rhs: expected string|function, got nil' , 3010pcall_err ( exec_lua , [[vim.keymap.set('n', 'x')]] ) 3011) 3012 3013matches ( 3014'lhs: expected string|table, got number' , 3015pcall_err ( exec_lua , [[vim.keymap.set('n', 5, print)]] ) 3016) 3017 3018matches ( 3019'rhs: expected string|function, got number' , 3020pcall_err ( exec_lua , [[vim.keymap.set({}, 'x', 42, function() end)]] ) 3021) 3022 3023matches ( 3024'opts: expected table, got function' , 3025pcall_err ( exec_lua , [[vim.keymap.set({}, 'x', 'x', function() end)]] ) 3026) 3027 3028matches ( 3029'rhs: expected string|function, got number' , 3030pcall_err ( exec_lua , [[vim.keymap.set('z', 'x', 42)]] ) 3031) 3032 3033matches ( 'Invalid mode shortname: "z"' , pcall_err ( exec_lua , [[vim.keymap.set('z', 'x', 'y')]] )) 3034end ) 3035 3036it ( 'mapping' , function () 3037eq ( 30380 , 3039exec_lua [[ 3040GlobalCount = 0 3041vim.keymap.set('n', 'asdf', function() GlobalCount = GlobalCount + 1 end) 3042vim.keymap.set('n', { 'ghjk', 'qwer' }, function() GlobalCount = GlobalCount + 1 end) 3043return GlobalCount 3044]] 3045) 3046 3047feed ( 'asdf\n' ) 3048eq ( 1 , exec_lua [[return GlobalCount]] ) 3049 3050feed ( 'ghjk\nqwer\n' ) 3051eq ( 3 , exec_lua [[return GlobalCount]] ) 3052end ) 3053 3054it ( 'expr mapping' , function () 3055exec_lua [[ 3056vim.keymap.set('n', 'aa', function() return '<Insert>π<C-V><M-π>foo<lt><Esc>' end, {expr = true}) 3057]] 3058 3059feed ( 'aa' ) 3060 3061eq ( { 'π<M-π>foo<' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )) 3062end ) 3063 3064it ( 'overwrite a mapping' , function () 3065eq ( 30660 , 3067exec_lua [[ 3068GlobalCount = 0 3069vim.keymap.set('n', 'asdf', function() GlobalCount = GlobalCount + 1 end) 3070return GlobalCount 3071]] 3072) 3073 3074feed ( 'asdf\n' ) 3075 3076eq ( 1 , exec_lua [[return GlobalCount]] ) 3077 3078exec_lua [[ 3079vim.keymap.set('n', 'asdf', function() GlobalCount = GlobalCount - 1 end) 3080]] 3081 3082feed ( 'asdf\n' ) 3083 3084eq ( 0 , exec_lua [[return GlobalCount]] ) 3085end ) 3086 3087it ( 'unmap' , function () 3088eq ( 30890 , 3090exec_lua [[ 3091GlobalCount = 0 3092vim.keymap.set('n', { 'asdf', 'ghjk', 'qwer' }, function() GlobalCount = GlobalCount + 1 end) 3093return GlobalCount 3094]] 3095) 3096 3097feed ( 'asdf\n' ) 3098 3099eq ( 1 , exec_lua [[return GlobalCount]] ) 3100 3101exec_lua [[ 3102vim.keymap.del('n', 'asdf') 3103]] 3104 3105feed ( 'asdf\n' ) 3106 3107eq ( 1 , exec_lua [[return GlobalCount]] ) 3108eq ( '\nNo mapping found' , n . exec_capture ( 'nmap asdf' )) 3109 3110exec_lua [[ 3111vim.keymap.del('n', { 'ghjk', 'qwer' }) 3112]] 3113 3114feed ( 'ghjk\nqwer\n' ) 3115 3116eq ( 1 , exec_lua [[return GlobalCount]] ) 3117eq ( '\nNo mapping found' , n . exec_capture ( 'nmap ghjk' )) 3118eq ( '\nNo mapping found' , n . exec_capture ( 'nmap qwer' )) 3119end ) 3120 3121it ( 'unmap with lhs option' , function () 3122eq ( 3123'q' , 3124exec_lua [[ 3125vim.keymap.set('n', 'ge', 'q') 3126local ok, err = pcall(vim.keymap.del, 'n', 'q', { lhs = true }) 3127assert(not ok and err:match('E31: No such mapping'), err) 3128return vim.fn.maparg('ge', 'n') 3129]] 3130) 3131 3132exec_lua [[ 3133vim.keymap.del('n', 'ge', { lhs = true }) 3134]] 3135eq ( '\nNo mapping found' , n . exec_capture ( 'nmap ge' )) 3136end ) 3137 3138it ( 'buffer-local mappings' , function () 3139eq ( 31400 , 3141exec_lua [[ 3142GlobalCount = 0 3143vim.keymap.set('n', 'asdf', function() GlobalCount = GlobalCount + 1 end, {buf=0}) 3144return GlobalCount 3145]] 3146) 3147 3148feed ( 'asdf\n' ) 3149 3150eq ( 1 , exec_lua [[return GlobalCount]] ) 3151 3152exec_lua [[ 3153vim.keymap.del('n', 'asdf', {buf=0}) 3154]] 3155 3156feed ( 'asdf\n' ) 3157 3158eq ( 1 , exec_lua [[return GlobalCount]] ) 3159eq ( '\nNo mapping found' , n . exec_capture ( 'nmap asdf' )) 3160end ) 3161 3162it ( 'does not mutate the opts parameter' , function () 3163eq ( 31640 , 3165exec_lua [[ 3166opts = {buf=0} 3167vim.keymap.set('n', 'asdf', function() end, opts) 3168return opts.buf 3169]] 3170) 3171eq ( 31720 , 3173exec_lua [[ 3174vim.keymap.del('n', 'asdf', opts) 3175return opts.buf 3176]] 3177) 3178end ) 3179 3180it ( '<Plug> mappings' , function () 3181eq ( 31820 , 3183exec_lua [[ 3184GlobalCount = 0 3185vim.keymap.set('n', '<plug>(asdf)', function() GlobalCount = GlobalCount + 1 end) 3186vim.keymap.set('n', 'ww', '<plug>(asdf)') 3187return GlobalCount 3188]] 3189) 3190 3191feed ( 'ww\n' ) 3192 3193eq ( 1 , exec_lua [[return GlobalCount]] ) 3194end ) 3195end ) 3196 3197describe ( 'vim.fn.exists()' , function () 3198it ( 'can check a Lua function' , function () 3199eq ( 32001 , 3201exec_lua [[ 3202_G.test = function() print("hello") end 3203return vim.fn.exists('*v:lua.test') 3204]] 3205) 3206 3207eq ( 1 , fn . exists ( '*v:lua.require("mpack").decode' )) 3208eq ( 1 , fn . exists ( "*v:lua.require('mpack').decode" )) 3209eq ( 1 , fn . exists ( '*v:lua.require"mpack".decode' )) 3210eq ( 1 , fn . exists ( "*v:lua.require'mpack'.decode" )) 3211eq ( 1 , fn . exists ( "*v:lua.require('vim.lsp').start" )) 3212eq ( 1 , fn . exists ( '*v:lua.require"vim.lsp".start' )) 3213eq ( 1 , fn . exists ( "*v:lua.require'vim.lsp'.start" )) 3214eq ( 0 , fn . exists ( "*v:lua.require'vim.lsp'.unknown" )) 3215eq ( 0 , fn . exists ( '*v:lua.?' )) 3216end ) 3217end )