line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mock::Data::GeneratorSub; |
2
|
12
|
|
|
12
|
|
83
|
use strict; |
|
12
|
|
|
|
|
23
|
|
|
12
|
|
|
|
|
379
|
|
3
|
12
|
|
|
12
|
|
61
|
use warnings; |
|
12
|
|
|
|
|
37
|
|
|
12
|
|
|
|
|
5546
|
|
4
|
|
|
|
|
|
|
require Mock::Data::Generator; |
5
|
|
|
|
|
|
|
our @ISA= qw( Mock::Data::Generator ); |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
# ABSTRACT: Wrap a coderef to become a blessed Generator object |
8
|
|
|
|
|
|
|
our $VERSION = '0.03'; # VERSION |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub new { |
12
|
72
|
|
|
72
|
1
|
144
|
my ($class, $coderef, @params)= @_; |
13
|
72
|
50
|
|
|
|
146
|
if (ref $coderef eq 'HASH') { |
14
|
0
|
0
|
|
|
|
0
|
@params= @{ $coderef->{params} || [] }; |
|
0
|
|
|
|
|
0
|
|
15
|
0
|
|
|
|
|
0
|
$coderef= $coderef->{coderef}; |
16
|
|
|
|
|
|
|
} |
17
|
72
|
50
|
|
|
|
127
|
if (ref $class) { |
18
|
0
|
|
0
|
|
|
0
|
$coderef ||= $class->{coderef}; |
19
|
0
|
|
|
|
|
0
|
@params= $class->_merge_params(@params); |
20
|
0
|
|
|
|
|
0
|
$class= ref $class; |
21
|
|
|
|
|
|
|
} |
22
|
72
|
50
|
|
|
|
205
|
Scalar::Util::reftype($coderef) eq 'CODE' or Carp::croak("Not a coderef"); |
23
|
72
|
|
|
|
|
270
|
bless { |
24
|
|
|
|
|
|
|
coderef => $coderef, |
25
|
|
|
|
|
|
|
params => \@params, |
26
|
|
|
|
|
|
|
}, $class; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub _merge_params { |
30
|
190
|
|
|
190
|
|
265
|
my $self= shift; |
31
|
190
|
|
|
|
|
293
|
my $p= $self->{params}; |
32
|
190
|
50
|
|
|
|
425
|
my $named_p= ref $p->[0] eq 'HASH'? $p->[0] : undef; |
33
|
|
|
|
|
|
|
# Merge any options-by-name newly supplied with options-by-name from @params |
34
|
190
|
0
|
|
|
|
346
|
unshift @_, (ref $_[0] eq 'HASH')? { %$named_p, %{shift @_} } : $named_p |
|
0
|
50
|
|
|
|
0
|
|
35
|
|
|
|
|
|
|
if $named_p; |
36
|
|
|
|
|
|
|
# Append positional params if none provided |
37
|
190
|
100
|
|
|
|
512
|
push @_, @{$p}[1..$#$p] |
|
189
|
|
|
|
|
341
|
|
38
|
|
|
|
|
|
|
unless @_ > 1; |
39
|
190
|
|
|
|
|
694
|
return @_; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub generate { |
44
|
191
|
|
|
191
|
1
|
359
|
my ($self, $mock)= (shift, shift); |
45
|
191
|
100
|
|
|
|
489
|
$self->{coderef}->($mock, @_? $self->_merge_params(@_) : @{$self->{params}}); |
|
1
|
|
|
|
|
22
|
|
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub compile { |
50
|
38
|
|
|
38
|
1
|
79
|
my $self= shift; |
51
|
38
|
|
|
|
|
80
|
my $params= $self->{params}; |
52
|
38
|
50
|
33
|
|
|
316
|
return $self->{coderef} unless @_ || @$params; |
53
|
0
|
|
|
|
|
|
my @new_params= $self->_merge_params(@_); |
54
|
0
|
|
|
|
|
|
my $coderef= $self->{coderef}; |
55
|
0
|
|
|
0
|
|
|
return sub { $coderef->(shift, @new_params) }; |
|
0
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
1; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
__END__ |