Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

RFC: Stub Test Generator perl module + vim script

by chargrill (Parson)
on Aug 09, 2007 at 06:29 UTC ( #631475=perlmeditation: print w/ replies, xml ) Need Help??

As a result of a recent "lunch and learn" session I gave at my office (discussed a bit here), I've created a vim script that maps a couple of key commands to perform the following functions:

  1. Analyze the source code of a perl file you're editing in vim, and create a test file in a t/ subdirectory of your current working directory (assuming one doesn't already exist), with an appropriate use_ok() or require_ok() test for your module/script, and ok() tests for all subroutines found.
  2. Open a split view to that test file for quick and easy modification, to flesh out the test stubs automatically created.
  3. Run the test file.
  4. Generate a Makefile (assuming your system supports make).
  5. Running the targets in that Makefile, which includes running your code through Devel::Cover.

I talked about this vim plugin during a lightning talk at a recent Chicago Perl Mongers meeting, and got some good feedback. Some of the questions/comments I got were:

  1. Is this released/available anywhere? (it wasn't)
  2. Can this be ported to emacs? (most likely not, as it's all vimscript)
  3. It would be cool if it were a perl script so I could call it from emacs.

So, I sought to address these.

  1. I've released the vim script.
  2. I've created a perl module and released it to the CPAN.

The documentation and usage is available, but a quick synopsis of the module is:

use Test::StubGenerator; my $stub = Test::StubGenerator->new( { file => '/path/to/MyModule.pm', } ); print $stub->gen_testfile;

Or, from the command line (for ease of use for vim or emacs):

$ perl -MTest::StubGenerator -e ' > my $stub = Test::StubGenerator->new({ file => "Module.pm" } +); > print $stub->gen_testfile;' > Module.t

There are a few more options for writing the test stubs directly to file, or to a given directory. I also have it passing the tests through perltidy, just to make sure I'm not creating unreadable test text - though that can be disabled.

Of note, you'll need to have PPI installed, at least version 1.118. Anyone who's using Perl::Critic shouldn't have a problem with this. And of course, Perl::Tidy needs to be installed.

So, I'm asking for any commentary and/or suggestions for improvement. But please be gentle, as these are my first releases :)

Thanks in advance!


--chargrill
s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)

Comment on RFC: Stub Test Generator perl module + vim script
Select or Download Code
Re: RFC: Stub Test Generator perl module + vim script
by naikonta (Curate) on Aug 11, 2007 at 01:19 UTC
    First of all, this is an excellent idea! I love both vim script and module versions (haven't got the chance to try it, but I will). As for the sequence of functionalities, I'd like to suggest to provide an option to not running the number 4 & 5 (makefile related), as some people writing modules already have a Makefile in their structure, specially if the skeleton was generated with h2xs. I'm talking about the vim script version, as for the module version I believe it is (or can be) more flexible.

    I also don't know whether the vim script version requires vim to be compiled with perl or not.


    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

      Actually, as far as the vim script/plugin goes, it will only write a t/filebasename.t and a Makefile if they don't already exist. This is especially useful if you've used it already to create a test stub (or a Makefile) and started fleshing out the tests (or customizing your Makefile) - "whoops, didn't mean to overwrite that!" :)


      --chargrill
      s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://631475]
Approved by Corion
Front-paged by liverpole
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2014-07-28 07:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (193 votes), past polls