Toggling test plans with vim

Ovid on 2006-08-09T09:52:15

For some reason, folks seem to complain a lot about the test plan in vim because, as you know, it's so much work to switch 'no_plan' to a real plan. Based on a couple of mappings that chromatic wrote, I wrote a plugin that toggles the test plan for you just by typing ',tp' ('t'oggle 'p'lan). Now I expect folks to complain because they still have to update the test count manually.


Complaining

cog on 2006-08-09T11:44:17

Could your plugin perhaps count the number of calls to qw/isa_ok is isnt ok is_deeply etc/ and update the test count with that information?

Sure, compiling that list would be tedious, but each person could maintain their own.

Of course... I'm forgetting about tests inside cycles, so... hum...

Well, I just felt like complaining :-)

Re:Complaining

Ovid on 2006-08-09T11:55:58

Yeah, the problem is that this involves parsing Perl, a notoriously unreliable thing. Otherwise, I'd be happy to. Fortunately, I work with a vim guru (smylers) and he's been helping me with the tricky bits.

Re:Complaining

sigzero on 2006-08-09T12:10:38

I am not complaining...just nudging you to post your plugin(s) to vim.org.

Re:Complaining

Ovid on 2006-08-09T12:55:00

I think I will, but now that I'm looking at it, I have a bunch more work to do on it. I have some automatic pod generation and boilerplate routines that I could use to create a 'PerlDeveloper.vim' script that could incorporate a lot of nifty things to make vim a very easy choice for Perl. However, can't do that at work :)

Re:Complaining

sigzero on 2006-08-09T13:38:18

There is a "perl-support.vim" that adds a bunch of stuff for Perl programmers. Maybe you can collaborate to add your stuff to his?

Re:Complaining

Ovid on 2006-08-09T13:51:37

Hey, that's a good idea. I'll check it out. Thanks!

Re:Complaining

Aristotle on 2006-08-20T07:20:57

Unfortunately the code in there is positively awful. But maybe Ovid has a higher revulsion than me, or something.

Emacs toggle-test-plan

jjore on 2006-08-09T14:10:30

Have some Elisp code.

(defun toggle-test-plan ()
  (interactive)
  (let ((new-pos))
    (save-excursion
      (goto-char (point-min))
      (cond ((re-search-forward "More[ \t]+tests[ \t]*=>[ \t]*" nil t)
         (replace-match "More 'no_plan'; # tests => " t t))
        ((re-search-forward "More[ \t]+'no_plan';[ \t]*#[ \t]*" nil t)
         (replace-match "More " t t)
         (setq new-pos (point)))))
    (if new-pos (goto-char new-pos))))

Re:Emacs toggle-test-plan

jjore on 2006-08-09T14:13:06

Argh. Tabs ruined my indentation.
(defun toggle-test-plan ()
  (interactive)
  (let ((new-pos))
    (save-excursion
      (goto-char (point-min))
      (cond ((re-search-forward "More[ \t]+tests[ \t]*=>[ \t]*" nil t)
             (replace-match "More 'no_plan'; # tests => " t t))
            ((re-search-forward "More[ \t]+'no_plan';[ \t]*#[ \t]*" nil t)
             (replace-match "More " t t)
             (setq new-pos (point)))))
    (if new-pos (goto-char new-pos))))

Re:Emacs toggle-test-plan

Ovid on 2006-08-09T14:32:47

Hey, that looks awesome (no surprise that someone interested in Prolog would have Lisp skills, I suppose).

I've been thinking about creating several vim tools specifically dedicated to making tests easier to write and maintain. Would you be interested in converting them to eLisp?

Re:Emacs toggle-test-plan

jjore on 2006-08-09T15:23:43

Sure, that's fine. What were you going to create? The only thing I really want to do is get *smart* autocompletion of identifiers. Right now all I have Emacs doing is noticing that fun\t might be short for things like function_name, function_fqfname, function_ref, etc. That is, anything \w+-like that it already has open somewhere. That's often good enough especially since I don't ever really write out whole variable names anymore but it'd be even nicer if it knew something about methods.

So what helpers do you think a person ought to have when writing tests?

Re:Emacs toggle-test-plan

Ovid on 2006-08-09T15:51:36

There are a few features I'm thinking about.

  1. ,tp Toggle plan
  2. ,tt Run test (even with Test::Class)
  3. ,tm Run Test::Class test method (requires a planned but unimplemented feature of Test::Class)
  4. ,gt and ,gm, "goto test" and "goto module", respectively.
  5. Other things I think up as I go along (I have vauge ideas)

Obviously I have 1 and 2 working. Number 3 requires a planned feature for Test::Class that lets you run a single test method. I've submitted a patch, but it wasn't as clean as Adrian would have liked and was not incorporated (I'm disappointed, but agree).

I'd also like TogglePlan to work with Test::Class and that shouldn't be too hard, but I've not written the code for it yet.

The ,gt and ,gm functions are tough. They require a bit of discipline on the part of developers in how they want to lay out their code so that one can find the appropriate test and/or implementation in a deterministic fashion. If the user has a non-standard layout, it would require that they set some variables in their .vimrc file to let PerlTests.vim know how to find things. Also, while this is fairly easy for Test::Class, I think special comments would be necessary in modules and tests if just using regular Test::More style scripts. I'm thinking something like this:

package Foo;

# :test:t/foo.t:

And in the test:

#!/usr/bin/perl -T

# :module:lib/Foo.pm

If that worked, you could easily pop back and forth between your test and its implementation with very little work.

For Test::Class, you would specify test package identifier (typically 'TEST' or 'Test') and whether it's prefix, postfix, or infix. Here are the allowed styles:

Test::My::Dedicated::Customer; # prefix
My::Dedicated::Customer::Test; # postfix
My::Dedicated::Test::Customer; # infix

In your .vimrc, you would then set:

set pt_testpath='t/tests/'
set pt_libpath='lib/'
set pt_testpackage='TEST'
set pt_testfix='prefix'

There's a reason why 'infix' is there, but it's a long story dealing with a company I used to work for. I doubt I'd actually include that.

Re:Emacs toggle-test-plan

Ovid on 2006-08-09T16:33:27

Ooh, and along with the regular documentation, it would be nice to include something like Ian Langworth's testing quick reference card information. I'm unsure of the syntax, but if someone new to testing wants a quick refresher of how to use Test::More, they could type something like:

:quickref Test::More

And get short output like what's on the card. Still, I don't like that syntax.

Re:Emacs toggle-test-plan

sigzero on 2006-08-10T01:13:14

If you collaborate with the already excellent "perl-support.vim" module author then you can include that PDF with the distro.