blib/lib/PGXN/Meta/Validator.pm

filter: Statementstmt 83.0% 211 / 254 Branchbran 67.3% 97 / 144 Conditioncond 58.8% 40 / 68 MC/DCmcdc 44.1% 19 / 43 Subroutinesub 86.4% 32 / 37 Podpod 100% 27 / 27 totaltotal 74.3% 426 / 573 CC 100 SCAR 24.0
CC
100
Cov
75%
urlspec
42.0
_list_value
33.0
string_or_undef
20.0
CRAP 11.0
↑ Summary
1 package PGXN::Meta::Validator;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use SemVer;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 our $VERSION = v0.16.0;
10  
11 =head1 Name
12  
13 PGXN::Meta::Validator - Validate PGXN distribution metadata structures
14  
15 =head1 Synopsis
16  
17 my $struct = decode_json_file('META.json');
18  
19 my $pmv = PGXN::Meta::Validator->new( $struct );
20  
21 unless ( $pmv->is_valid ) {
22 my $msg = "Invalid META structure. Errors found:\n";
23 $msg .= join( "\n", $pmv->errors );
24 die $msg;
25 }
26  
27 =head1 Description
28  
29 This module validates a PGXN Meta structure against the version of the the
30 specification claimed in the C<meta-spec> field of the structure. Currently,
31 there is only v1.0.0.
32  
33 =cut
34  
35 #--------------------------------------------------------------------------#
36 # This code copied and adapted from CPAN::Meta::Valicator by
37 # David Golden <dagolden@cpan.org> and Ricardo Signes <rjbs@cpan.org>,
38 # which in turn adapted and copied it from Test::CPAN::Meta
39 # by Barbie, <barbie@cpan.org> for Miss Barbell Productions,
40 # L<http://www.missbarbell.co.uk>
41 #--------------------------------------------------------------------------#
42  
43 #--------------------------------------------------------------------------#
44 # Specification Definitions
45 #--------------------------------------------------------------------------#
46  
47 2 my %known_specs = (
48 '1.0.0' => 'http://pgxn.org/spec/1.0.0/'
49 );
50 2 my %known_urls = map {$known_specs{$_} => $_} keys %known_specs;
51  
52 2 my $no_index = {
53 'map' => { file => { list => { value => \&string } },
54 directory => { list => { value => \&string } },
55 ':key' => { name => \&custom, value => \&anything },
56 }
57 };
58  
59 2 my $prereq_map = {
60 map => {
61 ':key' => {
62 name => \&phase,
63 'map' => {
64 ':key' => {
65 name => \&relation,
66 'map' => { ':key' => { name => \&term, value => \&exversion } }
67 },
68 },
69 }
70 },
71 };
72  
73 2 my %definitions = (
74 '1.0.0' => {
75 # REQUIRED
76 'abstract' => { mandatory => 1, value => \&string },
77 'maintainer' => { mandatory => 1, list => { value => \&string } },
78 'generated_by' => { mandatory => 0, value => \&string },
79 'license' => {
80 mandatory => 1,
81 listormap => {
82 list => { value => \&license },
83 map => {
84 ':key' => { name => \&anything, value => \&url }
85 },
86 }
87 },
88 'meta-spec' => {
89 mandatory => 1,
90 'map' => {
91 version => { mandatory => 1, value => \&version},
92 url => { value => \&url },
93 ':key' => { name => \&custom, value => \&anything },
94 }
95 },
96 'name' => { mandatory => 1, value => \&term },
97 'release_status' => { mandatory => 0, value => \&release_status },
98 'version' => { mandatory => 1, value => \&version },
99 'provides' => {
100 'mandatory' => 1,
101 'map' => {
102 ':key' => {
103 name => \&term,
104 'map' => {
105 file => { mandatory => 1, value => \&file },
106 version => { mandatory => 1, value => \&version },
107 abstract => { mandatory => 0, value => \&string },
108 docfile => { mandatory => 0, value => \&file },
109 ':key' => { name => \&custom, value => \&anything },
110 }
111 }
112 }
113 },
114  
115 # OPTIONAL
116 'description' => { value => \&string },
117 'tags' => { list => { value => \&tag } },
118 'no_index' => $no_index,
119 'prereqs' => $prereq_map,
120 'resources' => {
121 'map' => {
122 license => { list => { value => \&url } },
123 homepage => { value => \&url },
124 bugtracker => {
125 'map' => {
126 web => { value => \&url },
127 mailto => { value => \&email},
128 ':key' => { name => \&custom, value => \&anything },
129 }
130 },
131 repository => {
132 'map' => {
133 web => { value => \&url },
134 url => { value => \&url },
135 type => { value => \&lc_string },
136 ':key' => { name => \&custom, value => \&anything },
137 }
138 },
139 ':key' => { value => \&string, name => \&custom },
140 }
141 },
142  
143 # CUSTOM -- additional user defined key/value pairs
144 # note we can only validate the key name, as the structure is user defined
145 ':key' => { name => \&custom, value => \&anything },
146 },
147 );
148  
149 #--------------------------------------------------------------------------#
150 # Code
151 #--------------------------------------------------------------------------#
152  
153 =head1 Interface
154  
155 =head2 Constructors
156  
157 =head3 C<new>
158  
159 my $pmv = PGXN::Meta::Validator->new( $struct )
160  
161 The constructor must be passed a metadata structure.
162  
163 =cut
164  
165 185 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
166 185 my ($class, $data) = @_;
167  
168 # create an attributes hash
169 my $self = {
170 'data' => $data,
171 'spec' => $data->{'meta-spec'} && $data->{'meta-spec'}{version}
172 ? $data->{'meta-spec'}{version}
173 185 ▶ : '1.0.0',
Branch: 1 branch
BranchTrueFalseTotal
$data->{'meta-spec'} && $data->{'meta-spec'}{'version'} ? : 183 2 185
Condition100%3/3
$data->{"meta-spec"} and $data->{"meta-spec"}{"version"}
!ll&&!rl&&r
11183
MC/DC100%2/2
$data->{"meta-spec"} and $data->{"meta-spec"}{"version"}
$data->{"meta-spec"} $data->{"meta-spec"}{"version"}
Truth table100%3/3
A and B
A= $data->{"meta-spec"}B= $data->{"meta-spec"}{"version"}
ABresult
0X0
100
111
174 'errors' => undef,
175 };
176  
177 # create the object
178 185 return bless $self, $class;
179 }
180  
181 =head3 C<load_file>
182  
183 my $meta = PGXN::Meta::Validator->load_file('META.json');
184  
185 Reads in the specified JSON file and passes the resulting data structure to
186 C<new()>, returning the resulting PGXN::Meta::Validator object. An exception
187 will be thrown if the file does not exist or if its contents are not valid
188 JSON.
189  
190 =cut
191  
192 3 ▶sub load_file {
Subroutine load_file: called CC=3 SCAR=11.0
Pod documented
193 3 my ($class, $file) = @_;
194  
195 3 ▶ croak "load_file() requires a valid, readable filename"
Branch: 1 branch
BranchTrueFalseTotal
unless -r $file 1 2 3
196 unless -r $file;
197  
198 2 $class->new(JSON->new->decode(do {
199 2 local $/;
200 2 ▶ open my $fh, '<:raw', $file or croak "Cannot open $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or croak("Cannot open ${file}: $!\n") 2 0 2
201 2 <$fh>;
202 }));
203 }
204  
205 =head2 Class Method
206  
207 =head3 C<version_string>
208  
209 say 'PGXN::Meta::Validator ', PGXN::Meta::Validator->version_string;
210  
211 Returns a string representation of the PGXN::Meta::Validator version.
212  
213 =cut
214  
215 0 ▶sub version_string {
Subroutine version_string: not called CC=1 SCAR=6.9
Pod documented
216 0 sprintf 'v%vd', $VERSION;
217 }
218  
219 =head2 Instance Methods
220  
221 =head3 C<is_valid>
222  
223 if ( $pmv->is_valid ) {
224 ...
225 }
226  
227 Returns a boolean value indicating whether the metadata provided
228 is valid.
229  
230 =cut
231  
232 185 ▶sub is_valid {
Subroutine is_valid: called CC=1 SCAR=0.0
Pod documented
233 185 my $self = shift;
234 185 my $data = $self->{data};
235 185 my $spec_version = $self->{spec};
236 185 $self->check_map($definitions{$spec_version}, $data);
237 185 return ! $self->errors;
238 }
239  
240 =head3 C<errors>
241  
242 warn( join "\n", $pmv->errors );
243  
244 Returns a list of errors seen during validation.
245  
246 =cut
247  
248 261 ▶sub errors {
Subroutine errors: called CC=2 SCAR=6.9
Pod documented
249 261 my $self = shift;
250 261 ▶ return () unless defined $self->{errors};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"errors"} 109 152 261
251 152 return @{$self->{errors}};
252 }
253  
254 =begin internals
255  
256 =head2 Check Methods
257  
258 =over
259  
260 =item * check_map($spec,$data)
261  
262 Checks whether a map (or hash) part of the data structure conforms to the
263 appropriate specification definition.
264  
265 =item * check_list($spec,$data)
266  
267 Checks whether a list conforms, but converts strings to a single-element list
268  
269 =item * check_listormap($spec,$data)
270  
271 Checks whether a map or lazy list conforms to the appropriate specification
272 definition.
273  
274 =back
275  
276 =cut
277  
278 2 my $spec_error = "Missing validation action in specification. "
279 . "Must be one of 'map', 'list', or 'value'";
280  
281 2198 ▶sub check_map {
Subroutine check_map: called CC=17 SCAR=29.4
Pod documented
282 2198 my ($self,$spec,$data) = @_;
283  
284 2198 ▶ if(ref($spec) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $spec ne "HASH") 1 2197 2198
285 1 $self->_error( "Unknown META specification, cannot validate." );
286 1 return;
287 }
288  
289 2197 ▶ if(ref($data) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data ne "HASH") 6 2191 2197
290 6 $self->_error( 'Should be a map structure' );
291 6 return;
292 }
293  
294 2191 for my $key (keys %$spec) {
295 7516 ▶ next unless($spec->{$key}->{mandatory});
Branch: 1 branch
BranchTrueFalseTotal
unless $spec->{$key}{"mandatory"} 5681 1835 7516
296 1835 ▶ next if(defined $data->{$key});
Branch: 1 branch
BranchTrueFalseTotal
if defined $data->{$key} 1823 12 1835
297 12 push @{$self->{stack}}, $key;
298 12 $self->_error( 'missing', 'Required field' );
299 12 pop @{$self->{stack}};
300 }
301  
302 2191 for my $key (keys %$data) {
303 5856 push @{$self->{stack}}, $key;
304 5856 ▶ if($spec->{$key}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($spec->{$key}) { } 4384 1472 5856
elsif ($spec->{':key'}) { } 1472 0 1472
305 4384 ▶ if($spec->{$key}{value}) {
Branch: 4 branches
BranchTrueFalseTotal
if ($spec->{$key}{'value'}) { } 2712 1672 4384
elsif ($spec->{$key}{'map'}) { } 1106 566 1672
elsif ($spec->{$key}{'list'}) { } 383 183 566
elsif ($spec->{$key}{'listormap'}) { } 183 0 183
306 2712 $spec->{$key}{value}->($self,$key,$data->{$key});
307 } elsif($spec->{$key}{'map'}) {
308 1106 $self->check_map($spec->{$key}{'map'},$data->{$key});
309 } elsif($spec->{$key}{'list'}) {
310 383 $self->check_list($spec->{$key}{'list'},$data->{$key});
311 } elsif($spec->{$key}{'listormap'}) {
312 183 $self->check_listormap($spec->{$key}{'listormap'},$data->{$key});
313 } else {
314 0 $self->_error( "$spec_error for '$key'" );
315 }
316  
317 } elsif ($spec->{':key'}) {
318 1472 $spec->{':key'}{name}->($self,$key,$key);
319 1472 ▶ if($spec->{':key'}{value}) {
Branch: 4 branches
BranchTrueFalseTotal
if ($spec->{':key'}{'value'}) { } 717 755 1472
elsif ($spec->{':key'}{'map'}) { } 755 0 755
elsif ($spec->{':key'}{'list'}) { } 0 0 0
elsif ($spec->{':key'}{'listormap'}) { } 0 0 0
320 717 $spec->{':key'}{value}->($self,$key,$data->{$key});
321 } elsif($spec->{':key'}{'map'}) {
322 755 $self->check_map($spec->{':key'}{'map'},$data->{$key});
323 } elsif($spec->{':key'}{'list'}) {
324 0 $self->check_list($spec->{':key'}{'list'},$data->{$key});
325 } elsif($spec->{':key'}{'listormap'}) {
326 0 $self->check_listormap($spec->{':key'}{'listormap'},$data->{$key});
327 } else {
328 0 $self->_error( "$spec_error for ':key'" );
329 }
330  
331  
332 } else {
333 0 $self->_error( "Unknown key, '$key', found in map structure" );
334 }
335 5856 pop @{$self->{stack}};
336 }
337 }
338  
339 # if it's a string, make it into a list and check the list
340 414 ▶sub check_list {
Subroutine check_list: called CC=7 SCAR=19.5
Pod documented
341 414 my ($self,$spec,$data) = @_;
342  
343 414 ▶ if ( defined $data && ! ref $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data and not ref $data) { } 38 376 414
Condition100%3/3
defined $data and not ref $data
!ll&&!rl&&r
337338
MC/DC100%2/2
defined $data and not ref $data
defined $data not ref $data
Truth table100%3/3
A and not(B)
A= defined $dataB= not ref $data
ABresult
0X0
100
111
344 38 $self->_list_value($spec, $data);
345 } else {
346 376 ▶ $self->_error( 'Should be a list structure' ) if ref $data ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $data ne "ARRAY" 3 373 376
347 $self->_error( "Missing entries from required list" )
348 376 ▶ if defined $spec->{mandatory} && !defined $data->[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $spec->{"mandatory"} and not defined $data->[0] 0 376 376
Condition33%1/3
defined $spec->{"mandatory"} and not defined $data->[0]
!ll&&!rl&&r
37600
MC/DC0%0/2
defined $spec->{"mandatory"} and not defined $data->[0]
defined $spec->{"mandatory"} not defined $data->[0]
Truth table33%1/3
A and not(B)
A= defined $spec->{"mandatory"}B= not defined $data->[0]
ABresult
0X0
100
111
349 376 my $field = pop @{ $self->{stack} };
350 376 my $i = 0;
351 376 for my $value (@{ $data }) {
352 1265 push @{$self->{stack}}, $field . "[$i]";
353 1265 $i++;
354 1265 $self->_list_value($spec, $value);
355 1265 pop @{$self->{stack}};
356 }
357 376 push @{$self->{stack}}, $field;
358 }
359 }
360  
361 1303 ▶sub _list_value {
Subroutine _list_value: called CC=7 SCAR=35.0
362 1303 my ($self, $spec, $value) = @_;
363 1303 ▶ if(defined $spec->{value}) {
Branch: 6 branches
BranchTrueFalseTotal
if (defined $spec->{'value'}) { } 1303 0 1303
elsif (defined $spec->{'map'}) { } 0 0 0
elsif (defined $spec->{'list'}) { } 0 0 0
elsif (defined $spec->{'list'}) { } 0 0 0
elsif (defined $spec->{'listormap'}) { } 0 0 0
elsif ($spec->{':key'}) { } 0 0 0
364 1303 $spec->{value}->($self,'list',$value);
365 } elsif(defined $spec->{'map'}) {
366 0 $self->check_map($spec->{'map'},$value);
367 } elsif(defined $spec->{'list'}) {
368 0 $self->check_list($spec->{'list'},$value);
369 } elsif(defined $spec->{'list'}) {
370 0 $self->check_list($spec->{'list'},$value);
371 } elsif(defined $spec->{'listormap'}) {
372 0 $self->check_listormap($spec->{'listormap'},$value);
373 } elsif ($spec->{':key'}) {
374 0 $self->check_map($spec,$value);
375 } else {
376 0 $self->_error( "$spec_error associated with '$self->{stack}[-2]'" );
377 }
378 }
379  
380 183 ▶sub check_listormap {
Subroutine check_listormap: called CC=2 SCAR=6.9
Pod documented
381 183 my ($self,$spec,$data) = @_;
382  
383 return ref $data eq 'HASH'
384 ? $self->check_map($spec->{map}, $data)
385 183 ▶ : $self->check_list($spec->{list}, $data);
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'HASH' ? : 152 31 183
386 }
387  
388 =head2 Validator Methods
389  
390 =over
391  
392 =item * anything($self,$key,$value)
393  
394 Any value is valid, so this function always returns true.
395  
396 =item * url($self,$key,$value)
397  
398 Validates that a given value is in an acceptable URL format
399  
400 =item * email($self,$key,$value)
401  
402 Validates that a given value is in an acceptable EMAIL format
403  
404 =item * urlspec($self,$key,$value)
405  
406 Validates that the URL to a META specification is a known one.
407  
408 =item * string_or_undef($self,$key,$value)
409  
410 Validates that the value is either a string or an undef value. Bit of a
411 catchall function for parts of the data structure that are completely user
412 defined.
413  
414 =item * string($self,$key,$value)
415  
416 Validates that a string exists for the given key.
417  
418 =item * lc_string($self,$key,$value)
419  
420 Validates that a string exists for the given key and contains only lowercase
421 characters.
422  
423 =item * file($self,$key,$value)
424  
425 Validate that a file is passed for the given key. This may be made more
426 thorough in the future. For now it acts like \&string.
427  
428 =item * phase($self,$key,$value)
429  
430 Validate that a prereq phase is one of "configure", "build", "test",
431 "runtime", or "develop".
432  
433 =item * relation($self,$key,$value)
434  
435 Validate that a prereq relation is one of "requires", "recommends",
436 "suggests", or "conflicts".
437  
438 =item * release_status($self,$key,$value)
439  
440 Validate that release status is one of "stable", "testing", or "unstable".
441  
442 =item * exversion($self,$key,$value)
443  
444 Validates a list of versions, e.g. '<= 5, >=2, ==3, !=4, >1, <6, 0'.
445  
446 =item * version($self,$key,$value)
447  
448 Validates a single version string. Versions of the type '5.8.8' and '0.00_00'
449 are both valid. A leading 'v' like 'v1.2.3' is also valid.
450  
451 =item * boolean($self,$key,$value)
452  
453 Validates for a boolean value. Currently these values are '1', '0', 'true',
454 'false', however the latter 2 may be removed.
455  
456 =item * license($self,$key,$value)
457  
458 Validates that a value is given for the license. Returns 1 if an known license
459 type, or 2 if a value is given but the license type is not a recommended one.
460  
461 =item * custom($self,$key,$value)
462  
463 Validates that the given key begins with 'x_' or 'X_', to indicate a user
464 defined tag and only has characters in the class [-_a-zA-Z]
465  
466 =item * identifier($self,$key,$value)
467  
468 Validates that key is in an acceptable format for the META specification,
469 for an identifier, i.e. any that matches the regular expression
470 qr/[a-z][a-z_]/i.
471  
472 =item * term($self,$key,$value)
473  
474 Validates that a given value is in an acceptable term, i.e., at least two
475 characters and does not contain control characters, spaces, C</>, or C<\>.
476  
477 =item * tag($self,$key,$value)
478  
479 Validates that a given value is in an acceptable tag, i.e., between 1 and 256
480 characters and does not contain control characters, C</>, or C<\>.
481  
482 =back
483  
484 =end internals
485  
486 =cut
487  
488 5 ▶sub release_status {
Subroutine release_status: called CC=3 SCAR=11.0
Pod documented
489 5 my ($self,$key,$value) = @_;
490 5 ▶ $value = '' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 4 5
491 5 ▶ return 1 if $value =~ /\A(?:(?:un)?stable|testing)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\A(?:(?:un)?stable|testing)\z/ 3 2 5
492 2 $self->_error( qq{"$value" is not a valid release status; must be one of stable, testing, unstable} );
493 2 return 0;
494 }
495  
496 # _uri_split taken from URI::Split by Gisle Aas, Copyright 2003
497 1039 ▶sub _uri_split {
Subroutine _uri_split: called CC=1 SCAR=0.0
498 1039 return $_[0] =~ m,(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?,;
499 }
500  
501 1040 ▶sub url {
Subroutine url: called CC=7 SCAR=21.4
Pod documented
502 1040 my ($self,$key,$value) = @_;
503 1040 ▶ if (defined $value && $value ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and $value ne '') { } 1039 1 1040
Condition66%2/3
defined $value and $value ne ""
!ll&&!rl&&r
101039
MC/DC50%1/2
defined $value and $value ne ""
defined $value $value ne ""
Truth table66%2/3
A and B
A= defined $valueB= $value ne ""
ABresult
0X0
100
111
504 # XXX Consider using Data::Validate::URI.
505 1039 my ($scheme, $auth, $path, $query, $frag) = _uri_split($value);
506 1039 ▶ unless ( defined $scheme && length $scheme ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $scheme and length $scheme) 7 1032 1039
Condition66%2/3
defined $scheme and length $scheme
!ll&&!rl&&r
701032
MC/DC50%1/2
defined $scheme and length $scheme
defined $scheme length $scheme
Truth table66%2/3
A and B
A= defined $schemeB= length $scheme
ABresult
0X0
100
111
507 7 $self->_error( qq{"$value" is not a valid URL} );
508 7 return 0;
509 }
510 1032 ▶ unless ( defined $auth && length $auth ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $auth and length $auth) 0 1032 1032
Condition33%1/3
defined $auth and length $auth
!ll&&!rl&&r
001032
MC/DC0%0/2
defined $auth and length $auth
defined $auth length $auth
Truth table33%1/3
A and B
A= defined $authB= length $auth
ABresult
0X0
100
111
511 0 $self->_error( qq{"$value" is not a valid URL} );
512 0 return 0;
513 }
514 1032 return 1;
515 } else {
516 1 $self->_error('No value');
517 1 return 0;
518 }
519 0 $self->_error( qq{"$value" is not a valid URL} );
520 0 return 0;
521 }
522  
523 0 ▶sub urlspec {
Subroutine urlspec: not called CC=6 SCAR=37.4
Pod documented
524 0 my ($self,$key,$value) = @_;
525 0 ▶ if(defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
526 0 ▶ return 1 if($value && $known_specs{$self->{spec}} eq $value);
Branch: 1 branch
BranchTrueFalseTotal
if $value and $known_specs{$self->{"spec"}} eq $value 0 0 0
Condition0%0/3
$value and $known_specs{$self->{"spec"}} eq $value
!ll&&!rl&&r
000
MC/DC0%0/2
$value and $known_specs{$self->{"spec"}} eq $value
$value $known_specs{$self->{"spec"}} eq $value
Truth table0%0/3
A and B
A= $valueB= $known_specs{$self->{"spec"}} eq $value
ABresult
0X0
100
111
527 0 ▶ if($value && $known_urls{$value}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value and $known_urls{$value}) 0 0 0
Condition0%0/3
$value and $known_urls{$value}
!ll&&!rl&&r
000
MC/DC0%0/2
$value and $known_urls{$value}
$value $known_urls{$value}
Truth table0%0/3
A and B
A= $valueB= $known_urls{$value}
ABresult
0X0
100
111
528 0 $self->_error( 'META specification URL does not match version' );
529 0 return 0;
530 }
531 }
532 0 $self->_error( 'Unknown META specification' );
533 0 return 0;
534 }
535  
536 3 ▶sub email {
Subroutine email: called CC=3 SCAR=11.0
Pod documented
537 3 my ($self, $key, $value) = @_;
538 # XXX Consider using Email::Valid.
539 3 ▶ return 1 if defined $value && $value =~ /@/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and $value =~ /@/ 2 1 3
Condition66%2/3
defined $value and $value =~ /@/
!ll&&!rl&&r
012
MC/DC50%1/2
defined $value and $value =~ /@/
defined $value $value =~ /@/
Truth table66%2/3
A and B
A= defined $valueB= $value =~ /@/
ABresult
0X0
100
111
540 1 $self->_error( qq{"$value" is not a valid email address} );
541 1 return 0;
542 }
543  
544 164 ▶sub anything { return 1 }
Subroutine anything: called CC=1 SCAR=0.0
Pod documented
545  
546 1284 ▶sub string {
Subroutine string: called CC=4 SCAR=13.9
Pod documented
547 1284 my ($self,$key,$value) = @_;
548 1284 ▶ if(defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 1279 5 1284
549 1279 ▶ return 1 if($value || $value =~ /^0$/);
Branch: 1 branch
BranchTrueFalseTotal
if $value or $value =~ /^0$/ 1275 4 1279
Condition66%2/3
$value or $value =~ /^0$/
l!l&&r!l&&!r
127504
MC/DC50%1/2
$value or $value =~ /^0$/
$value $value =~ /^0$/
Truth table66%2/3
A or B
A= $valueB= $value =~ /^0$/
ABresult
1X1
011
000
550 }
551 9 $self->_error( "No value" );
552 9 return 0;
553 }
554  
555 175 ▶sub lc_string {
Subroutine lc_string: called CC=3 SCAR=11.0
Pod documented
556 175 my ($self, $key, $value) = @_;
557 175 ▶ $self->string($key, $value) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->string($key, $value) or return 0 175 0 175
558 175 ▶ return 1 if $value !~ /\p{Upper}/;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /\p{Upper}/ 174 1 175
559 1 $self->_error( qq{"$value" is not a lowercase string} );
560 1 return 0;
561 }
562  
563 918 ▶sub term {
Subroutine term: called CC=1 SCAR=0.0
Pod documented
564 918 shift->_string_class(@_, term => qr{[[:space:][:cntrl:]/\\]}, 2);
565 }
566  
567 892 ▶sub tag {
Subroutine tag: called CC=1 SCAR=0.0
Pod documented
568 892 shift->_string_class(@_, tag => qr{[[:cntrl:]/\\]}, 1, 256);
569 }
570  
571 1810 ▶sub _string_class {
Subroutine _string_class: called CC=9 SCAR=22.0
572 1810 my ($self, $key, $value, $type, $regex, $min, $max) = @_;
573 1810 ▶ unless (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 1 1809 1810
574 1 $self->_error( 'value is not defined' );
575 1 return 0;
576 }
577  
578 1809 ▶ if (($value || $value =~ /^0$/) && $value !~ $regex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value || $value =~ /^0$/ and not $value =~ /$regex/) { } 1801 8 1809
Condition83%5/6
$value || $value =~ /^0$/ and not $value =~ /$regex/
!ll&&!rl&&r
261801
$value or $value =~ /^0$/
l!l&&r!l&&!r
180702
MC/DC66%2/3
$value || $value =~ /^0$/ and not $value =~ /$regex/
$value $value =~ /^0$/ not $value =~ /$regex/
Truth table60%3/5
A or B and not(C)
A= $valueB= $value =~ /^0$/C= not $value =~ /$regex/
ABCresult
00X0
1X00
0100
1X11
0111
579 1801 ▶ if ($min && length $value < $min) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min and length $value < $min) 1 1800 1801
Condition66%2/3
$min and length $value < $min
!ll&&!rl&&r
018001
MC/DC50%1/2
$min and length $value < $min
$min length $value < $min
Truth table66%2/3
A and B
A= $minB= length $value < $min
ABresult
0X0
100
111
580 1 $self->_error("$type must be at least $min characters");
581 1 return 0;
582 }
583 1800 ▶ if ($max && length $value > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max and length $value > $max) 1 1799 1800
Condition100%3/3
$max and length $value > $max
!ll&&!rl&&r
9118881
MC/DC100%2/2
$max and length $value > $max
$max length $value > $max
Truth table100%3/3
A and B
A= $maxB= length $value > $max
ABresult
0X0
100
111
584 1 $self->_error("$type must be no more than $max characters");
585 1 return 0;
586 }
587 1799 return 1;
588 } else {
589 8 $self->_error(qq{"$value" is not a valid $type});
590 8 return 0;
591 }
592 }
593  
594 0 ▶sub string_or_undef {
Subroutine string_or_undef: not called CC=4 SCAR=30.0
Pod documented
595 0 my ($self,$key,$value) = @_;
596 0 ▶ return 1 unless(defined $value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
597 0 ▶ return 1 if($value || $value =~ /^0$/);
Branch: 1 branch
BranchTrueFalseTotal
if $value or $value =~ /^0$/ 0 0 0
Condition0%0/3
$value or $value =~ /^0$/
l!l&&r!l&&!r
000
MC/DC0%0/2
$value or $value =~ /^0$/
$value $value =~ /^0$/
Truth table0%0/3
A or B
A= $valueB= $value =~ /^0$/
ABresult
1X1
011
000
598 0 $self->_error( "No string defined for '$key'" );
599 0 return 0;
600 }
601  
602 183 ▶sub file {
Subroutine file: called CC=2 SCAR=6.9
Pod documented
603 183 my ($self,$key,$value) = @_;
604 183 ▶ return 1 if(defined $value);
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 181 2 183
605 2 $self->_error( 'No value' );
606 2 return 0;
607 }
608  
609 553 ▶sub exversion {
Subroutine exversion: called CC=11 SCAR=24.0
Pod documented
610 553 my ($self,$key,$value) = @_;
611 553 ▶ if (defined $value && ($value || $value eq '0')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and $value || $value eq 0) 552 1 553
Condition66%4/6
defined $value and $value || $value eq 0
!ll&&!rl&&r
10552
$value or $value eq 0
l!l&&r!l&&!r
3761760
MC/DC33%1/3
defined $value and $value || $value eq 0
defined $value $value $value eq 0
Truth table75%3/4
A and B or C
A= defined $valueB= $valueC= $value eq 0
ABCresult
0XX0
1000
11X1
1011
612 552 my $pass = 1;
613 552 for my $val (split /,\s*/, $value) {
614 557 ▶ if ($val ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($val ne '') { } 556 1 557
615 556 ▶ next if $val eq '0';
Branch: 1 branch
BranchTrueFalseTotal
if $val eq 0 176 380 556
616 380 ▶ if ($val =~ s/^([^\d\s]+)\s*//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ s/^([^\d\s]+)\s*//) 20 360 380
617 20 my $op = $1;
618 20 ▶ if ($op !~ /^[<>](?:=)?|==|!=$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($op =~ /^[<>](?:=)?|==|!=$/) 2 18 20
619 2 $self->_error(qq{"$op" is not a valid version range operator});
620 2 $pass = 0;
621 }
622 }
623 380 ▶ unless (eval { SemVer->new($val) }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { "SemVer"->new($val) } }) 2 378 380
624 # Field /version: Value "0.01.2" is not a valid version [Spec v1.0.0]
625 2 $self->_error( qq{"$val" is not a valid semantic version} );
626 2 $pass = 0;
627 }
628 } else {
629 1 $self->_error( qq{"" is not a valid semantic version} );
630 1 $pass = 0;
631 }
632 }
633 552 return $pass;
634 }
635 1 ▶ $self->_error($value ? qq{value "$value" is not a valid semantic version} : 'No value' );
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 0 1 1
636 1 return 0;
637 }
638  
639 546 ▶sub version {
Subroutine version: called CC=3 SCAR=11.0
Pod documented
640 546 my ($self,$key,$value) = @_;
641 546 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 545 1 546
642 545 ▶ return 1 if eval { SemVer->new($value) };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { "SemVer"->new($value) } } 539 6 545
643 } else {
644 1 $value = '<undef>';
645 }
646 7 $self->_error( qq{"$value" is not a valid semantic version} );
647 7 return 0;
648 }
649  
650 0 ▶sub boolean {
Subroutine boolean: not called CC=3 SCAR=24.8
Pod documented
651 0 my ($self,$key,$value) = @_;
652 0 ▶ if(defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
653 0 ▶ return 1 if($value =~ /^(0|1|true|false)$/);
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^(0|1|true|false)$/ 0 0 0
654 } else {
655 0 $value = '<undef>';
656 }
657 0 $self->_error( "'$value' for '$key' is not a boolean value." );
658 0 return 0;
659 }
660  
661 2 my %licenses = map { $_ => 1 } qw(
662 agpl_3
663 apache_1_1
664 apache_2_0
665 artistic_1
666 artistic_2
667 bsd
668 freebsd
669 gfdl_1_2
670 gfdl_1_3
671 gpl_1
672 gpl_2
673 gpl_3
674 lgpl_2_1
675 lgpl_3_0
676 mit
677 mozilla_1_0
678 mozilla_1_1
679 openssl
680 perl_5
681 postgresql
682 qpl_1_0
683 ssleay
684 sun
685 zlib
686 open_source
687 restricted
688 unrestricted
689 unknown
690 );
691  
692 33 ▶sub license {
Subroutine license: called CC=4 SCAR=14.3
Pod documented
693 33 my ($self,$key,$value) = @_;
694 33 ▶ if(defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 33 0 33
695 33 ▶ return 1 if $value && exists $licenses{$value};
Branch: 1 branch
BranchTrueFalseTotal
if $value and exists $licenses{$value} 31 2 33
Condition66%2/3
$value and exists $licenses{$value}
!ll&&!rl&&r
0231
MC/DC50%1/2
$value and exists $licenses{$value}
$value exists $licenses{$value}
Truth table66%2/3
A and B
A= $valueB= exists $licenses{$value}
ABresult
0X0
100
111
696 } else {
697 0 $value = '<undef>';
698 }
699 2 $self->_error( qq{"$value" is an unknown license} );
700 2 return 0;
701 }
702  
703 10 ▶sub custom {
Subroutine custom: called CC=4 SCAR=14.3
Pod documented
704 10 my ($self,$key) = @_;
705 10 ▶ if(defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) { } 10 0 10
706 10 ▶ return 1 if($key && $key =~ /^x_/i); # user defined
Branch: 1 branch
BranchTrueFalseTotal
if $key and $key =~ /^x_/i 5 5 10
Condition66%2/3
$key and $key =~ /^x_/i
!ll&&!rl&&r
055
MC/DC50%1/2
$key and $key =~ /^x_/i
$key $key =~ /^x_/i
Truth table66%2/3
A and B
A= $keyB= $key =~ /^x_/i
ABresult
0X0
100
111
707 } else {
708 0 $key = '<undef>';
709 }
710 5 $self->_error( 'Unknown key; custom keys must begin with "x_" or "X_"' );
711 5 return 0;
712 }
713  
714 0 ▶sub identifier {
Subroutine identifier: not called CC=4 SCAR=30.0
Pod documented
715 0 my ($self,$key) = @_;
716 0 ▶ if(defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) { } 0 0 0
717 0 ▶ return 1 if($key && $key =~ /^([a-z][_a-z]+)$/i); # spec 2.0 defined
Branch: 1 branch
BranchTrueFalseTotal
if $key and $key =~ /^([a-z][_a-z]+)$/i 0 0 0
Condition0%0/3
$key and $key =~ /^([a-z][_a-z]+)$/i
!ll&&!rl&&r
000
MC/DC0%0/2
$key and $key =~ /^([a-z][_a-z]+)$/i
$key $key =~ /^([a-z][_a-z]+)$/i
Truth table0%0/3
A and B
A= $keyB= $key =~ /^([a-z][_a-z]+)$/i
ABresult
0X0
100
111
718 } else {
719 0 $key = '<undef>';
720 }
721 0 $self->_error( "Key '$key' is not a legal identifier." );
722 0 return 0;
723 }
724  
725 2 my @valid_phases = qw/ configure build test runtime develop /;
726 196 ▶sub phase {
Subroutine phase: called CC=5 SCAR=16.4
Pod documented
727 196 my ($self,$key) = @_;
728 196 ▶ if(defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 196 0 196
729 196 ▶ return 1 if( length $key && grep { $key eq $_ } @valid_phases );
Branch: 1 branch
BranchTrueFalseTotal
if length $key and grep {$key eq $_;} @valid_phases 195 1 196
Condition66%2/3
length $key and grep {$key eq $_;} @valid_phases
!ll&&!rl&&r
01195
MC/DC50%1/2
length $key and grep {$key eq $_;} @valid_phases
length $key grep {$key eq $_;} @valid_phases
Truth table66%2/3
A and B
A= length $keyB= grep {$key eq $_;} @valid_phases
ABresult
0X0
100
111
730 1 ▶ return 1 if $key =~ /x_/i;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /x_/i 0 1 1
731 }
732 1 $self->_error('Unknown preqreq phase; must be one of ' . join ', ', @valid_phases);
733 1 return 0;
734 }
735  
736 2 my @valid_relations = qw/ requires recommends suggests conflicts/;
737 377 ▶sub relation {
Subroutine relation: called CC=5 SCAR=16.4
Pod documented
738 377 my ($self,$key) = @_;
739 377 ▶ if(defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 377 0 377
740 377 ▶ return 1 if( length $key && grep { $key eq $_ } @valid_relations );
Branch: 1 branch
BranchTrueFalseTotal
if length $key and grep {$key eq $_;} @valid_relations 376 1 377
Condition66%2/3
length $key and grep {$key eq $_;} @valid_relations
!ll&&!rl&&r
01376
MC/DC50%1/2
length $key and grep {$key eq $_;} @valid_relations
length $key grep {$key eq $_;} @valid_relations
Truth table66%2/3
A and B
A= length $keyB= grep {$key eq $_;} @valid_relations
ABresult
0X0
100
111
741 1 ▶ return 1 if $key =~ /x_/i;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /x_/i 0 1 1
742 }
743 1 $self->_error('Unknown preqreq relationship; must be one of ' . join ', ', @valid_relations);
744 1 return 0;
745 }
746  
747 78 ▶sub _error {
Subroutine _error: called CC=3 SCAR=11.0
748 78 my $self = shift;
749 78 my $mess = shift;
750 78 ▶ my $label = shift || 'Field';
Condition100%2/2
shift() || 'Field'
l!l
1266
MC/DC100%1/1
shift() || 'Field'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
751  
752 78 ▶ $mess = "$label /" . join( '/' => @{ $self->{stack} }) . ": $mess" if $self->{stack};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stack"} 77 1 78
753 78 $mess .= " [Spec v$self->{spec}]";
754  
755 78 push @{$self->{errors}}, $mess;
756 }
757  
758 2 1;
759  
760 __END__
↑ Summary