Spec/Run/Validator.pm

filter: Statementstmt 80.8% 118 / 146 Branchbran 78.4% 69 / 88 Conditioncond 71.8% 23 / 32 MC/DCmcdc 47.6% 10 / 21 Subroutinesub 70.0% 14 / 20 Podpod 100% 1 / 1 totaltotal 76.3% 235 / 308 CC 58 SCAR 41.3
CC
58
Cov
79%
_process
86.0
__ANON__
6.0
__ANON__
2.0
CRAP 62.0
« Spec/Completion/Zsh.pm ↑ Summary Spec/Plugin/Format.pm »
1 # ABSTRACT: Processes and validates options and parameters
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 package App::Spec::Run::Validator;
5  
6 6 our $VERSION = 'v0.15.1'; # TRIAL VERSION;
7  
8 6 ▶use List::Util qw/ any /;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use List::MoreUtils qw/ uniq /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Ref::Util qw/ is_arrayref is_hashref /;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 has options => ( is => 'ro' );
14 6 has option_specs => ( is => 'ro' );
15 6 has parameters => ( is => 'ro' );
16 6 has param_specs => ( is => 'ro' );
17  
18 my %validate = (
19 36 ▶ string => sub { length($_[0]) > 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
20 0 ▶ file => sub { $_[0] eq '-' or -f $_[0] },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] eq "-" or -f $_[0] 0 0 0
21 0 ▶ filename => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
22 0 ▶ dir => sub { -d $_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 0 ▶ dirname => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 6 ▶ integer => sub { $_[0] =~ m/^[+-]?\d+$/ },
Subroutine __ANON__: called CC=1 SCAR=0.0
25 0 ▶ float => sub { $_[0] =~ m/^[+-]?\d+(?:\.\d+)?$/ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
26 0 ▶ flag => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 7 ▶ enum => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
28 7 my ($value, $list) = @_;
29 7 ▶ any { $value eq $_ } @$list;
Subroutine __ANON__: called CC=1 SCAR=0.0
30 },
31 6 );
32  
33 31 ▶sub process {
Subroutine process: called CC=2 SCAR=6.9
Pod documented
34 31 my ($self, $run, $errs) = @_;
35 31 my ($ok) = $self->_process( $errs, type => "parameters", app => $run );
36 31 ▶ $ok &&= $self->_process( $errs, type => "options", app => $run );
Condition100%3/3
$ok &&= $self->_process($errs, "type", "options", "app", $run)
!ll&&!rl&&r
11416
MC/DC100%2/2
$ok &&= $self->_process($errs, "type", "options", "app", $run)
$ok $self->_process($errs, "type", "options", "app", $run)
Truth table100%3/3
A &&= B
A= $okB= $self->_process($errs, "type", "options", "app", $run)
ABresult
0X0
100
111
37 31 return $ok;
38 }
39  
40 51 ▶sub _process {
Subroutine _process: called CC=55 SCAR=44.5
41 51 my ($self, $errs, %args) = @_;
42 51 my $run = $args{app};
43 51 my $type = $args{type};
44 51 my ($items, $specs);
45 51 ▶ if ($args{type} eq "parameters") {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'type'} eq 'parameters') { } 31 20 51
46 31 $items = $self->parameters;
47 31 $specs = $self->param_specs;
48 }
49 else {
50 20 $items = $self->options;
51 20 $specs = $self->option_specs;
52 }
53  
54 # TODO: iterate over parameters in original cmdline order
55 51 for my $name (sort keys %$specs) {
56 136 my $spec = $specs->{ $name };
57 136 my $value = $items->{ $name };
58 136 my $param_type = $spec->type;
59 136 my $enum = $spec->enum;
60  
61 136 ▶ if ($spec->type eq "flag") {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->type eq "flag") 52 84 136
62 52 ▶ if ($spec->multiple) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->multiple) { } 17 35 52
63 17 ▶ if (defined $value and $value !~ m/^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and not $value =~ /^\d+$/) 0 17 17
Condition66%2/3
defined $value and not $value =~ /^\d+$/
!ll&&!rl&&r
1520
MC/DC0%0/2
defined $value and not $value =~ /^\d+$/
defined $value not $value =~ /^\d+$/
Truth table66%2/3
A and not(B)
A= defined $valueB= not $value =~ /^\d+$/
ABresult
0X0
100
111
64 0 die "Value for '$name': '$value' shouldn't happen";
65 }
66 }
67 else {
68 35 ▶ if (defined $value and $value != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and $value != 1) 0 35 35
Condition66%2/3
defined $value and $value != 1
!ll&&!rl&&r
3320
MC/DC0%0/2
defined $value and $value != 1
defined $value $value != 1
Truth table66%2/3
A and B
A= defined $valueB= $value != 1
ABresult
0X0
100
111
69 0 die "Value for '$name': '$value' shouldn't happen";
70 }
71 }
72 52 next;
73 }
74  
75 84 my $values;
76 84 ▶ if ($spec->multiple and $spec->mapping) {
Branch: 2 branches
BranchTrueFalseTotal
if ($spec->multiple and $spec->mapping) { } 5 79 84
elsif ($spec->multiple) { } 11 68 79
Condition100%3/3
$spec->multiple and $spec->mapping
!ll&&!rl&&r
68115
MC/DC100%2/2
$spec->multiple and $spec->mapping
$spec->multiple $spec->mapping
Truth table100%3/3
A and B
A= $spec->multipleB= $spec->mapping
ABresult
0X0
100
111
77 5 ▶ if (not defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 5 5
78 0 $items->{ $name } = $value = {};
79 }
80 5 $values = $value;
81  
82 5 ▶ if (not keys %$values) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %$values) 0 5 5
83 0 ▶ if (defined (my $default = $spec->default)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $default = $spec->default)) 0 0 0
84 0 $values = { split m/=/, $default, 2 };
85 0 $items->{ $name } = $values;
86 }
87 }
88  
89 5 ▶ if (not keys %$values and $spec->required) {
Branch: 1 branch
BranchTrueFalseTotal
if (not keys %$values and $spec->required) 0 5 5
Condition33%1/3
not keys %$values and $spec->required
!ll&&!rl&&r
500
MC/DC0%0/2
not keys %$values and $spec->required
not keys %$values $spec->required
Truth table33%1/3
not(A) and B
A= not keys %$valuesB= $spec->required
ABresult
0X0
100
111
90 0 $errs->{ $type }->{ $name } = "missing";
91 0 next;
92 }
93  
94 5 ▶ if (not keys %$values) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %$values) 0 5 5
95 0 next;
96 }
97  
98 }
99 elsif ($spec->multiple) {
100 11 ▶ if (not defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 11 11
101 0 $items->{ $name } = $value = [];
102 }
103 11 $values = $value;
104  
105 11 ▶ if (not @$values) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$values) 6 5 11
106 6 ▶ if (defined (my $default = $spec->default)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $default = $spec->default)) 0 6 6
107 0 $values = [ $default ];
108 0 $items->{ $name } = $values;
109 }
110 }
111  
112 11 ▶ if ( not @$values and $spec->required) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @$values and $spec->required) 6 5 11
Condition66%2/3
not @$values and $spec->required
!ll&&!rl&&r
506
MC/DC50%1/2
not @$values and $spec->required
not @$values $spec->required
Truth table66%2/3
not(A) and B
A= not @$valuesB= $spec->required
ABresult
0X0
100
111
113 6 $errs->{ $type }->{ $name } = "missing";
114 6 next;
115 }
116  
117 5 ▶ if (not @$values) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$values) 0 5 5
118 0 next;
119 }
120  
121 5 ▶ if ($spec->unique and (uniq @$values) != @$values) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->unique and &uniq(@$values) != @$values) 1 4 5
Condition66%2/3
$spec->unique and &uniq(@$values) != @$values
!ll&&!rl&&r
041
MC/DC50%1/2
$spec->unique and &uniq(@$values) != @$values
$spec->unique &uniq(@$values) != @$values
Truth table66%2/3
A and B
A= $spec->uniqueB= &uniq(@$values) != @$values
ABresult
0X0
100
111
122 1 $errs->{ $type }->{ $name } = "not_unique";
123 1 next;
124 }
125  
126 }
127 else {
128  
129 68 ▶ if (not defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 38 30 68
130 38 ▶ if (defined (my $default = $spec->default)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $default = $spec->default)) 0 38 38
131 0 $value = $default;
132 0 $items->{ $name } = $value;
133 }
134 }
135  
136 68 ▶ if ( not defined $value and $spec->required) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value and $spec->required) 4 64 68
Condition100%3/3
not defined $value and $spec->required
!ll&&!rl&&r
30344
MC/DC100%2/2
not defined $value and $spec->required
not defined $value $spec->required
Truth table100%3/3
not(A) and B
A= not defined $valueB= $spec->required
ABresult
0X0
100
111
137 4 $errs->{ $type }->{ $name } = "missing";
138 4 next;
139 }
140  
141 64 ▶ if (not defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 34 30 64
142 34 next;
143 }
144  
145 30 $values = [ $value ];
146 }
147  
148 39 my $def;
149 39 ▶ if (ref $param_type eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $param_type eq "HASH") 0 39 39
150 0 ($param_type, $def) = %$param_type;
151 }
152 39 ▶ my $code = $validate{ $param_type }
Branch: 1 branch
BranchTrueFalseTotal
my $code = $validate{$param_type} or die "Missing method for validation type $param_type" 39 0 39
153 or die "Missing method for validation type $param_type";
154  
155 39 ▶ my $possible_values = $spec->mapping ? {} : [];
Branch: 1 branch
BranchTrueFalseTotal
$spec->mapping ? : 5 34 39
156 39 ▶ if (my $spec_values = $spec->values) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $spec_values = $spec->values) 25 14 39
157 25 ▶ if (my $op = $spec_values->{op}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $op = $spec_values->{'op'}) { } 20 5 25
elsif ($spec->mapping) { } 5 0 5
158 20 my $args = {
159 runmode => "validation",
160 parameter => $name,
161 };
162 20 ▶ $possible_values = $run->cmd->$op($run, $args) || [];
Condition100%2/2
$run->cmd->$op($run, $args) || []
l!l
173
MC/DC100%1/1
$run->cmd->$op($run, $args) || []
$run->cmd->$op($run, $args)
Truth table100%2/2
A
A= $run->cmd->$op($run, $args)
Aresult
11
00
163 }
164 elsif ($spec->mapping) {
165 5 $possible_values = $spec_values->{mapping};
166 }
167 else {
168 0 $possible_values = $values->{enum};
169 }
170 }
171  
172 my @to_check = $spec->mapping
173 39 ▶ ? map { [ $_ => $values->{ $_ } ] } keys %$values
Branch: 1 branch
BranchTrueFalseTotal
$spec->mapping ? : 5 34 39
174 : @$values;
175 39 for my $item (@to_check) {
176 42 my ($key, $v);
177 42 ▶ if ($spec->mapping) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->mapping) { } 6 36 42
178 6 ($key, $v) = @$item;
179 }
180 else {
181 36 $v = $item;
182 }
183 # check type validity
184 42 my $ok = $code->($v, $def);
185 42 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 4 38 42
186 4 $errs->{ $type }->{ $name } = "invalid $param_type";
187 }
188 # check static enums
189 42 ▶ if ($enum) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enum) 7 35 42
190 my $code = $validate{enum}
191 7 ▶ or die "Missing method for validation type enum";
Branch: 1 branch
BranchTrueFalseTotal
my $code = $validate{"enum"} or die "Missing method for validation type enum" 7 0 7
192 7 my $ok = $code->($v, $enum);
193 7 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 6 7
194 1 $errs->{ $type }->{ $name } = "invalid enum";
195 }
196 }
197 42 ▶ if ($param_type eq 'file' and $v eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param_type eq "file" and $v eq "-") 0 42 42
Condition33%1/3
$param_type eq "file" and $v eq "-"
!ll&&!rl&&r
4200
MC/DC0%0/2
$param_type eq "file" and $v eq "-"
$param_type eq "file" $v eq "-"
Truth table33%1/3
A and B
A= $param_type eq "file"B= $v eq "-"
ABresult
0X0
100
111
198 0 $v = do { local $/; my $t = <STDIN>; \$t };
199 # TODO does not work for multiple
200 0 $items->{ $name } = $v;
201 }
202  
203 42 ▶ if ($spec->mapping and keys %$possible_values) {
Branch: 2 branches
BranchTrueFalseTotal
if ($spec->mapping and keys %$possible_values) { } 6 36 42
elsif (@$possible_values) { } 19 17 36
Condition66%2/3
$spec->mapping and keys %$possible_values
!ll&&!rl&&r
3606
MC/DC50%1/2
$spec->mapping and keys %$possible_values
$spec->mapping keys %$possible_values
Truth table66%2/3
A and B
A= $spec->mappingB= keys %$possible_values
ABresult
0X0
100
111
204 6 my $ok = 0;
205 6 ▶ if (exists $possible_values->{ $key }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $possible_values->{$key}) 3 3 6
206 3 ▶ if (my $list = $possible_values->{ $key }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $list = $possible_values->{$key}) { } 2 1 3
207 2 ▶ $ok = any { $_ eq $v } @$list;
Subroutine __ANON__: called CC=1 SCAR=0.0
208 }
209 else {
210 # can have any value
211 1 $ok = 1;
212 }
213 }
214 6 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 3 3 6
215 3 $errs->{ $type }->{ $name } = "invalid value";
216 }
217 }
218 elsif (@$possible_values) {
219 35 ▶ my $ok = any {
Subroutine __ANON__: called CC=2 SCAR=6.9
220 35 ▶ is_hashref($_) ? $_->{name} eq $v : $_ eq $v
Branch: 1 branch
BranchTrueFalseTotal
is_hashref($_) ? : 17 18 35
221 19 } @$possible_values;
222 19 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 3 16 19
223 3 $errs->{ $type }->{ $name } = "invalid value";
224 }
225 }
226 }
227 }
228 51 ▶ return (keys %$errs) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
keys %$errs ? : 15 36 51
229 }
230  
231 6 1;
232  
233 __END__
« Spec/Completion/Zsh.pm ↑ Summary Spec/Plugin/Format.pm »