首页 > 解决方案 > 测试 vim 脚本

问题描述

我正在探索测试 vim 脚本的选项。我想知道我是否需要像 Vader 这样的工具,或者我是否可以从命令行使用 vim 自己动手。

我正在使用 Perl(但它可以是任何语言),我可以这样做:

`$path_to_vi -c "normal iLink" -c "normal \r" -c wq ~/vimwiki/output.md`;

然后我可以output.md通过适当的测试检查内容。

感谢您提供任何提示和建议。

标签: testingvim

解决方案


您可以使用内置函数,例如:h assert_true()测试脚本。每次调用 assert 函数时,都会添加一条新的错误消息,v:error如果失败,请检查:h assert-return. 请注意,如果测试失败,断言函数返回 1,而不是 0。

断言家庭

assert_beeps
assert_equal
assert_equalfile
assert_exception
assert_fails
assert_false
assert_inrange
assert_match
assert_notequal
assert_notmatch
assert_report
assert_true

我使用两种测试方式:

运行所有测试用例,然后一一报告错误

" clear errors
let v:errors = []

call assert_true(...)
call assert_equal(...)
call assert_match(...)
...
echohl WarningMsg
for err in v:errors
  echo err
endfor
echohl None

逐个运行case,如果测试失败立即停止

if(assert_true(...)) | throw v:errors[-1] | endif
if(assert_equal(...)) | throw v:errors[-1] | endif
if(assert_match(...)) | throw v:errors[-1] | endif

推荐阅读