Skip to content

Instantly share code, notes, and snippets.

@farfanoide
Last active April 9, 2017 21:58
Show Gist options
  • Save farfanoide/ce2a2bc04ae83dd1ab7bd87dd268d20f to your computer and use it in GitHub Desktop.
Save farfanoide/ce2a2bc04ae83dd1ab7bd87dd268d20f to your computer and use it in GitHub Desktop.

Test Private Functions with Vader.vim

I recently started learning vimscript and while creating some tests with vader i ran into problems while trying to correctly scope functions locally to my script. The thing is, it gets rather messy when you try to invoke a function which is scoped only to the script (s:FunctionName) so i found a question in stackoverflow which helped me generalize a solution at least for my own purposes.

Please note that this solution was only meant for simple functions that i wanted to unit test, meaning, given a certain input, check the function returns the correct value.

This is an extract from the test suite i wrote for inflector.vim, you can see the whole file here:

" First get the SID for our plugin and define a function that receives the name
" of the private function we want to run as well as the parameter. Note that if
" you require an arbitrary number of arguments you should change the signature of
" the `ExecutePrivateFunc` to account for that `:help function-argument`
" This would be at the top of the vader file inside an Execute block:

let g:sid = matchlist(execute('scriptnames'), '\([0-9]\+\): [^ ]*plugin/inflector.vim')[1]

function! g:ExecutePrivateFunc(function_name, str)
    let l:function_name = "<snr>" . g:sid . "_" . a:function_name
    let l:PrivateFunction = function(l:function_name)
    return PrivateFunction(a:str)
endfunction

" And then call inside other Execute blocks like so:
AssertEqual 'SOME_TEXT', g:ExecutePrivateFunc('Constantize', 'some text')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment