Skip to content

Instantly share code, notes, and snippets.

@mannih
Last active December 15, 2015 00:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mannih/16c75db8a94f73c2f75d to your computer and use it in GitHub Desktop.
Save mannih/16c75db8a94f73c2f75d to your computer and use it in GitHub Desktop.
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.
" 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
#!/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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment