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:
- 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.
- Open a split view to that test file for quick and easy modification, to flesh out the test stubs automatically created.
- Run the test file.
- Generate a Makefile (assuming your system supports make).
- 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:
- Is this released/available anywhere? (it wasn't)
- Can this be ported to emacs? (most likely not, as it's all vimscript)
- It would be cool if it were a perl script so I could call it from emacs.
So, I sought to address these.
- I've released the vim script.
- 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($,,$;,$*,$/)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.