简体   繁体   中英

How to unit test vim function in vimrc file?

I have functions inside my.vimrc file and want to test that this function is doing what I want it to do. I want to know if there is a rapid way to unit test a function within.vimrc.

my function looks like:

fun! FindPythonMainFile(filename, )
    let file = findfile(filename)  
      if (file)
         echo "file exists"
      else
         echo "file doesn't exists"
     endif

endf

best regard.

First thing I would do is moving these functions out of your .vimrc into separate autoload-functions . You'll get additional benefits like unique namespaces, less clutter, faster Vim startup, and these can (still) be invoked from anywhere (mappings, commands, unit tests).

For a minimal, roll-your-own test, just invoke the function and make assertions. Your function as given would be hard to test, as its side effect is the :echo , and you'd have to do a cumbersome :redir or execute() to capture it. It would be better to split this into the actual check (that returns a Boolean) and a separate reporting function with the :echo s.

To notify yourself of the test results, you can just :echo any errors / results, or abort with :cquit . See here for using Vim's built-in assert_...() functions to make assertions and minimal reporting.

I don't think many people test their own personal customizations (but I applaud you for attempting to do that,) For plugins (that you may intend to publish to others). this is a different matter, Multiple approaches and frameworks exist there. so it may be worth to evaluate those if you intend to go that direction. My runVimTests plugin is one such framework (that I use to test dozens of my plugins); the plugin page has links to alternatives.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM