简体   繁体   English

如何在 vimrc 文件中对 vim function 进行单元测试?

[英]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.我在 my.vimrc 文件中有函数,想测试这个 function 正在做我想做的事情。 I want to know if there is a rapid way to unit test a function within.vimrc.我想知道是否有一种快速的方法可以在.vimrc 中对 function 进行单元测试。

my function looks like:我的 function 看起来像:

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 .我要做的第一件事是将这些函数从你的.vimrc移到单独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).您将获得额外的好处,例如独特的命名空间、更少的混乱、更快的 Vim 启动,并且可以(仍然)从任何地方(映射、命令、单元测试)调用这些。

For a minimal, roll-your-own test, just invoke the function and make assertions.对于一个最小的、自己动手的测试,只需调用 function 并做出断言。 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.给定的 function 很难测试,因为它的副作用是:echo ,你必须做一个麻烦的:redirexecute()来捕获它。 It would be better to split this into the actual check (that returns a Boolean) and a separate reporting function with the :echo s.最好将其拆分为实际检查(返回布尔值)和带有:echo的单独报告 function 。

To notify yourself of the test results, you can just :echo any errors / results, or abort with :cquit .要通知自己测试结果,您可以:echo任何错误/结果,或使用:cquit中止。 See here for using Vim's built-in assert_...() functions to make assertions and minimal reporting.请参阅此处了解如何使用 Vim 的内置assert_...()函数进行断言和最小化报告。

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.因此,如果您打算 go 那个方向,可能值得评估那些。 My runVimTests plugin is one such framework (that I use to test dozens of my plugins);我的runVimTests 插件就是这样一个框架(我用它来测试我的几十个插件); the plugin page has links to alternatives.插件页面有替代方案的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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