secret
Last active

This will open a test file if your current buffer is a module or open the module if your current buffer is a testfile. See http://blogs.perl.org/users/ovid/2013/03/discoverable-tests-and-creating-testing-standards.html for details. I provide my own version of make_test_stub which will build test stubs that are exactly what I like to do in tests.

  • Download Gist
goto testmodule
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
" see http://blogs.perl.org/users/ovid/2013/03/discoverable-tests-and-creating-testing-standards.html
 
noremap ,gg :call GotoCorresponding()<cr>
 
function! GetCorresponding()
let b:tmpname = expand( '%:p' )
if (match(b:tmpname, '.pm$') != -1)
let b:testfile = substitute(substitute(b:tmpname, '.\+\/lib\/', '', ''), '.pm$', '.t', '' )
let b:basepath = substitute(b:tmpname, '\/lib\/.\+', '', '' )
return b:basepath . '/t/' . b:testfile
endif
if (match(b:tmpname, '.t$') != -1)
let b:module = substitute(substitute(b:tmpname, '.\+\/t\/', '', ''), '.t$', '.pm', '' )
let b:basepath = substitute(b:tmpname, '\/t\/.\+', '', '' )
return b:basepath . '/lib/' . b:module
endif
return ''
endfunction
 
function! GotoCorresponding()
let file = GetCorresponding()
let module = expand( '%' )
if !empty(file)
let ignore = system("perl /home/manni/bin/make_test_stub ".module." ".file)
execute "tab drop " . file
else
echoerr("Cannot find corresponding file for: ".module)
endif
endfunction
make_test_stub
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#!/usr/bin/env perl
use strict;
use warnings;
use autodie ':all';
use Class::Inspector;
use Sub::Information;
 
my ( $package_file, $test_file ) = @ARGV;
 
exit if -e $test_file;
 
my $package = $package_file;
$package =~ s{\.pm$}{} or exit;
$package =~ s{^.+/lib/}{};
$package =~ s{/}{::}g;
 
eval "use $package";
if ( my $err = $@ ) {
die $err;
}
 
open my $fh, '>', $test_file;
print $fh <<"END";
use Test::Most;
 
BEGIN {
use_ok( '$package' );
}
 
END
 
my $functions = Class::Inspector->function_refs($package);
 
my @functions;
foreach my $function (@$functions) {
my $info = inspect($function);
my $name = $info->name;
next if $name =~ /^_/ or $name =~ /^[[:upper:]_]+$/;
next if $info->package ne $package;
push @functions => $name;
}
 
foreach my $function (@functions) {
print $fh "test_${function}();\n";
}
print $fh "\ndone_testing;\n\n";
 
foreach my $function (@functions) {
print $fh <<"END";
sub test_${function} {
can_ok '$package', '$function';
}
 
END
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.