Consider a script local function in a plugin/myplugin.vim
file. For example
myplugin.vim:
func! s:someFunction()
return 4
endfunc
I would like to unit test this function. In the simplest example I would like to execute something like the following in ANOTHER script. Without modifying the original script.
test_myplugin.vim:
if s:someFunction() != 4
throw "Error"
endif
This obviously does not work because s:someFunction
is not visible from outside of the myplugin.vim
.
Is there a way to unit test these script local functions?
Some options that come to mind:
- Move the function to be unit tested to
autoload
directory and rename itmyplugin#someFunction()
. Inspiration is from :help write-library-script. I do not prefer this approach. The function(s) are not reused they are not supposed to be a library. I also do not own theplugin/myplugin.vim
. - Apparently vim prepends a
<SNR>123_
like string to script-local-functions. (123
can be any number). I could get the list of all functions with the :function command. Then find the full function name that matches<SNR>\d+_someFunction
and call that function by name. Looking at this question it looks like calling functions is possible once you know their string name.
Update:
If anyone is interested in an implementation of the hacky approach in @IngoKarkat's answer, you can take a look at my take on it here