lib/SemVer/V2/Strict.pm

filter: Statementstmt 100% 91 / 91 Branchbran 100% 34 / 34 Conditioncond 80.7% 21 / 26 MC/DCmcdc 66.6% 10 / 15 Subroutinesub 100% 22 / 22 Podpod 100% 9 / 9 totaltotal 94.9% 187 / 197 CC 29 SCAR 18.4
CC
29
Cov
97%
_compare_pre_release
15.0
_init_by_version_numbers
4.2
new
4.0
CRAP 6.3
↑ Summary
1 package SemVer::V2::Strict;
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 our $VERSION = '0.18';
6  
7 8 ▶use constant PRE_RELEASE_FORMAT => qr/(?:-(?<pre_release>[a-zA-Z0-9.\-]+))?/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use constant BUILD_METADATA_FORMAT => qr/(?:\+(?<build_metadata>[a-zA-Z0-9.\-]+))?/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use constant VERSION_FORMAT =>
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 qr/(?<major>[0-9]+)(?:\.(?<minor>[0-9]+))?(?:\.(?<patch>[0-9]+))?@{[PRE_RELEASE_FORMAT]}@{[BUILD_METADATA_FORMAT]}/;
11  
12 8 ▶use List::Util qw/min max/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use Scalar::Util qw/looks_like_number/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 8 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 q{""} => \&as_string,
17 q{<=>} => \&_cmp,
18 fallback => 1,
19 8 );
20  
21 62 ▶sub major { shift->{major} }
Subroutine major: called CC=1 SCAR=0.0
Pod documented
22 48 ▶sub minor { shift->{minor} }
Subroutine minor: called CC=1 SCAR=0.0
Pod documented
23 46 ▶sub patch { shift->{patch} }
Subroutine patch: called CC=1 SCAR=0.0
Pod documented
24 47 ▶sub pre_release { shift->{pre_release} }
Subroutine pre_release: called CC=1 SCAR=0.0
Pod documented
25 22 ▶sub build_metadata { shift->{build_metadata} }
Subroutine build_metadata: called CC=1 SCAR=0.0
Pod documented
26  
27 47 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
28 47 my $class = shift;
29 47 my $self = bless {} => $class;
30  
31 47 ▶ $self->_init_by_version_numbers if @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 1 46 47
32 47 ▶ $self->_init_by_version_string(@_) if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 45 2 47
33 43 ▶ $self->_init_by_version_numbers(@_) if @_ >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ >= 2 1 42 43
34  
35 43 return $self;
36 }
37  
38 51 ▶sub _init_by_version_string {
Subroutine _init_by_version_string: called CC=4 SCAR=13.9
39 51 my ($self, $version) = @_;
40  
41 51 ▶ die 'Invalid format' unless $version =~ qr/^@{[VERSION_FORMAT]}$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $version =~ qr"^@{[qr/(?<major>[0-9]+)(?:\.(?<minor>[0-9]+))?(?:\.(?<patch>[0-9]+))?(?^:(?:-(?<pre_release>[a-zA-Z0-9.\-]+))?)(?^:(?:\+(?<build_metadata>[a-zA-Z0-9.\-]+))?)/];}$" 6 45 51
42  
43 45 $self->{major} = $+{major};
44 45 ▶ $self->{minor} = $+{minor} // 0;
Condition100%2/2
$+{"minor"} // 0
l!l
432
MC/DC100%1/1
$+{"minor"} // 0
$+{"minor"}
Truth table100%2/2
A
A= $+{"minor"}
Aresult
11
00
45 45 ▶ $self->{patch} = $+{patch} // 0;
Condition100%2/2
$+{"patch"} // 0
l!l
414
MC/DC100%1/1
$+{"patch"} // 0
$+{"patch"}
Truth table100%2/2
A
A= $+{"patch"}
Aresult
11
00
46 45 $self->{pre_release} = $+{pre_release};
47 45 $self->{build_metadata} = $+{build_metadata};
48 }
49  
50 1 ▶sub _init_by_version_numbers {
Subroutine _init_by_version_numbers: called CC=4 SCAR=14.5
51 1 my ($self, $major, $minor, $patch, $pre_release, $build_metadata) = @_;
52  
53 1 ▶ $self->{major} = $major // 0;
Condition50%1/2
$major // 0
l!l
10
MC/DC0%0/1
$major // 0
$major
Truth table50%1/2
A
A= $major
Aresult
11
00
54 1 ▶ $self->{minor} = $minor // 0;
Condition50%1/2
$minor // 0
l!l
10
MC/DC0%0/1
$minor // 0
$minor
Truth table50%1/2
A
A= $minor
Aresult
11
00
55 1 ▶ $self->{patch} = $patch // 0;
Condition50%1/2
$patch // 0
l!l
10
MC/DC0%0/1
$patch // 0
$patch
Truth table50%1/2
A
A= $patch
Aresult
11
00
56 1 $self->{pre_release} = $pre_release;
57 1 $self->{build_metadata} = $build_metadata;
58 }
59  
60 12 ▶sub clean {
Subroutine clean: called CC=1 SCAR=0.0
Pod documented
61 12 my ($class, $version) = @_;
62  
63 12 $version =~ s/^\s*(.*?)\s*$/$1/; # trim
64 12 $version =~ s/^[=v]+//;
65  
66 12 return eval { $class->new($version)->as_string };
67 }
68  
69 1 ▶sub sort {
Subroutine sort: called CC=1 SCAR=0.0
Pod documented
70 1 my ($class, @versions) = @_;
71 1 return sort { $class->new($a) <=> $class->new($b) } @versions;
72 }
73  
74 14 ▶sub as_string {
Subroutine as_string: called CC=3 SCAR=11.0
Pod documented
75 14 my $self = shift;
76  
77 14 my $string = $self->major.'.'.$self->minor.'.'.$self->patch;
78 14 ▶ $string .= '-'.$self->pre_release if $self->pre_release;
Branch: 1 branch
BranchTrueFalseTotal
if $self->pre_release 5 9 14
79 14 ▶ $string .= '+'.$self->build_metadata if $self->build_metadata;
Branch: 1 branch
BranchTrueFalseTotal
if $self->build_metadata 2 12 14
80  
81 14 return $string;
82 }
83  
84 17 ▶sub _cmp {
Subroutine _cmp: called CC=4 SCAR=13.9
85 17 my ($self, $other) = @_;
86  
87 17 ▶ return $self->major <=> $other->major if $self->major != $other->major;
Branch: 1 branch
BranchTrueFalseTotal
if $self->major != $other->major 4 13 17
88 13 ▶ return $self->minor <=> $other->minor if $self->minor != $other->minor;
Branch: 1 branch
BranchTrueFalseTotal
if $self->minor != $other->minor 1 12 13
89 12 ▶ return $self->patch <=> $other->patch if $self->patch != $other->patch;
Branch: 1 branch
BranchTrueFalseTotal
if $self->patch != $other->patch 1 11 12
90 11 return _compare_pre_release($self->pre_release, $other->pre_release);
91 }
92  
93 11 ▶sub _compare_pre_release {
Subroutine _compare_pre_release: called CC=15 SCAR=27.1
94 11 my ($a, $b) = @_;
95  
96 11 ▶ return 1 if !defined $a && $b;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $a and $b 1 10 11
Condition100%3/3
not defined $a and $b
!ll&&!rl&&r
911
MC/DC100%2/2
not defined $a and $b
not defined $a $b
Truth table100%3/3
not(A) and B
A= not defined $aB= $b
ABresult
0X0
100
111
97 10 ▶ return -1 if $a && !defined $b;
Branch: 1 branch
BranchTrueFalseTotal
if $a and not defined $b 1 9 10
Condition100%3/3
$a and not defined $b
!ll&&!rl&&r
181
MC/DC100%2/2
$a and not defined $b
$a not defined $b
Truth table100%3/3
A and not(B)
A= $aB= not defined $b
ABresult
0X0
100
111
98  
99 9 ▶ if ($a && $b) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a and $b) 8 1 9
Condition66%2/3
$a and $b
!ll&&!rl&&r
108
MC/DC50%1/2
$a and $b
$a $b
Truth table66%2/3
A and B
A= $aB= $b
ABresult
0X0
100
111
100 8 my @left = split /-|\./, $a;
101 8 my @right = split /-|\./, $b;
102 8 my $max = max(scalar @left, scalar @right);
103  
104 8 ▶ for (my $i = 0; $i < $max; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $max) 12 2 14
105 12 ▶ my $a = $left[$i] // 0;
Condition100%2/2
$left[$i] // 0
l!l
102
MC/DC100%1/1
$left[$i] // 0
$left[$i]
Truth table100%2/2
A
A= $left[$i]
Aresult
11
00
106 12 ▶ my $b = $right[$i] // 0;
Condition50%1/2
$right[$i] // 0
l!l
120
MC/DC0%0/1
$right[$i] // 0
$right[$i]
Truth table50%1/2
A
A= $right[$i]
Aresult
11
00
107  
108 12 ▶ if (looks_like_number($a) && looks_like_number($b)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($a) and &looks_like_number($b)) 4 8 12
Condition100%3/3
&looks_like_number($a) and &looks_like_number($b)
!ll&&!rl&&r
714
MC/DC100%2/2
&looks_like_number($a) and &looks_like_number($b)
&looks_like_number($a) &looks_like_number($b)
Truth table100%3/3
A and B
A= &looks_like_number($a)B= &looks_like_number($b)
ABresult
0X0
100
111
109 4 ▶ return $a <=> $b if $a != $b;
Branch: 1 branch
BranchTrueFalseTotal
if $a != $b 3 1 4
110 }
111  
112 9 my $min = min(length $a, length $b);
113 9 ▶ for (my $n = 0; $n < $min; ++$n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $min) 25 6 31
114 25 my $c = substr($a, $n, 1) cmp substr($b, $n, 1);
115 25 ▶ return $c if $c != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $c != 0 3 22 25
116 }
117 }
118 }
119  
120 3 return 0;
121 }
122  
123 8 1;
124 __END__
↑ Summary