Given the recent talks about test plans, here's what I have in
.vim/plugin/ToggleTestPlan.vim:

    if exists( "toggle_test_plan" )
        finish
    endif
    let toggle_test_plan = 1

    map <Leader>tp :call ToggleTestPlan()<cr>

    function ToggleTestPlan()
        call SavePosition()
        let curr_line = 1
        while curr_line <= line("$")
            if match(getline(curr_line), 'More\s*tests') > -1
                %s/More tests =>/More 'no_plan'; # tests =>/
                call RestorePosition()
            elseif match(getline(curr_line), 'More\s*''no_plan') > -1
                %s/More 'no_plan';\s*# /More /
            endif
            let curr_line = curr_line + 1
        endwhile
    endfunction

    function SavePosition()
        let s:curLine = winline()
        let s:curColumn = wincol()
    endfunction

    function RestorePosition()
        exe s:curLine
        exe "normal! ".s:curColumn."|"
    endfunction

That's not perfect and I tried to make it more robust, but I found
there were annoying problems with different vim versions, so I never
followed up on it.  (I have a version at home which has nicely
highlighted messages telling me if there was an error and it also does
better cursor positioning).

Basically, when writing a test, I do "vim t/some_test.t" and something
similar to the following is automatically added via a template:

    #!/usr/bin/perl

    use strict;
    use warnings;

    use lib 'lib', 't/lib';
    use Test::More 'no_plan'; # tests => 1;

That curious 'Test::More' line automatically matches what my
ToggleTestPlan is looking for.  If I type ',tp' (toggle plan), I get
this:

    #!/usr/bin/perl

    use strict;
    use warnings;

    use Test::More tests => 1;

(You may have to type '\tp' instead.  See the note at the bottom of
this email)

And my cursor is automatically on the line with the test number (on my
home version, the cursor is automatically on the test number).  So
let's say I change the test number to '3'.  Later, if I hit ',tp'
again, I get this:

    #!/usr/bin/perl

    use strict;
    use warnings;

    use Test::More 'no_plan'; # tests => 3;

However, then it leaves me on the line I'm currently editing since I'm
presumably wanting to add or alter some tests.  It really helps with
the minor test plan annoyance.

Note:  I'm using 'Leader' to set my leader to ',' for the plugin.  If
you do that, you might want this in your .vimrc:

  let mapleader=","

I understand that the comma actually has a special meaning in Vim and a
backslash '\' (the default leader) is a better choice, but that key is
so annoyingly placed (IMHO) on the British keyboard that I use a comma
instead.

Cheers,
Ovid

--

Buy the book -- http://www.oreilly.com/catalog/perlhks/
Perl and CGI -- http://users.easystreet.com/ovid/cgi_course/

Reply via email to