blib/lib/Version/Semantic.pm

filter: Statementstmt 100% 109 / 109 Branchbran 98.3% 61 / 62 Conditioncond 86.6% 13 / 15 MC/DCmcdc 77.7% 7 / 9 Subroutinesub 100% 25 / 25 Podpod 0.0% 0 / 17 totaltotal 90.7% 215 / 237 CC 41 SCAR 20.1
CC
41
Cov
98%
_compare_pre_release
16.0
new
9.0
increment
9.0
CRAP 7.4
↑ Summary Makefile.PL »
1 # Prefer numeric version for backwards compatibility
2 6 ▶BEGIN { require 5.010000 }; ## no critic ( RequireUseStrict, RequireUseWarnings )
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 #<<<
7 package Version::Semantic;
8 BEGIN {
9 6 ▶our $VERSION = 'v2.1.1';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 }
11 #>>>
12  
13 6 ▶use overload '<=>' => 'compare_to', '""' => 'to_string';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 sub _croakf ( $@ );
16  
17 6 my $prefix_re = qr/v/;
18  
19 # <numeric identifier>
20 6 my $num_id_re = qr/0 | [1-9] [0-9]*/x;
21  
22 # <build identifier>
23 6 my $build_id_re = qr/[0-9a-zA-Z-]+/;
24  
25 # <build>
26 6 my $build_re = qr/$build_id_re (?: \. $build_id_re )*/x;
27  
28 # <pre-release identifier>
29 6 my $pre_release_id_re = qr/$num_id_re | [0-9A-Za-z-]* [A-Za-z-] [0-9A-Za-z-]*/x;
30  
31 # <pre-release>
32 6 my $pre_release_re = qr/$pre_release_id_re (?: \. $pre_release_id_re )*/x;
33  
34 # Use BNF terminology
35 # https://semver.org/spec/v2.0.0.html#backusnaur-form-grammar-for-valid-semver-versions
36 9 ▶sub prefix { shift->{ prefix } }
Subroutine prefix: called CC=1 SCAR=0.0
Pod undocumented
37 91 ▶sub major { shift->{ major } }
Subroutine major: called CC=1 SCAR=0.0
Pod undocumented
38 81 ▶sub minor { shift->{ minor } }
Subroutine minor: called CC=1 SCAR=0.0
Pod undocumented
39 84 ▶sub patch { shift->{ patch } }
Subroutine patch: called CC=1 SCAR=0.0
Pod undocumented
40 20 ▶sub version_core { shift->{ version_core } }
Subroutine version_core: called CC=1 SCAR=0.0
Pod undocumented
41 67 ▶sub pre_release { shift->{ pre_release } }
Subroutine pre_release: called CC=1 SCAR=0.0
Pod undocumented
42 4 ▶sub build { shift->{ build } }
Subroutine build: called CC=1 SCAR=0.0
Pod undocumented
43  
44 4 ▶sub has_prefix { defined shift->{ prefix } }
Subroutine has_prefix: called CC=1 SCAR=0.0
Pod undocumented
45 64 ▶sub has_pre_release { defined shift->{ pre_release } }
Subroutine has_pre_release: called CC=1 SCAR=0.0
Pod undocumented
46 14 ▶sub has_build { defined shift->{ build } }
Subroutine has_build: called CC=1 SCAR=0.0
Pod undocumented
47  
48 sub is_core {
49 20 ▶ ( () = grep { defined } @{ +shift }{ qw( pre_release build ) } ) == 0
Subroutine is_core: called CC=1 SCAR=0.0
Pod undocumented
50 }
51  
52 {
53 ## no critic ( ProhibitComplexRegexes )
54 # On purpose use "build" (the BNF symbol name) instead of "buildmetadata" as
55 # the name of the last named capture group
56 # <valid semver> (ok)
57 6 my $semver_re = qr/
58 (?<prefix> $prefix_re)?
59 (?<major> $num_id_re) \. (?<minor> $num_id_re) \. (?<patch> $num_id_re)
60 (?: - (?<pre_release> $pre_release_re) )?
61 (?: \+ (?<build> $build_re) )?
62 /x;
63 3 ▶ sub semver_re { $semver_re }
Subroutine semver_re: called CC=1 SCAR=0.0
Pod undocumented
64  
65 # Constructor as factory method
66 sub parse {
67 157 ▶ my $options = ( ref $_[ -1 ] eq 'HASH' ) ? pop : {};
Subroutine parse: called CC=5 SCAR=16.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'HASH' ? : 42 115 157
68 157 my ( $class, $version ) = @_;
69 157 ▶ $version //= '';
Condition100%2/2
$version //= ""
l!l
1561
MC/DC100%1/1
$version //= ""
$version
Truth table100%2/2
A
A= $version
Aresult
11
00
70  
71 157 ▶ unless ( $version =~ m/\A$semver_re\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($version =~ /\A$semver_re\z/) 43 114 157
72 43 ▶ _croakf "Version '%s' is not a semantic version", $version if $options->{ fatal };
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"fatal"} 42 1 43
73 return
74 1 }
75  
76 114 $class->new( %+ )
77 }
78 }
79  
80 {
81 6 my %attrs = (
82 prefix => $prefix_re,
83 major => $num_id_re,
84 minor => $num_id_re,
85 patch => $num_id_re,
86 pre_release => $pre_release_re,
87 build => $build_re
88 );
89  
90 sub new {
91 129 ▶ my $invocant = shift;
Subroutine new: called CC=9 SCAR=22.0
Pod undocumented
92 129 my %args;
93  
94 # Validate args
95 {
96 6 ▶ use warnings FATAL => qw( misc uninitialized );
Subroutine BEGIN: called CC=1 SCAR=0.0
97 129 %args = @_
98 };
99 127 foreach ( keys %args ) {
100 # PerlX::Maybe::maybe
101 466 ▶ unless ( defined $args{ $_ } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{$_}) 4 462 466
102 4 delete $args{ $_ };
103 next
104 4 }
105 _croakf "Unknown attribute name '%s'", $_
106 462 ▶ unless exists $attrs{ $_ };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attrs{$_} 1 461 462
107 _croakf "Attribute '%s' has invalid value '%s'", $_, $args{ $_ }
108 461 ▶ unless $args{ $_ } =~ m/\A $attrs{ $_ } \z/x
Branch: 1 branch
BranchTrueFalseTotal
unless $args{$_} =~ /\A $attrs{$_} \z/x 1 460 461
109 }
110  
111 125 my $class;
112 125 ▶ if ( $class = ref $invocant ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class = ref $invocant) { } 8 117 125
113 # Shallow copy
114 8 %args = ( %$invocant, %args )
115 } else {
116 117 $class = $invocant;
117 }
118  
119 # Don't move this attribute checking
120 exists $args{ $_ } or _croakf "Required attribute '%s' not set", $_
121 125 ▶ foreach qw( major minor patch );
Branch: 1 branch
BranchTrueFalseTotal
exists $args{$_} or &_croakf("Required attribute '%s' not set", $_) 1 373 374
122  
123 bless { %args,
124 124 ▶ version_core => ( $args{ prefix } // '' ) . join( '.', map { $args{ $_ } } qw( major minor patch ) ) } => $class
Condition100%2/2
$args{"prefix"} // ""
l!l
11113
MC/DC100%1/1
$args{"prefix"} // ""
$args{"prefix"}
Truth table100%2/2
A
A= $args{"prefix"}
Aresult
11
00
125 }
126 }
127  
128 {
129 6 my $trial_pre_release = qr/\A ( TRIAL ) ( [0-9]* ) \z/x;
130  
131 sub increment {
132 # Obvious strategies are major|minor|patch
133 11 ▶ my ( $self, $strategy, $pre_release ) = @_;
Subroutine increment: called CC=9 SCAR=22.0
Pod undocumented
134 11 ▶ $strategy //= 'patch';
Condition100%2/2
$strategy //= "patch"
l!l
92
MC/DC100%1/1
$strategy //= "patch"
$strategy
Truth table100%2/2
A
A= $strategy
Aresult
11
00
135  
136 11 ▶ if ( $strategy eq 'trial' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($strategy eq "trial") 4 7 11
137 4 ▶ if ( $self->has_pre_release ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_pre_release) { } 3 1 4
138 3 ▶ if ( my ( $string, $number ) = $self->pre_release =~ $trial_pre_release ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $self->pre_release =~ /$trial_pre_release/) { } 2 1 3
139 2 ▶ return $self->new( pre_release => $string . ( ( $number eq '' ? 0 : $number ) + 1 ) )
Branch: 1 branch
BranchTrueFalseTotal
$number eq '' ? : 1 1 2
140 } else {
141 1 _croakf "Pre-release extension '%s' does not match '%s'", $self->pre_release, $trial_pre_release
142 }
143 } else {
144 1 _croakf "Cannot apply '%s' version incrementation strategy to non pre-release version '%s'", $strategy, $self
145 }
146 }
147 7 ▶ return $self->new( patch => $self->patch + 1, pre_release => $pre_release )
Branch: 1 branch
BranchTrueFalseTotal
if $strategy eq "patch" 4 3 7
148 if $strategy eq 'patch';
149 3 ▶ return $self->new( minor => $self->minor + 1, patch => 0, pre_release => $pre_release )
Branch: 1 branch
BranchTrueFalseTotal
if $strategy eq "minor" 1 2 3
150 if $strategy eq 'minor';
151 2 ▶ return $self->new( major => $self->major + 1, minor => 0, patch => 0, pre_release => $pre_release )
Branch: 1 branch
BranchTrueFalseTotal
if $strategy eq "major" 1 1 2
152 if $strategy eq 'major';
153  
154 1 _croakf "Version incrementation strategy '%s' is not implemented", $strategy
155 }
156 }
157  
158 # https://semver.org/spec/v2.0.0.html#spec-item-11
159 6 sub compare_to {
160 35 ▶ my ( $self, $other ) = @_;
Subroutine compare_to: called CC=3 SCAR=11.0
Pod undocumented
161  
162 # 11.2
163 35 for ( qw( major minor patch ) ) {
164 94 ▶ return $self->$_ <=> $other->$_ if $self->$_ != $other->$_
Branch: 1 branch
BranchTrueFalseTotal
if $self->$_ != $other->$_ 13 81 94
165 }
166 22 $self->_compare_pre_release( $other )
167 }
168  
169 sub to_string {
170 8 ▶ my ( $self ) = @_;
Subroutine to_string: called CC=3 SCAR=11.0
Pod undocumented
171  
172 8 my $string = $self->version_core;
173 8 ▶ $string .= '-' . $self->pre_release if $self->has_pre_release;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_pre_release 5 3 8
174 8 ▶ $string .= '+' . $self->build if $self->has_build;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_build 2 6 8
175 8 $string
176 }
177  
178 sub _compare_pre_release {
179 22 ▶ my ( $self, $other ) = @_;
Subroutine _compare_pre_release: called CC=16 SCAR=27.7
180  
181 # Split pre-release into list of dot separated identifiers
182 22 ▶ my @a = $self->has_pre_release ? split /\./, $self->pre_release : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->has_pre_release ? : 20 2 22
183 22 ▶ my @b = $other->has_pre_release ? split /\./, $other->pre_release : ();
Branch: 1 branch
BranchTrueFalseTotal
$other->has_pre_release ? : 19 3 22
184  
185 # 11.3
186 22 ▶ if ( @a ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@a) { } 20 2 22
187 20 ▶ return -1 if not @b
Branch: 1 branch
BranchTrueFalseTotal
unless @b 2 18 20
188 } else {
189 2 ▶ return ( @b ? 1 : 0 )
Branch: 1 branch
BranchTrueFalseTotal
@b ? : 1 1 2
190 }
191  
192 # 11.4
193 18 ▶ my $len = @a < @b ? @a : @b;
Branch: 1 branch
BranchTrueFalseTotal
@a < @b ? : 3 15 18
194 18 ▶ for ( my $i = 0 ; $i < $len ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 24 5 29
195 24 my $ai = $a[ $i ];
196 24 my $bi = $b[ $i ];
197  
198 24 my $ai_is_num = $ai =~ m/\A $num_id_re \z/x;
199 24 my $bi_is_num = $bi =~ m/\A $num_id_re \z/x;
200  
201 # 11.4.1
202 24 ▶ if ( $ai_is_num and $bi_is_num ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ai_is_num and $bi_is_num) { } 5 19 24
elsif ($ai_is_num and not $bi_is_num) { } 2 17 19
elsif (not $ai_is_num and $bi_is_num) { } 1 16 17
Condition100%3/3
$ai_is_num and $bi_is_num
!ll&&!rl&&r
1725
MC/DC100%2/2
$ai_is_num and $bi_is_num
$ai_is_num $bi_is_num
Truth table100%3/3
A and B
A= $ai_is_numB= $bi_is_num
ABresult
0X0
100
111
203 5 my $sign = $ai <=> $bi;
204 5 ▶ return $sign if $sign != 0
Branch: 1 branch
BranchTrueFalseTotal
if $sign != 0 3 2 5
205 # 11.4.3
206 ▶ } elsif ( $ai_is_num and not $bi_is_num ) {
Condition66%2/3
$ai_is_num and not $bi_is_num
!ll&&!rl&&r
1702
MC/DC50%1/2
$ai_is_num and not $bi_is_num
$ai_is_num not $bi_is_num
Truth table66%2/3
A and not(B)
A= $ai_is_numB= not $bi_is_num
ABresult
0X0
100
111
207 2 return -1
208 # 11.4.3
209 ▶ } elsif ( not $ai_is_num and $bi_is_num ) {
Condition66%2/3
not $ai_is_num and $bi_is_num
!ll&&!rl&&r
0161
MC/DC50%1/2
not $ai_is_num and $bi_is_num
not $ai_is_num $bi_is_num
Truth table66%2/3
not(A) and B
A= not $ai_is_numB= $bi_is_num
ABresult
0X0
100
111
210 1 return 1
211 } else {
212 16 my $sign = $ai cmp $bi;
213 16 ▶ return $sign if $sign != 0
Branch: 1 branch
BranchTrueFalseTotal
if $sign != 0 7 9 16
214 }
215 }
216  
217 # 11.4.4
218 5 @a <=> @b
219 }
220  
221 sub _croakf ( $@ ) {
222 48 ▶ require Carp;
Subroutine _croakf: called CC=2 SCAR=7.1
223 48 ▶ @_ = ( ( @_ == 1 ? shift : sprintf shift, @_ ) . ', stopped' );
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 48 48
224 48 goto &Carp::croak
225 }
226  
227 6 1
↑ Summary Makefile.PL »