| 1 | package Mock::Data::GeneratorSub; | ||||||||||||||||||||||||||||
| 2 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 12 | require Mock::Data::Generator; | |||||||||||||||||||||||||||
| 5 | 12 | our @ISA= qw( Mock::Data::Generator ); | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # ABSTRACT: Wrap a coderef to become a blessed Generator object | ||||||||||||||||||||||||||||
| 8 | 12 | our $VERSION = '0.04'; # VERSION | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 72 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=27.3
Pod
documented
| |||||||||||||||||||||||||||||
| 12 | 72 | my ($class, $coderef, @params)= @_; | |||||||||||||||||||||||||||
| 13 | 72 | ▶ | if (ref $coderef eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 14 | 0 | ▶ | @params= @{ $coderef->{params} || [] }; | ||||||||||||||||||||||||||
|
Condition0%0/2
$coderef->{'params'} || []
MC/DC0%0/1
$coderef->{'params'} || []
$coderef->{'params'}
Truth table0%0/2
A
A= $coderef->{'params'}
| |||||||||||||||||||||||||||||
| 15 | 0 | $coderef= $coderef->{coderef}; | |||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | 72 | ▶ | if (ref $class) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | 0 | ▶ | $coderef ||= $class->{coderef}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$coderef ||= $class->{"coderef"}
MC/DC0%0/2
$coderef ||= $class->{"coderef"}
$coderef
$class->{"coderef"}
Truth table0%0/3
A ||= B
A= $coderefB= $class->{"coderef"}
| |||||||||||||||||||||||||||||
| 19 | 0 | @params= $class->_merge_params(@params); | |||||||||||||||||||||||||||
| 20 | 0 | $class= ref $class; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | 72 | ▶ | Scalar::Util::reftype($coderef) eq 'CODE' or Carp::croak("Not a coderef"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 72 | bless { | |||||||||||||||||||||||||||
| 24 | coderef => $coderef, | ||||||||||||||||||||||||||||
| 25 | params => \@params, | ||||||||||||||||||||||||||||
| 26 | }, $class; | ||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 190 | ▶ | sub _merge_params { | ||||||||||||||||||||||||||
|
Subroutine
_merge_params: called CC=5 SCAR=17.5
| |||||||||||||||||||||||||||||
| 30 | 190 | my $self= shift; | |||||||||||||||||||||||||||
| 31 | 190 | my $p= $self->{params}; | |||||||||||||||||||||||||||
| 32 | 190 | ▶ | my $named_p= ref $p->[0] eq 'HASH'? $p->[0] : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | # Merge any options-by-name newly supplied with options-by-name from @params | ||||||||||||||||||||||||||||
| 34 | 0 | ▶ | unshift @_, (ref $_[0] eq 'HASH')? { %$named_p, %{shift @_} } : $named_p | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 35 | if $named_p; | ||||||||||||||||||||||||||||
| 36 | # Append positional params if none provided | ||||||||||||||||||||||||||||
| 37 | 190 | ▶ | push @_, @{$p}[1..$#$p] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | unless @_ > 1; | ||||||||||||||||||||||||||||
| 39 | 190 | return @_; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 191 | ▶ | sub generate { | ||||||||||||||||||||||||||
|
Subroutine
generate: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 191 | my ($self, $mock)= (shift, shift); | |||||||||||||||||||||||||||
| 45 | 191 | ▶ | $self->{coderef}->($mock, @_? $self->_merge_params(@_) : @{$self->{params}}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 38 | ▶ | sub compile { | ||||||||||||||||||||||||||
|
Subroutine
compile: called CC=3 SCAR=15.7
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 38 | my $self= shift; | |||||||||||||||||||||||||||
| 51 | 38 | my $params= $self->{params}; | |||||||||||||||||||||||||||
| 52 | 38 | ▶ | return $self->{coderef} unless @_ || @$params; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ or @$params
MC/DC0%0/2
@_ or @$params
@_
@$params
Truth table33%1/3
A or B
A= @_B= @$params
| |||||||||||||||||||||||||||||
| 53 | 0 | my @new_params= $self->_merge_params(@_); | |||||||||||||||||||||||||||
| 54 | 0 | my $coderef= $self->{coderef}; | |||||||||||||||||||||||||||
| 55 | 0 | ▶ | return sub { $coderef->(shift, @new_params) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 12 | 1; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | __END__ | ||||||||||||||||||||||||||||