Awesome Test::Class vim mapping

Ovid on 2007-06-21T09:17:21

In Test::Class, there is a terribly clunky way to tell it to run only one test (I can call it clunky because I submitted the patch for it). The way to do this is to create a TEST_METHOD environment variable:

package Tests::Customer;

use base 'My::Test::Class';

sub foo : Tests(12) { 
    ... 
}

sub bar : Tests {
    ...
}

sub baz : Tests(111) {
    ...
}

BEGIN { $ENV{TEST_METHOD} = 'quux' }
sub quux : Tests(no_plan) {
    ...
}

With the above, you will only have the 'quux' method run. This is very handy when you're focusing on a specific method and don't want to run your other long-running tests until you're done working on 'quux'. With the help of smylers (he really is a vim god, you know), I now have the following vim mapping:

noremap ,tm ?^sub.*:.*Testw"zyeOBEGIN { $ENV{TEST_METHOD} = 'z' }

It's a bit of a hack, but here's what it does. When you type ',tm', it does:

?^sub.*:.*Test<cr>
Search backwards until I find a test subroutine.
w"zye
Move to the next word (w) and yank to the end of the next word into the contents of the 'z' buffer.
OBEGIN { $ENV{TEST_METHOD} = 'z' }<esc>
Open a new line above the current one and create the BEGIN block, writing the contents of the 'z' buffer as the TEST_METHOD variable.

In other words, position your cursor in a method, type ',tm' while in command mode and it will try to create insert code which will only run the test method you're currently in. This really speeds up my testing!


Less hacky

Aristotle on 2009-08-22T06:56:12

fun! TestClassRunThisMethod()
    let save_cursor=getpos('.')
    let SAVE_TEST_METHOD=$TEST_METHOD

    call search('^sub \+\zs.*:.*Test','bcW')
    let $TEST_METHOD = expand('<cword>')
    exe '!printenv | sort'

    let $TEST_METHOD=SAVE_TEST_METHOD
    call setpos('.', save_cursor)
endfun

nnoremap <expr> ,tm TestClassRunThisMethod()

Re:Less hacky

Ovid on 2009-08-22T08:40:00

Excellent. I'll have to add this in.