blib/lib/Test/Smoke/App/AppOption.pm

filter: Statementstmt 98.4% 65 / 66 Branchbran 100% 30 / 30 Conditioncond 100% 14 / 14 MC/DCmcdc 100% 8 / 8 Subroutinesub 80.0% 8 / 10 Podpod 100% 4 / 4 totaltotal 97.7% 129 / 132 CC 22 SCAR 18.1
CC
22
Cov
99%
allowed
9.0
new
7.0
show_helptext
7.0
CRAP 6.1
« blib/lib/Test/Smoke/App/RunApp.pm ↑ Summary blib/lib/Test/Smoke/Util/Win32ErrorMode.pm »
1 package Test::Smoke::App::AppOption;
2 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 13 our $VERSION = '0.002';
7  
8 13 ▶use base 'Test::Smoke::ObjectBase';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 13 our $HTFMT = "%-30s - %s\n";
11  
12 =head1 NAME
13  
14 Test::Smoke::App::AppOption - Object that represents an Application Option.
15  
16 =head1 SYNOPSIS
17  
18 use Test::Smoke::App::AppOption;
19 my $o = Test::Smoke::App::AppOption->new(
20 );
21 printf "%s\n", $o->gol_option;
22 print $o->show_helptext;
23  
24 =head1 DESCRIPTION
25  
26 =head2 Test::Smoke::App::AppOption->new(%arguments)
27  
28 =head3 Arguments
29  
30 Named:
31  
32 =over
33  
34 =item name => $basic_option_name [required]
35  
36 =item option => $option_extention (see L<Getopt::Long>)
37  
38 =item allow => $arrary_ref_with alternatives
39  
40 =item default => $default_value
41  
42 =item helptext => $text_to_show_with help
43  
44 =back
45  
46 =head3 Returns
47  
48 An instance.
49  
50 =head3 Exceptions
51  
52 croak()s when:
53  
54 =over
55  
56 =item B<name not set>
57  
58 =item B<allow is not undef or ref($allow) is Regexp, CODE or ARRAY>
59  
60 =back
61  
62 =cut
63  
64 827 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
65 827 my $class = shift;
66 827 my %args = @_;
67  
68 my $struct = {
69 _name => undef,
70 _option => "",
71 _allow => undef,
72 _default => undef,
73 _helptext => "",
74 _configtext => "",
75 _configtype => "prompt",
76 0 ▶ _configalt => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
77 0 ▶ _configdft => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
78 827 _configfnex => 0,
79 _configord => 0,
80 };
81 827 $struct->{_had_default} = exists $args{default};
82 827 for my $known (keys %$struct) {
83 9924 (my $key = $known) =~ s/^_//;
84 9924 ▶ $struct->{$known} = delete $args{$key} if exists $args{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$key} 4664 5260 9924
85 }
86 827 ▶ if (!defined($struct->{_name}) || !length($struct->{_name})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $struct->{"_name"} and length $struct->{"_name"}) 2 825 827
Condition100%3/3
defined $struct->{"_name"} and length $struct->{"_name"}
!ll&&!rl&&r
11825
MC/DC100%2/2
defined $struct->{"_name"} and length $struct->{"_name"}
defined $struct->{"_name"} length $struct->{"_name"}
Truth table100%3/3
A and B
A= defined $struct->{"_name"}B= length $struct->{"_name"}
ABresult
0X0
100
111
87 2 croak("Required option 'name' not given.");
88 }
89 825 ▶ if ( defined($struct->{_allow})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $struct->{"_allow"} and not ref($struct->{"_allow"}) =~ /^(?:ARRAY|Regexp|CODE)$/) 1 824 825
Condition100%3/3
defined $struct->{"_allow"} and not ref($struct->{"_allow"}) =~ /^(?:ARRAY|Regexp|CODE)$/
!ll&&!rl&&r
6212031
MC/DC100%2/2
defined $struct->{"_allow"} and not ref($struct->{"_allow"}) =~ /^(?:ARRAY|Regexp|CODE)$/
defined $struct->{"_allow"} not ref($struct->{"_allow"}) =~ /^(?:ARRAY|Regexp|CODE)$/
Truth table100%3/3
A and not(B)
A= defined $struct->{"_allow"}B= not ref($struct->{"_allow"}) =~ /^(?:ARRAY|Regexp|CODE)$/
ABresult
0X0
100
111
90 and (ref($struct->{_allow}) !~ /^(?:ARRAY|Regexp|CODE)$/))
91 {
92 1 croak("Option 'allow' must be an ArrayRef|CodeRef|RegExp when set");
93 }
94 # had_default(): order == code < configfile < commandline
95  
96 824 return bless $struct, $class;
97 }
98  
99 =head2 $option->allowed($value[, $allow])
100  
101 Checks if a value is in a set of allowed values.
102  
103 =head3 Arguments
104  
105 Positional.
106  
107 =over
108  
109 =item $value (the value to check)
110  
111 =item $allow [optional]
112  
113 C<$allow> can be:
114  
115 =over 8
116  
117 =item * ArrayRef => a list of allowed() items
118  
119 =item * Regex => a regex to test C<$value> against.
120  
121 =item * CodeRef => a coderef that is executed with C<$value>
122  
123 =item * other_value => $value eq $other_value (checks for definedness)
124  
125 =back
126  
127 =back
128  
129 =head3 Returns
130  
131 (perl) True of False.
132  
133 =cut
134  
135 847 ▶sub allowed {
Subroutine allowed: called CC=9 SCAR=22.0
Pod documented
136 847 my $self = shift;
137 847 ▶ return 1 if !defined $self->allow;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->allow 348 499 847
138  
139 499 my ($value, $allow) = @_;
140 499 ▶ $allow = $self->allow if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 145 354 499
141 GIVEN: {
142 499 local $_ = ref($allow);
143  
144 499 ▶ /^ARRAY$/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/^ARRAY$/ and do { return scalar grep($self->allowed($value, $_), @$allow) } 123 376 499
145 123 return scalar grep $self->allowed($value, $_), @$allow;
146 };
147 376 ▶ /^Regexp$/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/^Regexp$/ and do { return ($value || "") =~ /$allow/ } 54 322 376
148 54 ▶ return ($value || '') =~ $allow;
Condition100%2/2
$value || ''
l!l
1737
MC/DC100%1/1
$value || ''
$value
Truth table100%2/2
A
A= $value
Aresult
11
00
149 };
150 322 ▶ /^CODE$/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/^CODE$/ and do { return &$allow($value) } 13 309 322
151 13 return $allow->($value);
152 };
153 # default
154 309 do {
155 309 ▶ if (!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 11 298 309
156 11 return !defined $allow;
157 }
158 298 ▶ return 0 if !defined $allow;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $allow 28 270 298
159 270 return $value eq $allow;
160 };
161 }
162 }
163  
164 =head2 $opt->gol_option
165  
166 Getopt::Long compatible option string.
167  
168 =cut
169  
170 16336 ▶sub gol_option {
Subroutine gol_option: called CC=2 SCAR=6.9
Pod documented
171 16336 my $self = shift;
172  
173 16336 my $gol = $self->name;
174 16336 ▶ if ($self->option !~ /^(=|!|\||$)/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->option =~ /^(=|!|\||$)/) 2210 14126 16336
175 2210 $gol .= "|";
176 }
177 16336 $gol .= $self->option;
178 16336 return $gol;
179 }
180  
181 =head2 $opt->show_helptext()
182  
183 sprintf "%-30s - %s", $option_with_allowd, $self->helptext
184  
185 =cut
186  
187 697 ▶sub show_helptext {
Subroutine show_helptext: called CC=7 SCAR=19.5
Pod documented
188 697 my $self = shift;
189  
190 697 my $prefix = '--';
191 697 ▶ if ($self->option =~ /!$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->option =~ /!$/) 80 617 697
192 80 $prefix .= '[no]';
193 }
194 697 my @option = ($prefix . $self->gol_option);
195  
196 697 ▶ if ( defined($self->allow)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->allow and ref $self->allow eq "ARRAY" and @{$self->allow;}) 141 556 697
Condition100%6/6
defined $self->allow and ref $self->allow eq "ARRAY" and @{$self->allow;}
!ll&&!rl&&r
5551141
defined $self->allow and ref $self->allow eq "ARRAY"
!ll&&!rl&&r
51837142
MC/DC100%3/3
defined $self->allow and ref $self->allow eq "ARRAY" and @{$self->allow;}
defined $self->allow ref $self->allow eq "ARRAY" @{$self->allow;}
Truth table100%4/4
A and B and C
A= defined $self->allowB= ref $self->allow eq "ARRAY"C= @{$self->allow;}
ABCresult
0XX0
10X0
1100
1111
197 142 && ref($self->allow) eq 'ARRAY' && @{$self->allow})
198 {
199 my @values = sort {
200 368 ▶ lc($a) cmp lc($b)
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 359 40 399
201 } map
202 defined($_) ? $_ : "'undef'"
203 141 , @{$self->allow};
204 141 my $allowed = join('|', @values);
205 141 push @option, "<$allowed>";
206 }
207  
208 697 my $text = join(" ", @option);
209  
210 697 ▶ return $text if !$self->helptext;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->helptext 1 696 697
211 696 return sprintf($HTFMT, $text, $self->helptext);
212 }
213  
214 13 1;
215  
216 =head1 COPYRIGHT
217  
218 (c) 2002-2013, Abe Timmerman <abeltje@cpan.org> All rights reserved.
219  
220 With contributions from Jarkko Hietaniemi, Merijn Brand, Campo
221 Weijerman, Alan Burlison, Allen Smith, Alain Barbet, Dominic Dunlop,
222 Rich Rauenzahn, David Cantrell.
223  
224 This library is free software; you can redistribute it and/or modify
225 it under the same terms as Perl itself.
226  
227 See:
228  
229 =over 4
230  
231 =item * L<http://www.perl.com/perl/misc/Artistic.html>
232  
233 =item * L<http://www.gnu.org/copyleft/gpl.html>
234  
235 =back
236  
237 This program is distributed in the hope that it will be useful,
238 but WITHOUT ANY WARRANTY; without even the implied warranty of
239 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
240  
241 =cut
« blib/lib/Test/Smoke/App/RunApp.pm ↑ Summary blib/lib/Test/Smoke/Util/Win32ErrorMode.pm »