TropicalCyclone/Validation.pm

filter: Statementstmt 100% 45 / 45 Branchbran 95.0% 19 / 20 Conditioncond 77.7% 14 / 18 MC/DCmcdc 63.6% 7 / 11 Subroutinesub 100% 6 / 6 Podpod 100% 3 / 3 totaltotal 91.2% 94 / 103 CC 21 SCAR 28.5
CC
21
Cov
94%
validate
21.2
BEGIN
1.0
BEGIN
1.0
CRAP 17.3
« TropicalCyclone/ForecastAdvisory.pm ↑ Summary TropicalCyclone/HTTP.pm »
1 package Weather::NHC::TropicalCyclone::Validation;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Scalar::Util qw/looks_like_number/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 our @REQUIRED_STORM_FIELDS = qw/id binNumber name classification/;
8  
9 4 our %CLASSIFICATIONS = (
10 TD => q{Tropical Depression},
11 STD => q{Subtropical Depression},
12 TS => q{Tropical Storm},
13 HU => q{Hurricane},
14 STS => q{Subtropical Storm},
15 PTC => q{Post-tropical Cyclone / Remnants},
16 TY => q{Typhoon},
17 PC => q{Potential Tropical Cyclone},
18 );
19  
20 4 our %KNOWN_STORM_FIELDS = map { $_ => 1 } qw/
21 id binNumber name classification intensity pressure latitude longitude
22 latitudeNumeric longitudeNumeric movementDir movementSpeed lastUpdate
23 publicAdvisory forecastAdvisory windSpeedProbabilities forecastDiscussion
24 forecastGraphics forecastTrack windWatchesWarnings trackCone initialWindExtent
25 forecastWindRadiiGIS bestTrackGIS earliestArrivalTimeTSWindsGIS
26 mostLikelyTimeTSWindsGIS windSpeedProbabilitiesGIS stormSurgeWatchWarningGIS
27 potentialStormSurgeFloodingGIS peakSurgeKML
28 /;
29  
30 4 ▶sub classifications { return { %CLASSIFICATIONS } }
Subroutine classifications: called CC=1 SCAR=0.0
Pod documented
31 1 ▶sub known_storm_fields { return [ sort keys %KNOWN_STORM_FIELDS ] }
Subroutine known_storm_fields: called CC=1 SCAR=0.0
Pod documented
32  
33 28 ▶sub validate {
Subroutine validate: called CC=21 SCAR=30.5
Pod documented
34 28 my ( $class, $data ) = @_;
35 28 my ( @errors, @warnings );
36  
37 28 ▶ if ( ref $data ne q{HASH} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data ne "HASH") 1 27 28
38 1 push @errors, q{NHC JSON root must be an object};
39 1 return { errors => \@errors, warnings => \@warnings };
40 }
41  
42 27 ▶ if ( ref $data->{activeStorms} ne q{ARRAY} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data->{"activeStorms"} ne "ARRAY") 2 25 27
43 2 push @errors, q{NHC JSON root must contain an activeStorms array};
44 2 return { errors => \@errors, warnings => \@warnings };
45 }
46  
47 25 for my $index ( 0 .. $#{ $data->{activeStorms} } ) {
48 31 my $storm = $data->{activeStorms}->[$index];
49 31 ▶ if ( ref $storm ne q{HASH} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $storm ne "HASH") 1 30 31
50 1 push @errors, "activeStorms[$index] must be an object";
51 1 next;
52 }
53  
54 30 for my $field (@REQUIRED_STORM_FIELDS) {
55 push @errors, "activeStorms[$index] is missing required field '$field'"
56 120 ▶ if !exists $storm->{$field} || !defined $storm->{$field} || $storm->{$field} eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if not exists $storm->{$field} && defined $storm->{$field} or $storm->{$field} eq "" 4 116 120
Condition66%4/6
not exists $storm->{$field} && defined $storm->{$field} or $storm->{$field} eq ""
l!l&&r!l&&!r
40116
exists $storm->{$field} && defined $storm->{$field}
!ll&&!rl&&r
40116
MC/DC33%1/3
not exists $storm->{$field} && defined $storm->{$field} or $storm->{$field} eq ""
exists $storm->{$field} defined $storm->{$field} $storm->{$field} eq ""
Truth table50%2/4
not(A && B) or C
A= exists $storm->{$field}B= defined $storm->{$field}C= $storm->{$field} eq ""
ABCresult
0XX1
10X1
1111
1100
57 }
58  
59 30 ▶ if ( defined $storm->{classification} && !exists $CLASSIFICATIONS{ $storm->{classification} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $storm->{"classification"} and not exists $Weather::NHC::TropicalCyclone::Validation::CLASSIFICATIONS{$storm->{"classification"}}) 1 29 30
Condition100%3/3
defined $storm->{"classification"} and not exists $Weather::NHC::TropicalCyclone::Validation::CLASSIFICATIONS{$storm->{"classification"}}
!ll&&!rl&&r
1281
MC/DC100%2/2
defined $storm->{"classification"} and not exists $Weather::NHC::TropicalCyclone::Validation::CLASSIFICATIONS{$storm->{"classification"}}
defined $storm->{"classification"} not exists $Weather::NHC::TropicalCyclone::Validation::CLASSIFICATIONS{$storm->{"classification"}}
Truth table100%3/3
A and not(B)
A= defined $storm->{"classification"}B= not exists $Weather::NHC::TropicalCyclone::Validation::CLASSIFICATIONS{$storm->{"classification"}}
ABresult
0X0
100
111
60 1 push @errors, "activeStorms[$index] has unknown classification '$storm->{classification}'";
61 }
62  
63 30 ▶ if ( defined $storm->{binNumber} && $storm->{binNumber} !~ /^(?:AT|EP|CP)[1-5]$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $storm->{"binNumber"} and not $storm->{"binNumber"} =~ /^(?:AT|EP|CP)[1-5]$/i) 1 29 30
Condition100%3/3
defined $storm->{"binNumber"} and not $storm->{"binNumber"} =~ /^(?:AT|EP|CP)[1-5]$/i
!ll&&!rl&&r
1281
MC/DC100%2/2
defined $storm->{"binNumber"} and not $storm->{"binNumber"} =~ /^(?:AT|EP|CP)[1-5]$/i
defined $storm->{"binNumber"} not $storm->{"binNumber"} =~ /^(?:AT|EP|CP)[1-5]$/i
Truth table100%3/3
A and not(B)
A= defined $storm->{"binNumber"}B= not $storm->{"binNumber"} =~ /^(?:AT|EP|CP)[1-5]$/i
ABresult
0X0
100
111
64 1 push @errors, "activeStorms[$index] has unexpected binNumber '$storm->{binNumber}'";
65 }
66  
67 30 ▶ if ( defined $storm->{id} && $storm->{id} !~ /^(?:al|ep|cp)\d{2}\d{4}$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $storm->{"id"} and not $storm->{"id"} =~ /^(?:al|ep|cp)\d{2}\d{4}$/i) 1 29 30
Condition100%3/3
defined $storm->{"id"} and not $storm->{"id"} =~ /^(?:al|ep|cp)\d{2}\d{4}$/i
!ll&&!rl&&r
1281
MC/DC100%2/2
defined $storm->{"id"} and not $storm->{"id"} =~ /^(?:al|ep|cp)\d{2}\d{4}$/i
defined $storm->{"id"} not $storm->{"id"} =~ /^(?:al|ep|cp)\d{2}\d{4}$/i
Truth table100%3/3
A and not(B)
A= defined $storm->{"id"}B= not $storm->{"id"} =~ /^(?:al|ep|cp)\d{2}\d{4}$/i
ABresult
0X0
100
111
68 1 push @errors, "activeStorms[$index] has unexpected storm id '$storm->{id}'";
69 }
70  
71 30 for my $field (qw/latitudeNumeric longitudeNumeric movementDir movementSpeed/) {
72 120 ▶ next if !exists $storm->{$field} || !defined $storm->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $storm->{$field} and defined $storm->{$field} 0 120 120
Condition33%1/3
exists $storm->{$field} and defined $storm->{$field}
!ll&&!rl&&r
00120
MC/DC0%0/2
exists $storm->{$field} and defined $storm->{$field}
exists $storm->{$field} defined $storm->{$field}
Truth table33%1/3
A and B
A= exists $storm->{$field}B= defined $storm->{$field}
ABresult
0X0
100
111
73 push @errors, "activeStorms[$index].$field must be numeric"
74 120 ▶ if !looks_like_number( $storm->{$field} );
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($storm->{$field}) 4 116 120
75 }
76  
77 30 for my $field ( sort keys %$storm ) {
78 634 ▶ next if $KNOWN_STORM_FIELDS{$field};
Branch: 1 branch
BranchTrueFalseTotal
if $Weather::NHC::TropicalCyclone::Validation::KNOWN_STORM_FIELDS{$field} 632 2 634
79 2 push @warnings, "activeStorms[$index] contains unknown field '$field'; preserved without interpretation";
80 }
81 }
82  
83 25 return { errors => \@errors, warnings => \@warnings };
84 }
85  
86 4 1;
87  
88 __END__
« TropicalCyclone/ForecastAdvisory.pm ↑ Summary TropicalCyclone/HTTP.pm »