robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each , pending =t . describe , t . it , t . before_each , t . pending 5local assert_alive =n . assert_alive 6local eval , command , feed =n . eval , n . command , n . feed 7local eq , clear , insert =t . eq , n . clear , n . insert 8local expect , write_file =n . expect , t . write_file 9local feed_command =n . feed_command 10local source =n . source 11local missing_provider =n . missing_provider 12local matches =t . matches 13local pcall_err =t . pcall_err 14local fn =n . fn 15local dedent =t . dedent 16 17do 18clear () 19local reason =missing_provider ( 'python' ) 20if reason then 21it ( ':python3 reports E319 if provider is missing' , function () 22local expected =[[Vim%(py3.*%):E319: No "python3" provider found.*]] 23matches ( expected , pcall_err ( command , 'py3 print("foo")' )) 24matches ( expected , pcall_err ( command , 'py3file foo' )) 25end ) 26it ( 'feature test when Python 3 provider is missing' , function () 27eq ( 0 , eval ( 'has("python3")' )) 28eq ( 0 , eval ( 'has("python3_compiled")' )) 29eq ( 0 , eval ( 'has("python3_dynamic")' )) 30eq ( 0 , eval ( 'has("pythonx")' )) 31end ) 32pending ( 33string . format ( 'Python 3 (or the pynvim module) is broken/missing (%s)' , reason ), 34function () end 35) 36return 37end 38end 39 40describe ( 'python3 provider' , function () 41before_each ( function () 42clear () 43command ( 'python3 import vim' ) 44end ) 45 46it ( 'feature test' , function () 47eq ( 1 , eval ( 'has("python3")' )) 48eq ( 1 , eval ( 'has("python3_compiled")' )) 49eq ( 1 , eval ( 'has("python3_dynamic")' )) 50eq ( 1 , eval ( 'has("pythonx")' )) 51eq ( 0 , eval ( 'has("python3_dynamic_")' )) 52eq ( 0 , eval ( 'has("python3_")' )) 53end ) 54 55it ( 'python3_execute' , function () 56command ( 'python3 vim.vars["set_by_python3"] = [100, 0]' ) 57eq ( { 100 , 0 } , eval ( 'g:set_by_python3' )) 58end ) 59 60it ( 'does not truncate error message <1 MB' , function () 61-- XXX: Python limits the error name to 200 chars, so this test is 62-- mostly bogus. 63local very_long_symbol =string . rep ( 'a' , 1200 ) 64feed_command ( ':silent! py3 print(' .. very_long_symbol .. ' b)' ) 65-- Error message will contain this (last) line. 66matches ( 67string . format ( 68dedent ( [[ 69^Invoking 'python_execute' on channel 3 %%(python3%%-script%%-host%%): 70File "<string>", line 1 71print%%(%s b%%) 72%%C* 73SyntaxError: invalid syntax%%C*$]] ), 74very_long_symbol 75), 76eval ( 'v:errmsg' ) 77) 78end ) 79 80it ( 'python3_execute with nested commands' , function () 81command ( 82[[python3 vim.command('python3 vim.command("python3 vim.command(\'let set_by_nested_python3 = 555\')")')]] 83) 84eq ( 555 , eval ( 'g:set_by_nested_python3' )) 85end ) 86 87it ( 'python3_execute with range' , function () 88insert ( [[ 89line1 90line2 91line3 92line4]] ) 93feed ( 'ggjvj:python3 vim.vars["range"] = vim.current.range[:]<CR>' ) 94eq ( { 'line2' , 'line3' } , eval ( 'g:range' )) 95end ) 96 97it ( 'py3file' , function () 98local fname ='py3file.py' 99write_file ( fname , 'vim.command("let set_by_py3file = 123")' ) 100command ( 'py3file py3file.py' ) 101eq ( 123 , eval ( 'g:set_by_py3file' )) 102os . remove ( fname ) 103end ) 104 105it ( 'py3do' , function () 106-- :pydo3 42 returns None for all lines, 107-- the buffer should not be changed 108command ( 'normal :py3do 42' ) 109eq ( 0 , eval ( '&mod' )) 110-- insert some text 111insert ( 'abc\ndef\nghi' ) 112expect ( [[ 113abc 114def 115ghi]] ) 116-- go to top and select and replace the first two lines 117feed ( 'ggvj:py3do return str(linenr)<CR>' ) 118expect ( [[ 1191 1202 121ghi]] ) 122end ) 123 124describe ( 'py3eval()' , function () 125it ( 'works' , function () 126eq ( { 1 , 2 , { [ 'key' ] ='val' } } , fn . py3eval ( '[1, 2, {"key": "val"}]' )) 127end ) 128 129it ( 'errors out when given non-string' , function () 130eq ( 'Vim:E474: Invalid argument' , pcall_err ( eval , 'py3eval(10)' )) 131eq ( 'Vim:E474: Invalid argument' , pcall_err ( eval , 'py3eval(v:_null_dict)' )) 132eq ( 'Vim:E474: Invalid argument' , pcall_err ( eval , 'py3eval(v:_null_list)' )) 133eq ( 'Vim:E474: Invalid argument' , pcall_err ( eval , 'py3eval(0.0)' )) 134eq ( 'Vim:E474: Invalid argument' , pcall_err ( eval , 'py3eval(function("tr"))' )) 135eq ( 'Vim:E474: Invalid argument' , pcall_err ( eval , 'py3eval(v:true)' )) 136eq ( 'Vim:E474: Invalid argument' , pcall_err ( eval , 'py3eval(v:false)' )) 137eq ( 'Vim:E474: Invalid argument' , pcall_err ( eval , 'py3eval(v:null)' )) 138end ) 139 140it ( 'accepts NULL string' , function () 141matches ( '.*SyntaxError.*' , pcall_err ( eval , 'py3eval($XXX_NONEXISTENT_VAR_XXX)' )) 142end ) 143end ) 144 145it ( 'pyxeval #10758' , function () 146eq ( 3 , eval ( [[&pyxversion]] )) 147eq ( 3 , eval ( [[pyxeval('sys.version_info[:3][0]')]] )) 148eq ( 3 , eval ( [[&pyxversion]] )) 149end ) 150 151it ( "setting 'pyxversion'" , function () 152command 'set pyxversion=3' -- no error 153eq ( 'Vim(set):E474: Invalid argument: pyxversion=2' , pcall_err ( command , 'set pyxversion=2' )) 154command 'set pyxversion=0' -- allowed, but equivalent to pyxversion=3 155eq ( 3 , eval '&pyxversion' ) 156end ) 157 158it ( 'RPC call to expand("<afile>") during BufDelete #5245 #5617' , function () 159n . add_builddir_to_rtp () 160source ( [=[ 161python3 << EOF 162import vim 163def foo(): 164vim.eval('expand("<afile>:p")') 165vim.eval('bufnr(expand("<afile>:p"))') 166EOF 167autocmd BufDelete * python3 foo() 168autocmd BufUnload * python3 foo()]=] ) 169feed_command ( "exe 'split' tempname()" ) 170feed_command ( 'bwipeout!' ) 171feed_command ( 'help help' ) 172assert_alive () 173end ) 174end ) 175 176describe ( 'python2 feature test' , function () 177-- python2 is not supported, so correct behaviour is to return 0 178it ( 'works' , function () 179eq ( 0 , fn . has ( 'python2' )) 180eq ( 0 , fn . has ( 'python' )) 181eq ( 0 , fn . has ( 'python_compiled' )) 182eq ( 0 , fn . has ( 'python_dynamic' )) 183eq ( 0 , fn . has ( 'python_dynamic_' )) 184eq ( 0 , fn . has ( 'python_' )) 185end ) 186end )