Vim: Run All Tests Which Cover This Module

Ovid on 2008-10-10T15:29:04

Update: Rufus Cable has written a vim syntax file for TAP, along with some other useful bindings. I think I'll put this together with my stuff and get around to that plugin.

It's trivial to set up vim mappings to run the current test program you're editing. I also wrote recently about how to run all tests in your vim buffers. Today, I'll explain a great technique to run all tests for the module you're currently editing.

First, grab Johan Lindstrom's Devel::CoverX::Covered. Once you have that set up and working, you can use something like this to your .vimrc (when, oh when, am I going to create a proper plugin for all of this and add it to github?):

function! PerlMappings()
    " run the code
    noremap  r :!perl %

    " or check that it compiles
    noremap  r :!perl -c %

    " or run tall of the tests for it
    noremap  t :call TestModuleCoverage()
endfunction

function! PerlTestMappings()
    noremap  r :!prove -vl %

    " or check that it compiles
    noremap  r :!perl -c %

    noremap  t :!prove -vl %
endfunction

function! TestModuleCoverage()
    let filename = bufname('%')

    let tests = system('covered covering --source_file="'. filename .'"')

    let result  = split( tests, "\n" )

    if empty(result)
        echomsg "No tests found for: ". filename
    else
        execute ':!prove  ' . join(result)
    endif
endfunction

Then, if you're in a test file and hit ",t" (assuming that the comma is your leader), then you'll run the test. If you're in a regular Perl program or module, hitting ",t" will run all tests which cover that program or module.