Class/AutoTest.pm

filter: Statementstmt 95.9% 95 / 99 Branchbran 50.0% 16 / 32 Conditioncond 22.2% 4 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 100% 18 / 18 Podpod 100% 1 / 1 totaltotal 74.4% 134 / 180 CC 25 SCAR 35.3
CC
25
Cov
77%
import
70.0
_find_package_filename
4.0
_find_test_modules
2.0
CRAP 34.2
↑ Summary Class.pm »
1 #!/usr/bin/perl
2 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 {
5  
6 package OOP::Perlish::Class::AutoTest;
7 6 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 ############################################################################################
10 ## Simple, somewhat hacky way of running unit-tests in batch to make testing framework
11 ## easier to manage.
12 ############################################################################################
13  
14 6 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶ use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶ use File::Basename qw(dirname fileparse basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶ use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶ use Test::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 ############################################################################################
21 ## Provide an import method to handle testing parameters.
22 ############################################################################################
23 sub import
24 5 ▶ {
Subroutine import: called CC=20 SCAR=42.5
25 5 my ( $self, @tags ) = @_;
26 5 ▶ my %test_opts = @tags if( scalar @tags % 2 == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @tags % 2 == 0 5 0 5
27 5 my @packages;
28 my @test_directories;
29 5 my @exclude;
30  
31 5 ▶ return unless( scalar keys %test_opts );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %test_opts 0 5 5
32  
33 5 ▶ if( exists $test_opts{package} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $test_opts{"package"}) 5 0 5
34 5 ▶ if( $test_opts{package} && ref( $test_opts{package} ) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($test_opts{'package'} and ref $test_opts{'package'} eq 'ARRAY') { } 0 5 5
elsif ($test_opts{'package'} and not ref $test_opts{'package'}) { } 5 0 5
Condition33%1/3
$test_opts{"package"} and ref $test_opts{"package"} eq "ARRAY"
!ll&&!rl&&r
050
MC/DC0%0/2
$test_opts{"package"} and ref $test_opts{"package"} eq "ARRAY"
$test_opts{"package"} ref $test_opts{"package"} eq "ARRAY"
Truth table33%1/3
A and B
A= $test_opts{"package"}B= ref $test_opts{"package"} eq "ARRAY"
ABresult
0X0
100
111
35 0 @packages = @{ $test_opts{package} };
36 }
37 ▶ elsif( $test_opts{package} && !ref( $test_opts{package} ) ) {
Condition33%1/3
$test_opts{"package"} and not ref $test_opts{"package"}
!ll&&!rl&&r
005
MC/DC0%0/2
$test_opts{"package"} and not ref $test_opts{"package"}
$test_opts{"package"} not ref $test_opts{"package"}
Truth table33%1/3
A and not(B)
A= $test_opts{"package"}B= not ref $test_opts{"package"}
ABresult
0X0
100
111
38 5 @packages = ( $test_opts{package} );
39 }
40 }
41 5 ▶ if( exists $test_opts{tests} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $test_opts{"tests"}) 5 0 5
42 5 ▶ if( $test_opts{tests} && ref( $test_opts{tests} ) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($test_opts{'tests'} and ref $test_opts{'tests'} eq 'ARRAY') { } 5 0 5
elsif ($test_opts{'tests'} and not ref $test_opts{'tests'}) { } 0 0 0
Condition33%1/3
$test_opts{"tests"} and ref $test_opts{"tests"} eq "ARRAY"
!ll&&!rl&&r
005
MC/DC0%0/2
$test_opts{"tests"} and ref $test_opts{"tests"} eq "ARRAY"
$test_opts{"tests"} ref $test_opts{"tests"} eq "ARRAY"
Truth table33%1/3
A and B
A= $test_opts{"tests"}B= ref $test_opts{"tests"} eq "ARRAY"
ABresult
0X0
100
111
43 5 @test_directories = @{ $test_opts{tests} };
44 }
45 ▶ elsif( $test_opts{tests} && !ref( $test_opts{tests} ) ) {
Condition0%0/3
$test_opts{"tests"} and not ref $test_opts{"tests"}
!ll&&!rl&&r
000
MC/DC0%0/2
$test_opts{"tests"} and not ref $test_opts{"tests"}
$test_opts{"tests"} not ref $test_opts{"tests"}
Truth table0%0/3
A and not(B)
A= $test_opts{"tests"}B= not ref $test_opts{"tests"}
ABresult
0X0
100
111
46 0 @test_directories = ( $test_opts{tests} );
47 }
48 }
49 5 ▶ if( exists $test_opts{exclude} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $test_opts{"exclude"}) 5 0 5
50 5 ▶ if( $test_opts{exclude} && ref( $test_opts{exclude} ) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($test_opts{'exclude'} and ref $test_opts{'exclude'} eq 'ARRAY') { } 5 0 5
elsif ($test_opts{'exclude'} and not ref $test_opts{'exclude'}) { } 0 0 0
Condition33%1/3
$test_opts{"exclude"} and ref $test_opts{"exclude"} eq "ARRAY"
!ll&&!rl&&r
005
MC/DC0%0/2
$test_opts{"exclude"} and ref $test_opts{"exclude"} eq "ARRAY"
$test_opts{"exclude"} ref $test_opts{"exclude"} eq "ARRAY"
Truth table33%1/3
A and B
A= $test_opts{"exclude"}B= ref $test_opts{"exclude"} eq "ARRAY"
ABresult
0X0
100
111
51 5 @exclude = @{ $test_opts{exclude} };
52 }
53 ▶ elsif( $test_opts{exclude} && !ref( $test_opts{exclude} ) ) {
Condition0%0/3
$test_opts{"exclude"} and not ref $test_opts{"exclude"}
!ll&&!rl&&r
000
MC/DC0%0/2
$test_opts{"exclude"} and not ref $test_opts{"exclude"}
$test_opts{"exclude"} not ref $test_opts{"exclude"}
Truth table0%0/3
A and not(B)
A= $test_opts{"exclude"}B= not ref $test_opts{"exclude"}
ABresult
0X0
100
111
54 0 @exclude = ( $test_opts{exclude} );
55 }
56 }
57  
58 5 for my $package ( $self->_find_test_modules( \@packages, \@test_directories, \@exclude ) ) {
59 18 ▶ eval "require $package" || die "$@";
Branch: 1 branch
BranchTrueFalseTotal
eval "require $package" or die "$@" 18 0 18
60 }
61 }
62  
63 ############################################################################################
64 ## Divine information from whence a subroutine is loaded
65 ############################################################################################
66 sub _introspect_sub
67 5 ▶ {
Subroutine _introspect_sub: called CC=1 SCAR=0.0
68 5 my ( $self, $sub ) = @_;
69 5 my $cv = B::svref_2object($sub);
70  
71 5 return ( ( $cv->STASH()->NAME(), $cv->FILE() ) );
72 }
73  
74 ############################################################################################
75 ## using introspect_sub, find any subroutine, introspect it to learn which package_name and
76 ## file_name it comes from.
77 ############################################################################################
78 sub _find_package_filename
79 5 ▶ {
Subroutine _find_package_filename: called CC=4 SCAR=13.9
80 5 my ( $self, $package ) = @_;
81  
82 5 my ( $package_name, $file_name );
83  
84 5 eval "require $package";
85  
86 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
87 5 my %sym = %{ *{ '::' . $package . '::' } };
88 6 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
89  
90 5 ▶ while( my ( $k, $v ) = each %sym ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %sym) 6 0 6
91 6 ▶ if( defined *{$v}{CODE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined *{$v;}{"CODE"}) 5 1 6
92 5 ( $package_name, $file_name ) = $self->_introspect_sub( *{$v}{CODE} );
93 5 ▶ last if( $package_name eq $package );
Branch: 1 branch
BranchTrueFalseTotal
if $package_name eq $package 5 0 5
94 }
95 }
96 5 return $file_name;
97 }
98  
99 ############################################################################################
100 ## Search the path of a module for subdirectories matching paths in 'tests => []', yet not
101 ## matching $exclude_re
102 ############################################################################################
103 sub _find_test_modules
104 5 ▶ {
Subroutine _find_test_modules: called CC=2 SCAR=6.9
105 5 my ( $self, $packages, $test_directories, $exclude ) = @_;
106 5 my @packages = @{$packages};
107 5 my @test_directories = @{$test_directories};
108 5 my @exclude = @{$exclude};
109  
110 5 my @test_packages;
111  
112 5 for my $package (@packages) {
113 5 my $package_path = $self->_find_package_filename($package);
114 5 my $search_path = join( '/', ( fileparse( $package_path, qr/\.pm/ ) )[ 1, 0 ] );
115  
116 File::Find::find(
117 {
118 follow => 0,
119 no_chdir => 1,
120 bydepth => 1,
121 untaint => 1,
122 untaint_exclude => 1,
123 dangling_symlinks => undef,
124 5 ▶ preprocess => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
125 5 my $exclude_re = '(?:' . join( '|', @exclude ) . ')';
126 5 return grep { !/$exclude_re/ } @_;
127 },
128 23 ▶ wanted => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
129 23 ▶ m/\.pm$/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/\.pm$/ and do { (my $module = $_) =~ s/^\Q$search_path\E//; $module =~ s[/+][/]g; $module =~ s[/][::]g; $module =~ s/\.pm$//; $module = $package . $module; push @test_packages, $module } 18 5 23
130 18 ( my $module = $_ ) =~ s/^\Q$search_path\E//;
131 18 $module =~ s,/+,/,g;
132 18 $module =~ s,/,::,g;
133 18 $module =~ s/\.pm$//;
134 18 $module = $package . $module;
135 18 push @test_packages, $module;
136 };
137 },
138 },
139 5 map { $search_path . '/' . $_ } @test_directories,
140 );
141 }
142 5 return @test_packages;
143 } ## end sub _find_test_modules
144  
145 ############################################################################################
146 ## convenience function to invoke Test::Class->runtests() indirectly
147 ############################################################################################
148 sub runtests
149 5 ▶ {
Subroutine runtests: called CC=1 SCAR=0.0
Pod documented
150 5 my ($self) = @_;
151 5 Test::Class->runtests();
152 }
153 }
154 6 1;
155 __END__
↑ Summary Class.pm »