blib/lib/Version/libversion/PP.pm

filter: Statementstmt 94.1% 192 / 204 Branchbran 90.4% 76 / 84 Conditioncond 75.6% 31 / 41 MC/DCmcdc 61.9% 13 / 21 Subroutinesub 89.7% 35 / 39 Podpod 71.4% 5 / 7 totaltotal 88.8% 352 / 396 CC 59 SCAR 21.4
CC
59
Cov
91%
_compare_components
20.4
cmp
20.0
version_compare4
12.0
CRAP 8.5
↑ Summary
1 package Version::libversion::PP;
2  
3 4 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 require Exporter;
9  
10 4 our @ISA = qw(Exporter);
11  
12 4 our @EXPORT = qw(
13 version_compare
14 version_compare2
15 version_compare4
16 );
17  
18 4 our @EXPORT_OK = qw(
19 VERSIONFLAG_P_IS_PATCH
20 VERSIONFLAG_ANY_IS_PATCH
21 VERSIONFLAG_LOWER_BOUND
22 VERSIONFLAG_UPPER_BOUND
23  
24 P_IS_PATCH
25 ANY_IS_PATCH
26 LOWER_BOUND
27 UPPER_BOUND
28 );
29  
30 4 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
31  
32 4 our $VERSION = '1.00';
33  
34 4 ▶use overload ('""' => \&value, cmp => \&cmp, '<=>' => \&cmp, fallback => 1);
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 4 ▶use constant VERSIONFLAG_P_IS_PATCH => 0x1;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 4 ▶use constant VERSIONFLAG_ANY_IS_PATCH => 0x2;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 4 ▶use constant VERSIONFLAG_LOWER_BOUND => 0x4;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 4 ▶use constant VERSIONFLAG_UPPER_BOUND => 0x8;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 4 ▶use constant P_IS_PATCH => 0x1;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 4 ▶use constant ANY_IS_PATCH => 0x2;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 4 ▶use constant LOWER_BOUND => 0x4;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 4 ▶use constant UPPER_BOUND => 0x8;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 4 ▶use constant METAORDER_LOWER_BOUND => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 4 ▶use constant METAORDER_PRE_RELEASE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 4 ▶use constant METAORDER_ZERO => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 4 ▶use constant METAORDER_POST_RELEASE => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 4 ▶use constant METAORDER_NONZERO => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 4 ▶use constant METAORDER_LETTER_SUFFIX => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 4 ▶use constant METAORDER_UPPER_BOUND => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 4 ▶use constant KEYWORD_UNKNOWN => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 4 ▶use constant KEYWORD_PRE_RELEASE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 4 ▶use constant KEYWORD_POST_RELEASE => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
59  
60 0 my ($class, $value, $flags) = @_;
61 0 my $self = {value => $value, flags => $flags};
62  
63 0 return bless $self, $class;
64  
65 }
66  
67 4 *parse = \&new;
68  
69 0 ▶sub value { shift->{value} }
Subroutine value: not called CC=1 SCAR=6.9
Pod undocumented
70 0 ▶sub flags { shift->{flags} }
Subroutine flags: not called CC=1 SCAR=6.9
Pod documented
71  
72 0 ▶sub cmp {
Subroutine cmp: not called CC=4 SCAR=30.0
Pod undocumented
73  
74 0 my ($left, $right) = @_;
75  
76 0 ▶ unless (ref($right)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $right) 0 0 0
77 0 $right = __PACKAGE__->new($right);
78 }
79  
80 0 ▶ return version_compare($left->value, $right->value, ($left->flags || 0), ($right->flags || 0));
Condition0%0/4
$left->flags || 0
l!l
00
$right->flags || 0
l!l
00
MC/DC0%0/1
$left->flags || 0
$left->flags
MC/DC0%0/1
$right->flags || 0
$right->flags
Truth table0%0/2
A
A= $left->flags
Aresult
11
00
Truth table0%0/2
A
A= $right->flags
Aresult
11
00
81  
82 }
83  
84 252528 ▶sub _skip_separator {
Subroutine _skip_separator: called CC=1 SCAR=0.0
85  
86 252528 my $string = shift;
87  
88 252528 $string =~ s/^[^[:alnum:]]+//;
89 252528 return $string;
90  
91 }
92  
93 192342 ▶sub _split_alpha {
Subroutine _split_alpha: called CC=2 SCAR=7.6
94  
95 192342 my $string = shift;
96  
97 192342 ▶ if ($string =~ /^([[:alpha:]]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^([[:alpha:]]+)/) 192342 0 192342
98 192342 return ($1, substr($string, length($1)));
99 }
100  
101 0 return ('', $string);
102  
103 }
104  
105 118832 ▶sub _split_number {
Subroutine _split_number: called CC=2 SCAR=7.1
106  
107 118832 my $string = shift;
108  
109 118832 ▶ if ($string =~ /^([[:digit:]]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^([[:digit:]]+)/) 118832 0 118832
110 118832 my $num = $1;
111 118832 my $rest = substr($string, length($1));
112  
113 118832 $num =~ s/^0+//; # skip_zeroes
114 118832 return ($num, $rest);
115 }
116  
117 0 return ('', $string);
118  
119 }
120  
121 150270 ▶sub _classify_keyword {
Subroutine _classify_keyword: called CC=7 SCAR=19.5
122  
123 150270 my ($string, $flags) = @_;
124  
125 150270 $string = lc($string);
126  
127 150270 ▶ return KEYWORD_PRE_RELEASE if ($string =~ /^(alpha|beta|rc)$/);
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /^(alpha|beta|rc)$/ 2960 147310 150270
128 147310 ▶ return KEYWORD_PRE_RELEASE if ($string =~ /^pre/);
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /^pre/ 581 146729 147310
129 146729 ▶ return KEYWORD_POST_RELEASE if ($string =~ /^(post|patch)/);
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /^(post|patch)/ 2862 143867 146729
130 143867 ▶ return KEYWORD_POST_RELEASE if ($string =~ /^(pl|errata)$/);
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /^(pl|errata)$/ 8 143859 143867
131 143859 ▶ return KEYWORD_POST_RELEASE if ($string eq 'p' && ($flags & VERSIONFLAG_P_IS_PATCH));
Branch: 1 branch
BranchTrueFalseTotal
if $string eq "p" and $flags & 1 22 143837 143859
Condition100%3/3
$string eq "p" and $flags & 1
!ll&&!rl&&r
1254371840022
MC/DC100%2/2
$string eq "p" and $flags & 1
$string eq "p" $flags & 1
Truth table100%3/3
A and B
A= $string eq "p"B= $flags & 1
ABresult
0X0
100
111
132  
133 143837 return KEYWORD_UNKNOWN;
134  
135 }
136  
137 250128 ▶sub _parse_token_to_component {
Subroutine _parse_token_to_component: called CC=7 SCAR=19.5
138  
139 250128 my ($string, $flags) = @_;
140  
141 250128 my $component = {};
142  
143 250128 ▶ if ($string =~ /^[[:alpha:]]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^[[:alpha:]]/) { } 131296 118832 250128
144  
145 131296 my ($alpha, $rest) = _split_alpha($string);
146  
147 131296 my $keyword_class = _classify_keyword($alpha, $flags);
148  
149 131296 ▶ if ($keyword_class == KEYWORD_UNKNOWN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keyword_class == 0) 125055 6241 131296
150 125055 ▶ $component->{order} = ($flags & VERSIONFLAG_ANY_IS_PATCH) ? METAORDER_POST_RELEASE : METAORDER_PRE_RELEASE;
Branch: 1 branch
BranchTrueFalseTotal
$flags & 2 ? : 10 125045 125055
151 }
152  
153 131296 ▶ $component->{order} = METAORDER_PRE_RELEASE if $keyword_class == KEYWORD_PRE_RELEASE;
Branch: 1 branch
BranchTrueFalseTotal
if $keyword_class == 1 3367 127929 131296
154 131296 ▶ $component->{order} = METAORDER_POST_RELEASE if $keyword_class == KEYWORD_POST_RELEASE;
Branch: 1 branch
BranchTrueFalseTotal
if $keyword_class == 2 2874 128422 131296
155  
156 131296 $component->{value} = lc(substr($alpha, 0, 1));
157  
158 131296 return ($component, $rest);
159 }
160 else {
161  
162 118832 my ($number, $rest) = _split_number($string);
163  
164 118832 $component->{value} = $number;
165 118832 ▶ $component->{order} = (length($number) == 0) ? METAORDER_ZERO : METAORDER_NONZERO;
Branch: 1 branch
BranchTrueFalseTotal
length $number == 0 ? : 38577 80255 118832
166  
167 118832 return ($component, $rest);
168  
169 }
170  
171 }
172  
173 11781 ▶sub _make_default_component {
Subroutine _make_default_component: called CC=4 SCAR=13.9
174  
175 11781 ▶ my $flags = shift || 0x0;
Condition100%2/2
shift() || 0
l!l
3611745
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
176  
177 11781 ▶ my $order
Branch: 2 branches
BranchTrueFalseTotal
$flags & 4 ? : 18 11763 11781
$flags & 8 ? : 18 11745 11763
178 = ($flags & VERSIONFLAG_LOWER_BOUND) ? METAORDER_LOWER_BOUND
179 : ($flags & VERSIONFLAG_UPPER_BOUND) ? METAORDER_UPPER_BOUND
180 : METAORDER_ZERO;
181  
182 11781 return {order => $order, value => ''};
183 }
184  
185 252528 ▶sub _get_next_version_component {
Subroutine _get_next_version_component: called CC=6 SCAR=17.9
186  
187 252528 my ($string, $flags) = @_;
188  
189 252528 $string = _skip_separator($string);
190  
191 252528 ▶ return ([_make_default_component($flags)], $string) if (length($string) == 0);
Branch: 1 branch
BranchTrueFalseTotal
if length $string == 0 2400 250128 252528
192  
193 250128 my ($component, $rest) = _parse_token_to_component($string, $flags);
194  
195 250128 my @components = ($component);
196  
197 # Special case for letter suffix:
198 # - We taste whether the next component is alpha not followed by a number,
199 # e.g 1a, 1a.1, but not 1a1
200 # - We check whether it's known keyword (in which case it's treated normally)
201 # - Otherwise, it's treated as letter suffix
202  
203 250128 ▶ if ($rest =~ /^[[:alpha:]]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /^[[:alpha:]]/) 61046 189082 250128
204  
205 61046 my ($alpha, $alpha_rest) = _split_alpha($rest);
206  
207 61046 ▶ if ($alpha_rest !~ /^[[:digit:]]/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($alpha_rest =~ /^[[:digit:]]/) 18974 42072 61046
208  
209 18974 my $keyword_class = _classify_keyword($alpha, $flags);
210  
211 18974 ▶ my $order
Branch: 2 branches
BranchTrueFalseTotal
$keyword_class == 0 ? : 18782 192 18974
$keyword_class == 1 ? : 174 18 192
212 = ($keyword_class == KEYWORD_UNKNOWN) ? METAORDER_LETTER_SUFFIX
213 : ($keyword_class == KEYWORD_PRE_RELEASE) ? METAORDER_PRE_RELEASE
214 : METAORDER_POST_RELEASE;
215  
216 18974 push @components, {value => lc(substr($alpha, 0, 1)), order => $order};
217  
218 18974 $rest = $alpha_rest;
219  
220 }
221 }
222  
223 250128 return (\@components, $rest);
224  
225 }
226  
227 133308 ▶sub _compare_components {
Subroutine _compare_components: called CC=19 SCAR=30.2
228  
229 133308 my ($u1, $u2) = @_;
230  
231 133308 ▶ my $o1 = $u1->{order} // 0;
Condition50%1/2
$u1->{"order"} // 0
l!l
1333080
MC/DC0%0/1
$u1->{"order"} // 0
$u1->{"order"}
Truth table50%1/2
A
A= $u1->{"order"}
Aresult
11
00
232 133308 ▶ my $o2 = $u2->{order} // 0;
Condition50%1/2
$u2->{"order"} // 0
l!l
1333080
MC/DC0%0/1
$u2->{"order"} // 0
$u2->{"order"}
Truth table50%1/2
A
A= $u2->{"order"}
Aresult
11
00
233  
234 133308 ▶ return -1 if $o1 < $o2;
Branch: 1 branch
BranchTrueFalseTotal
if $o1 < $o2 53328 79980 133308
235 79980 ▶ return 1 if $o1 > $o2;
Branch: 1 branch
BranchTrueFalseTotal
if $o1 > $o2 31226 48754 79980
236  
237 48754 ▶ my $v1 = $u1->{value} // '';
Condition50%1/2
$u1->{"value"} // ""
l!l
487540
MC/DC0%0/1
$u1->{"value"} // ""
$u1->{"value"}
Truth table50%1/2
A
A= $u1->{"value"}
Aresult
11
00
238 48754 ▶ my $v2 = $u2->{value} // '';
Condition50%1/2
$u2->{"value"} // ""
l!l
487540
MC/DC0%0/1
$u2->{"value"} // ""
$u2->{"value"}
Truth table50%1/2
A
A= $u2->{"value"}
Aresult
11
00
239  
240 48754 my $u1_is_empty = length($v1) == 0;
241 48754 my $u2_is_empty = length($v2) == 0;
242  
243 48754 ▶ return 0 if $u1_is_empty && $u2_is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $u1_is_empty and $u2_is_empty 5516 43238 48754
Condition66%2/3
$u1_is_empty and $u2_is_empty
!ll&&!rl&&r
4323805516
MC/DC50%1/2
$u1_is_empty and $u2_is_empty
$u1_is_empty $u2_is_empty
Truth table66%2/3
A and B
A= $u1_is_emptyB= $u2_is_empty
ABresult
0X0
100
111
244 43238 ▶ return -1 if $u1_is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $u1_is_empty 0 43238 43238
245 43238 ▶ return 1 if $u2_is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $u2_is_empty 0 43238 43238
246  
247 43238 my $u1_is_alpha = ($v1 =~ /^[[:alpha:]]/);
248 43238 my $u2_is_alpha = ($v2 =~ /^[[:alpha:]]/);
249  
250 43238 ▶ if ($u1_is_alpha && $u2_is_alpha) {
Branch: 1 branch
BranchTrueFalseTotal
if ($u1_is_alpha and $u2_is_alpha) 31198 12040 43238
Condition66%2/3
$u1_is_alpha and $u2_is_alpha
!ll&&!rl&&r
12040031198
MC/DC50%1/2
$u1_is_alpha and $u2_is_alpha
$u1_is_alpha $u2_is_alpha
Truth table66%2/3
A and B
A= $u1_is_alphaB= $u2_is_alpha
ABresult
0X0
100
111
251  
252 31198 ▶ return -1 if $v1 lt $v2;
Branch: 1 branch
BranchTrueFalseTotal
if $v1 lt $v2 16725 14473 31198
253 14473 ▶ return 1 if $v1 gt $v2;
Branch: 1 branch
BranchTrueFalseTotal
if $v1 gt $v2 11074 3399 14473
254 3399 return 0;
255  
256 }
257  
258 12040 ▶ return -1 if $u1_is_alpha;
Branch: 1 branch
BranchTrueFalseTotal
if $u1_is_alpha 0 12040 12040
259 12040 ▶ return 1 if $u2_is_alpha;
Branch: 1 branch
BranchTrueFalseTotal
if $u2_is_alpha 0 12040 12040
260  
261 12040 my $len1 = length($v1);
262 12040 my $len2 = length($v2);
263  
264 12040 ▶ return -1 if $len1 < $len2;
Branch: 1 branch
BranchTrueFalseTotal
if $len1 < $len2 8 12032 12040
265 12032 ▶ return 1 if $len1 > $len2;
Branch: 1 branch
BranchTrueFalseTotal
if $len1 > $len2 3234 8798 12032
266  
267 8798 return ($v1 cmp $v2);
268  
269 }
270  
271 117959 ▶sub version_compare2 {
Subroutine version_compare2: called CC=1 SCAR=0.0
Pod documented
272 117959 my ($v1, $v2) = @_;
273 117959 return version_compare4($v1, $v2, 0, 0);
274 }
275  
276 118103 ▶sub version_compare4 {
Subroutine version_compare4: called CC=12 SCAR=24.9
Pod documented
277  
278 118103 my ($v1, $v2, $v1_flags, $v2_flags) = @_;
279  
280 118103 ▶ return 0 if $v1 eq $v2 && $v1_flags == $v2_flags;
Branch: 1 branch
BranchTrueFalseTotal
if $v1 eq $v2 and $v1_flags == $v2_flags 40 118063 118103
Condition100%3/3
$v1 eq $v2 and $v1_flags == $v2_flags
!ll&&!rl&&r
1180491440
MC/DC100%2/2
$v1 eq $v2 and $v1_flags == $v2_flags
$v1 eq $v2 $v1_flags == $v2_flags
Truth table100%3/3
A and B
A= $v1 eq $v2B= $v1_flags == $v2_flags
ABresult
0X0
100
111
281  
282 118063 my @v1_components = ();
283 118063 my @v2_components = ();
284  
285 118063 ▶ my $v1_extra_components = ($v1_flags & (VERSIONFLAG_LOWER_BOUND | VERSIONFLAG_UPPER_BOUND)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$v1_flags & 12 ? : 8 118055 118063
286 118063 ▶ my $v2_extra_components = ($v2_flags & (VERSIONFLAG_LOWER_BOUND | VERSIONFLAG_UPPER_BOUND)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$v2_flags & 12 ? : 44 118019 118063
287  
288 118063 while (1) {
289  
290 133308 _components(\@v1_components, \$v1, $v1_flags, \$v1_extra_components);
291 133308 _components(\@v2_components, \$v2, $v2_flags, \$v2_extra_components);
292  
293 133308 my $c1 = shift @v1_components;
294 133308 my $c2 = shift @v2_components;
295  
296 133308 my $res = _compare_components($c1, $c2);
297 133308 ▶ return $res if $res != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $res != 0 115619 17689 133308
298  
299 17689 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
if not length $v1 || length $v2 and $v1_extra_components == 0 and $v2_extra_components == 0 and not @v1_components and not @v2_components 2444 15245 17689
Condition100%15/15
not length $v1 || length $v2 and $v1_extra_components == 0 and $v2_extra_components == 0 and not @v1_components and not @v2_components
!ll&&!rl&&r
1524322444
not length $v1 || length $v2 and $v1_extra_components == 0 and $v2_extra_components == 0 and not @v1_components
!ll&&!rl&&r
1385813852446
not length $v1 || length $v2 and $v1_extra_components == 0 and $v2_extra_components == 0
!ll&&!rl&&r
13842163831
not length $v1 || length $v2 and $v1_extra_components == 0
!ll&&!rl&&r
1384023847
length $v1 || length $v2
l!l&&r!l&&!r
1250813323849
MC/DC100%6/6
not length $v1 || length $v2 and $v1_extra_components == 0 and $v2_extra_components == 0 and not @v1_components and not @v2_components
length $v1 length $v2 $v1_extra_components == 0 $v2_extra_components == 0 not @v1_components not @v2_components
Truth table100%7/7
not(A || B) and C and D and not(E) and not(F)
A= length $v1B= length $v2C= $v1_extra_components == 0D= $v2_extra_components == 0E= not @v1_componentsF= not @v2_components
ABCDEFresult
1XXXXX0
01XXXX0
000XXX0
0010XX0
00110X0
0011100
0011111
300 if (!length($v1)
301 && !length($v2)
302 && $v1_extra_components == 0
303 && $v2_extra_components == 0
304 && !@v1_components
305 && !@v2_components);
306  
307 }
308  
309 0 return 0;
310  
311 }
312  
313 266616 ▶sub _components {
Subroutine _components: called CC=4 SCAR=13.9
314  
315 266616 my ($v_components, $v_string, $v_flags, $v_extra) = @_;
316  
317 266616 ▶ return if @{$v_components};
Branch: 1 branch
BranchTrueFalseTotal
if @{$v_components;} 4743 261873 266616
318  
319 261873 ▶ if (length(${$v_string})) {
Branch: 1 branch
BranchTrueFalseTotal
if (length ${$v_string;}) 252528 9345 261873
320  
321 252528 my ($components, $rest) = _get_next_version_component(${$v_string}, $v_flags);
322  
323 252528 push @{$v_components}, @{$components};
324 252528 ${$v_string} = $rest;
325  
326 252528 return;
327  
328 }
329  
330 # Fill extra components
331 9345 my $component = _make_default_component();
332  
333 9345 ▶ if (${$v_extra} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$v_extra;} > 0) 36 9309 9345
334 36 ${$v_extra}--;
335 36 $component = _make_default_component($v_flags);
336 }
337  
338 9345 push @{$v_components}, $component;
339  
340 9345 return;
341  
342 }
343  
344 227 ▶sub version_compare {
Subroutine version_compare: called CC=2 SCAR=6.9
Pod documented
345 227 ▶ (@_ == 2) ? version_compare2(@_) : version_compare4(@_);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 155 72 227
346 }
347  
348 4 1;
349 __END__
↑ Summary