Ask/API.pm

filter: Statementstmt 53.8% 49 / 91 Branchbran 50.0% 9 / 18 Conditioncond 25.0% 3 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 62.5% 10 / 16 Podpod 0.0% 0 / 8 totaltotal 46.7% 71 / 152 CC 16 SCAR 21.9
CC
16
Cov
50%
single_choice
20.0
file_selection
13.4
multiple_choice
12.0
CRAP 8.9
« Ask.pm ↑ Summary Ask/Zenity.pm »
1 4 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Ask::API;
6  
7 4 our $AUTHORITY = 'cpan:TOBYINK';
8 4 our $VERSION = '0.015';
9  
10 4 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Path::Tiny 'path';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 requires 'entry'; # get a string of text
14 4 requires 'info'; # display a string of text
15  
16 3 ▶sub _lang_support {
Subroutine _lang_support: called CC=1 SCAR=0.0
17 3 my $self = shift;
18 3 require Lingua::Boolean::Tiny;
19 3 "Lingua::Boolean::Tiny"->new( @_ );
20 }
21  
22 0 ▶sub is_usable {
Subroutine is_usable: not called CC=1 SCAR=6.9
Pod undocumented
23 0 my ( $self ) = @_;
24 0 return 1;
25 }
26  
27 0 ▶sub quality {
Subroutine quality: not called CC=1 SCAR=6.9
Pod undocumented
28 0 return 50;
29 }
30  
31 1 ▶sub warning {
Subroutine warning: called CC=1 SCAR=0.0
Pod undocumented
32 1 my ( $self, %o ) = @_;
33 1 $o{text} = "WARNING: $o{text}";
34 1 return $self->info( %o );
35 }
36  
37 2 ▶sub error {
Subroutine error: called CC=1 SCAR=0.0
Pod undocumented
38 2 my ( $self, %o ) = @_;
39 2 $o{text} = "ERROR: $o{text}";
40 2 return $self->info( %o );
41 }
42  
43 3 ▶sub question {
Subroutine question: called CC=1 SCAR=0.0
Pod undocumented
44 3 my ( $self, %o ) = @_;
45
46 3 my $response = $self->entry( text => $o{text} );
47 3 my $lang = $self->_lang_support( $o{lang} );
48 3 $lang->boolean( $response );
49 }
50  
51 2 ▶sub file_selection {
Subroutine file_selection: called CC=10 SCAR=26.0
Pod undocumented
52 2 my ( $self, %opts ) = ( shift, @_ );
53
54 2 ▶ $opts{text} ||= 'Enter file name';
Condition50%1/2
$opts{"text"} ||= "Enter file name"
l!l
20
MC/DC0%0/1
$opts{"text"} ||= "Enter file name"
$opts{"text"}
Truth table50%1/2
A
A= $opts{"text"}
Aresult
11
00
55
56 2 my @chosen;
57
58 FILE: {
59 2 my $got = $self->entry( text => $opts{text} );
60
61 5 ▶ if ( not length $got ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $got) 1 4 5
62 1 ▶ last FILE if $opts{multiple};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"multiple"} 1 0 1
63 0 redo FILE;
64 }
65
66 4 $got = path $got;
67
68 4 ▶ if ( $opts{existing} and not $got->exists ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"existing"} and not $got->exists) 0 4 4
Condition33%1/3
$opts{"existing"} and not $got->exists
!ll&&!rl&&r
400
MC/DC0%0/2
$opts{"existing"} and not $got->exists
$opts{"existing"} not $got->exists
Truth table33%1/3
A and not(B)
A= $opts{"existing"}B= not $got->exists
ABresult
0X0
100
111
69 0 $self->error( text => 'Does not exist.' );
70 0 redo FILE;
71 }
72
73 4 ▶ if ( $opts{directory} and not $got->is_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"directory"} and not $got->is_dir) 0 4 4
Condition33%1/3
$opts{"directory"} and not $got->is_dir
!ll&&!rl&&r
400
MC/DC0%0/2
$opts{"directory"} and not $got->is_dir
$opts{"directory"} not $got->is_dir
Truth table33%1/3
A and not(B)
A= $opts{"directory"}B= not $got->is_dir
ABresult
0X0
100
111
74 0 $self->error( text => 'Is not a directory.' );
75 0 redo FILE;
76 }
77
78 4 push @chosen, $got;
79
80 4 ▶ if ( $opts{multiple} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"multiple"}) 3 1 4
81 3 $self->info( text => 'Enter another file, or leave blank to finish.' );
82 3 redo FILE;
83 }
84 } #/ FILE:
85
86 2 ▶ $opts{multiple} ? @chosen : $chosen[0];
Branch: 1 branch
BranchTrueFalseTotal
$opts{'multiple'} ? : 1 1 2
87 } #/ sub file_selection
88  
89 0 ▶my $format_choices = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
90 0 my ( $self, $choices ) = @_;
91 0 join q[, ], map { sprintf( '"%s" (%s)', @$_ ) } @$choices;
92 4 };
93  
94 0 ▶my $filter_chosen = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
95 0 my ( $self, $choices, $response ) = @_;
96 0 my $valid = {};
97 0 $valid->{ $_->[0] }++ for @$choices;
98 0 my @choices = ( $response =~ /\w+/g );
99 return (
100 [ grep $valid->{$_}, @choices ],
101 0 [ grep !$valid->{$_}, @choices ],
102 );
103 4 };
104  
105 0 ▶sub multiple_choice {
Subroutine multiple_choice: not called CC=3 SCAR=24.8
Pod undocumented
106 0 my ( $self, %o ) = @_;
107 0 my $choices = $self->$format_choices( $o{choices} );
108
109 0 my ( $allowed, $disallowed, $repeat );
110
111 0 for ( ; ; ) {
112 0 ▶ my $response = $self->entry(
Condition0%0/2
$repeat || ''
l!l
00
MC/DC0%0/1
$repeat || ''
$repeat
Truth table0%0/2
A
A= $repeat
Aresult
11
00
113 text =>
114 "$o{text}. Choices: $choices. (Separate multiple choices with white space.)",
115 entry_text => $repeat || '',
116 );
117 0 ( $allowed, $disallowed ) = $self->$filter_chosen( $o{choices}, $response );
118 0 ▶ if ( @$disallowed ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$disallowed) { } 0 0 0
119 0 my $d = join q[, ], @$disallowed;
120 0 $self->error(
121 text => "Not valid: $d. Please try again.",
122 );
123 0 $repeat = join q[ ], @$allowed;
124 }
125 else {
126 0 last;
127 }
128 } #/ for ( ; ; )
129
130 0 return @$allowed;
131 } #/ sub multiple_choice
132  
133 0 ▶sub single_choice {
Subroutine single_choice: not called CC=4 SCAR=30.0
Pod undocumented
134 0 my ( $self, %o ) = @_;
135 0 my $choices = $self->$format_choices( $o{choices} );
136
137 0 my ( $allowed, $disallowed, $repeat );
138
139 0 for ( ; ; ) {
140 0 ▶ my $response = $self->entry(
Condition0%0/2
$repeat || ''
l!l
00
MC/DC0%0/1
$repeat || ''
$repeat
Truth table0%0/2
A
A= $repeat
Aresult
11
00
141 text => "$o{text}. Choices: $choices. (Choose one.)",
142 entry_text => $repeat || '',
143 );
144 0 ( $allowed, $disallowed ) = $self->$filter_chosen( $o{choices}, $response );
145 0 ▶ if ( @$disallowed ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$disallowed) { } 0 0 0
elsif (@$allowed != 1) { } 0 0 0
146 0 my $d = join q[, ], @$disallowed;
147 0 $self->error(
148 text => "Not valid: $d. Please try again.",
149 );
150 0 $repeat = $allowed->[0];
151 }
152 elsif ( @$allowed != 1 ) {
153 0 $self->error(
154 text => "Not valid: choose one.",
155 );
156 0 $repeat = $allowed->[0];
157 }
158 else {
159 0 last;
160 }
161 } #/ for ( ; ; )
162
163 0 return $allowed->[0];
164 } #/ sub single_choice
165  
166 4 1;
167  
168 __END__
« Ask.pm ↑ Summary Ask/Zenity.pm »