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
« blib/lib/SemVer/V2/Strict.pm ↑ Summary Build »
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.16';
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 =>
10 8 ▶    qr/(?<major>[0-9]+)(?:\.(?<minor>[0-9]+))?(?:\.(?<patch>[0-9]+))?@{[PRE_RELEASE_FORMAT]}@{[BUILD_METADATA_FORMAT]}/;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 use overload (
16 8     q{""} => \&as_string,
17     q{<=>} => \&_cmp,
18     fallback => 1,
19 8 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
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 sub new {
28 47 ▶    my $class = shift;
Subroutine new: called CC=4 SCAR=13.9
Pod documented
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 sub _init_by_version_string {
39 51 ▶    my ($self, $version) = @_;
Subroutine _init_by_version_string: called CC=4 SCAR=13.9
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 sub _init_by_version_numbers {
51 1 ▶    my ($self, $major, $minor, $patch, $pre_release, $build_metadata) = @_;
Subroutine _init_by_version_numbers: called CC=4 SCAR=14.5
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 sub clean {
61 12 ▶    my ($class, $version) = @_;
Subroutine clean: called CC=1 SCAR=0.0
Pod documented
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 sub sort {
70 1 ▶    my ($class, @versions) = @_;
Subroutine sort: called CC=1 SCAR=0.0
Pod documented
71 1     return sort { $class->new($a) <=> $class->new($b) } @versions;
72 }
73  
74 sub as_string {
75 14 ▶    my $self = shift;
Subroutine as_string: called CC=3 SCAR=11.0
Pod documented
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 sub _cmp {
85 17 ▶    my ($self, $other) = @_;
Subroutine _cmp: called CC=4 SCAR=13.9
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 sub _compare_pre_release {
94 11 ▶    my ($a, $b) = @_;
Subroutine _compare_pre_release: called CC=15 SCAR=27.1
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__
125
126 =encoding utf-8
127
128 =head1 NAME
129
130 SemVer::V2::Strict - Semantic version v2.0 object for Perl
131
132 =head1 SYNOPSIS
133
134 use SemVer::V2::Strict;
135
136 my $v1 = SemVer::V2::Strict->new('1.0.2');
137 my $v2 = SemVer::V2::Strict->new('2.0.0-alpha.10');
138
139 if ($v1 < $v2) {
140 print "$v1 < $v2\n"; # => '1.0.2 < 2.0.0-alpha.10'
141 }
142
143 =head1 DESCRIPTION
144
145 This module subclasses version to create semantic versions, as defined by the L<Semantic Versioning 2.0.0|http://semver.org/spec/v2.0.0.html> Specification.
146
147 =head1 METHODS
148
149 =head2 CLASS METHODS
150
151 =head3 C<new()>
152
153 Create new empty C<SemVer::V2::Strict> instance.
154 C<SemVer::V2::Strict-E<gt>new()> equals C<SemVer::V2::Strict-E<gt>new('0.0.0')>.
155
156 =head3 C<new($version_string)>
157
158 Create new C<SemVer::V2::Strict> instance from a version string.
159 C<SemVer::V2::Strict-E<gt>new('1.0.0')> equals C<SemVer::V2::Strict-E<gt>new(1, 0, 0)>.
160
161 =head3 C<new($major, $minor = 0, $patch = 0, $pre_release = undef, $build_metadata = undef)>
162
163 Create new C<SemVer::V2::Strict> instance from version numbers.
164 C<SemVer::V2::Strict-E<gt>new('1.0.0-alpha+100')> equals C<SemVer::V2::Strict-E<gt>new(1, 0, 0, 'alpha', '100')>.
165
166 =head3 C<clean($version_string)>
167
168 Clean version string. Trim spaces and C<'v'> prefix.
169
170 =head3 C<sort(... $version_string)>
171
172 Sort version strings.
173
174 =head2 METHODS
175
176 =head3 C<major>
177
178 Get the major version number.
179
180 =head3 C<minor>
181
182 Return the minor version number.
183
184 =head3 C<patch>
185
186 Return the patch version number.
187
188 =head3 C<pre_release>
189
190 Return the pre_release string.
191
192 =head3 C<build_metadata>
193
194 Return the build_metadata string.
195
196 =head3 C<E<lt>=E<gt>>
197
198 Compare two C<SemVer::V2::Strict> instances.
199
200 =head3 C<"">
201
202 Convert a C<SemVer::V2::Strict> instance to string.
203
204 =head3 C<as_string()>
205
206 Convert a C<SemVer::V2::Strict> instance to string.
207
208 =head1 SEE ALSO
209
210 =over
211
212 =item * L<SemVer>
213
214 =item * L<version>
215
216 =back
217
218 =head1 LICENSE
219
220 The MIT License (MIT)
221
222 Copyright (c) 2015-2017 Pine Mizune
223
224 Permission is hereby granted, free of charge, to any person obtaining a copy
225 of this software and associated documentation files (the "Software"), to deal
226 in the Software without restriction, including without limitation the rights
227 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
228 copies of the Software, and to permit persons to whom the Software is
229 furnished to do so, subject to the following conditions:
230
231 The above copyright notice and this permission notice shall be included in
232 all copies or substantial portions of the Software.
233
234 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
235 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
236 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
237 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
238 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
239 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
240 THE SOFTWARE.
241
242 =head1 ACKNOWLEDGEMENT
243
244 C<SemVer::V2::Strict> is based from rosylilly's L<semver|https://github.com/rosylilly/semver>.
245 Thank you.
246
247 =head1 AUTHOR
248
249 Pine Mizune E<lt>pinemz@gmail.comE<gt>
250
251 =cut
252
« blib/lib/SemVer/V2/Strict.pm ↑ Summary Build »