Data/Util.pm

filter: Statementstmt 93.2% 69 / 74 Branchbran 83.3% 45 / 54 Conditioncond 77.7% 7 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 78.5% 11 / 14 Podpod 100% 7 / 7 totaltotal 85.9% 141 / 164 CC 33 SCAR 17.8
CC
33
Cov
88%
mock_data_subclass
9.1
coerce_generator
8.4
_dump
8.3
CRAP 5.9
« Data/GeneratorSub.pm ↑ Summary Data/Generator.pm »
1 package Mock::Data::Util;
2 12 ▶use Exporter::Extensible -exporter_setup => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 export(qw(
4 uniform_set weighted_set inflate_template coerce_generator mock_data_subclass
5 charset template _parse_context _escape_str _dump
6 ));
7 12 require Scalar::Util;
8 12 require Carp;
9 12 our @CARP_NOT= qw( Mock::Data Mock::Data::Generator );
10  
11 # ABSTRACT: Exportable functions to assist with declaring mock data
12 12 our $VERSION = '0.04'; # VERSION
13  
14  
15 0 ▶sub uniform_set {
Subroutine uniform_set: not called CC=1 SCAR=6.9
Pod documented
16 0 return Mock::Data::Set->new(items => [@_]);
17 }
18  
19 0 ▶sub weighted_set {
Subroutine weighted_set: not called CC=1 SCAR=6.9
Pod documented
20 0 my $i= 0;
21 0 return Mock::Data::Set->new_weighted(@_);
22 }
23  
24 20 ▶sub charset {
Subroutine charset: called CC=1 SCAR=0.0
Pod documented
25 20 return Mock::Data::Charset->new(@_);
26 }
27  
28  
29 0 ▶sub template {
Subroutine template: not called CC=1 SCAR=6.9
Pod documented
30 0 Mock::Data::Template->new(@_);
31 }
32  
33 31 ▶sub inflate_template {
Subroutine inflate_template: called CC=4 SCAR=13.9
Pod documented
34 31 my ($tpl)= @_;
35 # If it does not contain '{', return as-is. Else parse.
36 31 ▶ return $tpl if index($tpl, '{') == -1;
Branch: 1 branch
BranchTrueFalseTotal
if index($tpl, "{") == -1 13 18 31
37 18 local $@;
38 18 my $cmp= eval { Mock::Data::Template->new($tpl) };
39 # If the template "compiled" to a simple scalar, return the scalar. Else return the generator.
40 18 ▶ return !$cmp? $tpl : ref $cmp->{_compiled}? $cmp : $cmp->{_compiled};
Branch: 2 branches
BranchTrueFalseTotal
!$cmp ? : 2 16 18
ref $cmp->{'_compiled'} ? : 14 2 16
41 }
42  
43  
44 164 ▶sub coerce_generator {
Subroutine coerce_generator: called CC=8 SCAR=21.3
Pod documented
45 164 my ($spec)= @_;
46 164 ▶ !defined $spec? Carp::croak("Can't coerce undef to a generator")
Branch: 7 branches
BranchTrueFalseTotal
!defined($spec) ? : 0 164 164
!ref($spec) ? : 70 94 164
ref $spec eq 'ARRAY' ? : 13 81 94
ref $spec eq 'HASH' ? : 0 81 81
ref $spec eq 'CODE' ? : 72 9 81
ref($spec)->can('generate') ? : 7 2 9
ref $spec eq 'Regexp' ? : 2 0 2
47 : !ref $spec? Mock::Data::Template->new($spec)
48 : ref $spec eq 'ARRAY'? Mock::Data::Set->new(items => [map &_maybe_coerce_set_item, @$spec])
49 : ref $spec eq 'HASH'? weighted_set(%$spec)
50 : ref $spec eq 'CODE'? Mock::Data::GeneratorSub->new($spec)
51 : ref($spec)->can('generate')? $spec
52 : ref $spec eq 'Regexp'? Mock::Data::Regex->new($spec)
53 : Carp::croak("Don't know how to make '$spec' into a generator");
54 }
55 14 ▶sub _maybe_coerce_set_item {
Subroutine _maybe_coerce_set_item: called CC=3 SCAR=11.0
56 14 ▶ !ref($_)? inflate_template($_)
Branch: 2 branches
BranchTrueFalseTotal
!ref($_) ? : 11 3 14
ref $_ eq 'ARRAY' ? : 1 2 3
57 : ref($_) eq 'ARRAY'? Mock::Data::Set->new(items => [map &_maybe_coerce_set_item, @$_])
58 : coerce_generator($_);
59 }
60  
61  
62 13 ▶sub mock_data_subclass {
Subroutine mock_data_subclass: called CC=9 SCAR=22.1
Pod documented
63 13 my $self= shift;
64 13 ▶ my $class= ref $self || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
1120
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
65 13 my @to_add= grep !$class->isa($_), @_;
66 # Nothing to do if already part of this class/object
67 13 ▶ return $self unless @to_add;
Branch: 1 branch
BranchTrueFalseTotal
unless @to_add 0 13 13
68 # Determine what the new @ISA will be
69 my @new_isa= defined $Mock::Data::auto_subclasses{$class}
70 0 ▶ ? @{$Mock::Data::auto_subclasses{$class}}
Branch: 1 branch
BranchTrueFalseTotal
defined $Mock::Data::auto_subclasses{$class} ? : 0 13 13
71 : ($class);
72 # Remove redundant classes
73 13 for my $next_class (@to_add) {
74 26 ▶ next if grep $_->isa($next_class), @new_isa;
Branch: 1 branch
BranchTrueFalseTotal
if grep $_->isa($next_class), @new_isa 2 24 26
75 24 @new_isa= grep !$next_class->isa($_), @new_isa;
76 24 push @new_isa, $next_class;
77 }
78 # If only one class remains, then this one class already defined an inheritance for all
79 # the others. Use it directly.
80 13 my $new_class;
81 13 ▶ if (@new_isa == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@new_isa == 1) { } 3 10 13
82 3 $new_class= $new_isa[0];
83 } else {
84 # Now find if this combination was already composed, else create it.
85 10 $new_class= _name_for_combined_isa(@new_isa);
86 10 ▶ if (!$Mock::Data::auto_subclasses{$new_class}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Mock::Data::auto_subclasses{$new_class}) 8 2 10
87 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
88 8 @{"${new_class}::ISA"}= @new_isa;
89 8 $Mock::Data::auto_subclasses{$new_class}= \@new_isa;
90 }
91 }
92 13 ▶ return ref $self? bless($self, $new_class) : $new_class;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 1 12 13
93 }
94  
95 # When choosing a name for a new @ISA list, the name could be something as simple as ::AUTO$n
96 # with an incrementing number, but that wouldn't be helpful in a stack dump. But, a package
97 # name fully containing the ISA package names could get really long and also be unhelpful.
98 # Compromise by shortening the names by removing Mock::Data prefix and removing '::' and '_'.
99 # If this results in a name collision (seems unlikely), add an incrementing number on the end.
100 10 ▶sub _name_for_combined_isa {
Subroutine _name_for_combined_isa: called CC=4 SCAR=13.9
101 10 my @parts= grep { $_ ne 'Mock::Data' } @_;
102 10 my $isa_key= join "\0", @parts;
103 10 for (@parts) {
104 18 $_ =~ s/^Mock::Data:://;
105 18 $_ =~ s/::|_//g;
106 }
107 10 my $class= join '_', 'Mock::Data::_AUTO', @parts;
108 10 my $iter= 0;
109 10 my $suffix= '';
110 # While iterating, check to see if that package uses the same ISA list as this new request.
111 10 ▶ while (defined $Mock::Data::auto_subclasses{$class . $suffix}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Mock::Data::auto_subclasses{$class . $suffix} and $isa_key ne join("\0", grep({$_ ne "Mock::Data";} @{$Mock::Data::auto_subclasses{$class . $suffix};}))) 4 10 14
Condition100%3/3
defined $Mock::Data::auto_subclasses{$class . $suffix} and $isa_key ne join("\0", grep({$_ ne "Mock::Data";} @{$Mock::Data::auto_subclasses{$class . $suffix};}))
!ll&&!rl&&r
824
MC/DC100%2/2
defined $Mock::Data::auto_subclasses{$class . $suffix} and $isa_key ne join("\0", grep({$_ ne "Mock::Data";} @{$Mock::Data::auto_subclasses{$class . $suffix};}))
defined $Mock::Data::auto_subclasses{$class . $suffix} $isa_key ne join("\0", grep({$_ ne "Mock::Data";} @{$Mock::Data::auto_subclasses{$class . $suffix};}))
Truth table100%3/3
A and B
A= defined $Mock::Data::auto_subclasses{$class . $suffix}B= $isa_key ne join("\0", grep({$_ ne "Mock::Data";} @{$Mock::Data::auto_subclasses{$class . $suffix};}))
ABresult
0X0
100
111
112 && $isa_key ne join("\0",
113 17 grep { $_ ne 'Mock::Data' } @{$Mock::Data::auto_subclasses{$class . $suffix}}
114 )
115 ) {
116 4 $suffix= '_' . ++$iter;
117 }
118 10 $class . $suffix;
119 }
120  
121 # For those cases where Data::Dumper is both overkill and insufficient...
122 12 my %_escape_common= ( "\n" => '\n', "\t" => '\t', "\0" => '\0' );
123 212 ▶sub _escape_str {
Subroutine _escape_str: called CC=2 SCAR=7.0
124 212 my $str= shift;
125 212 ▶ $str =~ s/([^\x20-\x7E])/ $_escape_common{$1} || sprintf("\\x{%02X}",ord $1) /ge;
Condition66%2/3
$_escape_common{$1} || sprintf('\\x{%02X}', ord $1)
l!l&&r!l&&!r
23780
MC/DC0%0/2
$_escape_common{$1} || sprintf('\\x{%02X}', ord $1)
$_escape_common{$1} sprintf('\\x{%02X}', ord $1)
Truth table66%2/3
A || B
A= $_escape_common{$1}B= sprintf('\\x{%02X}', ord $1)
ABresult
1X1
011
000
126 212 return $str;
127 }
128 sub _dump;
129 67 ▶sub _dump {
Subroutine _dump: called CC=8 SCAR=21.1
130 67 ▶ local $_= shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 23 44 67
131 !defined $_? 'undef'
132 : !ref $_? (Scalar::Util::looks_like_number($_)? $_ : '"'._escape_str($_).'"')
133 : ref $_ eq 'ARRAY'? '['.join(', ', map _dump, @$_).']'
134 67 ▶ : ref $_ eq 'HASH'? do {
Branch: 5 branches
BranchTrueFalseTotal
!defined($_) ? : 0 67 67
!ref($_) ? : 44 23 67
&Scalar::Util::looks_like_number($_) ? : 33 11 44
ref $_ eq 'ARRAY' ? : 4 19 23
ref $_ eq 'HASH' ? : 19 0 19
135 19 my $hash= $_;
136 '{'.join(', ', map {
137 19 ($_ =~ /^\w+\z/? $_ : '"'._escape_str($_).'"')
138 23 ▶ .' => '._dump($hash->{$_})
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /^\w+\z/ ? : 23 0 23
139 } sort keys %$hash).'}';
140 }
141 : "$_"
142 }
143 2 ▶sub _parse_context {
Subroutine _parse_context: called CC=2 SCAR=7.6
144 2 ▶ return '"' . _escape_str(substr($_, defined $_[0]? $_[0] : pos, 10)) .'"';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 2 2
145 }
146  
147 # included last, because they depend on this module.
148 12 require Mock::Data::Set;
149 12 require Mock::Data::Charset;
150 12 require Mock::Data::Regex;
151 12 require Mock::Data::Template;
152 12 require Mock::Data::GeneratorSub;
153  
154 __END__
« Data/GeneratorSub.pm ↑ Summary Data/Generator.pm »