I found a bug in the CoC plugin, and I was going to fix the bug, but first I wanted to write some documentation/learning tests as well as a regression test. The question was how: unit testing does not seem to be that much used, apart from the existence of the Vader.vim project.
The piece of code I would like to test does some tests for the existence of some patches in the version of the Vim running (i.e.
has('nvim-0.4.0')), but in order to test that in a manner that does not rely on my own version of Vim, I would need to control the
has function. Is it possible to override/mock that function (or any other built-in for that matter)? The author of Vader thinks the answer is no.
I could redo this function to do DI of some sorts:
let Local_has = has if exists("*Testing_has"): Local_has = Testing_has endif if Local_has('nvim-0.4.0'): do_something endif
In Learn VimScript the hard way they mention storing function references, and this seems to be a good beginning:
let MyHas = function("has") let YourHas = MyHas echo has('nvim-0.4.3') " => 1 echo MyHas('nvim-0.4.3') " => 1 echo YourHas('nvim-0.4.3') " => 1
The question remains on how to inject the testing version. Just write a test.vim that has created the overrides that then sources the main script?