blib/lib/Types/Common/String.pm

filter: Statementstmt 86.6% 123 / 142 Branchbran 63.4% 33 / 52 Conditioncond 45.0% 9 / 20 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 78.4% 40 / 51 Podpod n/a totaltotal 74.7% 207 / 277 CC 35 SCAR 12.9
CC
35
Cov
77%
__ANON__
12.0
__ANON__
9.1
__ANON__
6.2
CRAP 3.6
« blib/lib/Type/Tiny/Role.pm ↑ Summary blib/lib/Type/Tie.pm »
1 package Types::Common::String;
2  
3 43 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 43 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 43 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 43 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 43 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 43 $Types::Common::String::AUTHORITY = 'cpan:TOBYINK';
10 43 $Types::Common::String::VERSION = '2.010001';
11 }
12  
13 43 $Types::Common::String::VERSION =~ tr/_//d;
14  
15 43 ▶use Type::Library -base, -declare => qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
16 SimpleStr
17 NonEmptySimpleStr
18 NumericCode
19 LowerCaseSimpleStr
20 UpperCaseSimpleStr
21 Password
22 StrongPassword
23 NonEmptyStr
24 LowerCaseStr
25 UpperCaseStr
26 StrLength
27 DelimitedStr
28 43 );
29  
30 43 ▶use Type::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 43 ▶use Types::TypeTiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
32 43 ▶use Types::Standard qw( Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 43 my $meta = __PACKAGE__->meta;
35  
36 $meta->add_type(
37 name => SimpleStr,
38 parent => Str,
39 144 ▶ constraint => sub { length( $_ ) <= 255 and not /\n/ },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
length $_ <= 255 and not /\n/ 139 5 144
40 1743 ▶ inlined => sub { undef, qq(length($_) <= 255), qq($_ !~ /\\n/) },
Subroutine __ANON__: called CC=1 SCAR=0.0
41 0 ▶ message => sub { "Must be a single line of no more than 255 chars" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 1 ▶ type_default => sub { return ''; },
Subroutine __ANON__: called CC=1 SCAR=0.0
43 43 );
44  
45 $meta->add_type(
46 name => NonEmptySimpleStr,
47 parent => SimpleStr,
48 117 ▶ constraint => sub { length( $_ ) > 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
49 886 ▶ inlined => sub { undef, qq(length($_) > 0) },
Subroutine __ANON__: called CC=1 SCAR=0.0
50 0 ▶ message => sub { "Must be a non-empty single line of no more than 255 chars" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
51 43 );
52  
53 $meta->add_type(
54 name => NumericCode,
55 parent => NonEmptySimpleStr,
56 13 ▶ constraint => sub { /^[0-9]+$/ },
Subroutine __ANON__: called CC=1 SCAR=0.0
57 246 ▶ inlined => sub { SimpleStr->inline_check( $_ ), qq($_ =~ m/^[0-9]+\$/) },
Subroutine __ANON__: called CC=1 SCAR=0.0
58 0 ▶ message => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 0 'Must be a non-empty single line of no more than 255 chars that consists '
60 . 'of numeric characters only';
61 },
62 43 );
63  
64 43 NumericCode->coercion->add_type_coercions(
65 NonEmptySimpleStr,
66 q[ do { (my $code = $_) =~ s/[[:punct:][:space:]]//g; $code } ],
67 );
68  
69 $meta->add_type(
70 name => Password,
71 parent => NonEmptySimpleStr,
72 24 ▶ constraint => sub { length( $_ ) > 3 },
Subroutine __ANON__: called CC=1 SCAR=0.0
73 203 ▶ inlined => sub { SimpleStr->inline_check( $_ ), qq(length($_) > 3) },
Subroutine __ANON__: called CC=1 SCAR=0.0
74 0 ▶ message => sub { "Must be between 4 and 255 chars" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
75 43 );
76  
77 $meta->add_type(
78 name => StrongPassword,
79 parent => Password,
80 6 ▶ constraint => sub { length( $_ ) > 7 and /[^a-zA-Z]/ },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
length $_ > 7 and /[^a-zA-Z]/ 3 3 6
81 203 ▶ inlined => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
82 203 SimpleStr()->inline_check( $_ ), qq(length($_) > 7), qq($_ =~ /[^a-zA-Z]/);
83 },
84 0 ▶ message => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
85 0 "Must be between 8 and 255 chars, and contain a non-alpha char";
86 },
87 43 );
88  
89 43 my ( $nestr );
90 43 if ( Type::Tiny::_USE_XS ) {
91 43 $nestr = Type::Tiny::XS::get_coderef_for( 'NonEmptyStr' );
92 }
93  
94 $meta->add_type(
95 name => NonEmptyStr,
96 parent => Str,
97 75 ▶ constraint => sub { length( $_ ) > 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
98 792 ▶ inlined => sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
99 792 ▶ if ( $nestr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nestr) 792 0 792
100 792 my $xsub = Type::Tiny::XS::get_subname_for( $_[0]->name );
101 792 ▶ return "$xsub($_[1])" if $xsub && !$Type::Tiny::AvoidCallbacks;
Branch: 1 branch
BranchTrueFalseTotal
if $xsub and not $Type::Tiny::AvoidCallbacks 591 201 792
Condition66%2/3
$xsub and not $Type::Tiny::AvoidCallbacks
!ll&&!rl&&r
0201591
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
102 }
103 201 undef, qq(length($_) > 0);
104 },
105 0 ▶ message => sub { "Must not be empty" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
106 43 ▶ $nestr ? ( compiled_type_constraint => $nestr ) : (),
Branch: 1 branch
BranchTrueFalseTotal
$nestr ? : 43 0 43
107 );
108  
109 $meta->add_type(
110 name => LowerCaseStr,
111 parent => NonEmptyStr,
112 26 ▶ constraint => sub { !/\p{Upper}/ms },
Subroutine __ANON__: called CC=1 SCAR=0.0
113 272 ▶ inlined => sub { undef, qq($_ !~ /\\p{Upper}/ms) },
Subroutine __ANON__: called CC=1 SCAR=0.0
114 0 ▶ message => sub { "Must not contain upper case letters" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
115 43 );
116  
117 43 LowerCaseStr->coercion->add_type_coercions(
118 NonEmptyStr, q[ lc($_) ],
119 );
120  
121 $meta->add_type(
122 name => UpperCaseStr,
123 parent => NonEmptyStr,
124 26 ▶ constraint => sub { !/\p{Lower}/ms },
Subroutine __ANON__: called CC=1 SCAR=0.0
125 272 ▶ inlined => sub { undef, qq($_ !~ /\\p{Lower}/ms) },
Subroutine __ANON__: called CC=1 SCAR=0.0
126 0 ▶ message => sub { "Must not contain lower case letters" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
127 43 );
128  
129 43 UpperCaseStr->coercion->add_type_coercions(
130 NonEmptyStr, q[ uc($_) ],
131 );
132  
133 $meta->add_type(
134 name => LowerCaseSimpleStr,
135 parent => NonEmptySimpleStr,
136 26 ▶ constraint => sub { !/\p{Upper}/ms },
Subroutine __ANON__: called CC=1 SCAR=0.0
137 278 ▶ inlined => sub { undef, qq($_ !~ /\\p{Upper}/ms) },
Subroutine __ANON__: called CC=1 SCAR=0.0
138 0 ▶ message => sub { "Must not contain upper case letters" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
139 43 );
140  
141 43 LowerCaseSimpleStr->coercion->add_type_coercions(
142 NonEmptySimpleStr, q[ lc($_) ],
143 );
144  
145 $meta->add_type(
146 name => UpperCaseSimpleStr,
147 parent => NonEmptySimpleStr,
148 26 ▶ constraint => sub { !/\p{Lower}/ms },
Subroutine __ANON__: called CC=1 SCAR=0.0
149 278 ▶ inlined => sub { undef, qq($_ !~ /\\p{Lower}/ms) },
Subroutine __ANON__: called CC=1 SCAR=0.0
150 0 ▶ message => sub { "Must not contain lower case letters" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
151 43 );
152  
153 43 UpperCaseSimpleStr->coercion->add_type_coercions(
154 NonEmptySimpleStr, q[ uc($_) ],
155 );
156  
157 $meta->add_type(
158 name => StrLength,
159 parent => Str,
160 5 ▶ constraint_generator => sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
161 5 ▶ return $meta->get_type( 'StrLength' ) unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 5 5
162
163 5 Type::Tiny::check_parameter_count_for_parameterized_type( 'Types::Common::String', "StrLength", \@_, 2 );
164 5 my ( $min, $max ) = @_;
165 Types::Standard::is_Int( $_ )
166 || Types::Standard::_croak(
167 "Parameters for StrLength[`min, `max] expected to be integers; got $_" )
168 5 ▶ for @_;
Branch: 1 branch
BranchTrueFalseTotal
Types::Standard::is_Int($_) or &Types::Standard::_croak("Parameters for StrLength[`min, `max] expected to be integers; got $_") 8 0 8
169
170 5 ▶ if ( defined $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max) { } 3 2 5
171 3 ▶ return sub { length( $_[0] ) >= $min and length( $_[0] ) <= $max };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
length $_[0] >= $min and length $_[0] <= $max 17 6 23
172 }
173 else {
174 2 ▶ return sub { length( $_[0] ) >= $min };
Subroutine __ANON__: called CC=1 SCAR=0.0
175 }
176 },
177 5 ▶ inline_generator => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
178 5 my ( $min, $max ) = @_;
179
180 125 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
181 125 my $v = $_[1];
182 125 my @code = ( undef ); # parent constraint
183 125 push @code, "length($v) >= $min";
184 125 ▶ push @code, "length($v) <= $max" if defined $max;
Branch: 1 branch
BranchTrueFalseTotal
if defined $max 73 52 125
185 125 return @code;
186 5 };
187 },
188 0 ▶ deep_explanation => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
189 0 my ( $type, $value, $varname ) = @_;
190 0 ▶ my ( $min, $max ) = @{ $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
191 0 my @whines;
192 0 ▶ if ( defined $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max) { } 0 0 0
193 0 push @whines, sprintf(
194 '"%s" expects length(%s) to be between %d and %d',
195 $type,
196 $varname,
197 $min,
198 $max,
199 );
200 }
201 else {
202 0 push @whines, sprintf(
203 '"%s" expects length(%s) to be at least %d',
204 $type,
205 $varname,
206 $min,
207 );
208 }
209 0 push @whines, sprintf(
210 "length(%s) is %d",
211 $varname,
212 length( $value ),
213 );
214 0 return \@whines;
215 },
216 43 );
217  
218 $meta->add_type(
219 name => DelimitedStr,
220 parent => Str,
221 type_default => undef,
222 1 ▶ constraint_generator => sub {
Subroutine __ANON__: called CC=5 SCAR=16.6
223 1 ▶ return $meta->get_type( 'DelimitedStr' ) unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 1 1
224
225 1 Type::Tiny::check_parameter_count_for_parameterized_type( 'Types::Common::String', "DelimitedStr", \@_, 5 );
226 1 my ( $delimiter, $part_constraint, $min_parts, $max_parts, $ws ) = @_;
227
228 1 Types::Standard::assert_Str( $delimiter );
229 1 ▶ Types::TypeTiny::assert_TypeTiny( $part_constraint )
Branch: 1 branch
BranchTrueFalseTotal
if defined $part_constraint 1 0 1
230 if defined $part_constraint;
231 1 ▶ $min_parts ||= 0;
Condition50%1/2
$min_parts ||= 0
l!l
10
MC/DC0%0/1
$min_parts ||= 0
$min_parts
Truth table50%1/2
A
A= $min_parts
Aresult
11
00
232 1 ▶ my $q_delimiter = $ws
Branch: 1 branch
BranchTrueFalseTotal
$ws ? : 1 0 1
233 ? sprintf( '\s*%s\s*', quotemeta( $delimiter ) )
234 : quotemeta( $delimiter );
235
236 7 ▶ return sub {
Subroutine __ANON__: called CC=6 SCAR=18.2
237 my @split = $ws
238 7 ▶ ? split( $q_delimiter, do { ( my $trimmed = $_[0] ) =~ s{\A\s+|\s+\z}{}g; $trimmed } )
Branch: 1 branch
BranchTrueFalseTotal
$ws ? : 7 0 7
239 : split( $q_delimiter, $_[0] );
240 7 ▶ return if @split < $min_parts;
Branch: 1 branch
BranchTrueFalseTotal
if @split < $min_parts 1 6 7
241 6 ▶ return if defined($max_parts) && ( @split > $max_parts );
Branch: 1 branch
BranchTrueFalseTotal
if defined $max_parts and @split > $max_parts 1 5 6
Condition66%2/3
defined $max_parts and @split > $max_parts
!ll&&!rl&&r
051
MC/DC50%1/2
defined $max_parts and @split > $max_parts
defined $max_parts @split > $max_parts
Truth table66%2/3
A and B
A= defined $max_partsB= @split > $max_parts
ABresult
0X0
100
111
242 5 ▶ !$part_constraint or $part_constraint->all( @split );
Branch: 1 branch
BranchTrueFalseTotal
not $part_constraint or $part_constraint->all(@split) 0 5 5
243 1 };
244 },
245 1 ▶ inline_generator => sub {
Subroutine __ANON__: called CC=3 SCAR=11.9
246 1 my ( $delimiter, $part_constraint, $min_parts, $max_parts, $ws ) = @_;
247 1 ▶ $min_parts ||= 0;
Condition50%1/2
$min_parts ||= 0
l!l
10
MC/DC0%0/1
$min_parts ||= 0
$min_parts
Truth table50%1/2
A
A= $min_parts
Aresult
11
00
248 1 ▶ my $q_delimiter = $ws
Branch: 1 branch
BranchTrueFalseTotal
$ws ? : 1 0 1
249 ? sprintf( '\s*%s\s*', quotemeta( $delimiter ) )
250 : quotemeta( $delimiter );
251
252 28 ▶ return sub {
Subroutine __ANON__: called CC=7 SCAR=22.1
253 28 my $v = $_[1];
254 28 my @cond;
255 28 ▶ push @cond, "\@\$split >= $min_parts" if $min_parts > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $min_parts > 0 28 0 28
256 28 ▶ push @cond, "\@\$split <= $max_parts" if defined $max_parts;
Branch: 1 branch
BranchTrueFalseTotal
if defined $max_parts 28 0 28
257 push @cond, Types::Standard::ArrayRef->of( $part_constraint )->inline_check( '$split' )
258 28 ▶ if $part_constraint && $part_constraint->{uniq} != Types::Standard::Any->{uniq};
Branch: 1 branch
BranchTrueFalseTotal
if $part_constraint and $part_constraint->{"uniq"} != &Types::Standard::Any()->{"uniq"} 28 0 28
Condition33%1/3
$part_constraint and $part_constraint->{"uniq"} != &Types::Standard::Any()->{"uniq"}
!ll&&!rl&&r
0028
MC/DC0%0/2
$part_constraint and $part_constraint->{"uniq"} != &Types::Standard::Any()->{"uniq"}
$part_constraint $part_constraint->{"uniq"} != &Types::Standard::Any()->{"uniq"}
Truth table33%1/3
A and B
A= $part_constraintB= $part_constraint->{"uniq"} != &Types::Standard::Any()->{"uniq"}
ABresult
0X0
100
111
259 28 ▶ return ( undef ) if ! @cond;
Branch: 1 branch
BranchTrueFalseTotal
unless @cond 0 28 28
260 return (
261 undef,
262 28 ▶ sprintf(
Branch: 1 branch
BranchTrueFalseTotal
$ws ? : 28 0 28
263 'do { my $split = [ split %s, %s ]; %s }',
264 B::perlstring( $q_delimiter ),
265 $ws ? sprintf( 'do { ( my $trimmed = %s ) =~ s{\A\s+|\s+\z}{}g; $trimmed }', $v ) : $v,
266 join( q{ and }, @cond ),
267 ),
268 );
269 1 };
270 },
271 1 ▶ coercion_generator => sub {
Subroutine __ANON__: called CC=5 SCAR=17.3
272 1 my ( $parent, $self, $delimiter, $part_constraint, $min_parts, $max_parts ) = @_;
273 1 ▶ return unless $delimiter;
Branch: 1 branch
BranchTrueFalseTotal
unless $delimiter 0 1 1
274 1 ▶ $part_constraint ||= Types::Standard::Str;
Condition33%1/3
$part_constraint ||= &Types::Standard::Str()
l!l&&r!l&&!r
100
MC/DC0%0/2
$part_constraint ||= &Types::Standard::Str()
$part_constraint &Types::Standard::Str()
Truth table33%1/3
A ||= B
A= $part_constraintB= &Types::Standard::Str()
ABresult
1X1
011
000
275 1 ▶ $min_parts ||= 0;
Condition50%1/2
$min_parts ||= 0
l!l
10
MC/DC0%0/1
$min_parts ||= 0
$min_parts
Truth table50%1/2
A
A= $min_parts
Aresult
11
00
276
277 1 require Type::Coercion;
278 1 my $c = 'Type::Coercion'->new( type_constraint => $self );
279 1 ▶ $c->add_type_coercions(
Branch: 1 branch
BranchTrueFalseTotal
defined $max_parts ? : 1 0 1
280 Types::Standard::ArrayRef->of(
281 $part_constraint,
282 $min_parts,
283 defined $max_parts ? $max_parts : (),
284 ),
285 sprintf( 'join( %s, @$_ )', B::perlstring( $delimiter ) ),
286 );
287 1 return $c;
288 },
289 43 );
290  
291 43 DelimitedStr->coercion->add_type_coercions(
292 Types::Standard::ArrayRef->of( Types::Standard::Str ),
293 'join( $", @$_ )',
294 );
295  
296 43 __PACKAGE__->meta->make_immutable;
297  
298 43 1;
299  
300 __END__
« blib/lib/Type/Tiny/Role.pm ↑ Summary blib/lib/Type/Tie.pm »