blib/lib/Type/Tiny/Bitfield.pm

filter: Statementstmt 99.2% 139 / 140 Branchbran 86.3% 38 / 44 Conditioncond 62.2% 28 / 45 MC/DCmcdc 40.0% 10 / 25 Subroutinesub 100% 28 / 28 Podpod 90.0% 9 / 10 totaltotal 86.3% 252 / 292 CC 45 SCAR 19.1
CC
45
Cov
90%
new
15.2
new_combined
10.2
_exporter_fail
8.3
CRAP 6.7
« lib/Types/Standard/Map.pm ↑ Summary blib/lib/Type/Tiny/Enum.pm »
1 package Type::Tiny::Bitfield;
2  
3 4 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 $Type::Tiny::Bitfield::AUTHORITY = 'cpan:TOBYINK';
9 4 $Type::Tiny::Bitfield::VERSION = '2.010001';
10 }
11  
12 4 $Type::Tiny::Bitfield::VERSION =~ tr/_//d;
13  
14 14 ▶sub _croak ($;@) { require Error::TypeTiny; goto \&Error::TypeTiny::croak }
Subroutine _croak: called CC=1 SCAR=0.0
15  
16 4 ▶use Exporter::Tiny 1.004001 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Type::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Types::Common::Numeric qw( +PositiveOrZeroInt );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use Eval::TypeTiny qw( eval_closure );
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 4 our @ISA = qw( Type::Tiny Exporter::Tiny );
22  
23 4 __PACKAGE__->_install_overloads(
24 q[+] => 'new_combined',
25 );
26  
27 42 ▶sub _is_power_of_two { not $_[0] & $_[0]-1 }
Subroutine _is_power_of_two: called CC=1 SCAR=0.0
28  
29 6 ▶sub _exporter_fail {
Subroutine _exporter_fail: called CC=7 SCAR=21.2
30 6 my ( $class, $type_name, $args, $globals ) = @_;
31 6 my $caller = $globals->{into};
32 6 my %values = %$args;
33 6 ▶ /^[-]/ && delete( $values{$_} ) for keys %values;
Branch: 1 branch
BranchTrueFalseTotal
/^[-]/ and delete $values{$_} 1 18 19
34 6 my $type = $class->new(
35 name => $type_name,
36 values => \%values,
37 coercion => 1,
38 );
39 $INC{'Type/Registry.pm'}
40 ? 'Type::Registry'->for_class( $caller )->add_type( $type, $type_name )
41 : ( $Type::Registry::DELAYED{$caller}{$type_name} = $type )
42 4 ▶ unless( ref($caller) or $caller eq '-lexical' or $globals->{'lexical'} );
Branch: 2 branches
BranchTrueFalseTotal
unless ref $caller or $caller eq "-lexical" or $globals->{"lexical"} 4 0 4
$INC{'Type/Registry.pm'} ? : 0 4 4
Condition33%2/6
ref $caller or $caller eq "-lexical" or $globals->{"lexical"}
l!l&&r!l&&!r
004
ref $caller or $caller eq "-lexical"
l!l&&r!l&&!r
004
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
43 4 return map +( $_->{name} => $_->{code} ), @{ $type->exportables };
44 }
45  
46 27 ▶sub new {
Subroutine new: called CC=15 SCAR=27.2
Pod documented
47 27 my $proto = shift;
48
49 0 ▶ my %opts = ( @_ == 1 ) ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 27 27
50 _croak
51 "Bitfield type constraints cannot have a parent constraint passed to the constructor"
52 27 ▶ if exists $opts{parent};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"parent"} 1 26 27
53 _croak
54 "Bitfield type constraints cannot have a constraint coderef passed to the constructor"
55 26 ▶ if exists $opts{constraint};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"constraint"} 1 25 26
56 _croak
57 "Bitfield type constraints cannot have a inlining coderef passed to the constructor"
58 25 ▶ if exists $opts{inlined};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"inlined"} 1 24 25
59 _croak "Need to supply hashref of values"
60 24 ▶ unless exists $opts{values};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"values"} 1 23 24
61
62 23 $opts{parent} = PositiveOrZeroInt;
63
64 23 for my $key ( keys %{ $opts{values} } ) {
65 46 ▶ _croak "Not an all-caps name in a bitfield: $key"
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*/ 2 44 46
66 unless $key =~ /^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*/
67 }
68 21 my $ALL = 0;
69 21 my %already = ();
70 21 for my $value ( values %{ $opts{values} } ) {
71 42 ▶ _croak "Not a positive power of 2 in a bitfield: $value"
Branch: 1 branch
BranchTrueFalseTotal
unless is_PositiveOrZeroInt($value) and _is_power_of_two($value) 2 40 42
Condition66%2/3
is_PositiveOrZeroInt($value) and _is_power_of_two($value)
!ll&&!rl&&r
0240
MC/DC50%1/2
is_PositiveOrZeroInt($value) and _is_power_of_two($value)
is_PositiveOrZeroInt($value) _is_power_of_two($value)
Truth table66%2/3
A and B
A= is_PositiveOrZeroInt($value)B= _is_power_of_two($value)
ABresult
0X0
100
111
72 unless is_PositiveOrZeroInt( $value ) && _is_power_of_two( $value );
73 _croak "Duplicate value in a bitfield: $value"
74 40 ▶ if $already{$value}++;
Branch: 1 branch
BranchTrueFalseTotal
if $already{$value}++ 1 39 40
75 39 $ALL |= ( 0 + $value );
76 }
77 18 $opts{ALL} = $ALL;
78
79 92 ▶ $opts{constraint} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
80 92 not shift() & ~$ALL;
81 18 };
82
83 18 ▶ if ( defined $opts{coercion}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"coercion"} and not ref $opts{"coercion"} and 1 eq $opts{"coercion"}) 12 6 18
Condition66%4/6
defined $opts{"coercion"} and not ref $opts{"coercion"} and 1 eq $opts{"coercion"}
!ll&&!rl&&r
6012
defined $opts{"coercion"} and not ref $opts{"coercion"}
!ll&&!rl&&r
6012
MC/DC33%1/3
defined $opts{"coercion"} and not ref $opts{"coercion"} and 1 eq $opts{"coercion"}
defined $opts{"coercion"} not ref $opts{"coercion"} 1 eq $opts{"coercion"}
Truth table50%2/4
A and not(B) and C
A= defined $opts{"coercion"}B= not ref $opts{"coercion"}C= 1 eq $opts{"coercion"}
ABCresult
0XX0
10X0
1100
1111
84 and !ref $opts{coercion}
85 and 1 eq $opts{coercion} ) {
86 12 delete $opts{coercion};
87 11 ▶ $opts{_build_coercion} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
88 11 require Types::Standard;
89 11 my $c = shift;
90 11 my $t = $c->type_constraint;
91 11 $c->add_type_coercions(
92 Types::Standard::Str(),
93 $t->_stringy_coercion,
94 );
95 12 };
96 } #/ if ( defined $opts{coercion...})
97
98 18 return $proto->SUPER::new( %opts );
99 } #/ sub new
100  
101 10 ▶sub new_combined {
Subroutine new_combined: called CC=10 SCAR=23.3
Pod undocumented
102 10 my ( $self, $other, $swap ) = @_;
103
104 10 ▶ Scalar::Util::blessed( $self )
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($self) and $self->isa(__PACKAGE__) and builtin::blessed($other) and $other->isa(__PACKAGE__) or &_croak("Bad overloaded operation") 6 4 10
Condition66%6/9
builtin::blessed($self) and $self->isa(__PACKAGE__) and builtin::blessed($other) and $other->isa(__PACKAGE__)
!ll&&!rl&&r
226
builtin::blessed($self) and $self->isa(__PACKAGE__) and builtin::blessed($other)
!ll&&!rl&&r
028
builtin::blessed($self) and $self->isa(__PACKAGE__)
!ll&&!rl&&r
0010
MC/DC50%2/4
builtin::blessed($self) and $self->isa(__PACKAGE__) and builtin::blessed($other) and $other->isa(__PACKAGE__)
builtin::blessed($self) $self->isa(__PACKAGE__) builtin::blessed($other) $other->isa(__PACKAGE__)
Truth table60%3/5
A and B and C and D
A= builtin::blessed($self)B= $self->isa(__PACKAGE__)C= builtin::blessed($other)D= $other->isa(__PACKAGE__)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
105 && $self->isa( __PACKAGE__ )
106 && Scalar::Util::blessed( $other )
107 && $other->isa( __PACKAGE__ )
108 or _croak( "Bad overloaded operation" );
109
110 6 ▶ ( $other, $self ) = ( $self, $other ) if $swap;
Branch: 1 branch
BranchTrueFalseTotal
if $swap 0 6 6
111
112 6 for my $k ( keys %{ $self->values } ) {
113 _croak "Conflicting value: $k"
114 10 ▶ if exists $other->values->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $other->values->{$k} 1 9 10
115 }
116
117 5 my %all_values = ( %{ $self->values }, %{ $other->values } );
118 5 ▶ return ref( $self )->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->has_coercion || $other->has_coercion ? : 4 1 5
Condition100%3/3
$self->has_coercion or $other->has_coercion
l!l&&r!l&&!r
311
MC/DC100%2/2
$self->has_coercion or $other->has_coercion
$self->has_coercion $other->has_coercion
Truth table100%3/3
A or B
A= $self->has_coercionB= $other->has_coercion
ABresult
1X1
011
000
119 display_name => sprintf( '%s+%s', "$self", "$other" ),
120 values => \%all_values,
121 ( $self->has_coercion || $other->has_coercion )
122 ? ( coercion => 1 )
123 : (),
124 );
125 }
126  
127 64 ▶sub values {
Subroutine values: called CC=1 SCAR=0.0
Pod documented
128 64 $_[0]{values};
129 }
130  
131 18 ▶sub _lockdown {
Subroutine _lockdown: called CC=1 SCAR=0.0
132 18 my ( $self, $callback ) = @_;
133 18 $callback->( $self->{values} );
134 }
135  
136 5 ▶sub exportables {
Subroutine exportables: called CC=4 SCAR=14.0
Pod documented
137 5 my ( $self, $base_name ) = @_;
138 5 ▶ if ( not $self->is_anon ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_anon) 5 0 5
139 5 ▶ $base_name ||= $self->name;
Condition33%1/3
$base_name ||= $self->name
l!l&&r!l&&!r
050
MC/DC0%0/2
$base_name ||= $self->name
$base_name $self->name
Truth table33%1/3
A ||= B
A= $base_nameB= $self->name
ABresult
1X1
011
000
140 }
141
142 5 my $exportables = $self->SUPER::exportables( $base_name );
143
144 5 require Eval::TypeTiny;
145 5 require B;
146
147 5 for my $key ( keys %{ $self->values } ) {
148 17 my $value = $self->values->{$key};
149 17 push @$exportables, {
150 name => uc( sprintf '%s_%s', $base_name, $key ),
151 tags => [ 'constants' ],
152 code => Eval::TypeTiny::eval_closure(
153 source => sprintf( 'sub () { %d }', $value ),
154 environment => {},
155 ),
156 };
157 }
158
159 5 my $weak = $self;
160 5 require Scalar::Util;
161 5 Scalar::Util::weaken( $weak );
162 push @$exportables, {
163 name => sprintf( '%s_to_Str', $base_name ),
164 tags => [ 'from' ],
165 1 ▶ code => sub { $weak->to_string( @_ ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
166 5 };
167
168 5 return $exportables;
169 }
170  
171 1 ▶sub constant_names {
Subroutine constant_names: called CC=2 SCAR=7.0
Pod documented
172 1 my $self = shift;
173 4 return map { $_->{name} }
174 9 grep { my $tags = $_->{tags}; grep $_ eq 'constants', @$tags; }
175 1 ▶ @{ $self->exportables || [] };
Condition50%1/2
$self->exportables || []
l!l
10
MC/DC0%0/1
$self->exportables || []
$self->exportables
Truth table50%1/2
A
A= $self->exportables
Aresult
11
00
176 }
177  
178 126 ▶sub can_be_inlined {
Subroutine can_be_inlined: called CC=1 SCAR=0.0
Pod documented
179 126 !!1;
180 }
181  
182 221 ▶sub inline_check {
Subroutine inline_check: called CC=1 SCAR=0.0
Pod documented
183 221 my ( $self, $var ) = @_;
184
185 return sprintf(
186 '( %s and not %s & ~%d )',
187 PositiveOrZeroInt->inline_check( $var ),
188 $var,
189 $self->{ALL},
190 221 );
191 }
192  
193 12 ▶sub _stringy_coercion {
Subroutine _stringy_coercion: called CC=2 SCAR=6.9
194 12 my ( $self, $varname ) = @_;
195 12 ▶ $varname ||= '$_';
Condition100%2/2
$varname ||= "\$_"
l!l
111
MC/DC100%1/1
$varname ||= "\$_"
$varname
Truth table100%2/2
A
A= $varname
Aresult
11
00
196 12 my %vals = %{ $self->values };
197 12 my $pfx = uc( "$self" );
198 12 my $pfxl = length $pfx;
199 my $hash = sprintf(
200 '( %s )',
201 join(
202 q{, },
203 12 map sprintf( '%s => %d', B::perlstring($_), $vals{$_} ),
204 sort keys %vals,
205 ),
206 );
207 12 return qq{do { my \$bits = 0; my \%lookup = $hash; for my \$tok ( grep /\\w/, split /[\\s|+]+/, uc( $varname ) ) { if ( substr( \$tok, 0, $pfxl) eq "$pfx" ) { \$tok = substr( \$tok, $pfxl ); \$tok =~ s/^_//; } if ( exists \$lookup{\$tok} ) { \$bits |= \$lookup{\$tok}; next; } require Carp; Carp::carp("Unknown token: \$tok"); } \$bits; }};
208 }
209  
210 1 ▶sub from_string {
Subroutine from_string: called CC=2 SCAR=7.6
Pod documented
211 1 my ( $self, $str ) = @_;
212 1 ▶ $self->{from_string} ||= eval_closure(
Condition33%1/3
$self->{"from_string"} ||= eval_closure("environment", scalar {}, "source", sprintf("sub { my \$STR = shift; %s }", $self->_stringy_coercion("\$STR")))
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"from_string"} ||= eval_closure("environment", scalar {}, "source", sprintf("sub { my \$STR = shift; %s }", $self->_stringy_coercion("\$STR")))
$self->{"from_string"} eval_closure("environment", scalar {}, "source", sprintf("sub { my \$STR = shift; %s }", $self->_stringy_coercion("\$STR")))
Truth table33%1/3
A ||= B
A= $self->{"from_string"}B= eval_closure("environment", scalar {}, "source", sprintf("sub { my \$STR = shift; %s }", $self->_stringy_coercion("\$STR")))
ABresult
1X1
011
000
213 environment => {},
214 source => sprintf( 'sub { my $STR = shift; %s }', $self->_stringy_coercion( '$STR' ) ),
215 );
216 1 $self->{from_string}->( $str );
217 }
218  
219 6 ▶sub to_string {
Subroutine to_string: called CC=5 SCAR=16.1
Pod documented
220 6 my ( $self, $int ) = @_;
221 6 ▶ $self->check( $int ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->check($int) or return undef 4 2 6
222 4 my %values = %{ $self->values };
223 4 ▶ $self->{all_names} ||= [ sort { $values{$a} <=> $values{$b} } keys %values ];
Condition100%2/2
$self->{"all_names"} ||= [(sort {$values{$a} <=> $values{$b};} keys %values)]
l!l
31
MC/DC100%1/1
$self->{"all_names"} ||= [(sort {$values{$a} <=> $values{$b};} keys %values)]
$self->{"all_names"}
Truth table100%2/2
A
A= $self->{"all_names"}
Aresult
11
00
224 4 $int += 0;
225 4 my @names;
226 4 for my $n ( @{ $self->{all_names} } ) {
227 16 ▶ push @names, $n if $int & $values{$n};
Branch: 1 branch
BranchTrueFalseTotal
if $int & $values{$n} 7 9 16
228 }
229 4 return join q{|}, @names;
230 }
231  
232 5 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=4 SCAR=14.0
233 5 our $AUTOLOAD;
234 5 my $self = shift;
235 5 my ( $m ) = ( $AUTOLOAD =~ /::(\w+)$/ );
236 5 ▶ return if $m eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $m eq "DESTROY" 0 5 5
237 5 ▶ if ( ref $self and exists $self->{values}{$m} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and exists $self->{"values"}{$m}) 4 1 5
Condition66%2/3
ref $self and exists $self->{"values"}{$m}
!ll&&!rl&&r
014
MC/DC50%1/2
ref $self and exists $self->{"values"}{$m}
ref $self exists $self->{"values"}{$m}
Truth table66%2/3
A and B
A= ref $selfB= exists $self->{"values"}{$m}
ABresult
0X0
100
111
238 4 return 0 + $self->{values}{$m};
239 }
240 1 local $Type::Tiny::AUTOLOAD = $AUTOLOAD;
241 1 return $self->SUPER::AUTOLOAD( @_ );
242 }
243  
244 502 ▶sub can {
Subroutine can: called CC=3 SCAR=11.0
Pod documented
245 502 my ( $self, $m ) = ( shift, @_ );
246 502 ▶ if ( ref $self and exists $self->{values}{$m} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and exists $self->{"values"}{$m}) 2 500 502
Condition66%2/3
ref $self and exists $self->{"values"}{$m}
!ll&&!rl&&r
05002
MC/DC50%1/2
ref $self and exists $self->{"values"}{$m}
ref $self exists $self->{"values"}{$m}
Truth table66%2/3
A and B
A= ref $selfB= exists $self->{"values"}{$m}
ABresult
0X0
100
111
247 2 ▶ return sub () { 0 + $self->{values}{$m} };
Subroutine __ANON__: called CC=1 SCAR=0.0
248 }
249 500 return $self->SUPER::can( @_ );
250 }
251  
252 4 1;
253  
254 __END__
« lib/Types/Standard/Map.pm ↑ Summary blib/lib/Type/Tiny/Enum.pm »