lib/Devel/Trepan/Util.pm

filter: Statementstmt 42.8% 75 / 175 Branchbran 40.8% 40 / 98 Conditioncond 29.1% 7 / 24 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 57.1% 16 / 28 Podpod 0.0% 0 / 22 totaltotal 38.7% 140 / 361 CC 57 SCAR 22.6
CC
57
Cov
41%
safe_repr
21.8
safe_repr
21.8
extract_expression
14.8
CRAP 9.6
« blib/lib/Devel/Trepan/CmdProcessor/Command/Set_Subcmd/Variable.pm ↑ Summary lib/Devel/Trepan/DB/Display.pm »
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2011, 2012, 2014 Rocky Bernstein <rocky@cpan.org>
3  
4 package Devel::Trepan::Util;
5 59 ▶use strict; use warnings; use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 59 ▶use vars qw(@EXPORT @ISA @YN);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 59 @EXPORT = qw( hash_merge safe_repr uniq_abbrev extract_expression
9 parse_eval_suffix parse_eval_sigil
10 YES NO YES_OR_NO @YN bool2YN);
11 59 @ISA = qw(Exporter);
12  
13 59 ▶use constant YES => qw(y yes oui si yep ja);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 59 @YN = YES;
15 59 ▶use constant NO => qw(n no non nope nein);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 59 push(@YN, NO);
17  
18 sub YN($)
19 7 ▶{
Subroutine YN: called CC=1 SCAR=1.2
Pod undocumented
20 0 ▶ my $response = shift;
Subroutine YN: not called CC=1 SCAR=1.2
Pod undocumented
21 0 !!grep(/^${response}$/i, @YN);
22 }
23  
24 # Return 'Yes' for True and 'No' for False, and ?? for anything else
25 sub bool2YN($)
26 5 ▶{
Subroutine bool2YN: called CC=2 SCAR=9.2
Pod undocumented
27 0 ▶ my $bool = shift;
Subroutine bool2YN: not called CC=2 SCAR=9.2
Pod undocumented
28 0 ▶ $bool ? 'Yes' : 'No';
Branch: 2 branches
BranchTrueFalseTotal
$bool ? : 2 3 5
$bool ? : 0 0 0
29 }
30  
31 # Hash merge like Ruby has.
32 62 ▶sub hash_merge($$) {
Subroutine hash_merge: called CC=3 SCAR=14.2
Pod undocumented
33 0 ▶ my ($config, $default_opts) = @_;
Subroutine hash_merge: not called CC=3 SCAR=14.2
Pod undocumented
34 0 ▶ while (my ($field, $default_value) = each %$default_opts) {
Branch: 2 branches
BranchTrueFalseTotal
if (() = each %$default_opts) 472 62 534
if (() = each %$default_opts) 0 0 0
35 0 ▶ $config->{$field} = $default_value unless defined $config->{$field};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $config->{$field} 464 8 472
unless defined $config->{$field} 0 0 0
36 };
37 0 $config;
38 }
39  
40 sub safe_repr($$;$)
41 9 ▶{
Subroutine safe_repr: called CC=8 SCAR=30.8
Pod undocumented
42 0 ▶ my ($str, $max, $elipsis) = @_;
Subroutine safe_repr: not called CC=8 SCAR=30.8
Pod undocumented
43 0 ▶ $elipsis = '... ' unless defined $elipsis;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $elipsis 9 0 9
unless defined $elipsis 0 0 0
44 0 my $strlen = length($str);
45 0 ▶ return '' unless $strlen;
Branch: 2 branches
BranchTrueFalseTotal
unless $strlen 1 8 9
unless $strlen 0 0 0
46 0 ▶ $str = '' unless $str or $str =~ /\d+/;
Branch: 2 branches
BranchTrueFalseTotal
unless $str or $str =~ /\d+/ 0 8 8
unless $str or $str =~ /\d+/ 0 0 0
Condition16%1/6
$str or $str =~ /\d+/
l!l&&r!l&&!r
800
$str or $str =~ /\d+/
l!l&&r!l&&!r
000
MC/DC0%0/2
$str or $str =~ /\d+/
$str $str =~ /\d+/
MC/DC0%0/2
$str or $str =~ /\d+/
$str $str =~ /\d+/
Truth table33%1/3
A or B
A= $strB= $str =~ /\d+/
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $strB= $str =~ /\d+/
ABresult
1X1
011
000
47 0 ▶ if ($max > 0 && $strlen > $max && -1 == index($str, "\n")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($max > 0 and $strlen > $max and index($str, "\n") == -1) { } 1 7 8
if ($max > 0 and $strlen > $max and index($str, "\n") == -1) { } 0 0 0
Condition41%5/12
$max > 0 and $strlen > $max and index($str, "\n") == -1
!ll&&!rl&&r
521
$max > 0 and $strlen > $max
!ll&&!rl&&r
053
$max > 0 and $strlen > $max and index($str, "\n") == -1
!ll&&!rl&&r
000
$max > 0 and $strlen > $max
!ll&&!rl&&r
000
MC/DC66%2/3
$max > 0 and $strlen > $max and index($str, "\n") == -1
$max > 0 $strlen > $max index($str, "\n") == -1
MC/DC0%0/3
$max > 0 and $strlen > $max and index($str, "\n") == -1
$max > 0 $strlen > $max index($str, "\n") == -1
Truth table75%3/4
A and B and C
A= $max > 0B= $strlen > $maxC= index($str, "\n") == -1
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and B and C
A= $max > 0B= $strlen > $maxC= index($str, "\n") == -1
ABCresult
0XX0
10X0
1100
1111
48 0 sprintf("%s%s%s", substr($str, 0, $max/2),
49 $elipsis, substr($str, $strlen+1-($max)/2));
50 } else {
51 0 $str;
52 }
53 }
54  
55 # name is String and list is an Array of String.
56 # If name is a unique leading prefix of one of the entries of list,
57 # then return that. Otherwise return name.
58 sub uniq_abbrev($$)
59 5 ▶{
Subroutine uniq_abbrev: called CC=4 SCAR=17.9
Pod undocumented
60 0 ▶ my ($list, $name) = @_;
Subroutine uniq_abbrev: not called CC=4 SCAR=17.9
Pod undocumented
61 0 my @candidates = ();
62 0 for my $try_name (@$list) {
63 0 ▶ push @candidates, $try_name if 0 == index($try_name, $name);
Branch: 2 branches
BranchTrueFalseTotal
if 0 == index($try_name, $name) 5 15 20
if 0 == index($try_name, $name) 0 0 0
64 }
65 0 ▶ scalar @candidates == 1 ? $candidates[0] : $name;
Branch: 2 branches
BranchTrueFalseTotal
scalar @candidates == 1 ? : 2 3 5
scalar @candidates == 1 ? : 0 0 0
66 }
67  
68 # extract the "expression" part of a line of source code.
69 # Specifically
70 # if (expression) -> expression
71 # elsif (expression) -> expression
72 # else (expression) -> expression
73 # until (expression) -> expression
74 # while (expression) -> expression
75 # return (expression) -> expression
76 # my (...) = (expression) -> (...) = (expression)
77 # my ... = expression -> expression
78 # ditto for "our" and "local", e.g.
79 # local (...) = (expression) -> (...) = (expression
80 # local ... = expression -> expression
81 # $... = expression -> expression
82 sub extract_expression($)
83 10 ▶{
Subroutine extract_expression: called CC=7 SCAR=26.9
Pod undocumented
84 0 ▶ my $text = shift;
Subroutine extract_expression: not called CC=7 SCAR=26.9
Pod undocumented
85 0 ▶ if ($text =~ /^\s*(?:if|elsif|unless)\s*\(/) {
Branch: 12 branches
BranchTrueFalseTotal
if ($text =~ /^\s*(?:if|elsif|unless)\s*\(/) { } 3 7 10
elsif ($text =~ /^\s*(?:until|while)\s*\(/) { } 2 5 7
elsif ($text =~ /^\s*return\s+/) { } 2 3 5
elsif ($text =~ /^\s*(?:my|our|local)\s*(.+(\((?:.+)\s*\)\s*=.*);.*$)/) { } 1 2 3
elsif ($text =~ /^\s*(?:my|our|local)\s+(?:.+)\s*=\s*(.+);.*$/) { } 1 1 2
elsif ($text =~ /^\s*\$[A-Za-z_][A-Za-z0-9_\[\]]*\s*=[^=>]/) { } 0 1 1
if ($text =~ /^\s*(?:if|elsif|unless)\s*\(/) { } 0 0 0
elsif ($text =~ /^\s*(?:until|while)\s*\(/) { } 0 0 0
elsif ($text =~ /^\s*return\s+/) { } 0 0 0
elsif ($text =~ /^\s*(?:my|our|local)\s*(.+(\((?:.+)\s*\)\s*=.*);.*$)/) { } 0 0 0
elsif ($text =~ /^\s*(?:my|our|local)\s+(?:.+)\s*=\s*(.+);.*$/) { } 0 0 0
elsif ($text =~ /^\s*\$[A-Za-z_][A-Za-z0-9_\[\]]*\s*=[^=>]/) { } 0 0 0
86 0 $text =~ s/^\s*(?:if|elsif|unless)\s*\(//;
87 0 $text =~ s/\s*\)\s*\{?\s*$//;
88 } elsif ($text =~ /^\s*(?:until|while)\s*\(/) {
89 0 $text =~ s/^\s*(?:until|while)\s*\(//;
90 0 $text =~ s/\s*\)\{?\s*$//;
91 } elsif ($text =~ /^\s*return\s+/) {
92 # EXPRESSION in: return EXPRESSION
93 0 $text =~ s/^\s*return\s+//;
94 0 $text =~ s/;\s*$//;
95 } elsif ($text =~ /^\s*(?:my|our|local)\s*(.+(\((?:.+)\s*\)\s*=.*);.*$)/) {
96 # my (...) = ...;
97 # Note: This has to appear before the below assignment
98 0 $text =~ s/^\s*(?:my|our|local)\s*(\((?:.+)\)\s*=.*)[^;]*;.*$/$1/;
99 } elsif ($text =~ /^\s*(?:my|our|local)\s+(?:.+)\s*=\s*(.+);.*$/) {
100 # my ... = ...;
101 0 $text = $1;
102 # } elsif ($text =~ /^\s*case\s+/) {
103 # # EXPRESSION in: case EXPESSION
104 # $text =~ s/^\s*case\s*//;
105 # } elsif ($text =~ /^\s*sub\s*.*\(.+\)/) {
106 # $text =~ s/^\s*sub\s*.*\((.*)\)/\(\1\)/;
107 } elsif ($text =~ /^\s*\$[A-Za-z_][A-Za-z0-9_\[\]]*\s*=[^=>]/) {
108 # RHS of an assignment statement.
109 0 $text =~ s/^\s*[A-Za-z_][A-Za-z0-9_\[\]]*\s*=//;
110 }
111 0 return $text;
112 }
113  
114 sub invalid_filename($)
115 2 ▶{
Subroutine invalid_filename: called CC=3 SCAR=15.3
Pod undocumented
116 0 ▶ my $filename = shift;
Subroutine invalid_filename: not called CC=3 SCAR=15.3
Pod undocumented
117 0 ▶ return "Command file '$filename' doesn't exist" unless -f $filename;
Branch: 2 branches
BranchTrueFalseTotal
unless -f $filename 1 1 2
unless -f $filename 0 0 0
118 0 ▶ return "Command file '$filename' is not readable" unless -r $filename;
Branch: 2 branches
BranchTrueFalseTotal
unless -r $filename 0 1 1
unless -r $filename 0 0 0
119 0 return undef;
120 }
121  
122 # Return 'undef' arg $cmd_str is ok. If not return the message a Perl -c
123 # gives, dropping off the "-e had complation errors" message.
124 sub invalid_perl_syntax($;$)
125 47 ▶{
Subroutine invalid_perl_syntax: called CC=3 SCAR=14.2
Pod undocumented
126 0 ▶ my ($cmd_str, $have_e_opt) = @_;
Subroutine invalid_perl_syntax: not called CC=3 SCAR=14.2
Pod undocumented
127 0 ▶ my $cmd = sprintf("$EXECUTABLE_NAME -c %s",
Branch: 2 branches
BranchTrueFalseTotal
$have_e_opt ? : 45 2 47
$have_e_opt ? : 0 0 0
128 $have_e_opt ? $cmd_str : "-e '$cmd_str'");
129 0 my $output = `$cmd 2>&1`;
130 0 my $rc = $? >>8;
131 0 ▶ return undef if 0 == $rc;
Branch: 2 branches
BranchTrueFalseTotal
if 0 == $rc 42 5 47
if 0 == $rc 0 0 0
132 # Drop off: -e had compilation errors.
133 0 my @errmsg = split(/\n/, $output);
134 0 pop @errmsg;
135 0 return join("\n", @errmsg);
136 }
137  
138 sub parse_eval_suffix($)
139 18 ▶{
Subroutine parse_eval_suffix: called CC=2 SCAR=9.2
Pod undocumented
140 0 ▶ my $cmd = shift;
Subroutine parse_eval_suffix: not called CC=2 SCAR=9.2
Pod undocumented
141 0 my $suffix = substr($cmd, -1);
142 0 ▶ return ( index('%@$;>', $suffix) != -1) ? $suffix : '';
Branch: 2 branches
BranchTrueFalseTotal
(index('%@$;>', $suffix) != -1) ? : 14 4 18
(index('%@$;>', $suffix) != -1) ? : 0 0 0
143 }
144  
145 sub parse_eval_sigil($)
146 0 ▶{
Subroutine parse_eval_sigil: not called CC=2 SCAR=17.9
Pod undocumented
147 0 ▶ my $cmd = shift;
Subroutine parse_eval_sigil: not called CC=2 SCAR=17.9
Pod undocumented
148 0 ▶ return ($cmd =~ /^\s*([%\$\@>;])/) ? $1 : ';';
Branch: 2 branches
BranchTrueFalseTotal
$cmd =~ /^\s*([%\$\@>;])/ ? : 0 0 0
$cmd =~ /^\s*([%\$\@>;])/ ? : 0 0 0
149 }
150  
151 # This routine makes sure $pager is set up so that '|' can use it.
152 sub pager()
153 2 ▶{
Subroutine pager: called CC=4 SCAR=21.7
Pod undocumented
154 # If PAGER is defined in the environment, use it.
155 0 ▶ if (defined $ENV{PAGER}) {
Subroutine pager: not called CC=4 SCAR=21.7
Pod undocumented
Branch: 4 branches
BranchTrueFalseTotal
if (defined $ENV{'PAGER'}) { } 1 1 2
elsif (eval { do { require Config } } and defined $Config::Config{'pager'}) { } 1 0 1
if (defined $ENV{'PAGER'}) { } 0 0 0
elsif (eval { do { require Config } } and defined $Config::Config{'pager'}) { } 0 0 0
156 0 $ENV{PAGER};
157 0 ▶ } elsif (eval { require Config } && defined $Config::Config{pager} ) {
Condition16%1/6
eval { do { require Config } } and defined $Config::Config{"pager"}
!ll&&!rl&&r
001
eval { do { require Config } } and defined $Config::Config{"pager"}
!ll&&!rl&&r
000
MC/DC0%0/2
eval { do { require Config } } and defined $Config::Config{"pager"}
eval { do { require Config } } defined $Config::Config{"pager"}
MC/DC0%0/2
eval { do { require Config } } and defined $Config::Config{"pager"}
eval { do { require Config } } defined $Config::Config{"pager"}
Truth table33%1/3
A and B
A= eval { do { require Config } }B= defined $Config::Config{"pager"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= eval { do { require Config } }B= defined $Config::Config{"pager"}
ABresult
0X0
100
111
158 # if Config.pm defines it.
159 0 $Config::Config{pager};
160 } else {
161 # fall back to 'more'.
162 0 'more'
163 }
164 }
165  
166  
167 # Demo code
168 59 ▶unless (caller) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller) 0 59 59
169 0 my $default_config = {a => 1, b => 'c'};
170 0 require Data::Dumper;
171 0 import Data::Dumper;
172 0 my $config = {};
173 0 hash_merge $config, $default_config;
174 0 print Dumper($config), "\n";
175  
176 0 for my $file (__FILE__, 'bogus') {
177 0 my $result = invalid_filename($file);
178 0 ▶ if (defined($result)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result) { } 0 0 0
179 0 print "$result\n";
180 } else {
181 0 print "$file exists\n";
182 }
183 }
184  
185 $config = {
186 0 term_adjust => 1,
187 bogus => 'yep'
188 };
189 0 print Dumper($config), "\n";
190 0 hash_merge $config, $default_config;
191 0 print Dumper($config), "\n";
192  
193 0 my $string = 'The time has come to talk of many things.';
194 0 print safe_repr($string, 50), "\n";
195 0 print safe_repr($string, 17), "\n";
196 0 print safe_repr($string, 17, ''), "\n";
197  
198 0 my @list = qw(disassemble disable distance up);
199 0 uniq_abbrev(\@list, 'disas');
200 0 print join(' ', @list), "\n";
201 0 for my $name (qw(dis disas u upper foo)) {
202 0 printf("uniq_abbrev of %s is %s\n", $name,
203 uniq_abbrev(\@list, $name));
204 }
205 # ------------------------------------
206 # extract_expression
207 0 for my $stmt (
208 'if (condition("if"))',
209 'if (condition("if")) {',
210 'if(condition("if")){',
211 'until (until_termination)',
212 'until (until_termination){',
213 'return return_value',
214 'return return_value;',
215 'nothing to be done',
216 'my ($a,$b) = (5,6);',
217 ) {
218 0 print extract_expression($stmt), "\n";
219 }
220  
221 0 for my $cmd (qw(eval eval$ eval% eval@ evaluate% none)) {
222 0 print "parse_eval_suffix($cmd) => '". parse_eval_suffix($cmd) . "'\n";
223 }
224  
225 0 for my $resp (qw(yes no Y NO nein nien huh?)) {
226 0 printf "YN($resp) => '%s'\n", YN($resp);
227 }
228 0 for my $resp (1, 0, '', 'Foo', undef) {
229 0 ▶ my $resp_str = defined $resp ? $resp : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $resp ? : 0 0 0
230 0 printf "bool2YN($resp_str) => '%s'\n", bool2YN($resp);
231 }
232  
233 0 for my $expr ('1+', '{cmd=5}') {
234 0 print invalid_perl_syntax($expr), "\n";
235 }
236 0 for my $expr ('-e "$x="', '-e "(1,2"') {
237 0 print invalid_perl_syntax($expr, 1), "\n";
238 }
239  
240 0 $ENV{PAGER} = 'do-first';
241 0 print pager(), "\n";
242 0 delete $ENV{PAGER};
243 0 print pager(), "\n";
244 }
245  
246 59 1;
« blib/lib/Devel/Trepan/CmdProcessor/Command/Set_Subcmd/Variable.pm ↑ Summary lib/Devel/Trepan/DB/Display.pm »