Data.pm

filter: Statementstmt 73.1% 68 / 93 Branchbran 61.1% 33 / 54 Conditioncond 38.8% 7 / 18 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 75.0% 12 / 16 Podpod 100% 9 / 9 totaltotal 64.3% 130 / 202 CC 37 SCAR 20.8
CC
37
Cov
65%
wrap
20.0
clone
12.0
new
11.0
CRAP 8.0
« Data/Plugin/Number.pm ↑ Summary Data/Template.pm »
1 package Mock::Data;
2  
3 # ABSTRACT: Extensible toolkit for generating mock data
4 9 our $VERSION = '0.04'; # VERSION
5  
6  
7 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 # require MRO::Compat if "$]" < '5.009005'; # now requiring v5.10 for dist, anyway
11 9 require mro;
12 9 mro::set_mro(__PACKAGE__, 'c3');
13 }
14 9 require Storable;
15 9 require Module::Runtime;
16  
17  
18 17 ▶sub new {
Subroutine new: called CC=10 SCAR=24.0
Pod documented
19 17 my $class= shift;
20 17 ▶ my $self= ref $class? $class->clone
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 0 17 17
21 : bless {
22 generators => {}, # can't initialize, plugins go first
23 generator_state => {},
24 _generator_cache => {},
25 _loaded_plugins => {},
26 }, $class;
27 17 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 13 4 17
28 13 ▶ my $args
Branch: 2 branches
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'ARRAY' ? : 9 4 13
@_ == 1 && ref $_[0] eq 'HASH' ? : 0 4 4
Condition50%3/6
@_ == 1 and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
409
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
400
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "ARRAY"
@_ == 1 ref $_[0] eq "ARRAY"
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
29 = (@_ == 1 && ref $_[0] eq 'ARRAY')? { plugins => $_[0] }
30 : (@_ == 1 && ref $_[0] eq 'HASH')? $_[0]
31 : { @_ };
32 13 ▶ if (my $plugins= $args->{plugins}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $plugins = $args->{"plugins"}) 11 2 13
33 11 ▶ $self= $self->load_plugin(ref $plugins? @$plugins : ( $plugins ));
Branch: 1 branch
BranchTrueFalseTotal
ref $plugins ? : 11 0 11
34 }
35 $self->add_generators($args->{generators})
36 13 ▶ if $args->{generators};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"generators"} 3 10 13
37 }
38 17 return $self;
39 }
40  
41  
42 0 ▶sub clone {
Subroutine clone: not called CC=3 SCAR=24.8
Pod documented
43 0 my $self= shift;
44 my $new= {
45 %$self,
46 # Shallow clone generators and _loaded_plugins
47 0 _loaded_plugins => { %{ $self->{_loaded_plugins} } },
48 0 generators => { %{ $self->{generators} } },
49 # deep clone generator_state
50 0 generator_state => Storable::dclone($self->{generator_state}),
51 # clear cache
52 _generator_cache => {},
53 };
54 # Allow generators to handle cloned state
55 0 for (values %{ $new->{generators} }) {
56 0 ▶ $_= $_->clone if ref->can('clone');
Branch: 1 branch
BranchTrueFalseTotal
if ref($_)->can("clone") 0 0 0
57 }
58 0 bless $new, ref $self;
59 }
60  
61  
62 209 ▶sub generators {
Subroutine generators: called CC=3 SCAR=19.8
Pod documented
63 209 ▶ return $_[0]{generators} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 209 0 209
64 # Coerce generators
65 0 my %new= %{ $_[1] };
66 0 $_= Mock::Data::Util::coerce_generator($_) for values %new;
67 # clear cache first
68 0 %{$_[0]{_generator_cache}}= ();
69 0 return $_[0]{generators}= \%new;
70 }
71  
72 35 ▶sub generator_state {
Subroutine generator_state: called CC=2 SCAR=7.2
Pod documented
73 35 ▶ $_[0]{generator_state}= $_[1] if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 35 35
74 35 $_[0]{generator_state};
75 }
76  
77  
78 12 ▶sub load_plugin {
Subroutine load_plugin: called CC=7 SCAR=23.4
Pod documented
79 12 my ($self, @names)= @_;
80 12 for my $name (@names) {
81 14 ▶ next if $self->{_loaded_plugins}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_loaded_plugins"}{$name} 0 14 14
82 14 my $class= "Mock::Data::Plugin::$name";
83 14 ▶ unless ($class->can('apply_mockdata_plugin')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->can("apply_mockdata_plugin")) 0 14 14
84 0 Module::Runtime::require_module($class);
85 0 ▶ $class->can('apply_mockdata_plugin')
Branch: 1 branch
BranchTrueFalseTotal
$class->can("apply_mockdata_plugin") or Carp::croak("No such method $class->apply_mockdata_plugin") 0 0 0
86 or Carp::croak("No such method ${class}->apply_mockdata_plugin");
87 }
88 14 $self= $class->apply_mockdata_plugin($self);
89 14 ▶ ref($self) && ref($self)->isa(__PACKAGE__)
Branch: 1 branch
BranchTrueFalseTotal
ref $self and ref($self)->isa(__PACKAGE__) or Carp::croak("$class->apply_mockdata_plugin did not return a Mock::Data") 14 0 14
Condition33%1/3
ref $self and ref($self)->isa(__PACKAGE__)
!ll&&!rl&&r
0014
MC/DC0%0/2
ref $self and ref($self)->isa(__PACKAGE__)
ref $self ref($self)->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= ref $selfB= ref($self)->isa(__PACKAGE__)
ABresult
0X0
100
111
90 or Carp::croak("$class->apply_mockdata_plugin did not return a Mock::Data");
91 14 ++$self->{_loaded_plugins}{$name};
92 }
93 12 return $self;
94 }
95  
96  
97 13 ▶sub add_generators {
Subroutine add_generators: called CC=5 SCAR=16.2
Pod documented
98 13 my $self= shift;
99 13 ▶ my @args= @_ == 1? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 3 10 13
100 13 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 82 13 95
101 82 my ($name, $gen)= splice @args, 0, 2;
102 82 $gen= Mock::Data::Util::coerce_generator($gen);
103 82 $self->generators->{$name}= $gen;
104 82 delete $self->{_generator_cache}{$name};
105 82 ▶ $self->generators->{$1} //= $gen
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /::([^:]+)$/ 72 10 82
Condition33%1/3
$self->generators->{$1} //= $gen
l!l&&r!l&&!r
0720
MC/DC0%0/2
$self->generators->{$1} //= $gen
$self->generators->{$1} $gen
Truth table33%1/3
A //= B
A= $self->generators->{$1}B= $gen
ABresult
1X1
011
000
106 if $name =~ /::([^:]+)$/
107 }
108 13 $self;
109 }
110  
111 4 ▶sub combine_generators {
Subroutine combine_generators: called CC=6 SCAR=18.5
Pod documented
112 4 my $self= shift;
113 0 ▶ my @args= @_ == 1? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 4 4
114 4 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 4 4 8
115 4 my ($name, $gen)= splice @args, 0, 2;
116 4 $gen= Mock::Data::Util::coerce_generator($gen);
117 4 my $merged= $gen;
118 4 ▶ if (defined (my $cur= $self->generators->{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $cur = $self->generators->{$name})) 0 4 4
119 0 $merged= $cur->combine_generator($gen);
120 0 delete $self->{_generator_cache}{$name};
121 }
122 4 $self->generators->{$name}= $merged;
123
124 # If given a namespace-qualified name, also install as the 'leaf' of that name
125 4 ▶ if ($name =~ /::([^:]+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /::([^:]+)$/) 4 0 4
126 4 ($name, $merged)= ($1, $gen);
127 4 ▶ if (defined (my $cur= $self->generators->{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $cur = $self->generators->{$name})) 1 3 4
128 1 $merged= $cur->combine_generator($gen);
129 1 delete $self->{_generator_cache}{$name};
130 }
131 4 $self->generators->{$name}= $merged;
132 }
133 }
134 4 $self;
135 }
136  
137  
138 367 ▶sub call {
Subroutine call: called CC=4 SCAR=14.1
Pod documented
139 367 my ($self, $name)= (shift, shift);
140 367 ▶ defined $self->{generators}{$name}
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"generators"}{$name} or Carp::croak("No such generator '${name}'") 367 0 367
141 or Carp::croak("No such generator '$name'");
142 367 ▶ return $self->{generators}{$name}->generate($self, @_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 193 174 367
143 # If no params, call the cached compiled version
144 174 ▶ ($self->{_generator_cache}{$name} ||= $self->{generators}{$name}->compile)
Condition66%2/3
$self->{"_generator_cache"}{$name} ||= $self->{"generators"}{$name}->compile
l!l&&r!l&&!r
137370
MC/DC0%0/2
$self->{"_generator_cache"}{$name} ||= $self->{"generators"}{$name}->compile
$self->{"_generator_cache"}{$name} $self->{"generators"}{$name}->compile
Truth table66%2/3
A ||= B
A= $self->{"_generator_cache"}{$name}B= $self->{"generators"}{$name}->compile
ABresult
1X1
011
000
145 ->($self, @_);
146 }
147  
148  
149 0 ▶sub wrap {
Subroutine wrap: not called CC=4 SCAR=30.0
Pod documented
150 0 my ($self, $name)= (shift, shift);
151 0 my $gen= $self->{generators}{$name};
152 0 ▶ defined $gen or Carp::croak("No such generator '$name'");
Branch: 1 branch
BranchTrueFalseTotal
defined $gen or Carp::croak("No such generator '${name}'") 0 0 0
153 my $code= @_? $gen->compile(@_)
154 0 ▶ : ($self->{_generator_cache}{$name} ||= $gen->compile);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
Condition0%0/3
$self->{"_generator_cache"}{$name} ||= $gen->compile
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_generator_cache"}{$name} ||= $gen->compile
$self->{"_generator_cache"}{$name} $gen->compile
Truth table0%0/3
A ||= B
A= $self->{"_generator_cache"}{$name}B= $gen->compile
ABresult
1X1
011
000
155 0 ▶ return sub { $code->($self) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
156 0 }
157  
158 9 our $AUTOLOAD;
159 357 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=2 SCAR=7.0
160 357 my $self= shift;
161 357 ▶ Carp::croak("No method $AUTOLOAD in package $self") unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 357 357
162 357 my $name= substr($AUTOLOAD, rindex($AUTOLOAD,':')+1);
163 357 $self->call($name, @_);
164 # don't install, because generators are defined per-instance not per-package
165 }
166  
167 0 ▶sub DESTROY {} # prevent AUTOLOAD from triggering on ->DESTROY
Subroutine DESTROY: not called CC=1 SCAR=0.0
168  
169  
170 9 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
171 9 shift;
172 9 Mock::Data::Util->import_into(scalar caller, @_);
173 }
174  
175 9 require Mock::Data::Util;
176  
177 __END__
« Data/Plugin/Number.pm ↑ Summary Data/Template.pm »