blib/lib/Types/Common/Numeric.pm

filter: Statementstmt 83.3% 90 / 108 Branchbran 65.0% 26 / 40 Conditioncond 75.0% 15 / 20 MC/DCmcdc 69.2% 9 / 13 Subroutinesub 77.7% 35 / 45 Podpod n/a totaltotal 77.4% 175 / 226 CC 25 SCAR 18.7
CC
25
Cov
78%
__ANON__
42.0
__ANON__
10.0
__ANON__
4.1
CRAP 6.5
« blib/lib/Type/Tiny/Enum.pm ↑ Summary blib/lib/Type/Params/Alternatives.pm »
1 package Types::Common::Numeric;
2  
3 46 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 46 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 46 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 46 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 46 $Types::Common::Numeric::AUTHORITY = 'cpan:TOBYINK';
9 46 $Types::Common::Numeric::VERSION = '2.010001';
10 }
11  
12 46 $Types::Common::Numeric::VERSION =~ tr/_//d;
13  
14 46 ▶use Type::Library -base, -declare => qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
15 PositiveNum PositiveOrZeroNum
16 PositiveInt PositiveOrZeroInt
17 NegativeNum NegativeOrZeroNum
18 NegativeInt NegativeOrZeroInt
19 SingleDigit
20 NumRange IntRange
21 46 );
22  
23 46 ▶use Type::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 46 ▶use Types::Standard qw( Num Int );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 46 ▶use Types::TypeTiny qw( BoolLike );
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 14 ▶sub _croak ($;@) { require Error::TypeTiny; goto \&Error::TypeTiny::croak }
Subroutine _croak: called CC=1 SCAR=0.0
28  
29 46 my $meta = __PACKAGE__->meta;
30  
31 $meta->add_type(
32 name => 'PositiveNum',
33 parent => Num,
34 10 ▶ constraint => sub { $_ > 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
35 213 ▶ inlined => sub { undef, qq($_ > 0) },
Subroutine __ANON__: called CC=1 SCAR=0.0
36 0 ▶ message => sub { "Must be a positive number" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 46 );
38  
39 $meta->add_type(
40 name => 'PositiveOrZeroNum',
41 parent => Num,
42 10 ▶ constraint => sub { $_ >= 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
43 211 ▶ inlined => sub { undef, qq($_ >= 0) },
Subroutine __ANON__: called CC=1 SCAR=0.0
44 0 ▶ message => sub { "Must be a number greater than or equal to zero" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 1 ▶ type_default => sub { return 0; },
Subroutine __ANON__: called CC=1 SCAR=0.0
46 46 );
47  
48 46 my ( $pos_int, $posz_int );
49 46 if ( Type::Tiny::_USE_XS ) {
50 46 ▶ $pos_int = Type::Tiny::XS::get_coderef_for( 'PositiveInt' )
Branch: 1 branch
BranchTrueFalseTotal
if "Type::Tiny::XS"->VERSION >= "0.013" 46 0 46
51 if Type::Tiny::XS->VERSION >= 0.013; # fixed bug with "00"
52 46 $posz_int = Type::Tiny::XS::get_coderef_for( 'PositiveOrZeroInt' );
53 }
54  
55 $meta->add_type(
56 name => 'PositiveInt',
57 parent => Int,
58 16 ▶ constraint => sub { $_ > 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
59 235 ▶ inlined => sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
60 235 ▶ if ( $pos_int ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos_int) 235 0 235
61 235 my $xsub = Type::Tiny::XS::get_subname_for( $_[0]->name );
62 235 ▶ return "$xsub($_[1])" if $xsub && !$Type::Tiny::AvoidCallbacks;
Branch: 1 branch
BranchTrueFalseTotal
if $xsub and not $Type::Tiny::AvoidCallbacks 167 68 235
Condition66%2/3
$xsub and not $Type::Tiny::AvoidCallbacks
!ll&&!rl&&r
068167
MC/DC50%1/2
$xsub and not $Type::Tiny::AvoidCallbacks
$xsub not $Type::Tiny::AvoidCallbacks
Truth table66%2/3
A and not(B)
A= $xsubB= not $Type::Tiny::AvoidCallbacks
ABresult
0X0
100
111
63 }
64 68 undef, qq($_ > 0);
65 },
66 0 ▶ message => sub { "Must be a positive integer" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 46 ▶ $pos_int ? ( compiled_type_constraint => $pos_int ) : (),
Branch: 1 branch
BranchTrueFalseTotal
$pos_int ? : 46 0 46
68 );
69  
70 $meta->add_type(
71 name => 'PositiveOrZeroInt',
72 parent => Int,
73 102 ▶ constraint => sub { $_ >= 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
74 387 ▶ inlined => sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
75 387 ▶ if ( $posz_int ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($posz_int) 387 0 387
76 387 my $xsub = Type::Tiny::XS::get_subname_for( $_[0]->name );
77 387 ▶ return "$xsub($_[1])" if $xsub && !$Type::Tiny::AvoidCallbacks;
Branch: 1 branch
BranchTrueFalseTotal
if $xsub and not $Type::Tiny::AvoidCallbacks 229 158 387
Condition66%2/3
$xsub and not $Type::Tiny::AvoidCallbacks
!ll&&!rl&&r
0158229
MC/DC50%1/2
$xsub and not $Type::Tiny::AvoidCallbacks
$xsub not $Type::Tiny::AvoidCallbacks
Truth table66%2/3
A and not(B)
A= $xsubB= not $Type::Tiny::AvoidCallbacks
ABresult
0X0
100
111
78 }
79 158 undef, qq($_ >= 0);
80 },
81 0 ▶ message => sub { "Must be an integer greater than or equal to zero" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 $posz_int ? ( compiled_type_constraint => $posz_int ) : (),
83 1 ▶ type_default => sub { return 0; },
Subroutine __ANON__: called CC=1 SCAR=0.0
84 46 ▶);
Branch: 1 branch
BranchTrueFalseTotal
$posz_int ? : 46 0 46
85  
86 $meta->add_type(
87 name => 'NegativeNum',
88 parent => Num,
89 10 ▶ constraint => sub { $_ < 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
90 213 ▶ inlined => sub { undef, qq($_ < 0) },
Subroutine __ANON__: called CC=1 SCAR=0.0
91 0 ▶ message => sub { "Must be a negative number" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
92 46 );
93  
94 $meta->add_type(
95 name => 'NegativeOrZeroNum',
96 parent => Num,
97 10 ▶ constraint => sub { $_ <= 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
98 211 ▶ inlined => sub { undef, qq($_ <= 0) },
Subroutine __ANON__: called CC=1 SCAR=0.0
99 0 ▶ message => sub { "Must be a number less than or equal to zero" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
100 1 ▶ type_default => sub { return 0; },
Subroutine __ANON__: called CC=1 SCAR=0.0
101 46 );
102  
103 $meta->add_type(
104 name => 'NegativeInt',
105 parent => Int,
106 8 ▶ constraint => sub { $_ < 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
107 213 ▶ inlined => sub { undef, qq($_ < 0) },
Subroutine __ANON__: called CC=1 SCAR=0.0
108 0 ▶ message => sub { "Must be a negative integer" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
109 46 );
110  
111 $meta->add_type(
112 name => 'NegativeOrZeroInt',
113 parent => Int,
114 8 ▶ constraint => sub { $_ <= 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
115 211 ▶ inlined => sub { undef, qq($_ <= 0) },
Subroutine __ANON__: called CC=1 SCAR=0.0
116 0 ▶ message => sub { "Must be an integer less than or equal to zero" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
117 1 ▶ type_default => sub { return 0; },
Subroutine __ANON__: called CC=1 SCAR=0.0
118 46 );
119  
120 $meta->add_type(
121 name => 'SingleDigit',
122 parent => Int,
123 13 ▶ constraint => sub { $_ >= -9 and $_ <= 9 },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_ >= -9 and $_ <= 9 12 1 13
124 219 ▶ inlined => sub { undef, qq($_ >= -9), qq($_ <= 9) },
Subroutine __ANON__: called CC=1 SCAR=0.0
125 0 ▶ message => sub { "Must be a single digit" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
126 1 ▶ type_default => sub { return 0; },
Subroutine __ANON__: called CC=1 SCAR=0.0
127 46 );
128  
129 46 for my $base ( qw/Num Int/ ) {
130 $meta->add_type(
131 name => "${base}Range",
132 parent => Types::Standard->get_type( $base ),
133 289 ▶ constraint_generator => sub {
Subroutine __ANON__: called CC=10 SCAR=23.1
134 289 ▶ return $meta->get_type( "${base}Range" ) unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 289 289
135
136 289 my $base_obj = Types::Standard->get_type( $base );
137
138 289 Type::Tiny::check_parameter_count_for_parameterized_type( 'Types::Common::Numeric', "${base}Range", \@_, 4 );
139 289 my ( $min, $max, $min_excl, $max_excl ) = @_;
140 289 ▶ !defined( $min )
Branch: 1 branch
BranchTrueFalseTotal
not defined $min or $base_obj->check($min) or &_croak("${base}Range min must be a %s; got %s", lc $base, $min) 282 7 289
Condition66%2/3
not defined $min or $base_obj->check($min)
l!l&&r!l&&!r
02827
MC/DC50%1/2
not defined $min or $base_obj->check($min)
not defined $min $base_obj->check($min)
Truth table66%2/3
not(A) or B
A= not defined $minB= $base_obj->check($min)
ABresult
1X1
011
000
141 or $base_obj->check( $min )
142 or _croak(
143 "${base}Range min must be a %s; got %s", lc( $base ),
144 $min
145 );
146 282 ▶ !defined( $max )
Branch: 1 branch
BranchTrueFalseTotal
not defined $max or $base_obj->check($max) or &_croak("${base}Range max must be a %s; got %s", lc $base, $max) 279 3 282
Condition100%3/3
not defined $max or $base_obj->check($max)
l!l&&r!l&&!r
941853
MC/DC100%2/2
not defined $max or $base_obj->check($max)
not defined $max $base_obj->check($max)
Truth table100%3/3
not(A) or B
A= not defined $maxB= $base_obj->check($max)
ABresult
1X1
011
000
147 or $base_obj->check( $max )
148 or _croak(
149 "${base}Range max must be a %s; got %s", lc( $base ),
150 $max
151 );
152 279 ▶ !defined( $min_excl )
Branch: 1 branch
BranchTrueFalseTotal
not defined $min_excl or &BoolLike()->check($min_excl) or &_croak("${base}Range minexcl must be a boolean; got $min_excl") 277 2 279
Condition100%3/3
not defined $min_excl or &BoolLike()->check($min_excl)
l!l&&r!l&&!r
245322
MC/DC100%2/2
not defined $min_excl or &BoolLike()->check($min_excl)
not defined $min_excl &BoolLike()->check($min_excl)
Truth table100%3/3
not(A) or B
A= not defined $min_exclB= &BoolLike()->check($min_excl)
ABresult
1X1
011
000
153 or BoolLike->check( $min_excl )
154 or _croak( "${base}Range minexcl must be a boolean; got $min_excl" );
155 277 ▶ !defined( $max_excl )
Branch: 1 branch
BranchTrueFalseTotal
not defined $max_excl or &BoolLike()->check($max_excl) or &_croak("${base}Range maxexcl must be a boolean; got $max_excl") 275 2 277
Condition100%3/3
not defined $max_excl or &BoolLike()->check($max_excl)
l!l&&r!l&&!r
245302
MC/DC100%2/2
not defined $max_excl or &BoolLike()->check($max_excl)
not defined $max_excl &BoolLike()->check($max_excl)
Truth table100%3/3
not(A) or B
A= not defined $max_exclB= &BoolLike()->check($max_excl)
ABresult
1X1
011
000
156 or BoolLike->check( $max_excl )
157 or _croak( "${base}Range maxexcl must be a boolean; got $max_excl" );
158
159 # this is complicated so defer to the inline generator
160 275 eval sprintf(
161 'sub { %s }',
162 join ' and ',
163 grep defined,
164 $meta->get_type( "${base}Range" )->inline_generator->( @_ )->( undef, '$_[0]' ),
165 );
166 },
167 550 ▶ inline_generator => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
168 550 my ( $min, $max, $min_excl, $max_excl ) = @_;
169
170 550 ▶ my $gt = $min_excl ? '>' : '>=';
Branch: 1 branch
BranchTrueFalseTotal
$min_excl ? : 32 518 550
171 550 ▶ my $lt = $max_excl ? '<' : '<=';
Branch: 1 branch
BranchTrueFalseTotal
$max_excl ? : 32 518 550
172
173 1240 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
174 1240 my $v = $_[1];
175 1240 my @code = ( undef ); # parent constraint
176 1240 push @code, "$v $gt $min";
177 1240 ▶ push @code, "$v $lt $max" if defined $max;
Branch: 1 branch
BranchTrueFalseTotal
if defined $max 824 416 1240
178 1240 return @code;
179 550 };
180 },
181 0 ▶ deep_explanation => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
182 0 my ( $type, $value, $varname ) = @_;
183 0 ▶ my ( $min, $max, $min_excl, $max_excl ) = @{ $type->parameters || [] };
Condition0%0/2
$type->parameters || []
l!l
00
MC/DC0%0/1
$type->parameters || []
$type->parameters
Truth table0%0/2
A
A= $type->parameters
Aresult
11
00
184 0 my @whines;
185 0 ▶ if ( defined $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max) { } 0 0 0
186 0 ▶ push @whines, sprintf(
Branch: 2 branches
BranchTrueFalseTotal
$min_excl ? : 0 0 0
$max_excl ? : 0 0 0
187 '"%s" expects %s to be %s %d and %s %d',
188 $type,
189 $varname,
190 $min_excl ? 'greater than' : 'at least',
191 $min,
192 $max_excl ? 'less than' : 'at most',
193 $max,
194 );
195 } #/ if ( defined $max )
196 else {
197 0 ▶ push @whines, sprintf(
Branch: 1 branch
BranchTrueFalseTotal
$min_excl ? : 0 0 0
198 '"%s" expects %s to be %s %d',
199 $type,
200 $varname,
201 $min_excl ? 'greater than' : 'at least',
202 $min,
203 );
204 }
205 0 push @whines, sprintf(
206 "%s is %s",
207 $varname,
208 $value,
209 );
210 0 return \@whines;
211 },
212 92 );
213 } #/ for my $base ( qw/Num Int/)
214  
215 46 __PACKAGE__->meta->make_immutable;
216  
217 46 1;
218  
219 __END__
« blib/lib/Type/Tiny/Enum.pm ↑ Summary blib/lib/Type/Params/Alternatives.pm »