用于测试 vim 插件的工具

发布于 2024-09-05 14:46:41 字数 820 浏览 6 评论 0原文

我正在寻找一些用于测试 vim 脚本的工具。 的 vim 脚本

  • 执行单元/功能测试
  • ,或其他库的类(例如 Python 的 unittest 模块),方便
    • 使用参数运行 vim,使其对其环境进行一些测试,并且
    • 根据输出确定给定测试是否通过。

我知道有几个进行单元测试的 vim 脚本,但它们的记录有点模糊,实际上可能有用,也可能没有用:

vim-unit

  • 旨在“为 vim 脚本提供简单的单元测试框架和工具”,
  • 第一个也是唯一的版本 (v0.1) 于 2004 年发布
  • 文档没有提及它是否可靠地工作,只是说它是“从完成开始的票价[原文如此]”。

unit-test.vim

  • 这个看起来也相当实验性,可能不是特别可靠。
  • 可能已被放弃或后搁置:最后一次提交是在 2009-11(> 6 个月前)
  • 没有创建标记的修订(即没有发布)

因此来自使用这两个现有模块之一的人员的信息,和/或链接到其他更清晰可用的选项,非常受欢迎。

I'm looking for some tools for testing vim scripts. Either vim scripts that

  • do unit/functional testing, or
  • classes for some other library (eg Python's unittest module) that make it convenient to
    • run vim with parameters that cause it to do some tests on its environment, and
    • determine from the output whether or not a given test passed.

I'm aware of a couple of vim scripts that do unit testing, but they're sort of vaguely documented and may or may not actually be useful:

vim-unit:

  • purports "To provide vim scripts with a simple unit testing framework and tools"
  • first and only version (v0.1) was released in 2004
  • documentation doesn't mention whether or not it works reliably, other than to state that it is "fare [sic] from finished".

unit-test.vim:

  • This one also seems pretty experimental, and may not be particularly reliable.
  • May have been abandoned or back-shelved: last commit was in 2009-11 (> 6 months ago)
  • No tagged revisions have been created (ie no releases)

So information from people who are using one of those two existent modules, and/or links to other, more clearly usable, options, are very welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

悲歌长辞 2024-09-12 14:46:41

vader.vim 非常简单,而且令人惊叹。它没有外部依赖(不需要 ruby​​/rake),它是一个纯粹的 vimscript 插件。这是一个完全指定的测试:

Given (description of test):
  foo bar baz

Do (move around, insert some text):
  2Wiab\<Enter>c

Expect:
  foo bar ab
  cbaz

如果您打开了测试文件,则可以像这样运行它:

:Vader %

或者您可以指向文件路径:

:Vader ./test.vader

vader.vim is easy, and amazing. It has no external dependencies (doesn't require ruby/rake), it's a pure vimscript plugin. Here's a fully specified test:

Given (description of test):
  foo bar baz

Do (move around, insert some text):
  2Wiab\<Enter>c

Expect:
  foo bar ab
  cbaz

If you have the test file open, you can run it like this:

:Vader %

Or you can point to the file path:

:Vader ./test.vader
紫瑟鸿黎 2024-09-12 14:46:41

我已经成功地将 Andrew Radev 的 VimrunnerRSpec 用于测试 Vim 插件并将其设置在持续集成服务器上。

简而言之,Vimrunner 使用 Vim 的客户端-服务器功能来启动 Vim 服务器,然后发送远程命令,以便您可以检查(并验证)结果。它是 Ruby gem,因此您至少需要熟悉 Ruby,但如果您投入时间,那么您将获得 RSpec 的全部功能来编写测试。

例如,一个名为 spec/runspec.vim_spec.rb 的文件:

require "vimrunner"
require "fileutils"

describe "runspec.vim" do
  before(:suite) do
    VIM = Vimrunner.start_gui_vim
    VIM.add_plugin(File.expand_path('../..', __FILE__), 'plugin/runspec.vim')
  end

  after(:all) do
    VIM.kill
  end

  it "returns the current path if it ends in _test.rb" do
    VIM.echo('runspec#SpecPath("foo_test.rb")').should == "foo_test.rb"
    VIM.echo('runspec#SpecPath("bar/foo_test.rb")').should == "bar/foo_test.rb"
  end

  context "with a spec directory" do
    before do
      FileUtils.mkdir("spec")
    end

    after do
      FileUtils.remove_entry_secure("spec")
    end

    it "finds a spec with the same name" do
      FileUtils.touch("spec/foo_spec.rb")
      VIM.echo('runspec#SpecPath("foo.rb")').should == "spec/foo_spec.rb"
    end
  end
end

我在 “使用 RSpec 和 Vimrunner 在 Travis CI 上测试 Vim 插件” 如果您想了解更多详细信息。

I've had success using Andrew Radev's Vimrunner in conjunction with RSpec to both test Vim plugins and set them up on a continuous integration server.

In brief, Vimrunner uses Vim's client-server functionality to fire up a Vim server and then send remote commands so that you can inspect (and verify) the outcome. It's a Ruby gem so you'll need at least some familiarity with Ruby but if you put the time in then you get the full power of RSpec in order to write your tests.

For example, a file called spec/runspec.vim_spec.rb:

require "vimrunner"
require "fileutils"

describe "runspec.vim" do
  before(:suite) do
    VIM = Vimrunner.start_gui_vim
    VIM.add_plugin(File.expand_path('../..', __FILE__), 'plugin/runspec.vim')
  end

  after(:all) do
    VIM.kill
  end

  it "returns the current path if it ends in _test.rb" do
    VIM.echo('runspec#SpecPath("foo_test.rb")').should == "foo_test.rb"
    VIM.echo('runspec#SpecPath("bar/foo_test.rb")').should == "bar/foo_test.rb"
  end

  context "with a spec directory" do
    before do
      FileUtils.mkdir("spec")
    end

    after do
      FileUtils.remove_entry_secure("spec")
    end

    it "finds a spec with the same name" do
      FileUtils.touch("spec/foo_spec.rb")
      VIM.echo('runspec#SpecPath("foo.rb")').should == "spec/foo_spec.rb"
    end
  end
end

I've written about it at length in "Testing Vim Plugins on Travis CI with RSpec and Vimrunner" if you want more detail.

禾厶谷欠 2024-09-12 14:46:41

我正在维护另一个(纯 Vimscript)UT 插件

它有文档记录,附带几个示例,并且我的其他插件也使用它。

它的目的是测试函数结果和缓冲区内容,并在快速修复窗口中显示故障。异常调用堆栈也被解码。 AFAIK,这是迄今为止唯一一个(或至少是第一个)旨在填充快速修复窗口的插件。从那时起,我添加了帮助程序脚本来使用 rspec (+Vimrunner) 生成测试结果。

自 v2.0(2020 年 5 月)以来,该插件还可以测试缓冲区内容 - 在使用映射/片段/...进行更改后。直到那时我一直在使用其他插件。例如,我曾经使用 VimRunner+RSpec 在 travis 上测试我的 C++ 片段(来自 lh-cpp)。

关于语法,例如,

Assert 1 > 2
Assert 1 > 0
Assert s:foo > s:Bar(g:var + 28) / strlen("foobar")

debug AssertTxt (s:foo > s:Bar(g:var+28)
      \, s:foo." isn't bigger than s:Bar(".g:var."+28)")
AssertEquals!('a', 'a')
AssertDiffers('a', 'a')
let dict = {}
AssertIs(dict, dict)
AssertIsNot(dict, dict)
AssertMatch('abc', 'a')
AssertRelation(1, '<', 2)
AssertThrows 0 + [0]

将产生以下内容:

tests/lh/README.vim|| SUITE <[lh#UT] Demonstrate assertions in README>
tests/lh/README.vim|27 error| assertion failed: 1 > 2
tests/lh/README.vim|31 error| assertion failed: s:foo > s:Bar(g:var + 28) / strlen("foobar")
tests/lh/README.vim|33 error| assertion failed: -1 isn't bigger than s:Bar(5+28)
tests/lh/README.vim|37 error| assertion failed: 'a' is not different from 'a'
tests/lh/README.vim|40 error| assertion failed: {} is not identical to {}

或者,如果我们想要测试缓冲区内容

silent! call lh#window#create_window_with('new') " work around possible E36
try
    " :SetBufferContent a/file/name.txt 
    " or
    SetBufferContent << trim EOF
    1
    3
    2
    EOF

    %sort

    " AssertBufferMatch a/file/NAME.txt
    " or
    AssertBufferMatch << trim EOF
    1
    4
    3
    EOF
finally
    silent bw!
endtry

,其结果为

tests/lh/README.vim|78 error| assertion failed: Observed buffer does not match Expected reference:
|| ---
|| +++
|| @@ -1,3 +1,3 @@
||  1
|| -4
|| +2
||  3

(在快速修复窗口中点击 D 将在 diff 模式下打开生成的结果以及预期结果)一个新标签)

There is another (pure Vimscript) UT plugin that I'm maintaining.

It is documented, it comes with several examples, and it is also used by my other plugins.

It aims at testing function results and buffer contents, and displaying the failures in the quickfix window. Exception callstacks are also decoded. AFAIK, it's the only plugin so far (or at least the first) that's meant to fill the quickfix window. Since then, I've added helper scripts to produce test results with rspec (+Vimrunner)

Since v2.0 (May 2020), the plugin can also test buffer content -- after it has been altered with mappings/snippets/.... Up until then I've been using other plugins. For instance, I used to test my C++ snippets (from lh-cpp) on travis with VimRunner+RSpec.

Regarding the syntax, for instance the following

Assert 1 > 2
Assert 1 > 0
Assert s:foo > s:Bar(g:var + 28) / strlen("foobar")

debug AssertTxt (s:foo > s:Bar(g:var+28)
      \, s:foo." isn't bigger than s:Bar(".g:var."+28)")
AssertEquals!('a', 'a')
AssertDiffers('a', 'a')
let dict = {}
AssertIs(dict, dict)
AssertIsNot(dict, dict)
AssertMatch('abc', 'a')
AssertRelation(1, '<', 2)
AssertThrows 0 + [0]

would produce:

tests/lh/README.vim|| SUITE <[lh#UT] Demonstrate assertions in README>
tests/lh/README.vim|27 error| assertion failed: 1 > 2
tests/lh/README.vim|31 error| assertion failed: s:foo > s:Bar(g:var + 28) / strlen("foobar")
tests/lh/README.vim|33 error| assertion failed: -1 isn't bigger than s:Bar(5+28)
tests/lh/README.vim|37 error| assertion failed: 'a' is not different from 'a'
tests/lh/README.vim|40 error| assertion failed: {} is not identical to {}

Or, if we want to test buffer contents

silent! call lh#window#create_window_with('new') " work around possible E36
try
    " :SetBufferContent a/file/name.txt 
    " or
    SetBufferContent << trim EOF
    1
    3
    2
    EOF

    %sort

    " AssertBufferMatch a/file/NAME.txt
    " or
    AssertBufferMatch << trim EOF
    1
    4
    3
    EOF
finally
    silent bw!
endtry

which results into

tests/lh/README.vim|78 error| assertion failed: Observed buffer does not match Expected reference:
|| ---
|| +++
|| @@ -1,3 +1,3 @@
||  1
|| -4
|| +2
||  3

(hitting D in the quickfix window will open the produced result alongside the expected result in diff mode in a new tab)

自在安然 2024-09-12 14:46:41

我以前用过 vim-unit 。至少,这意味着您不必编写自己的 AssertEqualsAssertTrue 函数。它还有一个很好的功能,可以让您运行当前函数(如果该函数以“Test”开头),方法是将光标放在函数体内并键入 :call VUAutoRun()

该文档有点不确定且未完成,但如果您有使用其他 XUnit 测试库的经验,它对您来说不会陌生。

提到的两个脚本都没有方法来检查 vim 特定功能 - 您无法更改缓冲区,然后检查结果的预期 - 因此您必须以可测试的方式编写 vim 脚本。例如,将字符串传递到函数中,而不是使用函数本身内的 getline() 来将它们从缓冲区中取出,返回字符串而不是使用 setline() 之类的事情。

I've used vim-unit before. At the very least it means you don't have to write your own AssertEquals and AssertTrue functions. It also has a nice feature that lets you run the current function, if it begins with "Test", by placing the cursor within the function body and typing :call VUAutoRun().

The documentation is a bit iffy and unfinished, but if you have experience with other XUnit testing libraries it won't be unfamiliar to you.

Neither of the script mentioned have ways to check for vim specific features - you can't change buffers and then check expectations on the result - so you will have to write your vimscript in a testable way. For example, pass strings into functions rather than pulling them out of buffers with getline() inside the function itself, return strings instead of using setline(), that sort of thing.

燃情 2024-09-12 14:46:41

vim-vspec
您的测试是用 vimscript 编写的,您可以使用 BDD 风格编写它们(描述、它、期望……)

runtime! plugin/sandwich/function.vim
describe 'Adding Quotes'
  it 'should insert "" in an empty buffer'
    put! = ''
    call SmartQuotes("'")

    Expect getline(1) == "''"
    Expect col('.') == 2
  end
end

。GitHub 提供了视频和文章的链接,可帮助您入门:

There is vim-vspec.
Your tests are written in vimscript and you can write them using a BDD-style (describe, it, expect, ...)

runtime! plugin/sandwich/function.vim
describe 'Adding Quotes'
  it 'should insert "" in an empty buffer'
    put! = ''
    call SmartQuotes("'")

    Expect getline(1) == "''"
    Expect col('.') == 2
  end
end

The GitHub has links to a video and an article to get you started:

八巷 2024-09-12 14:46:41

对于功能测试,有一个名为 vroom 的工具。它有一些限制,可能需要几秒到几分钟的时间才能完成对大型项目的彻底测试,但它具有很好的文字测试/文档格式,并具有 vim 语法突出显示支持。

它用于测试 codefmt 插件和一些类似的项目。您可以查看 vroom/ dir 那里的示例。

For functional testing, there's a tool called vroom. It has some limitations and can take seconds-to-minutes to get through thorough tests for a good size project, but it has a nice literate testing / documentation format with vim syntax highlighting support.

It's used to test the codefmt plugin and a few similar projects. You can check out the vroom/ dir there for examples.

屌丝范 2024-09-12 14:46:41

另外几个候选者:

VimBot - 与 VimRunner 类似,它是用 Ruby 编写的,允许您控制 vim 实例远程。旨在与单元测试框架 RSpec 一起使用。

VimDriver - 与 VimBot 相同,只是使用 Python 而不是 Ruby 完成(作为 VimBot 的直接端口启动),因此您如果您更熟悉Python的单元测试框架,可以使用它。

Another few candidates:

VimBot - Similar to VimRunner in that it's written in Ruby and allows you to control a vim instance remotely. Is built to be used with the unit testing framework RSpec.

VimDriver - Same as VimBot except done in Python instead of Ruby (started as a direct port from VimBot) so you can use Python's unit testing framework if you're more familiar with that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文