lib/Data/Validation/Constraints.pm

filter: Statementstmt 100% 113 / 114 (1 uncoverable) Branchbran 82.6% 43 / 52 Conditioncond 64.2% 27 / 42 MC/DCmcdc 29.6% 8 / 27 Subroutinesub 100% 35 / 35 Podpod 100% 18 / 18 totaltotal 85.0% 244 / 288 (1 uncoverable) CC 42 SCAR 13.7
CC
42
Cov
88%
isValidTime
9.1
__ANON__
6.9
isBetweenValues
5.6
CRAP 3.9
« lib/Data/Validation.pm ↑ Summary lib/Data/Validation/Constraints/Password.pm »
1 package Data::Validation::Constraints;
2  
3 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use charnames qw( :full );
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Data::Validation::Constants qw( EXCEPTION_CLASS FALSE HASH TRUE );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Data::Validation::Utils qw( ensure_class_loaded load_class throw );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use List::Util qw( any );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Scalar::Util qw( looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Unexpected::Functions qw( KnownType );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Unexpected::Types qw( Any ArrayRef Bool Int Object Str Undef );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # Public attributes
16 93 ▶has 'allowed' => is => 'ro', iss => ArrayRef, builder => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
17  
18 1 has 'max_length' => is => 'ro', isa => Int;
19  
20 1 has 'max_value' => is => 'ro', isa => Int;
21  
22 1 has 'method' => is => 'ro', isa => Str, required => TRUE;
23  
24 1 has 'min_length' => is => 'ro', isa => Int;
25  
26 1 has 'min_value' => is => 'ro', isa => Int;
27  
28 1 has 'pattern' => is => 'ro', isa => Str;
29  
30 1 has 'required' => is => 'ro', isa => Bool, default => FALSE;
31  
32 1 has 'type' => is => 'ro', isa => Str | Undef;
33  
34 has 'type_libraries' => is => 'ro', isa => ArrayRef[Str],
35 110 ▶ builder => sub { [ 'Unexpected::Types' ] };
Subroutine __ANON__: called CC=1 SCAR=0.0
36  
37 5 ▶has 'type_registry' => is => 'lazy', isa => Object, builder => sub {
Subroutine _build_type_registry: called CC=2 SCAR=6.9
38 5 my $self = shift; ensure_class_loaded 'Type::Registry';
39 5 my $reg = Type::Registry->for_me;
40  
41 5 $reg->add_types( $_ ) for (@{ $self->type_libraries });
42  
43 5 return $reg;
44 1 };
45  
46 1 has 'value' => is => 'ro', isa => Any;
47  
48 # Public methods
49 112 ▶sub new_from_method {
Subroutine new_from_method: called CC=2 SCAR=6.9
Pod documented
50 112 my ($class, $attr) = @_;
51  
52 112 ▶ $class->can( $attr->{method} ) and return $class->new( $attr );
Branch: 1 branch
BranchTrueFalseTotal
$class->can($attr->{"method"}) and return $class->new($attr) 79 33 112
53  
54 33 return (load_class $class, 'isValid', $attr->{method})->new( $attr );
55 }
56  
57 79 ▶sub validate {
Subroutine validate: called CC=1 SCAR=0.0
Pod documented
58 79 my ($self, $v) = @_; my $method = $self->method; return $self->$method( $v );
59 }
60  
61 79 ▶around 'validate' => sub {
Subroutine __ANON__: called CC=6 SCAR=19.3
62 79 my ($orig, $self, $v) = @_;
63  
64 79 ▶ not defined $v and $self->required and return FALSE;
Branch: 1 branch
BranchTrueFalseTotal
not defined $v and $self->required and return 0 0 79 79
Condition66%2/3
not defined $v and $self->required
!ll&&!rl&&r
7720
MC/DC0%0/2
not defined $v and $self->required
not defined $v $self->required
Truth table66%2/3
not(A) and B
A= not defined $vB= $self->required
ABresult
0X0
100
111
65  
66 79 ▶ not defined $v and not $self->required and $self->method ne 'isMandatory'
Branch: 1 branch
BranchTrueFalseTotal
not defined $v || $self->required and $self->method ne "isMandatory" and return 1 0 79 79
Condition66%4/6
not defined $v || $self->required and $self->method ne "isMandatory"
!ll&&!rl&&r
7720
defined $v || $self->required
l!l&&r!l&&!r
7702
MC/DC0%0/3
not defined $v || $self->required and $self->method ne "isMandatory"
defined $v $self->required $self->method ne "isMandatory"
Truth table50%2/4
not(A || B) and C
A= defined $vB= $self->requiredC= $self->method ne "isMandatory"
ABCresult
1XX0
01X0
0000
0011
67 and return TRUE;
68  
69 79 return $orig->( $self, $v );
70 1 };
71  
72 # Builtin factory validation methods
73 2 ▶sub isAllowed {
Subroutine isAllowed: called CC=2 SCAR=6.9
Pod documented
74 2 my ($self, $v) = @_;
75  
76 2 ▶ return (any { $_ eq $v } @{ $self->allowed }) ? TRUE : FALSE;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
&any(sub { $_ eq $v; } , @{$self->allowed;}) ? : 1 1 2
77 }
78  
79 2 ▶sub isBetweenValues {
Subroutine isBetweenValues: called CC=5 SCAR=17.2
Pod documented
80 2 my ($self, $v) = @_;
81  
82 2 ▶ defined $self->min_value and $v < $self->min_value and return FALSE;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->min_value and $v < $self->min_value and return 0 0 2 2
Condition33%1/3
defined $self->min_value and $v < $self->min_value
!ll&&!rl&&r
020
MC/DC0%0/2
defined $self->min_value and $v < $self->min_value
defined $self->min_value $v < $self->min_value
Truth table33%1/3
A and B
A= defined $self->min_valueB= $v < $self->min_value
ABresult
0X0
100
111
83 2 ▶ defined $self->max_value and $v > $self->max_value and return FALSE;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->max_value and $v > $self->max_value and return 0 1 1 2
Condition66%2/3
defined $self->max_value and $v > $self->max_value
!ll&&!rl&&r
011
MC/DC50%1/2
defined $self->max_value and $v > $self->max_value
defined $self->max_value $v > $self->max_value
Truth table66%2/3
A and B
A= defined $self->max_valueB= $v > $self->max_value
ABresult
0X0
100
111
84 1 return TRUE;
85 }
86  
87 3 ▶sub isEqualTo {
Subroutine isEqualTo: called CC=5 SCAR=16.3
Pod documented
88 3 my ($self, $v) = @_;
89  
90 3 ▶ $self->isValidNumber( $v ) and $self->isValidNumber( $self->value )
Branch: 2 branches
BranchTrueFalseTotal
$self->isValidNumber($v) and $self->isValidNumber($self->value) and return $v == $self->value ? 1 : 0 2 1 3
$v == $self->value ? : 1 1 2
Condition66%2/3
$self->isValidNumber($v) and $self->isValidNumber($self->value)
!ll&&!rl&&r
102
MC/DC50%1/2
$self->isValidNumber($v) and $self->isValidNumber($self->value)
$self->isValidNumber($v) $self->isValidNumber($self->value)
Truth table66%2/3
A and B
A= $self->isValidNumber($v)B= $self->isValidNumber($self->value)
ABresult
0X0
100
111
91 and return $v == $self->value ? TRUE : FALSE;
92  
93 1 ▶ return $v eq $self->value ? TRUE : FALSE;
Branch: 1 branch
BranchTrueFalseTotal
$v eq $self->value ? : 1 0 1
94 }
95  
96 6 ▶sub isHexadecimal {
Subroutine isHexadecimal: called CC=1 SCAR=0.0
Pod documented
97 6 my ($self, $v) = @_;
98  
99 6 my $pat = '\A (?:(?i)(?:[-+]?)(?:(?=[.]?[0123456789ABCDEF])'
100 . '(?:[0123456789ABCDEF]*)(?:(?:[.])(?:[0123456789ABCDEF]{0,}))?)'
101 . '(?:(?:[G])(?:(?:[-+]?)(?:[0123456789ABCDEF]+))|)) \z';
102  
103 6 return $self->isMatchingRegex( $v, $pat );
104 }
105  
106 3 ▶sub isMandatory {
Subroutine isMandatory: called CC=3 SCAR=11.1
Pod documented
107 3 ▶ return defined $_[ 1 ] && length $_[ 1 ] ? TRUE : FALSE;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] && length $_[1] ? : 1 2 3
Condition66%2/3
defined $_[1] and length $_[1]
!ll&&!rl&&r
201
MC/DC50%1/2
defined $_[1] and length $_[1]
defined $_[1] length $_[1]
Truth table66%2/3
A and B
A= defined $_[1]B= length $_[1]
ABresult
0X0
100
111
108 }
109  
110 36 ▶sub isMatchingRegex {
Subroutine isMatchingRegex: called CC=4 SCAR=14.1
Pod documented
111 36 my ($self, $v, $pat) = @_;
112  
113 36 ▶ $pat //= $self->pattern; defined $pat or return FALSE;
Branch: 1 branch
BranchTrueFalseTotal
defined $pat or return 0 36 0 36
Condition66%2/3
$pat //= $self->pattern
l!l&&r!l&&!r
24120
MC/DC0%0/2
$pat //= $self->pattern
$pat $self->pattern
Truth table66%2/3
A //= B
A= $patB= $self->pattern
ABresult
1X1
011
000
114  
115 36 ▶ return $v =~ m{ $pat }msx ? TRUE : FALSE;
Branch: 1 branch
BranchTrueFalseTotal
$v =~ / $pat /msx ? : 25 11 36
116 }
117  
118 5 ▶sub isMatchingType {
Subroutine isMatchingType: called CC=4 SCAR=14.3
Pod documented
119 5 my ($self, $v, $type_name) = @_; my $type;
120  
121 5 ▶ $type_name //= $self->type; defined $type_name or return FALSE;
Branch: 1 branch
BranchTrueFalseTotal
defined $type_name or return 0 5 0 5
Condition33%1/3
$type_name //= $self->type
l!l&&r!l&&!r
050
MC/DC0%0/2
$type_name //= $self->type
$type_name $self->type
Truth table33%1/3
A //= B
A= $type_nameB= $self->type
ABresult
1X1
011
000
122  
123 5 ▶ try { $type = $self->type_registry->lookup( $type_name ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
124 1 ▶ catch {
Subroutine __ANON__: called CC=2 SCAR=7.2
125 1 ▶ $_ =~ m{ \Qnot a known type constraint\E }mx
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ / not\ a\ known\ type\ constraint /mx and &throw(KnownType(), [$type_name]) 1 0 1
126 and throw KnownType, [ $type_name ];
127 0 throw "${_}"; # uncoverable statement
128 5 };
129  
130 4 ▶ return $type->check( $v ) ? TRUE : FALSE;
Branch: 1 branch
BranchTrueFalseTotal
$type->check($v) ? : 2 2 4
131 }
132  
133 2 ▶sub isPrintable {
Subroutine isPrintable: called CC=1 SCAR=0.0
Pod documented
134 2 return $_[ 0 ]->isMatchingRegex( $_[ 1 ], '\A \p{IsPrint}+ \z' );
135 }
136  
137 2 ▶sub isSimpleText {
Subroutine isSimpleText: called CC=1 SCAR=0.0
Pod documented
138 2 return $_[ 0 ]->isMatchingRegex( $_[ 1 ], '\A [a-zA-Z0-9_ \-\.]+ \z' );
139 }
140  
141 14 ▶sub isValidHostname {
Subroutine isValidHostname: called CC=2 SCAR=6.9
Pod documented
142 14 ▶ return (gethostbyname $_[ 1 ])[ 0 ] ? TRUE : FALSE;
Branch: 1 branch
BranchTrueFalseTotal
(gethostbyname $_[1])[0] ? : 6 8 14
143 }
144  
145 2 ▶sub isValidIdentifier {
Subroutine isValidIdentifier: called CC=1 SCAR=0.0
Pod documented
146 2 return $_[ 0 ]->isMatchingRegex( $_[ 1 ], '\A [a-zA-Z_] \w* \z' );
147 }
148  
149 4 ▶sub isValidInteger {
Subroutine isValidInteger: called CC=3 SCAR=11.0
Pod documented
150 4 my ($self, $v) = @_;
151  
152 4 my $pat = '\A (?:(?:[-+]?)(?:[0123456789]{1,3}(?:[_]?[0123456789]{3})*)) \z';
153  
154 4 ▶ $self->isMatchingRegex( $v, $pat ) or return FALSE;
Branch: 1 branch
BranchTrueFalseTotal
$self->isMatchingRegex($v, $pat) or return 0 3 1 4
155 3 ▶ int $v == $v or return FALSE;
Branch: 1 branch
BranchTrueFalseTotal
int $v == $v or return 0 3 0 3
156 3 return TRUE;
157 }
158  
159 8 ▶sub isValidLength {
Subroutine isValidLength: called CC=5 SCAR=16.2
Pod documented
160 8 my ($self, $v) = @_;
161  
162 8 ▶ defined $self->min_length and length $v < $self->min_length and return FALSE;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->min_length and length $v < $self->min_length and return 0 0 8 8
Condition66%2/3
defined $self->min_length and length $v < $self->min_length
!ll&&!rl&&r
530
MC/DC0%0/2
defined $self->min_length and length $v < $self->min_length
defined $self->min_length length $v < $self->min_length
Truth table66%2/3
A and B
A= defined $self->min_lengthB= length $v < $self->min_length
ABresult
0X0
100
111
163 8 ▶ defined $self->max_length and length $v > $self->max_length and return FALSE;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->max_length and length $v > $self->max_length and return 0 2 6 8
Condition100%3/3
defined $self->max_length and length $v > $self->max_length
!ll&&!rl&&r
512
MC/DC100%2/2
defined $self->max_length and length $v > $self->max_length
defined $self->max_length length $v > $self->max_length
Truth table100%3/3
A and B
A= defined $self->max_lengthB= length $v > $self->max_length
ABresult
0X0
100
111
164 6 return TRUE;
165 }
166  
167 11 ▶sub isValidNumber {
Subroutine isValidNumber: called CC=2 SCAR=6.9
Pod documented
168 11 ▶ my ($self, $v) = @_; return looks_like_number( $v ) ? TRUE : FALSE;
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($v) ? : 8 3 11
169 }
170  
171 2 ▶sub isValidText {
Subroutine isValidText: called CC=1 SCAR=0.0
Pod documented
172 2 return $_[ 0 ]->isMatchingRegex( $_[ 1 ],
173 '\A [\t\n !\"#%&\'\(\)\*\+\,\-\./0-9:;=\?@A-Z\[\]_a-z\|\~]+ \z' );
174 }
175  
176 6 ▶sub isValidTime {
Subroutine isValidTime: called CC=9 SCAR=22.1
Pod documented
177 6 my ($self, $v) = @_; my $pat = '\A (\d\d ): (\d\d) (?: : (\d\d) )? \z';
178  
179 6 ▶ $self->isMatchingRegex( $v, $pat ) or return FALSE;
Branch: 1 branch
BranchTrueFalseTotal
$self->isMatchingRegex($v, $pat) or return 0 5 1 6
180  
181 5 my ($hours, $minutes, $seconds) = $v =~ m{ $pat }msx;
182  
183 5 ▶ ($hours >= 0 and $hours <= 23) or return FALSE;
Branch: 1 branch
BranchTrueFalseTotal
$hours >= 0 and $hours <= 23 or return 0 4 1 5
Condition66%2/3
$hours >= 0 and $hours <= 23
!ll&&!rl&&r
014
MC/DC50%1/2
$hours >= 0 and $hours <= 23
$hours >= 0 $hours <= 23
Truth table66%2/3
A and B
A= $hours >= 0B= $hours <= 23
ABresult
0X0
100
111
184 4 ▶ ($minutes >= 0 and $minutes <= 59) or return FALSE;
Branch: 1 branch
BranchTrueFalseTotal
$minutes >= 0 and $minutes <= 59 or return 0 3 1 4
Condition66%2/3
$minutes >= 0 and $minutes <= 59
!ll&&!rl&&r
013
MC/DC50%1/2
$minutes >= 0 and $minutes <= 59
$minutes >= 0 $minutes <= 59
Truth table66%2/3
A and B
A= $minutes >= 0B= $minutes <= 59
ABresult
0X0
100
111
185  
186 3 ▶ defined $seconds or return TRUE;
Branch: 1 branch
BranchTrueFalseTotal
defined $seconds or return 1 2 1 3
187  
188 2 ▶ return ($seconds >= 0 && $seconds <= 59) ? TRUE : FALSE;
Branch: 1 branch
BranchTrueFalseTotal
$seconds >= 0 && $seconds <= 59 ? : 1 1 2
Condition66%2/3
$seconds >= 0 and $seconds <= 59
!ll&&!rl&&r
011
MC/DC50%1/2
$seconds >= 0 and $seconds <= 59
$seconds >= 0 $seconds <= 59
Truth table66%2/3
A and B
A= $seconds >= 0B= $seconds <= 59
ABresult
0X0
100
111
189 }
190  
191 1 1;
192  
193 __END__
« lib/Data/Validation.pm ↑ Summary lib/Data/Validation/Constraints/Password.pm »