问题
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