| 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
| |||||||||||||||||||||||||||||||||
| 27 | 5 | my @packages; | |||||||||||||||||||||||||||||||
| 28 | my @test_directories; | ||||||||||||||||||||||||||||||||
| 29 | 5 | my @exclude; | |||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | 5 | ▶ | return unless( scalar keys %test_opts ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | 5 | ▶ | if( exists $test_opts{package} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 34 | 5 | ▶ | if( $test_opts{package} && ref( $test_opts{package} ) eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
$test_opts{"package"} and ref $test_opts{"package"} eq "ARRAY"
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"
| |||||||||||||||||||||||||||||||||
| 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"}
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"}
| |||||||||||||||||||||||||||||||||
| 38 | 5 | @packages = ( $test_opts{package} ); | |||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||
| 41 | 5 | ▶ | if( exists $test_opts{tests} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 42 | 5 | ▶ | if( $test_opts{tests} && ref( $test_opts{tests} ) eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
$test_opts{"tests"} and ref $test_opts{"tests"} eq "ARRAY"
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"
| |||||||||||||||||||||||||||||||||
| 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"}
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"}
| |||||||||||||||||||||||||||||||||
| 46 | 0 | @test_directories = ( $test_opts{tests} ); | |||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||
| 49 | 5 | ▶ | if( exists $test_opts{exclude} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 50 | 5 | ▶ | if( $test_opts{exclude} && ref( $test_opts{exclude} ) eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
$test_opts{"exclude"} and ref $test_opts{"exclude"} eq "ARRAY"
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"
| |||||||||||||||||||||||||||||||||
| 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"}
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"}
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 91 | 6 | ▶ | if( defined *{$v}{CODE} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 92 | 5 | ( $package_name, $file_name ) = $self->_introspect_sub( *{$v}{CODE} ); | |||||||||||||||||||||||||||||||
| 93 | 5 | ▶ | last if( $package_name eq $package ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||