lib/SemVer.pm

filter: Statementstmt 95.4% 127 / 133 Branchbran 90.3% 47 / 52 Conditioncond 80.9% 17 / 21 MC/DCmcdc 61.5% 8 / 13 Subroutinesub 100% 17 / 17 Podpod 100% 8 / 8 totaltotal 91.8% 224 / 244 SCAR 35.7
CC
35
Cov
93%
vcmp
22.7
parse
4.0
new
4.0
CRAP 35.5
« blib/lib/SemVer.pm ↑ Summary Build »
1 package SemVer;
2
3 1 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use version 0.82;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 use overload (
9 1 '""' => 'stringify',
10 '<=>' => 'vcmp',
11 'cmp' => 'vcmp',
12 1 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
13
14 1 our @ISA = qw(version);
15 1 our $VERSION = '0.7.0'; # For Module::Build
16
17 9 ▶sub _die { require Carp; Carp::croak(@_) }
Subroutine _die: called CC=1 SCAR=0.0
18
19 # Prevent version.pm from mucking with our internals.
20 ▶sub import {}
Subroutine import: called CC=1 SCAR=0.0
21
22 # Adapted from version.pm.
23 1 my $STRICT_INTEGER_PART = qr/0|[1-9][0-9]*/;
24 1 my $DOT_SEPARATOR = qr/\./;
25 1 my $PLUS_SEPARATOR = qr/\+/;
26 1 my $DASH_SEPARATOR = qr/-/;
27 1 my $STRICT_DOTTED_INTEGER_PART = qr/$DOT_SEPARATOR$STRICT_INTEGER_PART/;
28 1 my $STRICT_DOTTED_INTEGER_VERSION = qr/ $STRICT_INTEGER_PART $STRICT_DOTTED_INTEGER_PART{2,} /x;
29 1 my $IDENTIFIER = qr/[-0-9A-Za-z]+/;
30 1 my $DOTTED_IDENTIFIER = qr/(?:$DOT_SEPARATOR$IDENTIFIER)*/;
31 1 my $PRERELEASE = qr/$IDENTIFIER$DOTTED_IDENTIFIER/;
32 1 my $METADATA = qr/$IDENTIFIER$DOTTED_IDENTIFIER/;
33
34 1 my $OPTIONAL_EXTRA_PART = qr/$PRERELEASE($PLUS_SEPARATOR$METADATA)?/;
35
36 sub new {
37 596 ▶ my ($class, $ival) = @_;
Subroutine new: called CC=4 SCAR=13.9
Pod documented
38
39 # Handle vstring.
40 596 ▶ return $class->SUPER::new($ival) if Scalar::Util::isvstring($ival);
Branch: 1 branch
BranchTrueFalseTotal
if &Scalar::Util::isvstring($ival) 5 591 596
41
42 # Let version handle cloning.
43 591 ▶ if (eval { $ival->isa('version') }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $ival->isa("version") } }) 315 276 591
44 315 my $self = $class->SUPER::new($ival);
45 315 $self->{extra} = $ival->{extra};
46 315 $self->{dash} = $ival->{dash};
47 315 $self->_evalPreRelease($self->{extra});
48 315 return $self;
49 }
50
51 276 my ($val, $dash, $extra) = (
52 $ival =~ /^v?($STRICT_DOTTED_INTEGER_VERSION)(?:($DASH_SEPARATOR)($OPTIONAL_EXTRA_PART))?$/
53 );
54 276 ▶ _die qq{Invalid semantic version string format: "$ival"}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 8 268 276
55 unless defined $val;
56
57 268 my $self = $class->SUPER::new($val);
58 268 $self->{dash} = $dash;
59 268 $self->{extra} = $extra;
60 268 $self->_evalPreRelease($self->{extra});
61
62 268 return $self;
63 }
64
65 # Internal function to split up given string into prerelease- and patch-components
66 sub _evalPreRelease {
67 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
68 643 ▶ my $self = shift;
Subroutine _evalPreRelease: called CC=2 SCAR=6.9
69 643 my $v = shift;
70 643 my ($preRelease, $plus, $patch) = (
71 $v =~ /^($PRERELEASE)(?:($PLUS_SEPARATOR)($METADATA))?$/
72 );
73 643 @{$self->{prerelease}} = split $DOT_SEPARATOR,$preRelease;
74 643 $self->{plus} = $plus;
75 643 ▶ @{$self->{patch}} = (split $DOT_SEPARATOR, $patch || undef);
Condition50%1/2
$patch || undef
l!l
0643
MC/DC0%0/1
$patch || undef
$patch
Truth table50%1/2
A
A= $patch
Aresult
11
00
76 643 return;
77 }
78
79 1 $VERSION = __PACKAGE__->new($VERSION); # For ourselves.
80
81 sub declare {
82 347 ▶ my ($class, $ival) = @_;
Subroutine declare: called CC=4 SCAR=13.9
Pod documented
83 return $class->new($ival) if Scalar::Util::isvstring($ival)
84 347 ▶ or eval { $ival->isa('version') };
Branch: 1 branch
BranchTrueFalseTotal
if &Scalar::Util::isvstring($ival) or eval { do { $ival->isa("version") } } 296 51 347
Condition100%3/3
&Scalar::Util::isvstring($ival) or eval { do { $ival->isa("version") } }
l!l&&r!l&&!r
229451
MC/DC100%2/2
&Scalar::Util::isvstring($ival) or eval { do { $ival->isa("version") } }
&Scalar::Util::isvstring($ival) eval { do { $ival->isa("version") } }
Truth table100%3/3
A or B
A= &Scalar::Util::isvstring($ival)B= eval { do { $ival->isa("version") } }
ABresult
1X1
011
000
85
86 51 (my $v = $ival) =~ s/^v?$STRICT_DOTTED_INTEGER_VERSION(?:($DASH_SEPARATOR)($OPTIONAL_EXTRA_PART))[[:space:]]*$//;
87 51 my $dash = $1;
88 51 my $extra = $2;
89 51 ▶ $v += 0 if $v =~ s/_//g; # ignore underscores.
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ s/_//g 2 49 51
90 51 my $self = $class->SUPER::declare($v);
91 44 $self->{dash} = $dash;
92 44 $self->{extra} = $extra;
93 44 $self->_evalPreRelease($self->{extra});
94 44 return $self;
95 }
96
97 sub parse {
98 17 ▶ my ($class, $ival) = @_;
Subroutine parse: called CC=4 SCAR=13.9
Pod documented
99 return $class->new($ival) if Scalar::Util::isvstring($ival)
100 17 ▶ or eval { $ival->isa('version') };
Branch: 1 branch
BranchTrueFalseTotal
if &Scalar::Util::isvstring($ival) or eval { do { $ival->isa("version") } } 1 16 17
Condition66%2/3
&Scalar::Util::isvstring($ival) or eval { do { $ival->isa("version") } }
l!l&&r!l&&!r
1016
MC/DC50%1/2
&Scalar::Util::isvstring($ival) or eval { do { $ival->isa("version") } }
&Scalar::Util::isvstring($ival) eval { do { $ival->isa("version") } }
Truth table66%2/3
A or B
A= &Scalar::Util::isvstring($ival)B= eval { do { $ival->isa("version") } }
ABresult
1X1
011
000
101
102 16 (my $v = $ival) =~ s/^v?$STRICT_DOTTED_INTEGER_VERSION(?:($DASH_SEPARATOR)($OPTIONAL_EXTRA_PART))[[:space:]]*$//;
103 16 my $dash = $1;
104 16 my $extra = $2;
105 16 ▶ $v += 0 if $v =~ s/_//g; # ignore underscores.
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ s/_//g 1 15 16
106 16 my $self = $class->SUPER::parse($v);
107 16 $self->{dash} = $dash;
108 16 $self->{extra} = $extra;
109 16 $self->_evalPreRelease($self->{extra});
110 16 return $self;
111 }
112
113 sub stringify {
114 665 ▶ my $self = shift;
Subroutine stringify: called CC=4 SCAR=13.9
Pod documented
115 665 my $str = $self->SUPER::stringify;
116 # This is purely for SemVers constructed from version objects.
117 665 ▶ $str += 0 if $str =~ s/_//g; # ignore underscores.
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ s/_//g 1 664 665
118 665 ▶ return $str . ($self->{dash} || '') . ($self->{extra} || '');
Condition100%4/4
$self->{'dash'} || ''
l!l
252413
$self->{'extra'} || ''
l!l
252413
MC/DC100%1/1
$self->{'dash'} || ''
$self->{'dash'}
MC/DC100%1/1
$self->{'extra'} || ''
$self->{'extra'}
Truth table100%2/2
A
A= $self->{'dash'}
Aresult
11
00
Truth table100%2/2
A
A= $self->{'extra'}
Aresult
11
00
119 }
120
121 sub normal {
122 111 ▶ my $self = shift;
Subroutine normal: called CC=2 SCAR=6.9
Pod documented
123 111 (my $norm = $self->SUPER::normal) =~ s/^v//;
124 111 $norm =~ s/_/./g;
125 111 ▶ return $norm . ($self->{extra} ? "-$self->{extra}" : '');
Branch: 1 branch
BranchTrueFalseTotal
$self->{'extra'} ? : 5 106 111
126 }
127
128 1 ▶sub numify { _die 'Semantic versions cannot be numified'; }
Subroutine numify: called CC=1 SCAR=0.0
Pod documented
129 12 ▶sub is_alpha { !!shift->{extra} }
Subroutine is_alpha: called CC=1 SCAR=0.0
Pod documented
130
131
132 # Sort Ordering:
133 # Precedence refers to how versions are compared to each other when ordered. Precedence MUST be calculated by
134 # separating the version into major, minor, patch and pre-release identifiers in that order (Build metadata does not figure into precedence).
135 # Precedence is determined by the first difference when comparing each of these identifiers from left to right as follows:
136 # 1. Major, minor, and patch versions are always compared numerically. Example: 1.0.0 < 2.0.0 < 2.1.0 < 2.1.1.
137 # 2. When major, minor, and patch are equal, a pre-release version has lower precedence than a normal version.
138 # Example: 1.0.0-alpha < 1.0.0.
139 # 3. Precedence for two pre-release versions with the same major, minor, and patch version MUST be determined by
140 # comparing each dot separated identifier from left to right until a difference is found as follows:
141 # 3.a. identifiers consisting of only digits are compared numerically and identifiers with letters or hyphens are
142 # compared lexically in ASCII sort order.
143 # 3.b. Numeric identifiers always have lower precedence than non-numeric identifiers.
144 # 3.c. A larger set of pre-release fields has a higher precedence than a smaller set, if all of the preceding identifiers are equal.
145 # Example: 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta < 1.0.0-beta < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0.
146 sub vcmp {
147 321 ▶ my $left = shift;
Subroutine vcmp: called CC=21 SCAR=31.2
Pod documented
148 321 my $right = ref($left)->declare(shift);
149
150 # Reverse?
151 321 ▶ ($left, $right) = shift() ? ($right, $left): ($left, $right);
Branch: 1 branch
BranchTrueFalseTotal
shift() ? : 23 298 321
152
153 # Major and minor win. - case 1.
154 321 ▶ if (my $ret = $left->SUPER::vcmp($right, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ret = $left->SUPER::vcmp($right, 0)) { } 108 213 321
155 108 return $ret;
156 } else { #cases 2, 3
157 213 my $lenLeft = 0;
158 213 my $lenRight = 0;
159 213 ▶ if (defined $left->{prerelease}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $left->{"prerelease"}) 211 2 213
160 211 $lenLeft = scalar(@{$left->{prerelease}});
161 }
162 213 ▶ if (defined $right->{prerelease}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $right->{"prerelease"}) 213 0 213
163 213 $lenRight = scalar(@{$right->{prerelease}});
164 }
165 213 my $lenMin = ($lenLeft, $lenRight)[$lenLeft > $lenRight];
166 213 ▶ if ( $lenLeft == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lenLeft == 0) { } 108 105 213
167 108 ▶ if ($lenRight == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lenRight == 0) { } 100 8 108
168 100 return 0; # Neither LEFT nor RIGHT have prerelease identifiers - versions are equal
169 } else {
170 # Case 2: When major, minor, and patch are equal, a pre-release version has lower precedence than a normal version.
171 8 return 1; # Only RIGHT has prelease - not LEFT -> LEFT wins
172 }
173 } else {
174 105 ▶ if ($lenRight == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lenRight == 0) { } 17 88 105
175 # Case 2: When major, minor, and patch are equal, a pre-release version has lower precedence than a normal version.
176 17 return -1; # Only LEFT has prelease identifiers - not RIGHT -> RIGHT wins
177 } else {
178 # LEFT and RIGHT have prelease identifiers - compare each part separately
179 88 ▶ for (my $i = 0; $i < $lenMin; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $lenMin) 95 32 127
180 95 my $isNumLeft = Scalar::Util::looks_like_number($left->{prerelease}->[$i]);
181 95 my $isNumRight = Scalar::Util::looks_like_number($right->{prerelease}->[$i]);
182 # Case 3.b: Numeric identifiers always have lower precedence than non-numeric identifiers
183 95 ▶ if (!$isNumLeft && $isNumRight) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $isNumLeft and $isNumRight) { } 0 95 95
elsif ($isNumLeft and not $isNumRight) { } 1 94 95
elsif ($isNumLeft and $isNumRight) { } 6 88 94
Condition66%2/3
not $isNumLeft and $isNumRight
!ll&&!rl&&r
7880
MC/DC0%0/2
not $isNumLeft and $isNumRight
not $isNumLeft $isNumRight
Truth table66%2/3
not(A) and B
A= not $isNumLeftB= $isNumRight
ABresult
0X0
100
111
184 0 return 1; # LEFT identifier is Non-numeric - RIGHT identifier is numeric -> LEFT wins
185 ▶ } elsif ($isNumLeft && !$isNumRight) {
Condition100%3/3
$isNumLeft and not $isNumRight
!ll&&!rl&&r
8861
MC/DC100%2/2
$isNumLeft and not $isNumRight
$isNumLeft not $isNumRight
Truth table100%3/3
A and not(B)
A= $isNumLeftB= not $isNumRight
ABresult
0X0
100
111
186 1 return -1; # LEFT identifier is numeric - RIGHT identifier is non-numeric -> RIGHT wins
187 ▶ } elsif ($isNumLeft && $isNumRight) {
Condition66%2/3
$isNumLeft and $isNumRight
!ll&&!rl&&r
8806
MC/DC50%1/2
$isNumLeft and $isNumRight
$isNumLeft $isNumRight
Truth table66%2/3
A and B
A= $isNumLeftB= $isNumRight
ABresult
0X0
100
111
188 # Case 3.a.1: identifiers consisting of only digits are compared numerically
189 6 ▶ if ($left->{prerelease}->[$i] == $right->{prerelease}->[$i] ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($left->{'prerelease'}[$i] == $right->{'prerelease'}[$i]) { } 0 6 6
elsif ($left->{'prerelease'}[$i] > $right->{'prerelease'}[$i]) { } 0 6 6
190 0 next; # LEFT identifier and RIGHT identifier are equal - step to next part
191 } elsif ($left->{prerelease}->[$i] > $right->{prerelease}->[$i] ) {
192 0 return 1; # LEFT identifier is bigger than RIGHT identifier -> LEFT wins
193 } else {
194 6 return -1; return 1; # LEFT identifier is smaller than RIGHT identifier -> RIGHT wins
195 }
196 } else {
197 # Case 3.a.2: identifiers with letters or hyphens are compared lexically in ASCII sort order.
198 88 ▶ if (lc $left->{prerelease}->[$i] eq lc $right->{prerelease}->[$i] ) {
Branch: 2 branches
BranchTrueFalseTotal
if (lc $left->{'prerelease'}[$i] eq lc $right->{'prerelease'}[$i]) { } 39 49 88
elsif (lc $left->{'prerelease'}[$i] gt lc $right->{'prerelease'}[$i]) { } 10 39 49
199 39 next; # LEFT identifier and RIGHT identifier are equal - step to next part
200 } elsif (lc $left->{prerelease}->[$i] gt lc $right->{prerelease}->[$i] ) {
201 10 return 1; # LEFT identifier is bigger than RIGHT identifier -> LEFT wins
202 } else {
203 39 return -1; return 1; # LEFT identifier is smaller than RIGHT identifier -> RIGHT wins
204 }
205 }
206 }
207 # Case 3.c: A larger set of pre-release fields has a higher precedence than a smaller set, if all of the preceding identifiers are equal
208 32 ▶ if ($lenLeft > $lenRight) {
Branch: 2 branches
BranchTrueFalseTotal
if ($lenLeft > $lenRight) { } 0 32 32
elsif ($lenLeft < $lenRight) { } 6 26 32
209 0 return 1; # All existing identifiers are equal, but LEFT has more identifiers -> LEFT wins
210 } elsif ($lenLeft < $lenRight) {
211 6 return -1; # All existing identifiers are equal, but RIGHT has more identifiers -> RIGHT wins
212 }
213 # All identifiers are equal
214 26 return 0;
215 }
216 }
217 }
218 }
219
220 1 1;
221 __END__
« blib/lib/SemVer.pm ↑ Summary Build »