Versioning/Scheme/Dotted.pm

filter: Statementstmt 100% 65 / 65 Branchbran 96.8% 31 / 32 Conditioncond 81.4% 22 / 27 MC/DCmcdc 64.2% 9 / 14 Subroutinesub 100% 10 / 10 Podpod 100% 5 / 5 totaltotal 92.8% 142 / 153 CC 32 SCAR 22.7
CC
32
Cov
95%
bump_version
16.1
cmp_version
8.0
normalize_version
8.0
CRAP 9.7
« Versioning/Scheme/Semantic.pm ↑ Summary Versioning/Scheme/Monotonic.pm »
1 package Versioning::Scheme::Dotted;
2  
3 2 our $DATE = '2020-10-01'; # DATE
4 2 our $VERSION = '0.011'; # VERSION
5  
6 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 with 'Role::Versioning::Scheme';
13  
14 2 our $re = qr/\A[0-9]+(?:\.[0-9]+)*\z/;
15  
16 39 ▶sub is_valid_version {
Subroutine is_valid_version: called CC=2 SCAR=6.9
Pod documented
17 39 my ($self, $v) = @_;
18 39 ▶ $v =~ $re ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$v =~ /$Versioning::Scheme::Dotted::re/ ? : 33 6 39
19 }
20  
21 2 ▶sub parse_version {
Subroutine parse_version: called CC=2 SCAR=6.9
Pod documented
22 2 my ($self, $v) = @_;
23 2 ▶ $v =~ $re or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$v =~ /$Versioning::Scheme::Dotted::re/ or return undef 1 1 2
24 1 {parts => [split /\./, $v]};
25 }
26  
27 7 ▶sub normalize_version {
Subroutine normalize_version: called CC=8 SCAR=20.8
Pod documented
28 7 my ($self, $v, $opts) = @_;
29 7 ▶ $opts //= {};
Condition100%2/2
$opts //= {}
l!l
43
MC/DC100%1/1
$opts //= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
30  
31 7 ▶ die "Invalid version '$v'" unless $self->is_valid_version($v);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid_version($v) 1 6 7
32  
33 6 my @parts = split /\./, $v;
34 6 ▶ if (defined $opts->{parts}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{"parts"}) 4 2 6
35 4 ▶ die "parts must at least be 1" unless $opts->{parts} >= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"parts"} >= 1 1 3 4
36 3 ▶ if ($opts->{parts} < @parts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'parts'} < @parts) { } 1 2 3
37 1 splice @parts, $opts->{parts};
38 } else {
39 2 ▶ $parts[$opts->{parts}-1] //= 0;
Condition50%1/2
$parts[$opts->{"parts"} - 1] //= 0
l!l
02
MC/DC0%0/1
$parts[$opts->{"parts"} - 1] //= 0
$parts[$opts->{"parts"} - 1]
Truth table50%1/2
A
A= $parts[$opts->{"parts"} - 1]
Aresult
11
00
40 }
41 }
42 5 ▶ join(".", map { $_ // "0" } @parts);
Condition100%2/2
$_ // 0
l!l
141
MC/DC100%1/1
$_ // 0
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
43 }
44  
45 7 ▶sub cmp_version {
Subroutine cmp_version: called CC=8 SCAR=20.9
Pod documented
46 7 my ($self, $v1, $v2) = @_;
47  
48 7 ▶ die "Invalid version '$v1'" unless $self->is_valid_version($v1);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid_version($v1) 1 6 7
49 6 ▶ die "Invalid version '$v2'" unless $self->is_valid_version($v2);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid_version($v2) 1 5 6
50  
51 5 my @parts1 = split /\./, $v1;
52 5 my @parts2 = split /\./, $v2;
53 5 ▶ my $n = @parts1 < @parts2 ? @parts2 : @parts1;
Branch: 1 branch
BranchTrueFalseTotal
@parts1 < @parts2 ? : 0 5 5
54 5 for my $i (0..$n-1) {
55 12 ▶ my $cmp = ($parts1[$i] // 0) <=> ($parts2[$i] // 0);
Condition75%3/4
$parts1[$i] // 0
l!l
120
$parts2[$i] // 0
l!l
102
MC/DC0%0/1
$parts1[$i] // 0
$parts1[$i]
MC/DC100%1/1
$parts2[$i] // 0
$parts2[$i]
Truth table50%1/2
A
A= $parts1[$i]
Aresult
11
00
Truth table100%2/2
A
A= $parts2[$i]
Aresult
11
00
56 12 ▶ return $cmp if $cmp;
Branch: 1 branch
BranchTrueFalseTotal
if $cmp 2 10 12
57 }
58 3 0;
59 }
60  
61 12 ▶sub bump_version {
Subroutine bump_version: called CC=16 SCAR=27.8
Pod documented
62 12 my ($self, $v, $opts) = @_;
63 12 ▶ $opts //= {};
Condition100%2/2
$opts //= {}
l!l
102
MC/DC100%1/1
$opts //= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
64 12 ▶ $opts->{num} //= 1;
Condition100%2/2
$opts->{"num"} //= 1
l!l
66
MC/DC100%1/1
$opts->{"num"} //= 1
$opts->{"num"}
Truth table100%2/2
A
A= $opts->{"num"}
Aresult
11
00
65 12 ▶ $opts->{part} //= -1;
Condition100%2/2
$opts->{"part"} //= -1
l!l
57
MC/DC100%1/1
$opts->{"part"} //= -1
$opts->{"part"}
Truth table100%2/2
A
A= $opts->{"part"}
Aresult
11
00
66 12 ▶ $opts->{reset_smaller} //= 1;
Condition100%2/2
$opts->{"reset_smaller"} //= 1
l!l
111
MC/DC100%1/1
$opts->{"reset_smaller"} //= 1
$opts->{"reset_smaller"}
Truth table100%2/2
A
A= $opts->{"reset_smaller"}
Aresult
11
00
67  
68 12 ▶ die "Invalid version '$v'" unless $self->is_valid_version($v);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid_version($v) 1 11 12
69 11 ▶ die "Invalid 'num', must be non-zero" unless $opts->{num} != 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"num"} != 0 1 10 11
70 10 my @parts = split /\./, $v;
71 die "Invalid 'part', must not be smaller than -".@parts
72 10 ▶ if $opts->{part} < -@parts;
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"part"} < -@parts 1 9 10
73  
74 9 my $idx = $opts->{part};
75 9 ▶ $parts[$idx] //= 0;
Condition50%1/2
$parts[$idx] //= 0
l!l
90
MC/DC0%0/1
$parts[$idx] //= 0
$parts[$idx]
Truth table50%1/2
A
A= $parts[$idx]
Aresult
11
00
76 die "Cannot decrease version, would result in a negative part"
77 9 ▶ if $parts[$idx] + $opts->{num} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $parts[$idx] + $opts->{"num"} < 0 1 8 9
78 $parts[$idx] = sprintf("%0".length($parts[$idx])."d",
79 8 $parts[$idx]+$opts->{num});
80 8 ▶ if ($opts->{reset_smaller} && $opts->{num} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"reset_smaller"} and $opts->{"num"} > 0) 4 4 8
Condition100%3/3
$opts->{"reset_smaller"} and $opts->{"num"} > 0
!ll&&!rl&&r
134
MC/DC100%2/2
$opts->{"reset_smaller"} and $opts->{"num"} > 0
$opts->{"reset_smaller"} $opts->{"num"} > 0
Truth table100%3/3
A and B
A= $opts->{"reset_smaller"}B= $opts->{"num"} > 0
ABresult
0X0
100
111
81 4 ▶ $idx = @parts + $idx if $idx < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $idx < 0 3 1 4
82 4 for my $i ($idx+1 .. $#parts) {
83 3 ▶ $parts[$i] //= 0;
Condition50%1/2
$parts[$i] //= 0
l!l
30
MC/DC0%0/1
$parts[$i] //= 0
$parts[$i]
Truth table50%1/2
A
A= $parts[$i]
Aresult
11
00
84 3 $parts[$i] = sprintf("%0".length($parts[$i])."d", 0);
85 }
86 }
87 8 ▶ join(".", map {$_//0} @parts);
Condition50%1/2
$_ // 0
l!l
240
MC/DC0%0/1
$_ // 0
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
88 }
89  
90 2 1;
91 # ABSTRACT: Version as dotted numbers
92  
93 __END__
« Versioning/Scheme/Semantic.pm ↑ Summary Versioning/Scheme/Monotonic.pm »