Kit/Options.pm

filter: Statementstmt 100% 101 / 101 Branchbran 100% 74 / 74 Conditioncond 94.5% 35 / 37 MC/DCmcdc 90.9% 20 / 22 Subroutinesub 100% 10 / 10 Podpod 100% 6 / 6 totaltotal 98.4% 246 / 250 CC 53 SCAR 26.5
CC
53
Cov
99%
validate
21.0
step_value
15.0
argv_for
11.0
CRAP 14.1
« Kit/Picker.pm ↑ Summary Kit/Steer.pm »
1 package Game::Terminal::Kit::Options;
2  
3 4 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Text::ParseWords ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 our $VERSION = '0.02';
10  
11 4 my %TYPE = map { $_ => 1 } qw/int enum flag bool str/;
12 4 my %RESERVED = map { $_ => 1 } qw/ascii pick help version extra/;
13  
14 1 ▶sub types { return sort keys %TYPE }
Subroutine types: called CC=1 SCAR=0.0
Pod documented
15  
16 1 ▶sub reserved { return sort keys %RESERVED }
Subroutine reserved: called CC=1 SCAR=0.0
Pod documented
17  
18 48 ▶sub step_value {
Subroutine step_value: called CC=15 SCAR=27.1
Pod documented
19 48 my ($field, $current, $direction) = @_;
20 48 my $type = $field->{type};
21 48 ▶ if ($type eq 'int') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "int") 23 25 48
22 23 ▶ return $direction > 0 ? $field->{min} : $field->{max} unless defined $current;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $current 6 17 23
$direction > 0 ? : 4 2 6
23 17 my $next = $current + $direction;
24 17 ▶ return $next < $field->{min} ? $field->{min} : $next > $field->{max} ? $field->{max} : $next;
Branch: 2 branches
BranchTrueFalseTotal
$next < $field->{'min'} ? : 1 16 17
$next > $field->{'max'} ? : 8 8 16
25 }
26 25 ▶ if ($type eq 'enum') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "enum") 10 15 25
27 10 my @choices = @{$field->{choices}};
28 10 ▶ return $direction > 0 ? $choices[0] : $choices[-1] unless defined $current;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $current 4 6 10
$direction > 0 ? : 3 1 4
29 6 my ($index) = grep { $choices[$_] eq $current } 0 .. $#choices;
30 6 ▶ $index = 0 unless defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 1 5 6
31 6 return $choices[ ($index + $direction) % @choices ];
32 }
33 15 ▶ if ($type eq 'flag') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "flag") 5 10 15
34 5 ▶ return $current ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
$current ? : 3 2 5
35 }
36 10 ▶ if ($type eq 'bool') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "bool") 8 2 10
37 8 my @cycle = (undef, 1, 0);
38 8 ▶ my $index = !defined $current ? 0 : $current ? 1 : 2;
Branch: 2 branches
BranchTrueFalseTotal
!defined($current) ? : 3 5 8
$current ? : 2 3 5
39 8 return $cycle[ ($index + $direction) % 3 ];
40 }
41 2 return $current;
42 }
43  
44 24 ▶sub check_value {
Subroutine check_value: called CC=9 SCAR=22.0
Pod documented
45 24 my ($field, $text) = @_;
46 24 ▶ $text = '' unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 1 23 24
47 24 $text =~ s/^\s+|\s+$//g;
48 24 ▶ return (undef, undef) unless length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 4 20 24
49 20 my $type = $field->{type};
50 20 ▶ if ($type eq 'int') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "int") 13 7 20
51 return (undef, "$field->{name} takes a whole number from $field->{min} to $field->{max}")
52 13 ▶ unless $text =~ m/^-?\d+$/ && $text >= $field->{min} && $text <= $field->{max};
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /^-?\d+$/u and $text >= $field->{"min"} and $text <= $field->{"max"} 8 5 13
Condition100%6/6
$text =~ /^-?\d+$/u and $text >= $field->{"min"} and $text <= $field->{"max"}
!ll&&!rl&&r
355
$text =~ /^-?\d+$/u and $text >= $field->{"min"}
!ll&&!rl&&r
2110
MC/DC100%3/3
$text =~ /^-?\d+$/u and $text >= $field->{"min"} and $text <= $field->{"max"}
$text =~ /^-?\d+$/u $text >= $field->{"min"} $text <= $field->{"max"}
Truth table100%4/4
A and B and C
A= $text =~ /^-?\d+$/uB= $text >= $field->{"min"}C= $text <= $field->{"max"}
ABCresult
0XX0
10X0
1100
1111
53 5 return ($text + 0, undef);
54 }
55 7 ▶ if ($type eq 'enum') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "enum") 2 5 7
56 2 my ($match) = grep { lc $_ eq lc $text } @{$field->{choices}};
57 2 ▶ return ($match, undef) if defined $match;
Branch: 1 branch
BranchTrueFalseTotal
if defined $match 1 1 2
58 1 return (undef, "$field->{name} takes " . join(', ', @{$field->{choices}}));
59 }
60 5 return ($text, undef);
61 }
62  
63 23 ▶sub argv_for {
Subroutine argv_for: called CC=11 SCAR=24.0
Pod documented
64 23 my ($fields, $values) = @_;
65 23 ▶ $values ||= {};
Condition100%2/2
$values ||= {}
l!l
221
MC/DC100%1/1
$values ||= {}
$values
Truth table100%2/2
A
A= $values
Aresult
11
00
66 23 my @argv;
67 23 ▶ for my $field (@{ $fields || [] }) {
Condition100%2/2
$fields || []
l!l
221
MC/DC100%1/1
$fields || []
$fields
Truth table100%2/2
A
A= $fields
Aresult
11
00
68 104 my $value = $values->{ $field->{name} };
69 104 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 81 23 104
70 23 my $type = $field->{type};
71 23 ▶ if ($type eq 'flag') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'flag') { } 3 20 23
elsif ($type eq 'bool') { } 4 16 20
72 3 ▶ push @argv, "--$field->{name}" if $value;
Branch: 1 branch
BranchTrueFalseTotal
if $value 2 1 3
73 } elsif ($type eq 'bool') {
74 4 ▶ push @argv, $value ? "--$field->{name}" : "--no-$field->{name}";
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 2 2 4
75 } else {
76 16 push @argv, "--$field->{name}", $value;
77 }
78 }
79 push @argv, Text::ParseWords::shellwords($values->{extra})
80 23 ▶ if defined $values->{extra} && length $values->{extra};
Branch: 1 branch
BranchTrueFalseTotal
if defined $values->{"extra"} and length $values->{"extra"} 3 20 23
Condition66%2/3
defined $values->{"extra"} and length $values->{"extra"}
!ll&&!rl&&r
2003
MC/DC50%1/2
defined $values->{"extra"} and length $values->{"extra"}
defined $values->{"extra"} length $values->{"extra"}
Truth table66%2/3
A and B
A= defined $values->{"extra"}B= length $values->{"extra"}
ABresult
0X0
100
111
81 23 return @argv;
82 }
83  
84 24 ▶sub validate {
Subroutine validate: called CC=21 SCAR=30.4
Pod documented
85 24 my ($fields) = @_;
86 24 ▶ return ('the options are not an array reference') unless ref $fields eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fields eq "ARRAY" 1 23 24
87 23 my (@wrong, %seen);
88 23 for my $index (0 .. $#{$fields}) {
89 32 my $field = $fields->[$index];
90 32 ▶ unless (ref $field eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $field eq "HASH") 2 30 32
91 2 push @wrong, "option $index is not a hash reference";
92 2 next;
93 }
94 30 my $name = $field->{name};
95 30 ▶ unless (defined $name && $name =~ m/^[a-z][a-z0-9-]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name and $name =~ /^[a-z][a-z0-9-]*$/u) 3 27 30
Condition100%3/3
defined $name and $name =~ /^[a-z][a-z0-9-]*$/u
!ll&&!rl&&r
1227
MC/DC100%2/2
defined $name and $name =~ /^[a-z][a-z0-9-]*$/u
defined $name $name =~ /^[a-z][a-z0-9-]*$/u
Truth table100%3/3
A and B
A= defined $nameB= $name =~ /^[a-z][a-z0-9-]*$/u
ABresult
0X0
100
111
96 3 push @wrong, "option $index has no name a command line could carry";
97 3 next;
98 }
99 27 ▶ push @wrong, "$name is there twice" if $seen{$name}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$name}++ 2 25 27
100 27 ▶ push @wrong, "$name is a name the launcher keeps" if $RESERVED{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $RESERVED{$name} 3 24 27
101 push @wrong, "$name has no help"
102 27 ▶ unless defined $field->{help} && length $field->{help};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field->{"help"} and length $field->{"help"} 3 24 27
Condition100%3/3
defined $field->{"help"} and length $field->{"help"}
!ll&&!rl&&r
2124
MC/DC100%2/2
defined $field->{"help"} and length $field->{"help"}
defined $field->{"help"} length $field->{"help"}
Truth table100%3/3
A and B
A= defined $field->{"help"}B= length $field->{"help"}
ABresult
0X0
100
111
103 27 my $type = $field->{type};
104 27 ▶ unless (defined $type && $TYPE{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type and $TYPE{$type}) 3 24 27
Condition100%3/3
defined $type and $TYPE{$type}
!ll&&!rl&&r
1224
MC/DC100%2/2
defined $type and $TYPE{$type}
defined $type $TYPE{$type}
Truth table100%3/3
A and B
A= defined $typeB= $TYPE{$type}
ABresult
0X0
100
111
105 3 push @wrong, "$name has no known type";
106 3 next;
107 }
108 24 ▶ if ($type eq 'int') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "int") 6 18 24
109 6 ▶ my @ends = grep { defined $_ && m/^-?\d+$/ } $field->{min}, $field->{max};
Condition100%3/3
defined $_ && /^-?\d+$/u
!ll&&!rl&&r
1110
MC/DC100%2/2
defined $_ && /^-?\d+$/u
defined $_ /^-?\d+$/u
Truth table100%3/3
A && B
A= defined $_B= /^-?\d+$/u
ABresult
0X0
100
111
110 push @wrong, "$name needs a whole min and max, min first"
111 6 ▶ unless @ends == 2 && $field->{min} <= $field->{max};
Branch: 1 branch
BranchTrueFalseTotal
unless @ends == 2 and $field->{"min"} <= $field->{"max"} 3 3 6
Condition100%3/3
@ends == 2 and $field->{"min"} <= $field->{"max"}
!ll&&!rl&&r
213
MC/DC100%2/2
@ends == 2 and $field->{"min"} <= $field->{"max"}
@ends == 2 $field->{"min"} <= $field->{"max"}
Truth table100%3/3
A and B
A= @ends == 2B= $field->{"min"} <= $field->{"max"}
ABresult
0X0
100
111
112 }
113 24 ▶ if ($type eq 'enum') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "enum") 5 19 24
114 5 my $choices = $field->{choices};
115 push @wrong, "$name needs two or more choices"
116 4 unless ref $choices eq 'ARRAY' && @{$choices} >= 2
117 5 ▶ && !grep { !defined $_ || !length $_ } @{$choices};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $choices eq "ARRAY" and @{$choices;} >= 2 and not grep({not defined $_ && length $_;} @{$choices;}) 3 2 5
Condition88%8/9
ref $choices eq "ARRAY" and @{$choices;} >= 2 and not grep({not defined $_ && length $_;} @{$choices;})
!ll&&!rl&&r
212
ref $choices eq "ARRAY" and @{$choices;} >= 2
!ll&&!rl&&r
113
defined $_ && length $_
!ll&&!rl&&r
016
MC/DC100%3/3
ref $choices eq "ARRAY" and @{$choices;} >= 2 and not grep({not defined $_ && length $_;} @{$choices;})
ref $choices eq "ARRAY" @{$choices;} >= 2 not grep({not defined $_ && length $_;} @{$choices;})
MC/DC50%1/2
defined $_ && length $_
defined $_ length $_
Truth table100%4/4
A and B and not(C)
A= ref $choices eq "ARRAY"B= @{$choices;} >= 2C= not grep({not defined $_ && length $_;} @{$choices;})
ABCresult
0XX0
10X0
1100
1111
Truth table66%2/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
118 }
119 }
120 23 return @wrong;
121 }
122  
123 4 1;
124  
125 __END__
« Kit/Picker.pm ↑ Summary Kit/Steer.pm »