Testing vim scripts

时光怂恿深爱的人放手 提交于 2020-03-03 07:38:07

问题


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.

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`;

Then I can just inspect the contents of output.md with an appropriate test.

Thanks for any tips and advice.


回答1:


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 families

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:

Run all test cases, then report errors one by one:

" 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

Run cases one by one, stop immediately if test failed

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


来源:https://stackoverflow.com/questions/56052213/testing-vim-scripts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!