blib/lib/Version/Semantic.pm

filter: Statementstmt 99.3% 148 / 149 Branchbran 97.5% 78 / 80 Conditioncond 87.5% 21 / 24 MC/DCmcdc 73.3% 11 / 15 Subroutinesub 96.8% 31 / 32 Podpod 0.0% 0 / 20 totaltotal 90.3% 289 / 320 CC 55 SCAR 19.6
CC
55
Cov
98%
compare_to
17.0
new
10.0
parse
7.0
CRAP 7.1
↑ Summary
1 # Prefer numeric version for backwards compatibility
2 7 ▶BEGIN { require 5.010000 }; ## no critic ( RequireUseStrict, RequireUseWarnings )
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 58 ▶my $croak = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
7 58 require Carp;
8 58 goto &Carp::croak
9 7 };
10  
11 7 my $prefix_re = qr/v/;
12  
13 # <numeric identifier>
14 7 my $num_id_re = qr/0 | [1-9] [0-9]*/x;
15  
16 # <build identifier>
17 7 my $build_id_re = qr/[0-9a-zA-Z-]+/;
18  
19 # <build>
20 7 my $build_re = qr/$build_id_re (?: \. $build_id_re )*/x;
21  
22 # <pre-release identifier>
23 7 my $pre_release_id_re = qr/$num_id_re | [0-9A-Za-z-]* [A-Za-z-] [0-9A-Za-z-]*/x;
24  
25 # <pre-release>
26 7 my $pre_release_re = qr/$pre_release_id_re (?: \. $pre_release_id_re )*/x;
27  
28 ## no critic ( ProhibitComplexRegexes )
29 7 my $corever_re = qr/
30 (?<prefix> $prefix_re)?
31 (?<major> $num_id_re) \. (?<minor> $num_id_re) \. (?<patch> $num_id_re)
32 /x;
33  
34 # On purpose use "build" (the BNF symbol name) instead of "buildmetadata" as
35 # the name of the last named capture group
36 # <valid semver>
37 7 my $semver_re = qr/
38 $corever_re
39 (?: - (?<pre_release> $pre_release_re) )?
40 (?: \+ (?<build> $build_re) )?
41 /x;
42  
43 #<<<
44 package Version::Core; ## no critic ( RequireFilenameMatchesPackage )
45 7 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
46 7 our $VERSION = 'v2.2.0';
47 }
48 #>>>
49  
50 7 ▶use overload '<=>' => 'compare_to', '""' => 'to_string';
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 13 ▶sub prefix { shift->{ prefix } }
Subroutine prefix: called CC=1 SCAR=0.0
Pod undocumented
53 104 ▶sub major { shift->{ major } }
Subroutine major: called CC=1 SCAR=0.0
Pod undocumented
54 89 ▶sub minor { shift->{ minor } }
Subroutine minor: called CC=1 SCAR=0.0
Pod undocumented
55 98 ▶sub patch { shift->{ patch } }
Subroutine patch: called CC=1 SCAR=0.0
Pod undocumented
56  
57 # Derived attribute
58 47 ▶sub version_core { shift->{ version_core } }
Subroutine version_core: called CC=1 SCAR=0.0
Pod undocumented
59  
60 5 ▶sub has_prefix { exists shift->{ prefix } }
Subroutine has_prefix: called CC=1 SCAR=0.0
Pod undocumented
61  
62 0 ▶sub corever_re { $corever_re }
Subroutine corever_re: not called CC=1 SCAR=6.9
Pod undocumented
63  
64 # Constructor as factory method
65 171 ▶sub parse {
Subroutine parse: called CC=7 SCAR=19.5
Pod undocumented
66 171 ▶ my $options = ( ref $_[ -1 ] eq 'HASH' ) ? pop : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'HASH' ? : 84 87 171
67 171 my ( $class, $version ) = @_;
68 171 ▶ $version //= '';
Condition100%2/2
$version //= ""
l!l
1692
MC/DC100%1/1
$version //= ""
$version
Truth table100%2/2
A
A= $version
Aresult
11
00
69  
70 171 ▶ my $ver_re = $class ne __PACKAGE__ ? $semver_re : $corever_re;
Branch: 1 branch
BranchTrueFalseTotal
$class ne __PACKAGE__ ? : 157 14 171
71 171 ▶ unless ( $version =~ m/\A$ver_re\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($version =~ /\A$ver_re\z/) 46 125 171
72 45 ▶ $croak->( "Version '$version' is not a ${ \( $class ne __PACKAGE__ ? 'semantic': 'core' ) } version" )
Branch: 1 branch
BranchTrueFalseTotal
$class ne __PACKAGE__ ? : 42 3 45
73 46 ▶ if $options->{ fatal };
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"fatal"} 45 1 46
74 return
75 1 }
76  
77 125 $class->new( %+ )
78 }
79  
80 148 ▶sub new {
Subroutine new: called CC=10 SCAR=23.0
Pod undocumented
81 148 my $invocant = shift;
82 148 ▶ my $class = ref $invocant || $invocant;
Condition66%2/3
ref $invocant || $invocant
l!l&&r!l&&!r
111370
MC/DC0%0/2
ref $invocant || $invocant
ref $invocant $invocant
Truth table66%2/3
A || B
A= ref $invocantB= $invocant
ABresult
1X1
011
000
83  
84 148 my $self = do {
85  
86 # Validate @_
87 148 my $length;
88 148 my @tmp = @_;
89 148 ▶ while ( ( $length = scalar( my ( $name, $value ) = splice @tmp, 0, 2 ) ) == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($length = scalar(() = splice(@tmp, 0, 2))) == 2) 519 146 665
90 519 ▶ $croak->( "Parameter with undefined name passed to \"$class\" constructor" )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 2 517 519
91 unless defined $name
92 }
93 146 ▶ $croak->( "Odd number of arguments passed to \"$class\" constructor" )
Branch: 1 branch
BranchTrueFalseTotal
if $length == 1 2 144 146
94 if $length == 1;
95  
96 # Remove parameters that have undef values
97 144 my %tmp = @_;
98 144 for ( keys %tmp ) {
99 515 ▶ delete $tmp{ $_ } unless defined $tmp{ $_ }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tmp{$_} 0 515 515
100 }
101  
102 144 ▶ bless { ref $invocant ? %$invocant : (), %tmp } => $class
Branch: 1 branch
BranchTrueFalseTotal
ref $invocant ? : 11 133 144
103 };
104  
105 144 delete $self->{ version_core };
106  
107 144 my %params = map { $_ => 1 } $self->_init;
108 140 my @unknown_params = grep { not exists $params{ $_ } } keys %$self;
109 # Diagnostic message is copied from 'class' feature
110 140 ▶ $croak->( "Unrecognised parameters for \"$class\" constructor: " . join( ', ', @unknown_params ) )
Branch: 1 branch
BranchTrueFalseTotal
if @unknown_params 2 138 140
111 if @unknown_params;
112  
113 138 ▶ $self->{ version_core } = ( $self->{ prefix } // '' ) . join( '.', map { $self->{ $_ } } qw( major minor patch ) );
Condition100%2/2
$self->{"prefix"} // ""
l!l
16122
MC/DC100%1/1
$self->{"prefix"} // ""
$self->{"prefix"}
Truth table100%2/2
A
A= $self->{"prefix"}
Aresult
11
00
114  
115 138 $self
116 }
117  
118 {
119 7 my %isa = (
120 prefix => $prefix_re,
121 major => $num_id_re,
122 minor => $num_id_re,
123 patch => $num_id_re,
124 );
125  
126 144 ▶ sub _init {
Subroutine _init: called CC=6 SCAR=17.9
127 144 my $self = shift;
128  
129 144 foreach ( qw( major minor patch ) ) {
130 # Diagnostic message is copied from 'class' feature
131 2 $croak->( "Required parameter '$_' is missing for \"${ \ __PACKAGE__ }\" constructor" )
132 430 ▶ unless exists $self->{ $_ }
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$_} 2 428 430
133 }
134  
135 142 foreach ( keys %isa ) {
136 566 ▶ next unless exists $self->{ $_ };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$_} 125 441 566
137 $croak->( "Parameter '$_' has invalid value '$self->{ $_ }'" )
138 441 ▶ unless $self->{ $_ } =~ m/\A $isa{ $_ } \z/x
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$_} =~ /\A $isa{$_} \z/x 2 439 441
139 }
140  
141 qw( prefix major minor patch )
142 140 }
143 }
144  
145 7 ▶sub increment {
Subroutine increment: called CC=5 SCAR=16.1
Pod undocumented
146 # Obvious strategies are major, minor, and patch
147 10 my ( $self, $strategy ) = @_;
148 10 ▶ $strategy //= 'patch';
Condition100%2/2
$strategy //= "patch"
l!l
73
MC/DC100%1/1
$strategy //= "patch"
$strategy
Truth table100%2/2
A
A= $strategy
Aresult
11
00
149  
150 10 ▶ return $self->new( patch => $self->patch + 1 )
Branch: 1 branch
BranchTrueFalseTotal
if $strategy eq "patch" 6 4 10
151 if $strategy eq 'patch';
152 4 ▶ return $self->new( minor => $self->minor + 1, patch => 0 )
Branch: 1 branch
BranchTrueFalseTotal
if $strategy eq "minor" 1 3 4
153 if $strategy eq 'minor';
154 3 ▶ return $self->new( major => $self->major + 1, minor => 0, patch => 0 )
Branch: 1 branch
BranchTrueFalseTotal
if $strategy eq "major" 2 1 3
155 if $strategy eq 'major';
156  
157 1 $croak->( "Version incrementation strategy '$strategy' is not implemented" )
158 }
159  
160 # https://semver.org/spec/v2.0.0.html#spec-item-11
161 38 ▶sub compare_to {
Subroutine compare_to: called CC=3 SCAR=11.0
Pod undocumented
162 38 my ( $self, $other ) = @_;
163  
164 # 11.2
165 38 for ( qw( major minor patch ) ) {
166 101 ▶ return $self->$_ <=> $other->$_ if $self->$_ != $other->$_
Branch: 1 branch
BranchTrueFalseTotal
if $self->$_ != $other->$_ 16 85 101
167 }
168  
169 0
170 22 }
171  
172 {
173 7 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
174 7 *to_string = \&version_core
175 }
176  
177 #<<<
178 package Version::Semantic; ## no critic ( ProhibitMultiplePackages )
179 7 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
180 7 our $VERSION = 'v2.2.0';
181 }
182 #>>>
183  
184 7 ▶use parent -norequire, 'Version::Core';
Subroutine BEGIN: called CC=1 SCAR=0.0
185  
186 7 ▶use overload '<=>' => 'compare_to', '""' => 'to_string';
Subroutine BEGIN: called CC=1 SCAR=0.0
187  
188 sub _croakf ( $@ );
189  
190 75 ▶sub pre_release { shift->{ pre_release } }
Subroutine pre_release: called CC=1 SCAR=0.0
Pod undocumented
191 4 ▶sub build { shift->{ build } }
Subroutine build: called CC=1 SCAR=0.0
Pod undocumented
192  
193 100 ▶sub has_pre_release { exists shift->{ pre_release } }
Subroutine has_pre_release: called CC=1 SCAR=0.0
Pod undocumented
194 40 ▶sub has_build { exists shift->{ build } }
Subroutine has_build: called CC=1 SCAR=0.0
Pod undocumented
195  
196 20 ▶sub is_core {
Subroutine is_core: called CC=2 SCAR=6.9
Pod undocumented
197 20 my $self = shift;
198  
199 20 ▶ not( $self->has_pre_release ) and not( $self->has_build )
Condition100%3/3
$self->has_pre_release || $self->has_build
l!l&&r!l&&!r
1019
MC/DC100%2/2
$self->has_pre_release || $self->has_build
$self->has_pre_release $self->has_build
Truth table100%3/3
A || B
A= $self->has_pre_releaseB= $self->has_build
ABresult
1X1
011
000
200 }
201  
202 3 ▶sub semver_re { $semver_re }
Subroutine semver_re: called CC=1 SCAR=0.0
Pod undocumented
203  
204 {
205 7 my %isa = (
206 pre_release => $pre_release_re,
207 build => $build_re
208 );
209  
210 127 ▶ sub _init {
Subroutine _init: called CC=4 SCAR=13.9
211 127 my $self = shift;
212  
213 127 my @params = $self->SUPER::_init;
214  
215 125 foreach ( qw( pre_release build ) ) {
216 250 ▶ next unless exists $self->{ $_ };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$_} 157 93 250
217 $croak->( "Parameter '$_' has invalid value '$self->{ $_ }'" )
218 93 ▶ unless $self->{ $_ } =~ m/\A $isa{ $_ } \z/x
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$_} =~ /\A $isa{$_} \z/x 0 93 93
219 }
220  
221 125 ( @params, qw( pre_release build ) )
222 }
223 }
224  
225 {
226 7 my $trial_pre_release = qr/\A ( TRIAL ) ( [0-9]* ) \z/x;
227  
228 11 ▶ sub increment {
Subroutine increment: called CC=7 SCAR=19.5
Pod undocumented
229 # Obvious strategies are major, minor, and patch
230 11 my ( $self, $strategy, $pre_release ) = @_;
231  
232 11 ▶ if ( defined $strategy and $strategy eq 'trial' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $strategy and $strategy eq "trial") 4 7 11
Condition100%3/3
defined $strategy and $strategy eq "trial"
!ll&&!rl&&r
254
MC/DC100%2/2
defined $strategy and $strategy eq "trial"
defined $strategy $strategy eq "trial"
Truth table100%3/3
A and B
A= defined $strategyB= $strategy eq "trial"
ABresult
0X0
100
111
233 4 ▶ if ( $self->has_pre_release ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_pre_release) { } 3 1 4
234 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
235 2 ▶ return $self->new( pre_release => $string . ( ( $number eq '' ? 0 : $number ) + 1 ) )
Branch: 1 branch
BranchTrueFalseTotal
$number eq '' ? : 1 1 2
236 } else {
237 1 $croak->( "Pre-release extension '${ \( $self->pre_release ) }' does not match '$trial_pre_release'" )
238 }
239 } else {
240 1 $croak->( "Cannot apply '$strategy' version incrementation strategy to non pre-release version '$self'" )
241 }
242 }
243  
244 7 my $other = $self->SUPER::increment( $strategy );
245 6 ▶ $other->{ pre_release } = $pre_release
Branch: 1 branch
BranchTrueFalseTotal
if defined $pre_release 2 4 6
246 if defined $pre_release;
247 6 $other
248 }
249 }
250  
251 # https://semver.org/spec/v2.0.0.html#spec-item-11
252 7 ▶sub compare_to {
Subroutine compare_to: called CC=17 SCAR=28.3
Pod undocumented
253 35 my ( $self, $other ) = @_;
254  
255 # 11.2
256 {
257 35 my $sign = $self->SUPER::compare_to( $other );
258 35 ▶ return $sign unless $sign == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $sign == 0 13 22 35
259 }
260  
261 # Split pre-release into list of dot separated identifiers
262 22 ▶ my @a = $self->has_pre_release ? split /\./, $self->pre_release : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->has_pre_release ? : 20 2 22
263 22 ▶ my @b = $other->has_pre_release ? split /\./, $other->pre_release : ();
Branch: 1 branch
BranchTrueFalseTotal
$other->has_pre_release ? : 19 3 22
264  
265 # 11.3
266 22 ▶ if ( @a ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@a) { } 20 2 22
267 20 ▶ return -1 if not @b
Branch: 1 branch
BranchTrueFalseTotal
unless @b 2 18 20
268 } else {
269 2 ▶ return ( @b ? 1 : 0 )
Branch: 1 branch
BranchTrueFalseTotal
@b ? : 1 1 2
270 }
271  
272 # 11.4
273 18 ▶ my $len = @a < @b ? @a : @b;
Branch: 1 branch
BranchTrueFalseTotal
@a < @b ? : 3 15 18
274 18 ▶ for ( my $i = 0 ; $i < $len ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 24 5 29
275 24 my $ai = $a[ $i ];
276 24 my $bi = $b[ $i ];
277  
278 24 my $ai_is_num = $ai =~ m/\A $num_id_re \z/x;
279 24 my $bi_is_num = $bi =~ m/\A $num_id_re \z/x;
280  
281 # 11.4.1
282 24 my $sign;
283 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
284 5 $sign = $ai <=> $bi;
285 5 ▶ return $sign if $sign != 0
Branch: 1 branch
BranchTrueFalseTotal
if $sign != 0 3 2 5
286 # 11.4.3
287 ▶ } 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
288 2 return -1
289 # 11.4.3
290 ▶ } 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
291 1 return 1
292 } else {
293 16 $sign = $ai cmp $bi;
294 16 ▶ return $sign if $sign != 0
Branch: 1 branch
BranchTrueFalseTotal
if $sign != 0 7 9 16
295 }
296 }
297  
298 # 11.4.4
299 5 @a <=> @b
300 }
301  
302 24 ▶sub to_string {
Subroutine to_string: called CC=3 SCAR=11.0
Pod undocumented
303 24 my ( $self ) = @_;
304  
305 24 my $string = $self->SUPER::to_string;
306 24 ▶ $string .= '-' . $self->pre_release if $self->has_pre_release;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_pre_release 13 11 24
307 24 ▶ $string .= '+' . $self->build if $self->has_build;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_build 2 22 24
308 24 $string
309 }
310  
311 7 1
↑ Summary