lib/Types/Standard/Map.pm

filter: Statementstmt 100% 135 / 136 (1 uncoverable) Branchbran 76.9% 60 / 78 Conditioncond 61.9% 26 / 42 MC/DCmcdc 34.7% 8 / 23 Subroutinesub 100% 23 / 23 Podpod n/a totaltotal 83.7% 252 / 302 (1 uncoverable) CC 58 SCAR 19.2
CC
58
Cov
87%
_exporter_fail
13.5
__coercion_generator
11.4
__ANON__
7.6
CRAP 6.8
« blib/lib/Type/Coercion/FromMoose.pm ↑ Summary blib/lib/Type/Tiny/Bitfield.pm »
1 # INTERNAL MODULE: guts for Map type from Types::Standard.
2  
3 package Types::Standard::Map;
4  
5 8 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 8 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 $Types::Standard::Map::AUTHORITY = 'cpan:TOBYINK';
11 8 $Types::Standard::Map::VERSION = '2.010001';
12 }
13  
14 8 $Types::Standard::Map::VERSION =~ tr/_//d;
15  
16 8 ▶use Type::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use Types::Standard ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use Types::TypeTiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 ▶sub _croak ($;@) { require Error::TypeTiny; goto \&Error::TypeTiny::croak }
Subroutine _croak: called CC=1 SCAR=0.0
21  
22 8 ▶use Exporter::Tiny 1.004001 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 8 our @ISA = qw( Exporter::Tiny );
24  
25 2 ▶sub _exporter_fail {
Subroutine _exporter_fail: called CC=12 SCAR=26.0
26 2 my ( $class, $type_name, $values, $globals ) = @_;
27 2 my $caller = $globals->{into};
28
29 2 ▶ my ( $keys, $vals ) = exists( $values->{of} ) ? @{ $values->{of} } : ( $values->{keys}, $values->{values} );
Branch: 1 branch
BranchTrueFalseTotal
exists $values->{'of'} ? : 1 1 2
30 2 ▶ defined $keys or _croak( qq{Expected option "keys" for type "$type_name"} );
Branch: 1 branch
BranchTrueFalseTotal
defined $keys or &_croak(qq[Expected option "keys" for type "$type_name"]) 2 0 2
31 2 ▶ defined $vals or _croak( qq{Expected option "values" for type "$type_name"} );
Branch: 1 branch
BranchTrueFalseTotal
defined $vals or &_croak(qq[Expected option "values" for type "$type_name"]) 2 0 2
32
33 2 ▶ if ( not Types::TypeTiny::is_TypeTiny($keys) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Types::TypeTiny::is_TypeTiny($keys)) 1 1 2
34 1 require Type::Utils;
35 1 $keys = Type::Utils::dwim_type( $keys, for => $caller );
36 }
37  
38 2 ▶ if ( not Types::TypeTiny::is_TypeTiny($vals) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Types::TypeTiny::is_TypeTiny($vals)) 1 1 2
39 1 require Type::Utils;
40 1 $vals = Type::Utils::dwim_type( $vals, for => $caller );
41 }
42
43 2 my $type = Types::Standard::Map->of( $keys, $vals );
44 $type = $type->create_child_type(
45 name => $type_name,
46 $type->has_coercion ? ( coercion => 1 ) : (),
47 2 ▶ exists( $values->{where} ) ? ( constraint => $values->{where} ) : (),
Branch: 2 branches
BranchTrueFalseTotal
$type->has_coercion ? : 0 2 2
exists $values->{'where'} ? : 0 2 2
48 );
49
50 $INC{'Type/Registry.pm'}
51 ? 'Type::Registry'->for_class( $caller )->add_type( $type, $type_name )
52 : ( $Type::Registry::DELAYED{$caller}{$type_name} = $type )
53 2 ▶ unless( ref($caller) or $caller eq '-lexical' or $globals->{'lexical'} );
Branch: 2 branches
BranchTrueFalseTotal
unless ref $caller or $caller eq "-lexical" or $globals->{"lexical"} 2 0 2
$INC{'Type/Registry.pm'} ? : 1 1 2
Condition33%2/6
ref $caller or $caller eq "-lexical" or $globals->{"lexical"}
l!l&&r!l&&!r
002
ref $caller or $caller eq "-lexical"
l!l&&r!l&&!r
002
MC/DC0%0/3
ref $caller or $caller eq "-lexical" or $globals->{"lexical"}
ref $caller $caller eq "-lexical" $globals->{"lexical"}
Truth table25%1/4
A or B or C
A= ref $callerB= $caller eq "-lexical"C= $globals->{"lexical"}
ABCresult
1XX1
01X1
0011
0000
54 2 return map +( $_->{name} => $_->{code} ), @{ $type->exportables };
55 }
56  
57 8 my $meta = Types::Standard->meta;
58  
59 8 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 30 ▶sub __constraint_generator {
Subroutine __constraint_generator: called CC=7 SCAR=19.5
62 30 ▶ return $meta->get_type( 'Map' ) unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 30 30
63
64 30 Type::Tiny::check_parameter_count_for_parameterized_type( 'Types::Standard', 'Map', \@_, 2, 2 );
65 28 my ( $keys, $values ) = @_;
66 28 ▶ Types::TypeTiny::is_TypeTiny( $keys )
Branch: 1 branch
BranchTrueFalseTotal
Types::TypeTiny::is_TypeTiny($keys) or &_croak("First parameter to Map[`k,`v] expected to be a type constraint; got $keys") 26 2 28
67 or _croak(
68 "First parameter to Map[`k,`v] expected to be a type constraint; got $keys" );
69 26 ▶ Types::TypeTiny::is_TypeTiny( $values )
Branch: 1 branch
BranchTrueFalseTotal
Types::TypeTiny::is_TypeTiny($values) or &_croak("Second parameter to Map[`k,`v] expected to be a type constraint; got $values") 25 1 26
70 or _croak(
71 "Second parameter to Map[`k,`v] expected to be a type constraint; got $values"
72 );
73
74 25 my @xsub;
75 25 if ( Type::Tiny::_USE_XS ) {
76 my @known = map {
77 25 my $known = Type::Tiny::XS::is_known( $_->compiled_check );
78 50 ▶ defined( $known ) ? $known : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $known ? : 42 8 50
79 } ( $keys, $values );
80
81 25 ▶ if ( @known == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@known == 2) 18 7 25
82 18 my $xsub = Type::Tiny::XS::get_coderef_for( sprintf "Map[%s,%s]", @known );
83 18 ▶ push @xsub, $xsub if $xsub;
Branch: 1 branch
BranchTrueFalseTotal
if $xsub 18 0 18
84 }
85 } #/ if ( Type::Tiny::_USE_XS)
86
87 25 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
88 25 my $hash = shift;
89 25 ▶ $keys->check( $_ ) || return for keys %$hash;
Branch: 1 branch
BranchTrueFalseTotal
$keys->check($_) or return 24 4 28
90 21 ▶ $values->check( $_ ) || return for values %$hash;
Branch: 1 branch
BranchTrueFalseTotal
$values->check($_) or return 18 6 24
91 15 return !!1;
92 25 }, @xsub;
93 } #/ sub __constraint_generator
94  
95 25 ▶sub __inline_generator {
Subroutine __inline_generator: called CC=5 SCAR=16.1
96 25 my ( $k, $v ) = @_;
97 25 ▶ return unless $k->can_be_inlined && $v->can_be_inlined;
Branch: 1 branch
BranchTrueFalseTotal
unless $k->can_be_inlined and $v->can_be_inlined 0 25 25
Condition33%1/3
$k->can_be_inlined and $v->can_be_inlined
!ll&&!rl&&r
0025
MC/DC0%0/2
$k->can_be_inlined and $v->can_be_inlined
$k->can_be_inlined $v->can_be_inlined
Truth table33%1/3
A and B
A= $k->can_be_inlinedB= $v->can_be_inlined
ABresult
0X0
100
111
98
99 25 my $xsubname;
100 25 if ( Type::Tiny::_USE_XS ) {
101 my @known = map {
102 25 my $known = Type::Tiny::XS::is_known( $_->compiled_check );
103 50 ▶ defined( $known ) ? $known : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $known ? : 42 8 50
104 } ( $k, $v );
105
106 25 ▶ if ( @known == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@known == 2) 18 7 25
107 18 $xsubname = Type::Tiny::XS::get_subname_for( sprintf "Map[%s,%s]", @known );
108 }
109 } #/ if ( Type::Tiny::_USE_XS)
110
111 328 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
112 328 my $h = $_[1];
113 328 ▶ return "$xsubname\($h\)" if $xsubname && !$Type::Tiny::AvoidCallbacks;
Branch: 1 branch
BranchTrueFalseTotal
if $xsubname and not $Type::Tiny::AvoidCallbacks 259 69 328
Condition100%3/3
$xsubname and not $Type::Tiny::AvoidCallbacks
!ll&&!rl&&r
4425259
MC/DC100%2/2
$xsubname and not $Type::Tiny::AvoidCallbacks
$xsubname not $Type::Tiny::AvoidCallbacks
Truth table100%3/3
A and not(B)
A= $xsubnameB= not $Type::Tiny::AvoidCallbacks
ABresult
0X0
100
111
114 69 my $p = Types::Standard::HashRef->inline_check( $h );
115 69 my $k_check = $k->inline_check( '$k' );
116 69 my $v_check = $v->inline_check( '$v' );
117 69 "$p and do { "
118 . "my \$ok = 1; "
119 . "for my \$v (values \%{$h}) { "
120 . "(\$ok = 0, last) unless $v_check " . "}; "
121 . "for my \$k (keys \%{$h}) { "
122 . "(\$ok = 0, last) unless $k_check " . "}; " . "\$ok " . "}";
123 25 };
124 } #/ sub __inline_generator
125  
126 2 ▶sub __deep_explanation {
Subroutine __deep_explanation: called CC=4 SCAR=13.9
127 2 require B;
128 2 my ( $type, $value, $varname ) = @_;
129 2 my ( $kparam, $vparam ) = @{ $type->parameters };
130
131 2 for my $k ( sort keys %$value ) {
132 3 ▶ unless ( $kparam->check( $k ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($kparam->check($k)) 1 2 3
133 return [
134 sprintf( '"%s" constrains each key in the hash with "%s"', $type, $kparam ),
135 @{
136 1 $kparam->validate_explain(
137 $k, sprintf( 'key %s->{%s}', $varname, B::perlstring( $k ) )
138 )
139 },
140 ];
141 } #/ unless ( $kparam->check( $k...))
142
143 2 ▶ unless ( $vparam->check( $value->{$k} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($vparam->check($value->{$k})) 1 1 2
144 return [
145 sprintf( '"%s" constrains each value in the hash with "%s"', $type, $vparam ),
146 @{
147 1 $vparam->validate_explain(
148 1 $value->{$k}, sprintf( '%s->{%s}', $varname, B::perlstring( $k ) )
149 )
150 },
151 ];
152 } #/ unless ( $vparam->check( $value...))
153 } #/ for my $k ( sort keys %$value)
154
155 # This should never happen...
156 0 return; # uncoverable statement
157 } #/ sub __deep_explanation
158  
159 16 ▶sub __coercion_generator {
Subroutine __coercion_generator: called CC=11 SCAR=24.4
160 16 my ( $parent, $child, $kparam, $vparam ) = @_;
161 16 ▶ return unless $kparam->has_coercion || $vparam->has_coercion;
Branch: 1 branch
BranchTrueFalseTotal
unless $kparam->has_coercion or $vparam->has_coercion 10 6 16
Condition100%3/3
$kparam->has_coercion or $vparam->has_coercion
l!l&&r!l&&!r
5110
MC/DC100%2/2
$kparam->has_coercion or $vparam->has_coercion
$kparam->has_coercion $vparam->has_coercion
Truth table100%3/3
A or B
A= $kparam->has_coercionB= $vparam->has_coercion
ABresult
1X1
011
000
162
163 6 ▶ my $kcoercable_item =
Branch: 1 branch
BranchTrueFalseTotal
$kparam->has_coercion ? : 5 1 6
164 $kparam->has_coercion
165 ? $kparam->coercion->_source_type_union
166 : $kparam;
167 6 ▶ my $vcoercable_item =
Branch: 1 branch
BranchTrueFalseTotal
$vparam->has_coercion ? : 6 0 6
168 $vparam->has_coercion
169 ? $vparam->coercion->_source_type_union
170 : $vparam;
171 6 my $C = "Type::Coercion"->new( type_constraint => $child );
172
173 6 ▶ if ( ( !$kparam->has_coercion or $kparam->coercion->can_be_inlined )
Branch: 1 branch
BranchTrueFalseTotal
if (!$kparam->has_coercion || $kparam->coercion->can_be_inlined and !$vparam->has_coercion || $vparam->coercion->can_be_inlined and $kcoercable_item->can_be_inlined and $vcoercable_item->can_be_inlined) { } 4 2 6
Condition80%12/15
!$kparam->has_coercion || $kparam->coercion->can_be_inlined and !$vparam->has_coercion || $vparam->coercion->can_be_inlined and $kcoercable_item->can_be_inlined and $vcoercable_item->can_be_inlined
!ll&&!rl&&r
204
!$kparam->has_coercion || $kparam->coercion->can_be_inlined and !$vparam->has_coercion || $vparam->coercion->can_be_inlined and $kcoercable_item->can_be_inlined
!ll&&!rl&&r
204
!$kparam->has_coercion || $kparam->coercion->can_be_inlined and !$vparam->has_coercion || $vparam->coercion->can_be_inlined
!ll&&!rl&&r
114
not $kparam->has_coercion or $kparam->coercion->can_be_inlined
l!l&&r!l&&!r
141
not $vparam->has_coercion or $vparam->coercion->can_be_inlined
l!l&&r!l&&!r
041
MC/DC50%3/6
!$kparam->has_coercion || $kparam->coercion->can_be_inlined and !$vparam->has_coercion || $vparam->coercion->can_be_inlined and $kcoercable_item->can_be_inlined and $vcoercable_item->can_be_inlined
not $kparam->has_coercion $kparam->coercion->can_be_inlined not $vparam->has_coercion $vparam->coercion->can_be_inlined $kcoercable_item->can_be_inlined $vcoercable_item->can_be_inlined
Truth table26%4/15
not(A) or B and not(C) or D and E and F
A= not $kparam->has_coercionB= $kparam->coercion->can_be_inlinedC= not $vparam->has_coercionD= $vparam->coercion->can_be_inlinedE= $kcoercable_item->can_be_inlinedF= $vcoercable_item->can_be_inlined
ABCDEFresult
00XXXX0
1X00XX0
0100XX0
1X1X0X0
1X010X0
011X0X0
01010X0
1X1X100
1X01100
011X100
0101100
1X1X111
1X01111
011X111
0101111
174 and ( !$vparam->has_coercion or $vparam->coercion->can_be_inlined )
175 and $kcoercable_item->can_be_inlined
176 and $vcoercable_item->can_be_inlined )
177 {
178 $C->add_type_coercions(
179 3 ▶ $parent => Types::Standard::Stringable {
Subroutine __ANON__: called CC=3 SCAR=11.1
180 3 my @code;
181 3 push @code, 'do { my ($orig, $return_orig, %new) = ($_, 0);';
182 3 push @code, 'for (keys %$orig) {';
183 3 push @code,
184 sprintf(
185 '++$return_orig && last unless (%s);',
186 $kcoercable_item->inline_check( '$_' )
187 );
188 3 push @code,
189 sprintf(
190 '++$return_orig && last unless (%s);',
191 $vcoercable_item->inline_check( '$orig->{$_}' )
192 );
193 3 ▶ push @code, sprintf(
Branch: 2 branches
BranchTrueFalseTotal
$kparam->has_coercion ? : 3 0 3
$vparam->has_coercion ? : 3 0 3
194 '$new{(%s)} = (%s);',
195 $kparam->has_coercion ? $kparam->coercion->inline_coercion( '$_' ) : '$_',
196 $vparam->has_coercion
197 ? $vparam->coercion->inline_coercion( '$orig->{$_}' )
198 : '$orig->{$_}',
199 );
200 3 push @code, '}';
201 3 push @code, '$return_orig ? $orig : \\%new';
202 3 push @code, '}';
203 3 "@code";
204 }
205 4 );
206 } #/ if ( ( !$kparam->has_coercion...))
207 else {
208 $C->add_type_coercions(
209 3 ▶ $parent => sub {
Subroutine __ANON__: called CC=7 SCAR=20.3
210 3 ▶ my $value = @_ ? $_[0] : $_;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 3 0 3
211 3 my %new;
212 3 for my $k ( keys %$value ) {
213 return $value
214 unless $kcoercable_item->check( $k )
215 8 ▶ && $vcoercable_item->check( $value->{$k} );
Branch: 1 branch
BranchTrueFalseTotal
unless $kcoercable_item->check($k) and $vcoercable_item->check($value->{$k}) 1 7 8
Condition66%2/3
$kcoercable_item->check($k) and $vcoercable_item->check($value->{$k})
!ll&&!rl&&r
017
MC/DC50%1/2
$kcoercable_item->check($k) and $vcoercable_item->check($value->{$k})
$kcoercable_item->check($k) $vcoercable_item->check($value->{$k})
Truth table66%2/3
A and B
A= $kcoercable_item->check($k)B= $vcoercable_item->check($value->{$k})
ABresult
0X0
100
111
216 $new{ $kparam->has_coercion ? $kparam->coerce( $k ) : $k } =
217 $vparam->has_coercion
218 ? $vparam->coerce( $value->{$k} )
219 7 ▶ : $value->{$k};
Branch: 2 branches
BranchTrueFalseTotal
$vparam->has_coercion ? : 7 0 7
$kparam->has_coercion ? : 7 0 7
220 }
221 2 return \%new;
222 },
223 2 );
224 } #/ else [ if ( ( !$kparam->has_coercion...))]
225
226 6 return $C;
227 } #/ sub __coercion_generator
228  
229 45 ▶sub __hashref_allows_key {
Subroutine my_hashref_allows_key: called CC=3 SCAR=11.2
230 45 my $self = shift;
231 45 my ( $key ) = @_;
232
233 45 ▶ return Types::Standard::is_Str( $key ) if $self == Types::Standard::Map();
Branch: 1 branch
BranchTrueFalseTotal
if $self == &Types::Standard::Map() 6 39 45
234
235 my $map = $self->find_parent(
236 39 ▶ sub { $_->has_parent && $_->parent == Types::Standard::Map() } );
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$_->has_parent and $_->parent == &Types::Standard::Map() 43 0 43
237 39 my ( $kcheck, $vcheck ) = @{ $map->parameters };
238
239 39 ▶ ( $kcheck or Types::Standard::Any() )->check( $key );
Condition33%1/3
$kcheck || &Types::Standard::Any()
l!l&&r!l&&!r
3900
MC/DC0%0/2
$kcheck || &Types::Standard::Any()
$kcheck &Types::Standard::Any()
Truth table33%1/3
A || B
A= $kcheckB= &Types::Standard::Any()
ABresult
1X1
011
000
240 } #/ sub __hashref_allows_key
241  
242 10 ▶sub __hashref_allows_value {
Subroutine my_hashref_allows_value: called CC=6 SCAR=19.5
243 10 my $self = shift;
244 10 my ( $key, $value ) = @_;
245
246 10 ▶ return !!0 unless $self->my_hashref_allows_key( $key );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->my_hashref_allows_key($key) 3 7 10
247 7 ▶ return !!1 if $self == Types::Standard::Map();
Branch: 1 branch
BranchTrueFalseTotal
if $self == &Types::Standard::Map() 0 7 7
248
249 my $map = $self->find_parent(
250 7 ▶ sub { $_->has_parent && $_->parent == Types::Standard::Map() } );
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$_->has_parent and $_->parent == &Types::Standard::Map() 7 0 7
251 7 my ( $kcheck, $vcheck ) = @{ $map->parameters };
252
253 7 ▶ ( $kcheck or Types::Standard::Any() )->check( $key )
Branch: 1 branch
BranchTrueFalseTotal
($kcheck || &Types::Standard::Any())->check($key) and ($vcheck || &Types::Standard::Any())->check($value) 7 0 7
Condition33%2/6
$kcheck || &Types::Standard::Any()
l!l&&r!l&&!r
700
$vcheck || &Types::Standard::Any()
l!l&&r!l&&!r
700
MC/DC0%0/2
$kcheck || &Types::Standard::Any()
$kcheck &Types::Standard::Any()
MC/DC0%0/2
$vcheck || &Types::Standard::Any()
$vcheck &Types::Standard::Any()
Truth table33%1/3
A || B
A= $kcheckB= &Types::Standard::Any()
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $vcheckB= &Types::Standard::Any()
ABresult
1X1
011
000
254 and ( $vcheck or Types::Standard::Any() )->check( $value );
255 } #/ sub __hashref_allows_value
256  
257 8 1;
258  
259 __END__
« blib/lib/Type/Coercion/FromMoose.pm ↑ Summary blib/lib/Type/Tiny/Bitfield.pm »