line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::TestCoverage; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT - Test if your test covers all "public" subroutines of the package |
4
|
|
|
|
|
|
|
|
5
|
4
|
|
|
4
|
|
107091
|
use strict; |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
134
|
|
6
|
4
|
|
|
4
|
|
20
|
use warnings; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
111
|
|
7
|
4
|
|
|
4
|
|
20
|
use Devel::Symdump; |
|
4
|
|
|
|
|
11
|
|
|
4
|
|
|
|
|
96
|
|
8
|
4
|
|
|
4
|
|
20
|
use Test::Builder; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
85
|
|
9
|
4
|
|
|
4
|
|
18
|
use B; |
|
4
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
230
|
|
10
|
4
|
|
|
4
|
|
20
|
use base qw(Exporter); |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
1183
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our @EXPORT = qw( |
13
|
|
|
|
|
|
|
test_coverage |
14
|
|
|
|
|
|
|
ok_test_coverage |
15
|
|
|
|
|
|
|
all_test_coverage_ok |
16
|
|
|
|
|
|
|
reset_test_coverage |
17
|
|
|
|
|
|
|
reset_all_test_coverage |
18
|
|
|
|
|
|
|
test_coverage_except |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
our $VERSION = '0.11'; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my $self = {}; |
23
|
|
|
|
|
|
|
my $test = Test::Builder->new(); |
24
|
|
|
|
|
|
|
my $invokes = {}; |
25
|
|
|
|
|
|
|
my $last = ''; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub test_coverage { |
29
|
3
|
|
|
3
|
1
|
954
|
my ($package) = @_; |
30
|
3
|
50
|
|
|
|
17
|
return unless defined $package; |
31
|
3
|
|
|
|
|
8
|
$last = $package; |
32
|
3
|
|
|
|
|
14
|
_get_subroutines($package); |
33
|
|
|
|
|
|
|
|
34
|
3
|
|
|
|
|
8
|
$invokes->{$package} = {}; |
35
|
|
|
|
|
|
|
|
36
|
3
|
50
|
|
|
|
15
|
my $moosified = $INC{"Moose.pm"} ? 1 : 0; |
37
|
|
|
|
|
|
|
|
38
|
3
|
|
|
|
|
6
|
for my $subref(@{$self->{subs}->{$package}}){ |
|
3
|
|
|
|
|
10
|
|
39
|
6
|
|
|
|
|
12
|
my $sub = $subref->[0]; |
40
|
|
|
|
|
|
|
|
41
|
6
|
|
|
|
|
13
|
my $sub_with = $package . '::' . $sub; |
42
|
6
|
50
|
|
|
|
19
|
unless(exists $invokes->{$package}->{$sub}){ |
43
|
6
|
|
|
|
|
13
|
$invokes->{$package}->{$sub} = 0; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
4
|
|
|
4
|
|
23
|
no strict 'refs'; |
|
4
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
124
|
|
47
|
4
|
|
|
4
|
|
30
|
no warnings 'redefine'; |
|
4
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
4036
|
|
48
|
|
|
|
|
|
|
|
49
|
6
|
|
|
|
|
42
|
my $old = $package->can( $sub ); |
50
|
|
|
|
|
|
|
|
51
|
6
|
50
|
|
|
|
16
|
if ( !$moosified ) { |
52
|
6
|
|
|
|
|
28
|
*{ $package . '::' . $sub } = sub { |
53
|
4
|
|
|
4
|
|
900
|
$invokes->{$package}->{$sub}++; |
54
|
4
|
|
|
|
|
12
|
$old->( @_ ); |
55
|
6
|
|
|
|
|
26
|
}; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
else { |
58
|
0
|
|
|
|
|
0
|
require Class::MOP; |
59
|
0
|
0
|
|
|
|
0
|
my $meta |
60
|
|
|
|
|
|
|
= $package->can('add_before_method_modifier') |
61
|
|
|
|
|
|
|
? $package |
62
|
|
|
|
|
|
|
: Class::MOP::class_of( $package ); |
63
|
|
|
|
|
|
|
$meta->add_after_method_modifier( $sub, sub { |
64
|
0
|
|
|
0
|
|
0
|
$invokes->{$package}->{$sub}++; |
65
|
0
|
|
|
|
|
0
|
} ); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
3
|
|
|
|
|
11
|
1; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub test_coverage_except { |
73
|
2
|
|
|
2
|
1
|
14
|
my ($package,@subroutines) = @_; |
74
|
|
|
|
|
|
|
|
75
|
2
|
|
|
|
|
5
|
for my $subname(@subroutines){ |
76
|
2
|
50
|
33
|
|
|
24
|
if(exists $invokes->{$package} and |
|
|
|
33
|
|
|
|
|
77
|
|
|
|
|
|
|
exists $invokes->{$package}->{$subname} and |
78
|
|
|
|
|
|
|
exists $self->{subs}->{$package}){ |
79
|
2
|
|
|
|
|
3
|
@{$self->{subs}->{$package}} = grep{$_->[0] ne $subname}@{$self->{subs}->{$package}}; |
|
2
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
11
|
|
|
2
|
|
|
|
|
6
|
|
80
|
2
|
|
|
|
|
10
|
delete $invokes->{$package}->{$subname}; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub all_test_coverage_ok { |
86
|
1
|
|
|
1
|
1
|
5
|
my ($msg) = @_; |
87
|
|
|
|
|
|
|
|
88
|
1
|
|
|
|
|
4
|
for my $package(keys %$invokes){ |
89
|
1
|
|
|
|
|
2
|
ok_test_coverage($package,$msg); |
90
|
|
|
|
|
|
|
} |
91
|
1
|
|
|
|
|
86
|
1; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub ok_test_coverage { |
95
|
6
|
|
|
6
|
1
|
24
|
my ($package,$msg) = @_; |
96
|
|
|
|
|
|
|
|
97
|
6
|
50
|
66
|
|
|
38
|
if(!$package or (!exists $invokes->{$package}) |
|
|
|
66
|
|
|
|
|
98
|
|
|
|
|
|
|
and $package !~ /^(?:\w+(?:::)?)+$/){ |
99
|
2
|
|
|
|
|
3
|
$package = $last; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
6
|
50
|
|
|
|
16
|
unless(exists $invokes->{$package}){ |
103
|
0
|
|
|
|
|
0
|
warn $package.' was not tested'; |
104
|
0
|
|
|
|
|
0
|
return; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
6
|
100
|
|
|
|
14
|
my $bool_msg = defined $msg ? 1 : 0; |
108
|
6
|
|
|
|
|
8
|
my $title = 'Test test-coverage '; |
109
|
6
|
|
|
|
|
7
|
my $missing; |
110
|
|
|
|
|
|
|
|
111
|
6
|
|
|
|
|
9
|
my $bool_coverage = 1; |
112
|
6
|
|
|
|
|
7
|
for my $sub(map{$_->[0]}@{$self->{subs}->{$package}}){ |
|
10
|
|
|
|
|
23
|
|
|
6
|
|
|
|
|
17
|
|
113
|
10
|
50
|
33
|
|
|
62
|
if(!exists $invokes->{$package}->{$sub} or $invokes->{$package}->{$sub} == 0){ |
114
|
0
|
0
|
0
|
|
|
0
|
$missing = defined $missing && !$bool_msg ? $missing . $sub . ' ' : $sub . ' '; |
115
|
0
|
|
|
|
|
0
|
$bool_coverage = 0; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
6
|
100
|
|
|
|
18
|
if(!$bool_msg){ |
120
|
5
|
|
|
|
|
6
|
$msg = $title; |
121
|
5
|
50
|
|
|
|
11
|
$msg .= $missing.' are missing' if(defined $missing); |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
6
|
|
|
|
|
24
|
$test->cmp_ok($bool_coverage,"==",1,$msg); |
125
|
6
|
|
|
|
|
2766
|
1; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub reset_test_coverage{ |
129
|
0
|
|
|
0
|
1
|
0
|
my ($self,$pkg) = @_; |
130
|
0
|
|
|
|
|
0
|
for my $key(keys %{$invokes->{$pkg}}){ |
|
0
|
|
|
|
|
0
|
|
131
|
0
|
|
|
|
|
0
|
$invokes->{$pkg}->{$key} = 0; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub reset_all_test_coverage{ |
136
|
0
|
|
|
0
|
1
|
0
|
my ($self) = @_; |
137
|
0
|
|
|
|
|
0
|
for my $pkg(keys %{$invokes}){ |
|
0
|
|
|
|
|
0
|
|
138
|
0
|
|
|
|
|
0
|
$self->reset_test_coverage($pkg); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub _get_subroutines{ |
143
|
3
|
|
|
3
|
|
7
|
my ($pkg,$test) = @_; |
144
|
|
|
|
|
|
|
|
145
|
3
|
|
|
|
|
264
|
eval qq{ require $pkg }; |
146
|
3
|
50
|
|
|
|
391
|
print STDERR $@ if $@; |
147
|
3
|
50
|
|
|
|
20
|
return if $@; |
148
|
|
|
|
|
|
|
|
149
|
3
|
|
33
|
|
|
21
|
$test ||= $pkg; |
150
|
|
|
|
|
|
|
|
151
|
3
|
|
|
|
|
270
|
my $symdump = Devel::Symdump->new($pkg); |
152
|
|
|
|
|
|
|
|
153
|
3
|
|
|
|
|
7
|
my @symbols; |
154
|
3
|
|
|
|
|
119
|
for my $func ($symdump->functions ) { |
155
|
6
|
|
|
|
|
11
|
my $owner = _get_sub(\&{$func}); |
|
6
|
|
|
|
|
30
|
|
156
|
6
|
|
|
|
|
79
|
$owner =~ s/^\*(.*)::.*?$/$1/; |
157
|
6
|
50
|
|
|
|
24
|
next if $owner ne $test; |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
# check if it's on the whitelist |
160
|
6
|
|
|
|
|
70
|
$func =~ s/${pkg}:://; |
161
|
|
|
|
|
|
|
|
162
|
6
|
50
|
|
|
|
1172
|
push @symbols, [$func,$owner] unless $func =~ /^_/; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
3
|
|
|
|
|
567
|
$self->{subs}->{$pkg} = \@symbols; |
166
|
|
|
|
|
|
|
|
167
|
3
|
|
|
|
|
69
|
1; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
sub _get_sub { |
171
|
6
|
|
|
6
|
|
11
|
my ($svref) = @_; |
172
|
6
|
|
|
|
|
34
|
my $b_cv = B::svref_2object($svref); |
173
|
4
|
|
|
4
|
|
25
|
no strict 'refs'; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
372
|
|
174
|
6
|
|
|
|
|
10
|
return *{ $b_cv->GV->STASH->NAME . "::" . $b_cv->GV->NAME }; |
|
6
|
|
|
|
|
114
|
|
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
1; |
178
|
|
|
|
|
|
|
__END__ |