| 1 | package Test::StubGenerator; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 16 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 16 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 16 | ▶ | use PPI 1.118; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 16 | ▶ | use Perl::Tidy; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 16 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 16 | ▶ | use English qw( -no_match_vars ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 16 | ▶ | use version; our $VERSION = qv('0.9.6'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 16 | my %DEFAULT_OPTIONS = ( file => undef, | |||||||||||||||||||||||||||
| 14 | source => undef, | ||||||||||||||||||||||||||||
| 15 | output => undef, | ||||||||||||||||||||||||||||
| 16 | out_dir => undef, | ||||||||||||||||||||||||||||
| 17 | tidy => 1, | ||||||||||||||||||||||||||||
| 18 | pertidyrc => '~/.perltidyrc', ); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 17 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | 17 | my( $class, $arg_ref ) = @_; | |||||||||||||||||||||||||||
| 22 | 17 | my $self = {}; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # Collect the options passed in, defaulting to the, uh ... defaults. | ||||||||||||||||||||||||||||
| 25 | my %option_args = | ||||||||||||||||||||||||||||
| 26 | ref $arg_ref eq 'HASH' | ||||||||||||||||||||||||||||
| 27 | 17 | ▶ | ? ( %DEFAULT_OPTIONS, %{$arg_ref} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | : %DEFAULT_OPTIONS; | ||||||||||||||||||||||||||||
| 29 | 17 | $self->{file} = $option_args{file}; | |||||||||||||||||||||||||||
| 30 | 17 | $self->{source} = $option_args{source}; | |||||||||||||||||||||||||||
| 31 | 17 | $self->{output} = $option_args{output}; | |||||||||||||||||||||||||||
| 32 | 17 | $self->{out_dir} = $option_args{out_dir}; | |||||||||||||||||||||||||||
| 33 | 17 | $self->{tidy} = $option_args{tidy}; | |||||||||||||||||||||||||||
| 34 | 17 | $self->{perltidyrc} = $option_args{perltidyrc}; | |||||||||||||||||||||||||||
| 35 | 17 | $self->{structure} = {}; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # Trim trailing slashes if present for easier interpolation later. | ||||||||||||||||||||||||||||
| 38 | 17 | ▶ | $self->{out_dir} =~ s{ / $ }{}xms if $self->{out_dir}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # One or the other of these need to be non-false. | ||||||||||||||||||||||||||||
| 41 | 17 | ▶ | my $code = $self->{file} || $self->{source}; | ||||||||||||||||||||||||||
|
Condition100%3/3
$self->{'file'} || $self->{'source'}
MC/DC100%2/2
$self->{'file'} || $self->{'source'}
$self->{'file'}
$self->{'source'}
Truth table100%3/3
A || B
A= $self->{'file'}B= $self->{'source'}
| |||||||||||||||||||||||||||||
| 42 | 17 | ▶ | $code or die "No code provided to Test::StubGenerator\n"; # :) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # Also, if we can't create a new PPI document from the file or | ||||||||||||||||||||||||||||
| 45 | # code passed in, we're in trouble. | ||||||||||||||||||||||||||||
| 46 | 15 | ▶ | $self->{doc} = PPI::Document->new( $code, readonly => 1, ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | or croak "Unable to initialize PPI document: $!"; | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 14 | return bless $self, $class; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # Find something in the PPI doc that we're looking for. | ||||||||||||||||||||||||||||
| 53 | 30 | ▶ | sub _find { | ||||||||||||||||||||||||||
|
Subroutine
_find: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 54 | 30 | my( $self, $sub_ref, $item_type ) = @_; | |||||||||||||||||||||||||||
| 55 | 30 | my $item_ref = $self->{doc}->find($sub_ref); | |||||||||||||||||||||||||||
| 56 | 30 | ▶ | if($item_ref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 25 | return $item_ref; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | else { | ||||||||||||||||||||||||||||
| 60 | 5 | carp "No $item_type found"; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 5 | return; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # In this case, we want things that are PPI::Subs and have names, but are | ||||||||||||||||||||||||||||
| 66 | # not 'Scheduled' - i.e. BEGIN, CHECK, INIT, END blocks. | ||||||||||||||||||||||||||||
| 67 | 15 | ▶ | sub _find_subs { | ||||||||||||||||||||||||||
|
Subroutine
_find_subs: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 68 | 15 | my $self = shift; | |||||||||||||||||||||||||||
| 69 | my $subs_ref = $self->_find( | ||||||||||||||||||||||||||||
| 70 | 4411 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 71 | 4411 | ▶ | $_[1]->isa('PPI::Statement::Sub') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$_[1]->isa("PPI::Statement::Sub") and not $_[1]->isa("PPI::Statement::Scheduled")
MC/DC100%2/2
$_[1]->isa("PPI::Statement::Sub") and not $_[1]->isa("PPI::Statement::Scheduled")
$_[1]->isa("PPI::Statement::Sub")
not $_[1]->isa("PPI::Statement::Scheduled")
Truth table100%3/3
A and not(B)
A= $_[1]->isa("PPI::Statement::Sub")B= not $_[1]->isa("PPI::Statement::Scheduled")
| |||||||||||||||||||||||||||||
| 72 | && ! $_[1]->isa('PPI::Statement::Scheduled') | ||||||||||||||||||||||||||||
| 73 | && $_[1]->name; | ||||||||||||||||||||||||||||
| 74 | }, | ||||||||||||||||||||||||||||
| 75 | 15 | 'subs', ); | |||||||||||||||||||||||||||
| 76 | 15 | for my $sub ( @{$subs_ref} ) { | |||||||||||||||||||||||||||
| 77 | 45 | $self->_process_sub($sub); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 15 | return; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # We're looking for package declarations. Not all code has a declared | ||||||||||||||||||||||||||||
| 83 | # package, and that may or may not be a problem. | ||||||||||||||||||||||||||||
| 84 | 15 | ▶ | sub _find_package { | ||||||||||||||||||||||||||
|
Subroutine
_find_package: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 85 | 15 | my $self = shift; | |||||||||||||||||||||||||||
| 86 | my $pkg_ref = $self->_find( | ||||||||||||||||||||||||||||
| 87 | 4411 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 88 | 4411 | ▶ | $_[1]->isa('PPI::Statement::Package') && $_[1]->namespace; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | }, | ||||||||||||||||||||||||||||
| 90 | 15 | 'packages', ); | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 15 | for my $pkg ( @{$pkg_ref} ) { | |||||||||||||||||||||||||||
| 93 | 11 | $self->{structure}->{package} = $pkg->namespace; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 15 | return; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # We've been passed a named, non-scheduled PPI::Statement::Sub. | ||||||||||||||||||||||||||||
| 99 | 45 | ▶ | sub _process_sub { | ||||||||||||||||||||||||||
|
Subroutine
_process_sub: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 100 | 45 | my( $self, $sub ) = @_; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # Let's examine the block defined for it. | ||||||||||||||||||||||||||||
| 103 | 45 | my $block = $sub->block; | |||||||||||||||||||||||||||
| 104 | 45 | my @variables; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # Keep track of all the variables passed into the subroutine. | ||||||||||||||||||||||||||||
| 107 | 45 | for my $statement ( $block->children ) { | |||||||||||||||||||||||||||
| 108 | 512 | ▶ | if( $statement->isa('PPI::Statement::Variable') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 60 | $self->_get_variables( $statement, \@variables ); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # Add the subroutine to the methods hash along with all associated variables | ||||||||||||||||||||||||||||
| 114 | # that we were able to find. | ||||||||||||||||||||||||||||
| 115 | 45 | $self->{structure}->{methods}->{ $sub->name } = [@variables]; | |||||||||||||||||||||||||||
| 116 | 45 | return; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 60 | ▶ | sub _get_variables { | ||||||||||||||||||||||||||
|
Subroutine
_get_variables: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 120 | 60 | my( $self, $statement, $vars_ref ) = @_; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # If any of the statements children contains "@_"... | ||||||||||||||||||||||||||||
| 123 | 60 | ▶ | if( scalar grep { $_->isa('PPI::Token::Magic') } $statement->children ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 19 | push @{$vars_ref}, # keep all variables unpacked from @_. | |||||||||||||||||||||||||||
| 125 | 19 | grep { $_ ne '$self' } $statement->variables; # other than $self | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # If any of the statements' children assigns using shift... | ||||||||||||||||||||||||||||
| 129 | 60 | ▶ | if( scalar grep { $_->content eq 'shift' } $statement->children ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 24 | push @{$vars_ref}, # keep all variables shifted to. | |||||||||||||||||||||||||||
| 131 | # other than $self, $class, or $package | ||||||||||||||||||||||||||||
| 132 | 24 | grep { $_ !~ /(?:\$self|\$class|\$package)/ } $statement->variables; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 60 | return; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 15 | ▶ | sub gen_testfile { | ||||||||||||||||||||||||||
|
Subroutine
gen_testfile: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 138 | 15 | my $self = shift; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # do a majority of the work here. | ||||||||||||||||||||||||||||
| 141 | 15 | $self->_find_package(); | |||||||||||||||||||||||||||
| 142 | 15 | $self->_find_subs(); | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # start the testfile text. | ||||||||||||||||||||||||||||
| 145 | 15 | my $test_file = _test_file_header(); | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 15 | my $package = $self->{structure}->{package}; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | # Add a little extra testing goodness if we're dealing with a package. | ||||||||||||||||||||||||||||
| 150 | 15 | $test_file .= $self->_generate_preamble($package); | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 15 | my $declarations = q(); | |||||||||||||||||||||||||||
| 153 | 15 | my $tests = q(); | |||||||||||||||||||||||||||
| 154 | 15 | my @vars; | |||||||||||||||||||||||||||
| 155 | 15 | for my $sub ( sort keys %{ $self->{structure}->{methods} } ) { | |||||||||||||||||||||||||||
| 156 | 34 | my $vars_ref = $self->{structure}->{methods}->{$sub}; | |||||||||||||||||||||||||||
| 157 | 34 | for my $var ( @{$vars_ref} ) { | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | # Add handy testing variable declarations to the test file... | ||||||||||||||||||||||||||||
| 160 | 22 | ▶ | if( ! scalar grep { $_ eq $var } @vars ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 20 | my $arg_decl; | |||||||||||||||||||||||||||
| 162 | 20 | ▶ | if( $var =~ /^\%/ ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 163 | 1 | $arg_decl = q{( '' => '', )}; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | elsif( $var =~ /^\@/ ) { | ||||||||||||||||||||||||||||
| 166 | 8 | $arg_decl = q{( '', )}; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | else { | ||||||||||||||||||||||||||||
| 169 | 11 | $arg_decl = q{''}; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | 20 | $declarations .= "my $var = " . sprintf "%s;\n", $arg_decl; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | # declare properly hash v. arr v. sclr | ||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | # ... assuming we haven't run across them already. | ||||||||||||||||||||||||||||
| 177 | 22 | push @vars, $var; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | # If we've got a package, precede all method calls with the object. | ||||||||||||||||||||||||||||
| 181 | 34 | ▶ | my $object_call = $package ? '$obj->' : q(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | { # A little easier to interpolate the array directly. | ||||||||||||||||||||||||||||
| 183 | 34 | local $LIST_SEPARATOR = ', '; | |||||||||||||||||||||||||||
| 184 | 17 | $tests .= "ok( $object_call$sub( @{ $vars_ref } ), " | |||||||||||||||||||||||||||
| 185 | . "'can call $object_call$sub()' );\n" | ||||||||||||||||||||||||||||
| 186 | 34 | ▶ | if @{$vars_ref}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | # add a test calling the subroutine without parameters. | ||||||||||||||||||||||||||||
| 190 | 34 | $tests .= "ok( $object_call$sub(), " | |||||||||||||||||||||||||||
| 191 | . "'can call $object_call$sub() without params' );\n"; | ||||||||||||||||||||||||||||
| 192 | 34 | $tests .= "\n"; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | # Put it all together. | ||||||||||||||||||||||||||||
| 196 | 15 | $test_file .= _assemble_tests( $package, $declarations, $tests ); | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | # Tidy the output if desired | ||||||||||||||||||||||||||||
| 199 | 15 | ▶ | if( $self->{tidy} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | perltidy( source => \$test_file, | ||||||||||||||||||||||||||||
| 201 | destination => \$test_file, | ||||||||||||||||||||||||||||
| 202 | 2 | perltidyrc => $self->{perltidyrc} ); | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | 15 | return $self->_handle_output($test_file); | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 17 | ▶ | sub _generate_preamble { | ||||||||||||||||||||||||||
|
Subroutine
_generate_preamble: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 208 | 17 | my( $self, $package ) = @_; | |||||||||||||||||||||||||||
| 209 | 17 | my $test_file = q(); | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 17 | ▶ | if($package) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | # Well packaged modules may not need the explicit 'use lib' statement. | ||||||||||||||||||||||||||||
| 214 | # But in the off chance that `make test` doesn't set -I, the tests | ||||||||||||||||||||||||||||
| 215 | # will still run. | ||||||||||||||||||||||||||||
| 216 | 11 | my $pkg_hierarchy = $package =~ m/::/g; | |||||||||||||||||||||||||||
| 217 | 11 | ▶ | if( $pkg_hierarchy > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | 2 | my $use_lib = join q(/), q(..) x $pkg_hierarchy; | |||||||||||||||||||||||||||
| 219 | 2 | $test_file .= "use lib '$use_lib';\n\n"; | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | # Add the BEGIN block to the tests. | ||||||||||||||||||||||||||||
| 223 | 11 | $test_file .= "BEGIN { use_ok( '$package' ); }\n\n"; | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | else { | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | # If it's not a package, chances are it should be required instead of used. | ||||||||||||||||||||||||||||
| 228 | $test_file .= "BEGIN { require_ok( '$self->{ file }' ); }\n\n" | ||||||||||||||||||||||||||||
| 229 | 6 | ▶ | if $self->{file}; # If it's not a file, we really can't require it. | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 17 | my $constructor_found = 0; | |||||||||||||||||||||||||||
| 233 | 17 | for my $constructor (qw{ new instance }) { | |||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | # If it's a package and has a constructor... | ||||||||||||||||||||||||||||
| 236 | 34 | ▶ | if( $package && defined $self->{structure}->{methods}->{$constructor} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$package and defined $self->{"structure"}{"methods"}{$constructor}
MC/DC100%2/2
$package and defined $self->{"structure"}{"methods"}{$constructor}
$package
defined $self->{"structure"}{"methods"}{$constructor}
Truth table100%3/3
A and B
A= $packageB= defined $self->{"structure"}{"methods"}{$constructor}
| |||||||||||||||||||||||||||||
| 237 | 11 | $constructor_found++; # controls whether or not we test the interface | |||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | # Add tests for it. | ||||||||||||||||||||||||||||
| 240 | 11 | $test_file .= 'ok( my $obj = ' . $package | |||||||||||||||||||||||||||
| 241 | . "->$constructor(), 'can create object $package' );\n"; | ||||||||||||||||||||||||||||
| 242 | 11 | $test_file .= "isa_ok( \$obj, '$package', 'object \$obj' );\n"; | |||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | # It seems that testing Test::StubGenerator->can( '$constructor' ); as an | ||||||||||||||||||||||||||||
| 245 | # element of its interface makes less sense since by this | ||||||||||||||||||||||||||||
| 246 | # point in the test file, we've aready used it. :) | ||||||||||||||||||||||||||||
| 247 | 11 | delete $self->{structure}->{methods}->{$constructor}; | |||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | # Add interface tests. | ||||||||||||||||||||||||||||
| 253 | 17 | ▶ | if($constructor_found) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 254 | 11 | my @methods = sort keys %{ $self->{structure}->{methods} }; | |||||||||||||||||||||||||||
| 255 | 11 | ▶ | if( scalar @methods ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | # A little easier to interpolate the array directly. | ||||||||||||||||||||||||||||
| 258 | 11 | local $LIST_SEPARATOR = q(', '); | |||||||||||||||||||||||||||
| 259 | 11 | $test_file .= "can_ok( \$obj, '@methods' );\n\n"; | |||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | 17 | return $test_file; | |||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | 15 | ▶ | sub _handle_output { | ||||||||||||||||||||||||||
|
Subroutine
_handle_output: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||
| 266 | 15 | my( $self, $test_file ) = @_; | |||||||||||||||||||||||||||
| 267 | 15 | ▶ | if( defined $self->{output} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | 5 | ▶ | if( ref $self->{output} eq 'GLOB' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | # We've got a filehandle - print to it. | ||||||||||||||||||||||||||||
| 271 | 1 | ▶ | print { $self->{output} } $test_file | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 272 | or croak "Can't write to file specified: $!"; | ||||||||||||||||||||||||||||
| 273 | 1 | return 1; | |||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | ▶ | elsif( defined $self->{out_dir} && -d $self->{out_dir} ) { | |||||||||||||||||||||||||||
|
Condition66%2/3
defined $self->{"out_dir"} and -d $self->{"out_dir"}
MC/DC50%1/2
defined $self->{"out_dir"} and -d $self->{"out_dir"}
defined $self->{"out_dir"}
-d $self->{"out_dir"}
Truth table66%2/3
A and B
A= defined $self->{"out_dir"}B= -d $self->{"out_dir"}
| |||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | # We've got an existent directory for output. | ||||||||||||||||||||||||||||
| 278 | 2 | return $self->_write_file($test_file) | |||||||||||||||||||||||||||
| 279 | or croak | ||||||||||||||||||||||||||||
| 280 | "Can't write the test file to the directory as specified: $!"; | ||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||
| 282 | else { | ||||||||||||||||||||||||||||
| 283 | croak sprintf q(Can't write to file '%s' in directory '%s/'.), | ||||||||||||||||||||||||||||
| 284 | 2 | $self->{output}, $self->{out_dir}; | |||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||
| 287 | else { | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | # Must be looking to have the text returned to them. | ||||||||||||||||||||||||||||
| 290 | 10 | return $test_file; | |||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||
| 292 | 0 | return; | |||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | 2 | ▶ | sub _write_file { | ||||||||||||||||||||||||||
|
Subroutine
_write_file: called CC=5 SCAR=16.4
| |||||||||||||||||||||||||||||
| 296 | 2 | my( $self, $test_file ) = @_; | |||||||||||||||||||||||||||
| 297 | my $filename = $self->{structure}->{package} | ||||||||||||||||||||||||||||
| 298 | ? $self->{structure}->{package} # give preference to found package name | ||||||||||||||||||||||||||||
| 299 | 2 | ▶ | : $self->{output}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | 2 | $filename =~ s{ | |||||||||||||||||||||||||||
| 301 | ^ # Start of string | ||||||||||||||||||||||||||||
| 302 | (?: # Don't capture this grouping | ||||||||||||||||||||||||||||
| 303 | [\w/]* # zero or more word or slash chars | ||||||||||||||||||||||||||||
| 304 | / # followed by a slash | ||||||||||||||||||||||||||||
| 305 | )? # end (optional) grouping | ||||||||||||||||||||||||||||
| 306 | (\w+) # one or more word characters | ||||||||||||||||||||||||||||
| 307 | (?: # Don't capture this grouping | ||||||||||||||||||||||||||||
| 308 | \.p[ml] # possibly with a pm or pl extension | ||||||||||||||||||||||||||||
| 309 | )? # end (optional) grouping | ||||||||||||||||||||||||||||
| 310 | $ # End of string; | ||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||
| 312 | {$1.t}x; # Give it a .t extension | ||||||||||||||||||||||||||||
| 313 | 2 | ▶ | open my $test_fh, '>', "$self->{out_dir}/$filename" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 314 | or croak "Can't open file for writing: $!"; | ||||||||||||||||||||||||||||
| 315 | 1 | ▶ | print {$test_fh} $test_file or croak "Can't write to file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 316 | 1 | ▶ | close $test_fh or carp "Can't close file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | 1 | return 1; | |||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | # A "theredoc", to keep it out of the other subroutines. | ||||||||||||||||||||||||||||
| 321 | 16 | ▶ | sub _assemble_tests { | ||||||||||||||||||||||||||
|
Subroutine
_assemble_tests: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 322 | 16 | my( $package, $declarations, $tests ) = @_; | |||||||||||||||||||||||||||
| 323 | 16 | my $assemblage; | |||||||||||||||||||||||||||
| 324 | 16 | ▶ | $assemblage = <<"ASSEMBLED_TESTS" if $package; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | # Create some variables with which to test the $package objects' methods | ||||||||||||||||||||||||||||
| 326 | # Note: give these some reasonable values. Then try unreasonable values :) | ||||||||||||||||||||||||||||
| 327 | ASSEMBLED_TESTS | ||||||||||||||||||||||||||||
| 328 | 16 | $assemblage .= <<"ASSEMBLED_TESTS"; | |||||||||||||||||||||||||||
| 329 | $declarations | ||||||||||||||||||||||||||||
| 330 | # And now to test the methods/subroutines. | ||||||||||||||||||||||||||||
| 331 | $tests | ||||||||||||||||||||||||||||
| 332 | ASSEMBLED_TESTS | ||||||||||||||||||||||||||||
| 333 | 16 | return $assemblage; | |||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | # A "theredoc", to keep it out of the other subroutines. | ||||||||||||||||||||||||||||
| 337 | 16 | ▶ | sub _test_file_header { | ||||||||||||||||||||||||||
|
Subroutine
_test_file_header: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 338 | 16 | return <<'TEST_FILE_HEADER'; | |||||||||||||||||||||||||||
| 339 | #!/usr/bin/perl | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | use strict; | ||||||||||||||||||||||||||||
| 342 | use warnings; | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | use Test::More qw/no_plan/; | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | TEST_FILE_HEADER | ||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | 16 | 1; | |||||||||||||||||||||||||||
| 350 | __END__ | ||||||||||||||||||||||||||||