1

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.

6
  • Why are you using backticks (which capture output) if you're just going to ignore the result? Commented May 9, 2019 at 4:32
  • Doesn't matter. The result is tossed. It's quicker to type. Commented May 9, 2019 at 4:54
  • Would you prefer I used system and toss the response code instead? Commented May 9, 2019 at 4:57
  • If you just need to verify the output and that is all you need, than that is definitely okay and you don't need a testing framework like vader. That's basically what I do for my tests as well. I don't actually see the sense for my limited requirements. Commented May 9, 2019 at 11:03
  • @StevieD Yes, for two reasons: It's simpler and more efficient to just run the program without capturing its output, and (more importantly) it doesn't involve the shell. If you can avoid going through the shell, that's always a win. Commented May 9, 2019 at 18:26

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I didn't know about these tests. But with regard to my question, is there any advantage to be gained by using Vader or using an outside script to manipulate a vim file with commands to see if it responds as expected? I guess what I'm asking is what the workflow is.
I think it depends on your vim scripts. It's hard to give specific workflow without knowing your vim scripts. eg: if you want to test result file is the same as expected, you can create an expect file manually and use assert_equalfile to test the result.
It seems the simples approach would be to just create a directory full of vim scripts that did various things to a file and then run the a test at the end of each script to see if results are as expected.
This look interesting: github.com/google/vroom Looks like it makes it very quick and easy to install and run tests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.