File Coverage

blib/lib/Test/NaiveStubs.pm
Criterion Covered Total %
statement 51 51 100.0
branch 12 14 85.7
condition n/a
subroutine 9 9 100.0
pod 3 3 100.0
total 75 77 97.4


line stmt bran cond sub pod time code
1             package Test::NaiveStubs;
2             our $AUTHORITY = 'cpan:GENE';
3              
4             # ABSTRACT: Generate test stubs for methods and functions
5              
6             our $VERSION = '0.0702';
7              
8 1     1   1242 use Moo;
  1         11645  
  1         4  
9 1     1   1880 use strictures 2;
  1         1647  
  1         39  
10 1     1   651 use namespace::clean;
  1         11342  
  1         5  
11              
12 1     1   275 use Package::Stash;
  1         2  
  1         26  
13 1     1   471 use Sub::Identify 'stash_name';
  1         969  
  1         711  
14              
15              
16             has module => (
17             is => 'ro',
18             required => 1,
19             );
20              
21              
22             has name => (
23             is => 'ro',
24             builder => 1,
25             );
26              
27             sub _build_name {
28 1     1   7 my ($self) = @_;
29 1         6 ( my $name = $self->module ) =~ s/::/-/g;
30 1         3 $name = lc $name;
31 1         6 return "$name.t";
32             }
33              
34              
35             has subs => (
36             is => 'rw',
37             init_arg => undef,
38             );
39              
40              
41             has is_oo => (
42             is => 'rw',
43             init_arg => undef,
44             default => sub { 0 },
45             );
46              
47              
48             sub gather_subs {
49 2     2 1 1012 my ($self) = @_;
50              
51 2         52 my $subs = Package::Stash->new($self->module)->get_all_symbols('CODE');
52              
53 2         7 my @subs;
54 2         8 for my $sub ( keys %$subs ) {
55 20         47 my $packagename = stash_name($subs->{$sub});
56 20 100       125 push @subs, $sub if $packagename eq $self->module;
57             }
58              
59 2         5 my %subs;
60 2         10 @subs{@subs} = undef;
61              
62 2         20 $self->subs( \%subs );
63             }
64              
65              
66             sub unit_test {
67 11     11 1 1821 my ($self, $subroutine) = @_;
68              
69 11         28 my $test = '';
70              
71 11 100       33 if ( $subroutine eq 'new' ) {
    100          
72 2         11 $test = 'use_ok "' . $self->module . '";'
73             . "\n\n"
74             . 'my $obj = ' . $self->module . '->new;'
75             . "\n"
76             . 'isa_ok $obj, "' . $self->module . '";';
77             }
78             elsif ( $self->is_oo ) {
79 7         15 $test = 'ok $obj->can("' . $subroutine . '"), "' . $subroutine . '";';
80             }
81             else {
82 2         7 $test = "ok $subroutine(), " . '"' . $subroutine . '";';
83             }
84              
85 11         18 return $test;
86             }
87              
88              
89             sub create_test {
90 1     1 1 549 my ($self) = @_;
91              
92 1 50       97 open my $fh, '>', $self->name or die "Can't write " . $self->name . ": $!";
93              
94 1         4 my $text =<<'END';
95             use strict;
96             use warnings;
97              
98             use Test::More;
99              
100             END
101              
102             # Set the subs attribute
103 1         4 $self->gather_subs;
104              
105 1 50       5 if ( exists $self->subs->{new} ) {
106 1         3 $self->is_oo(1);
107              
108 1         2 my $test = $self->unit_test('new');
109              
110 1         4 $text .= "$test\n\n";
111             }
112              
113 1         8 for my $sub ( sort keys %{ $self->subs } ) {
  1         9  
114 9 100       21 next if $sub =~ /^_/;
115 8 100       18 next if $sub eq 'new';
116              
117 7         9 my $test = $self->unit_test($sub);
118              
119 7         17 $text .= "$test\n\n";
120             }
121              
122 1         3 $text .= 'done_testing();
123             ';
124              
125 1         63 print $fh $text;
126             }
127              
128             1;
129              
130             __END__