blib/lib/Acme/Text/Rhombus.pm

filter: Statementstmt 92.9% 66 / 71 Branchbran 83.3% 25 / 30 Conditioncond 86.6% 13 / 15 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 92.8% 13 / 14 Podpod 100% 4 / 4 totaltotal 89.4% 127 / 142 CC 21 SCAR 23.6
CC
21
Cov
90%
_draw_rhombus
18.3
__ANON__
4.1
rhombus_random
2.0
CRAP 10.6
↑ Summary
1 package Acme::Text::Rhombus;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use constant LINES => 25;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use constant FILLUP => '.';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use constant FORWARD => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our ($VERSION, @EXPORT_OK, %EXPORT_TAGS);
11 2 my @subs;
12  
13 2 $VERSION = '0.25';
14 2 @subs = qw(rhombus rhombus_letter rhombus_digit rhombus_random);
15 2 @EXPORT_OK = @subs;
16 2 %EXPORT_TAGS = ('all' => [ @subs ]);
17  
18 my $get_opt = sub
19 23 ▶{
Subroutine __ANON__: called CC=4 SCAR=14.2
20 23 my ($opts, $opt, $regex, $default) = @_;
21 return (exists $opts->{$opt}
22 && defined $opts->{$opt}
23 23 ▶ and $opts->{$opt} =~ $regex) ? $opts->{$opt} : $default;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{$opt} && defined $opts->{$opt} && $opts->{$opt} =~ /$regex/ ? : 18 5 23
Condition66%4/6
exists $opts->{$opt} and defined $opts->{$opt} and $opts->{$opt} =~ /$regex/
!ll&&!rl&&r
5018
exists $opts->{$opt} and defined $opts->{$opt}
!ll&&!rl&&r
5018
MC/DC33%1/3
exists $opts->{$opt} and defined $opts->{$opt} and $opts->{$opt} =~ /$regex/
exists $opts->{$opt} defined $opts->{$opt} $opts->{$opt} =~ /$regex/
Truth table50%2/4
A and B and C
A= exists $opts->{$opt}B= defined $opts->{$opt}C= $opts->{$opt} =~ /$regex/
ABCresult
0XX0
10X0
1100
1111
24 2 };
25  
26 sub _draw_rhombus
27 5 ▶{
Subroutine _draw_rhombus: called CC=18 SCAR=29.1
28 5 my ($mode, $lines, $char, $case, $fillup, $forward) = @_;
29  
30 5 my ($is_letter, $is_digit, $is_random) = ($mode eq 'letter', $mode eq 'digit', $mode eq 'random');
31  
32 my %alter = (
33 2 ▶ lower => sub { lc $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 3 ▶ upper => sub { uc $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
35 5 );
36 5 ▶ $char = $alter{$case}->($char) if $is_letter;
Branch: 1 branch
BranchTrueFalseTotal
if $is_letter 3 2 5
37  
38 5 my @chars = map chr, (48..57, 65..90, 97..122);
39 5 ▶ $char = $chars[int(rand(@chars))] unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 0 5 5
40  
41 5 ▶ $lines++ if $lines % 2 == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $lines % 2 == 0 0 5 5
42  
43 5 my ($line, $repeat, $rhombus);
44  
45 5 ▶ for ($line = $repeat = 1; $line <= $lines; $line++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line <= $lines) 117 5 122
46 117 my $spaces = ($lines - $repeat) / 2;
47  
48 117 $rhombus .= $fillup x $spaces;
49 117 $rhombus .= $char x $repeat;
50 117 $rhombus .= $fillup x $spaces;
51 117 $rhombus .= "\n";
52  
53 117 ▶ $repeat = $line < ($lines / 2) ? $repeat + 2 : $repeat - 2;
Branch: 1 branch
BranchTrueFalseTotal
$line < $lines / 2 ? : 56 61 117
54  
55 117 ▶ if ($is_letter) {
Branch: 3 branches
BranchTrueFalseTotal
if ($is_letter) { } 71 46 117
elsif ($is_digit) { } 46 0 46
elsif ($is_random) { } 0 0 0
56 71 ▶ $char = $forward ? chr(ord($char) + 1) : chr(ord($char) - 1);
Branch: 1 branch
BranchTrueFalseTotal
$forward ? : 56 15 71
57 }
58 elsif ($is_digit) {
59 46 ▶ $char = $forward ? $char + 1 : $char - 1;
Branch: 1 branch
BranchTrueFalseTotal
$forward ? : 31 15 46
60 }
61 elsif ($is_random) {
62 0 $char = $chars[int(rand(@chars))];
63 }
64  
65 117 ▶ if ($is_letter && $char !~ /[a-zA-Z]/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_letter and not $char =~ /[a-zA-Z]/) { } 2 115 117
elsif ($is_digit and $char > 9 || $char < 0) { } 5 179 184
Condition100%3/3
$is_letter and not $char =~ /[a-zA-Z]/
!ll&&!rl&&r
46692
MC/DC100%2/2
$is_letter and not $char =~ /[a-zA-Z]/
$is_letter not $char =~ /[a-zA-Z]/
Truth table100%3/3
A and not(B)
A= $is_letterB= not $char =~ /[a-zA-Z]/
ABresult
0X0
100
111
66 2 ▶ $char = $alter{$case}->($forward ? 'a' : 'z');
Branch: 1 branch
BranchTrueFalseTotal
$forward ? : 1 1 2
67 }
68 ▶ elsif ($is_digit and $char > 9 || $char < 0) {
Condition100%6/6
$is_digit and $char > 9 || $char < 0
!ll&&!rl&&r
69415
$char > 9 or $char < 0
l!l&&r!l&&!r
3241
MC/DC100%3/3
$is_digit and $char > 9 || $char < 0
$is_digit $char > 9 $char < 0
Truth table100%4/4
A and B or C
A= $is_digitB= $char > 9C= $char < 0
ABCresult
0XX0
1000
11X1
1011
69 5 ▶ $char = $forward ? 0 : 9;
Branch: 1 branch
BranchTrueFalseTotal
$forward ? : 3 2 5
70 }
71 }
72  
73 5 return $rhombus;
74 }
75  
76 1 ▶sub rhombus { return rhombus_letter(@_); }
Subroutine rhombus: called CC=1 SCAR=0.0
Pod documented
77  
78 sub rhombus_letter
79 3 ▶{
Subroutine rhombus_letter: called CC=1 SCAR=0.0
Pod documented
80 3 my %opts = @_;
81  
82 3 my $lines = $get_opt->(\%opts, 'lines', qr/^\d+$/, LINES);
83 3 my $letter = $get_opt->(\%opts, 'letter', qr/^[a-zA-Z]$/, 'a');
84 3 my $case = $get_opt->(\%opts, 'case', qr/^(?:low|upp)er$/, 'upper');
85 3 my $fillup = $get_opt->(\%opts, 'fillup', qr/^\S$/, FILLUP);
86 3 my $forward = $get_opt->(\%opts, 'forward', qr/^[01]$/, FORWARD);
87  
88 3 return _draw_rhombus('letter', $lines, $letter, $case, $fillup, $forward);
89 }
90  
91 sub rhombus_digit
92 2 ▶{
Subroutine rhombus_digit: called CC=1 SCAR=0.0
Pod documented
93 2 my %opts = @_;
94  
95 2 my $lines = $get_opt->(\%opts, 'lines', qr/^\d+$/, LINES);
96 2 my $digit = $get_opt->(\%opts, 'digit', qr/^\d$/, 0);
97 2 my $fillup = $get_opt->(\%opts, 'fillup', qr/^\S$/, FILLUP);
98 2 my $forward = $get_opt->(\%opts, 'forward', qr/^[01]$/, FORWARD);
99  
100 2 return _draw_rhombus('digit', $lines, $digit, undef, $fillup, $forward);
101 }
102  
103 sub rhombus_random
104 0 ▶{
Subroutine rhombus_random: not called CC=1 SCAR=6.9
Pod documented
105 0 my %opts = @_;
106  
107 0 my $lines = $get_opt->(\%opts, 'lines', qr/^\d+$/, LINES);
108 0 my $fillup = $get_opt->(\%opts, 'fillup', qr/^\S$/, FILLUP);
109  
110 0 return _draw_rhombus('random', $lines, undef, undef, $fillup, undef);
111 }
112  
113 2 1;
114 __END__
↑ Summary