Consider a script local function in a
plugin/myplugin.vim file. For example
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.
if s:someFunction() != 4 throw "Error" endif
This obviously does not work because
s:someFunction is not visible from outside of the
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
autoloaddirectory and rename it
myplugin#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 the
- Apparently vim prepends a
<SNR>123_like string to script-local-functions. (
123can 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+_someFunctionand call that function by name. Looking at this question it looks like calling functions is possible once you know their string name.