blib/lib/Test/Skeleton/Generator.pm

filter: Statementstmt 87.1% 115 / 132 Branchbran 81.5% 31 / 38 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 91.3% 21 / 23 Podpod 0.0% 0 / 9 totaltotal 82.5% 175 / 212 CC 26 SCAR 14.4
CC
26
Cov
86%
get_test
12.0
get_package_functions
8.0
new
5.0
CRAP 4.2
↑ Summary
1 package Test::Skeleton::Generator;
2  
3 1 ▶use 5.001000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = "0.06";
9  
10 1 ▶use HTML::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Class::Inspector;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Sub::Information;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::Path qw/ make_path /;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 my $debug = 0;
17  
18 =head1 NAME
19  
20 Test::Skeleton::Generator - Create a skeleton for a test file based on an existing module
21  
22 =head1 SYNOPSIS
23  
24 simply
25  
26 generate_perl_test_skeleton -p ./lib/Module.pm -t t/test.t
27  
28 Or from another script/module:
29  
30 use Test::Skeleton::Generator;
31 my $generator = Test::Skeleton::Generator->new( {
32 package_file => './lib/Some/Module.pm',
33 skip_private_methods => 1,
34 } );
35 my $test_file_content = $generator->get_test;
36  
37 =head1 DESCRIPTION
38  
39 Test::Skeleton::Generator is supposed to be used from within your editor to quickly
40 generate skeletons/stubs for a test file that is will test the module you are
41 currently working on.
42  
43 So suppose you are working on the file C<./lib/Foo/Bar.pm> which hasn't got any tests
44 yet. Now you simply press a keyboard shortcut or click an icon (if you really have to)
45 and your editor will simply call perl like in the SYNOPSIS above to generate a .t file
46 in your ./t/ directory. You don't have to write the boiler-plate code yourself.
47  
48 Basically, there are two ways to use this module:
49  
50 The simplest way to use this module is from the command line. Simply use the distributed
51 script C<generate_perl_test_skeleton>. See its documentation for more details.
52  
53 But if you find it useful, you can also use it from another script or module which
54 will give you more option and lets you handle the content of the future test file yourself.
55  
56 =head2 Generated Code
57  
58 The code generated by this module, basically looks like this:
59  
60 use Test::Most;
61  
62 BEGIN {
63 use_ok( 'Your::Module' );
64 }
65  
66 test_sub1();
67 test_sub2();
68  
69 done_testing;
70  
71 sub get_object {
72 return Your::Module->new( @_ );
73 }
74  
75 sub test_sub1 {
76 can_ok 'Your::Module', 'sub1';
77 }
78  
79 sub test_sub2 {
80 can_ok 'Your::Module', 'sub2';
81 }
82  
83 So for each subroutine in your original module, you get a subroutine in the generated
84 test code that is supposed to test the original sub's code. You also get a function
85 that I find very handy, that you can call to get an object instance of your package.
86 I should probably add an option to Test::Skeletion::Generator to suppress the generation
87 of that sub. But you can also simply delete it, of course.
88  
89 =head1 SEE ALSO
90  
91 L<Test::StubGenerator> is a module that is very similar to this one. It uses L<PPI>
92 to analyze a given module and it will then generate a test stub. It even has an
93 option to run L<Perl::Tidy> on the generated test code. The only thing I don't
94 like about it is that it won't generate a test subroutine for each subroutine in
95 the module.
96  
97 =head1 LICENSE
98  
99 Copyright (C) Manni Heumann.
100  
101 This library is free software; you can redistribute it and/or modify
102 it under the same terms as Perl itself.
103  
104 =head1 AUTHOR
105  
106 Manni Heumann E<lt>github@lxxi.orgE<gt>
107  
108 =cut
109  
110  
111 12 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod undocumented
112 12 my $class = shift;
113 12 my $options = shift;
114  
115 12 my $self = bless {
116 package_file => '',
117 test_file => '',
118 skip_private_methods => 0,
119 debug => 0,
120 }, $class;
121  
122 12 ▶ if ( $options ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options) { } 11 1 12
123 11 foreach my $key ( keys %$self ) {
124 44 ▶ $self->{ $key } = $options->{ $key } if exists $options->{ $key };
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{$key} 14 30 44
125 };
126 11 ▶ $debug = 1 if $self->{ debug };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 1 10 11
127 11 return $self;
128 }
129 else {
130 1 die 'You need to provide the name of the package and the path to the test file.';
131 }
132 }
133  
134 0 ▶sub get_test {
Subroutine get_test: not called CC=3 SCAR=24.8
Pod undocumented
135 0 my $self = shift;
136  
137 0 my $package = $self->get_package_name;
138 0 my $existing_test_subs = $self->analyze_t_file;
139 0 my $subs_needing_tests = $self->get_package_functions( $package, $existing_test_subs );
140  
141 0 ▶ $self->prepare_test_file_path unless -e $self->{ test_file };
Branch: 1 branch
BranchTrueFalseTotal
unless -e $self->{"test_file"} 0 0 0
142  
143 0 my $tmpl = $self->prepare_template( $package, $subs_needing_tests );
144 0 ▶ $tmpl->param( update => %$existing_test_subs ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
%$existing_test_subs ? : 0 0 0
145  
146 0 my $existing_content = $self->get_updated_calls( $subs_needing_tests );
147  
148 0 return $existing_content . $tmpl->output;
149 }
150  
151 2 ▶sub get_updated_calls {
Subroutine get_updated_calls: called CC=4 SCAR=13.9
Pod undocumented
152 2 my $self = shift;
153 2 my $functions = shift;
154  
155 2 ▶ if ( defined $self->{ test_file } && -e $self->{ test_file } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'test_file'} and -e $self->{'test_file'}) { } 1 1 2
Condition66%2/3
defined $self->{"test_file"} and -e $self->{"test_file"}
!ll&&!rl&&r
011
MC/DC50%1/2
defined $self->{"test_file"} and -e $self->{"test_file"}
defined $self->{"test_file"} -e $self->{"test_file"}
Truth table66%2/3
A and B
A= defined $self->{"test_file"}B= -e $self->{"test_file"}
ABresult
0X0
100
111
156 1 local $/;
157 1 open my $fh, '<', $self->{ test_file };
158 1 my $content = <$fh>;
159 1 close $fh;
160  
161 1 my $missing_calls = '';
162 1 foreach my $fun ( @$functions ) {
163 2 $missing_calls .= sprintf "test_%s();\n", $fun->{ function };
164 }
165  
166 1 $content =~ s/^done_testing/$missing_calls\ndone_testing/ms;
167 1 return $content;
168 }
169 else {
170 1 return '';
171 }
172 }
173  
174 0 ▶sub write_test_file {
Subroutine write_test_file: not called CC=1 SCAR=6.9
Pod undocumented
175 0 my $self = shift;
176 0 my $content = shift;
177  
178 0 open my $fh, '>', $self->{ test_file };
179 0 print $fh $content;
180 0 close $fh;
181 }
182  
183 1 ▶sub prepare_template {
Subroutine prepare_template: called CC=1 SCAR=0.0
Pod undocumented
184 1 my $self = shift;
185 1 my $package = shift;
186 1 my $functions = shift;
187  
188 1 my $tmpl = HTML::Template->new(
189 filehandle => \*DATA,
190 global_vars => 1,
191 die_on_bad_params => 0,
192 );
193 1 $tmpl->param( package => $package );
194 1 $tmpl->param( functions => $functions );
195  
196 1 return $tmpl;
197 }
198  
199 3 ▶sub prepare_test_file_path {
Subroutine prepare_test_file_path: called CC=2 SCAR=6.9
Pod undocumented
200 3 my $self = shift;
201  
202 3 my $dir = dirname( $self->{ test_file } );
203 3 _debug( "dirname of $self->{ test_file } is $dir." );
204  
205 3 ▶ unless ( -d $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 2 1 3
206 2 _debug( "Making path $dir" );
207 2 make_path( $dir );
208 }
209 }
210  
211 4 ▶sub get_package_functions {
Subroutine get_package_functions: called CC=8 SCAR=20.8
Pod undocumented
212 4 my $self = shift;
213 4 my $package = shift;
214 4 my $existing_test_subs = shift;
215  
216 4 _debug( "Trying to use package $package." );
217 0 ▶ eval "use $package";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
218 4 ▶ if ( my $err = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 1 3 4
219 1 die "could not 'use' package $package: $err";
220 }
221  
222 3 my $wanted_subs = [];
223 3 my $found_subs = Class::Inspector->function_refs( $package );
224 3 foreach my $function ( @$found_subs ) {
225 201 my $info = inspect( $function );
226 201 my $name = $info->name;
227 201 ▶ next if $name =~ m/^[[:upper:]_]+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^[[:upper:]_]+$/ 153 48 201
228 48 ▶ next if $name =~ m/^_/ && $self->{ skip_private_methods };
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^_/ and $self->{"skip_private_methods"} 2 46 48
Condition100%3/3
$name =~ /^_/ and $self->{"skip_private_methods"}
!ll&&!rl&&r
4512
MC/DC100%2/2
$name =~ /^_/ and $self->{"skip_private_methods"}
$name =~ /^_/ $self->{"skip_private_methods"}
Truth table100%3/3
A and B
A= $name =~ /^_/B= $self->{"skip_private_methods"}
ABresult
0X0
100
111
229 46 ▶ next if $info->package ne $package;
Branch: 1 branch
BranchTrueFalseTotal
if $info->package ne $package 18 28 46
230  
231 28 ▶ if ( ! $existing_test_subs->{ "test_$name" } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$existing_test_subs{"test_$name"}) 27 1 28
232 27 push @$wanted_subs, { function => $name };
233 }
234 }
235  
236 3 return $wanted_subs;
237 }
238  
239 2 ▶sub get_package_name {
Subroutine get_package_name: called CC=4 SCAR=13.9
Pod undocumented
240 2 my $self = shift;
241  
242 2 my $package = $self->{ package_file };
243 2 ▶ if ( $package =~ m/\./ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package =~ /\./) 1 1 2
244 1 _debug( 'Package provided by file path' );
245 1 open my $fh, '<', $self->{ package_file };
246 1 ▶ while ( my $ln = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $ln = readline $fh)) 1 0 1
247 1 ▶ if ( $ln =~ m/^package (.+);\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ln =~ /^package (.+);\s*$/) 1 0 1
248 1 $package = $1;
249 1 last;
250 }
251 }
252 }
253 2 _debug( 'package name is ' . $package );
254  
255 2 return $package;
256 }
257  
258 12 ▶sub _debug {
Subroutine _debug: called CC=3 SCAR=11.0
259 12 ▶ return unless $debug;
Branch: 1 branch
BranchTrueFalseTotal
unless $debug 7 5 12
260 5 my @msgs = @_;
261 5 foreach ( @msgs ) {
262 5 print $_, "\n";
263 }
264 }
265  
266 1 ▶sub analyze_t_file {
Subroutine analyze_t_file: called CC=4 SCAR=14.0
Pod undocumented
267 1 my $self = shift;
268  
269 1 ▶ if ( ! -e $self->{ test_file } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $self->{"test_file"}) 0 1 1
270 0 return {};
271 }
272  
273 1 open my $fh, '<', $self->{ test_file };
274 1 my $subs = {};
275 1 ▶ while ( my $ln = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $ln = readline $fh)) 170 1 171
276 170 ▶ if ( $ln =~ m/^\bsub\s+(\w+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ln =~ /^\bsub\s+(\w+)/) 10 160 170
277 10 $subs->{ $1 } = 1;
278 }
279 }
280  
281 1 return $subs;
282 }
283  
284 1 1;
285  
286 __DATA__
↑ Summary