0
0
mirror of https://github.com/vim/vim.git synced 2025-10-03 05:14:07 -04:00

patch 8.2.4057: Vim9: not fully implementing the autoload mechanism

Problem:    Vim9: not fully implementing the autoload mechanism.
Solution:   Allow for exporting a legacy function.  Improve test coverage.
This commit is contained in:
Bram Moolenaar
2022-01-10 21:29:57 +00:00
parent 6990b78f25
commit 160aa86a9d
4 changed files with 248 additions and 177 deletions

View File

@@ -2994,182 +2994,6 @@ def Test_vim9_copen()
quit
enddef
" test using an auto-loaded function and variable
def Test_vim9_autoload()
var lines =<< trim END
vim9script
def some#gettest(): string
return 'test'
enddef
g:some#name = 'name'
g:some#dict = {key: 'value'}
def some#varargs(a1: string, ...l: list<string>): string
return a1 .. l[0] .. l[1]
enddef
END
mkdir('Xdir/autoload', 'p')
writefile(lines, 'Xdir/autoload/some.vim')
var save_rtp = &rtp
exe 'set rtp^=' .. getcwd() .. '/Xdir'
assert_equal('test', g:some#gettest())
assert_equal('name', g:some#name)
assert_equal('value', g:some#dict.key)
g:some#other = 'other'
assert_equal('other', g:some#other)
assert_equal('abc', some#varargs('a', 'b', 'c'))
# upper case script name works
lines =<< trim END
vim9script
def Other#getOther(): string
return 'other'
enddef
END
writefile(lines, 'Xdir/autoload/Other.vim')
assert_equal('other', g:Other#getOther())
# using "vim9script autoload" prefix is not needed
lines =<< trim END
vim9script autoload
g:prefixed_loaded = 'yes'
export def Gettest(): string
return 'test'
enddef
export var name = 'name'
END
writefile(lines, 'Xdir/autoload/prefixed.vim')
lines =<< trim END
vim9script
import autoload 'prefixed.vim'
assert_false(exists('g:prefixed_loaded'))
assert_equal('test', prefixed.Gettest())
assert_equal('yes', g:prefixed_loaded)
assert_equal('name', prefixed.name)
END
CheckScriptSuccess(lines)
# can also get the items by autoload name
lines =<< trim END
call assert_equal('test', prefixed#Gettest())
call assert_equal('name', prefixed#name)
END
CheckScriptSuccess(lines)
delete('Xdir', 'rf')
&rtp = save_rtp
enddef
" test disassembling an auto-loaded function starting with "debug"
def Test_vim9_autoload_disass()
mkdir('Xdir/autoload', 'p')
var save_rtp = &rtp
exe 'set rtp^=' .. getcwd() .. '/Xdir'
var lines =<< trim END
vim9script
def debugit#test(): string
return 'debug'
enddef
END
writefile(lines, 'Xdir/autoload/debugit.vim')
lines =<< trim END
vim9script
def profileit#test(): string
return 'profile'
enddef
END
writefile(lines, 'Xdir/autoload/profileit.vim')
lines =<< trim END
vim9script
assert_equal('debug', debugit#test())
disass debugit#test
assert_equal('profile', profileit#test())
disass profileit#test
END
CheckScriptSuccess(lines)
delete('Xdir', 'rf')
&rtp = save_rtp
enddef
" test using a vim9script that is auto-loaded from an autocmd
def Test_vim9_aucmd_autoload()
var lines =<< trim END
vim9script
def foo#test()
echomsg getreg('"')
enddef
END
mkdir('Xdir/autoload', 'p')
writefile(lines, 'Xdir/autoload/foo.vim')
var save_rtp = &rtp
exe 'set rtp^=' .. getcwd() .. '/Xdir'
augroup test
autocmd TextYankPost * call foo#test()
augroup END
normal Y
augroup test
autocmd!
augroup END
delete('Xdir', 'rf')
&rtp = save_rtp
enddef
" This was causing a crash because suppress_errthrow wasn't reset.
def Test_vim9_autoload_error()
var lines =<< trim END
vim9script
def crash#func()
try
for x in List()
endfor
catch
endtry
g:ok = true
enddef
fu List()
invalid
endfu
try
alsoinvalid
catch /wontmatch/
endtry
END
call mkdir('Xruntime/autoload', 'p')
call writefile(lines, 'Xruntime/autoload/crash.vim')
# run in a separate Vim to avoid the side effects of assert_fails()
lines =<< trim END
exe 'set rtp^=' .. getcwd() .. '/Xruntime'
call crash#func()
call writefile(['ok'], 'Xdidit')
qall!
END
writefile(lines, 'Xscript')
RunVim([], [], '-S Xscript')
assert_equal(['ok'], readfile('Xdidit'))
delete('Xdidit')
delete('Xscript')
delete('Xruntime', 'rf')
lines =<< trim END
vim9script
var foo#bar = 'asdf'
END
CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
enddef
def Test_script_var_in_autocmd()
# using a script variable from an autocommand, defined in a :def function in a
# legacy Vim script, cannot check the variable type.