测试 vim 脚本 [英] Testing vim scripts
问题描述
我正在探索测试 vim 脚本的选项.我想知道我是否需要一个像 Vader 这样的工具,或者我是否可以从命令行使用 vim 来推出我自己的工具.
I'm exploring options for testing vim scripts. I'm wondering if I need a tool like Vader or if I can just roll my own using vim from the command line.
我正在使用 Perl(但它可以是任何语言),我可以这样做:
I'm using Perl (but it could be any language), and I can do this:
`$path_to_vi -c "normal iLink" -c "normal \r" -c wq ~/vimwiki/output.md`;
然后我可以通过适当的测试检查 output.md
的内容.
Then I can just inspect the contents of output.md
with an appropriate test.
感谢您的任何提示和建议.
Thanks for any tips and advice.
推荐答案
您可以使用诸如 :h assert_true()
之类的内置函数来测试脚本.每次调用 assert 函数时,都会在 v:error
中添加一条新的错误消息,如果失败,请检查 :h assert-return
.请注意,如果测试失败,assert 函数返回 1,而不是 0.
You can use built in functions such as :h assert_true()
to test scripts. Every time you call an assert function, a new error message will be added to v:error
if it failed, check :h assert-return
. Note that assert function returns 1 if test failed, not 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
我使用两种风格的测试:
I use two styles of test:
运行所有测试用例,然后一一报错:
" 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
一个一个运行案例,测试失败立即停止
if(assert_true(...)) | throw v:errors[-1] | endif
if(assert_equal(...)) | throw v:errors[-1] | endif
if(assert_match(...)) | throw v:errors[-1] | endif
这篇关于测试 vim 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!