blib/lib/Number/Continuation.pm

filter: Statementstmt 97.2% 104 / 107 Branchbran 96.6% 29 / 30 Conditioncond 100% 19 / 19 MC/DCmcdc 100% 10 / 10 Subroutinesub 93.7% 15 / 16 Podpod 100% 1 / 1 totaltotal 97.2% 178 / 183 CC 27 SCAR 16.8
CC
27
Cov
97%
_init
9.0
_format
8.0
_construct
6.0
CRAP 5.4
↑ Summary
1 package Number::Continuation;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use boolean qw(true);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Params::Validate ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 our ($VERSION, @EXPORT_OK);
12  
13 4 $VERSION = '0.06';
14 4 @EXPORT_OK = qw(continuation);
15  
16 validation_options(
17 on_fail => sub
18 0 ▶{
Subroutine __ANON__: not called CC=1 SCAR=6.9
19 0 my ($error) = @_;
20 0 chomp $error;
21 0 croak $error;
22 },
23 4 stack_skip => 2,
24 );
25  
26 sub continuation
27 15 ▶{
Subroutine continuation: called CC=2 SCAR=6.9
Pod documented
28 15 my (@list, %opts, $set);
29  
30 15 _init(\$set, \%opts, \@_);
31  
32 15 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 7 8 15
33 7 _construct($set, \@list);
34 7 return @list;
35 }
36 else {
37 8 return _format($set, \%opts);
38 }
39 }
40  
41 sub _init
42 15 ▶{
Subroutine _init: called CC=9 SCAR=22.0
43 15 my ($set, $opts, $args) = @_;
44  
45 15 ▶ if (ref $args->[-1] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->[-1] eq "HASH") 1 14 15
46 1 %$opts = %{$args->[-1]};
47 1 pop @$args;
48 }
49  
50 15 my $re_digits = qr!^\-?\d+$!;
51  
52 my $spec = sub
53 2 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
54 2 my ($args, $spec) = @_;
55 2 my @spec;
56 2 ▶ push @spec, $spec while $args--;
Branch: 1 branch
BranchTrueFalseTotal
$args-- and do { push @spec, $spec } 18 2 20
57 2 return @spec;
58 15 };
59  
60 15 ▶ if (@$args == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$args == 1) { } 11 4 15
elsif (@$args > 1) { } 2 2 4
61 validate_pos(@$args, {
62 type => SCALAR | ARRAYREF,
63 callbacks => {
64 'valid set' => sub
65 11 ▶ {
Subroutine __ANON__: called CC=5 SCAR=16.1
66 11 ▶ foreach my $num (ref $_[0] ? @{$_[0]} : (split /\s+/, $_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 9 11
67 133 ▶ die "invalid number\n" unless $num =~ $re_digits;
Branch: 1 branch
BranchTrueFalseTotal
unless $num =~ /$re_digits/ 0 133 133
68 }
69 11 ▶ $$set = ref $_[0] ? $_[0] : [ split /\s+/, $_[0] ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 9 11
70 11 return true;
71 }
72 },
73 11 });
74 }
75 elsif (@$args > 1) {
76 2 my %spec = (
77 type => SCALAR,
78 regex => $re_digits,
79 );
80 2 validate_pos(@$args, $spec->(scalar @$args, \%spec));
81 2 $$set = $args;
82 }
83 else {
84 2 $$set = [];
85 }
86  
87 15 my @args = %$opts;
88 15 validate(@args, {
89 delimiter => {
90 type => SCALAR,
91 optional => true,
92 regex => qr!^\S{2}$!,
93 },
94 range => {
95 type => SCALAR,
96 optional => true,
97 regex => qr!^\S{1,2}$!,
98 },
99 separator => {
100 type => SCALAR,
101 optional => true,
102 regex => qr!^\S$!,
103 },
104 });
105  
106 15 ▶ $opts->{delimiter} ||= '';
Condition100%2/2
$opts->{"delimiter"} ||= ""
l!l
114
MC/DC100%1/1
$opts->{"delimiter"} ||= ""
$opts->{"delimiter"}
Truth table100%2/2
A
A= $opts->{"delimiter"}
Aresult
11
00
107 15 ▶ $opts->{range} ||= '-';
Condition100%2/2
$opts->{"range"} ||= "-"
l!l
114
MC/DC100%1/1
$opts->{"range"} ||= "-"
$opts->{"range"}
Truth table100%2/2
A
A= $opts->{"range"}
Aresult
11
00
108 15 ▶ $opts->{separator} ||= ',';
Condition100%2/2
$opts->{"separator"} ||= ","
l!l
114
MC/DC100%1/1
$opts->{"separator"} ||= ","
$opts->{"separator"}
Truth table100%2/2
A
A= $opts->{"separator"}
Aresult
11
00
109  
110 15 @{$opts->{delimiters}} = split //, $opts->{delimiter};
111 15 ▶ $opts->{delimiters}[0] ||= '';
Condition100%2/2
$opts->{"delimiters"}[0] ||= ""
l!l
114
MC/DC100%1/1
$opts->{"delimiters"}[0] ||= ""
$opts->{"delimiters"}[0]
Truth table100%2/2
A
A= $opts->{"delimiters"}[0]
Aresult
11
00
112 15 ▶ $opts->{delimiters}[1] ||= '';
Condition100%2/2
$opts->{"delimiters"}[1] ||= ""
l!l
114
MC/DC100%1/1
$opts->{"delimiters"}[1] ||= ""
$opts->{"delimiters"}[1]
Truth table100%2/2
A
A= $opts->{"delimiters"}[1]
Aresult
11
00
113 }
114  
115 sub _construct
116 7 ▶{
Subroutine _construct: called CC=6 SCAR=17.9
117 7 my ($set, $list) = @_;
118  
119 7 my $prev_number = undef;
120  
121 7 my $entry = [];
122 7 foreach my $num (@$set) {
123 71 ▶ if (defined $prev_number
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prev_number and not $num - $prev_number == 1 || $prev_number - $num == 1) 28 43 71
Condition100%6/6
defined $prev_number and not $num - $prev_number == 1 || $prev_number - $num == 1
!ll&&!rl&&r
63728
$num - $prev_number == 1 || $prev_number - $num == 1
l!l&&r!l&&!r
33428
MC/DC100%3/3
defined $prev_number and not $num - $prev_number == 1 || $prev_number - $num == 1
defined $prev_number $num - $prev_number == 1 $prev_number - $num == 1
Truth table100%4/4
A and not(B || C)
A= defined $prev_numberB= $num - $prev_number == 1C= $prev_number - $num == 1
ABCresult
0XX0
11X0
1010
1001
124 && !(($num - $prev_number == 1) # positive continuation
125 || ($prev_number - $num == 1) # negative continuation
126 )) {
127 28 push @$list, $entry;
128 28 $entry = [];
129 }
130 71 push @$entry, $num;
131 71 $prev_number = $num;
132 }
133 7 ▶ push @$list, $entry if @$entry;
Branch: 1 branch
BranchTrueFalseTotal
if @$entry 6 1 7
134 }
135  
136 sub _format
137 8 ▶{
Subroutine _format: called CC=8 SCAR=20.8
138 8 my ($set, $opts) = @_;
139  
140 8 my $string = '';
141  
142 my $begin = sub
143 37 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
144 37 my ($string, $num) = @_;
145 37 $$string .= $opts->{delimiters}[0];
146 37 $$string .= $num;
147 8 };
148 my $range = sub
149 24 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
150 24 my ($string, $num) = @_;
151 24 $$string .= $opts->{range};
152 24 $$string .= $num;
153 8 };
154 my $end = sub
155 37 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
156 37 my ($string) = @_;
157 37 $$string .= $opts->{delimiters}[1];
158 37 $$string .= "$opts->{separator} ";
159 8 };
160  
161 8 my $consecutive = 0;
162 8 my $prev_number = undef;
163  
164 8 foreach my $num (@$set) {
165 80 ▶ if (!defined $prev_number) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $prev_number) { } 7 73 80
166 7 $begin->(\$string, $num);
167 }
168 else {
169 73 ▶ if (($num - $prev_number == 1) # positive continuation
Branch: 2 branches
BranchTrueFalseTotal
if ($num - $prev_number == 1 or $prev_number - $num == 1) { } 43 30 73
elsif ($consecutive) { } 21 9 30
Condition100%3/3
$num - $prev_number == 1 or $prev_number - $num == 1
l!l&&r!l&&!r
39430
MC/DC100%2/2
$num - $prev_number == 1 or $prev_number - $num == 1
$num - $prev_number == 1 $prev_number - $num == 1
Truth table100%3/3
A or B
A= $num - $prev_number == 1B= $prev_number - $num == 1
ABresult
1X1
011
000
170 || ($prev_number - $num == 1) # negative continuation
171 ) {
172 43 $consecutive++;
173 }
174 elsif ($consecutive) {
175 21 $range->(\$string, $prev_number);
176 21 $end->(\$string);
177 21 $begin->(\$string, $num);
178 21 $consecutive = 0;
179 }
180 else {
181 9 $end->(\$string);
182 9 $begin->(\$string, $num);
183 }
184 }
185 80 $prev_number = $num;
186 }
187 8 ▶ if ($consecutive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($consecutive) 3 5 8
188 3 $range->(\$string, $prev_number);
189 }
190 8 ▶ if (@$set) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$set) 7 1 8
191 7 $end->(\$string);
192 }
193  
194 8 $string =~ s/\Q$opts->{separator}\E $//;
195  
196 8 return $string;
197 }
198  
199 4 1;
200 __END__
↑ Summary