VersionRange.pm

filter: Statementstmt 99.3% 150 / 151 Branchbran 82.1% 46 / 56 Conditioncond 85.1% 23 / 27 MC/DCmcdc 62.5% 10 / 16 Subroutinesub 100% 37 / 37 Podpod 100% 12 / 12 totaltotal 92.9% 278 / 299 CC 42 SCAR 22.0
CC
42
Cov
94%
contains
23.4
constraint_contains
8.2
new
5.0
CRAP 9.0
« PackageURL.pm ↑ Summary VersionRange/Constraint.pm »
1 package URI::VersionRange;
2  
3 4 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 ▶use URI::VersionRange::Constraint;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use URI::VersionRange::Util qw(native_range_to_vers version_compare);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use URI::VersionRange::Version;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶use constant DEBUG => $ENV{VERS_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use constant TRUE => !!1;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use constant FALSE => !!0;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 4 ▶use overload '""' => 'to_string', fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 4 ▶BEGIN { *VERS:: = *URI::VersionRange:: }
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 4 our $VERSION = '2.25';
25 4 our @EXPORT = qw(encode_vers decode_vers);
26  
27 4 my $VERS_REGEXP = qr{^vers:[a-z\\.\\-\\+][a-z0-9\\.\\-\\+]*/.+};
28  
29 791 ▶sub new {
Subroutine new: called CC=5 SCAR=16.2
Pod documented
30  
31 791 my ($class, %params) = @_;
32  
33 791 ▶ my $scheme = delete $params{scheme} or Carp::croak "Invalid Version Range: 'scheme' is required";
Branch: 1 branch
BranchTrueFalseTotal
my $scheme = delete $params{"scheme"} or Carp::croak("Invalid Version Range: 'scheme' is required") 791 0 791
34 791 ▶ my $constraints = delete $params{constraints} or Carp::croak "Invalid Version Range: 'constraints' is required";
Branch: 1 branch
BranchTrueFalseTotal
my $constraints = delete $params{"constraints"} or Carp::croak("Invalid Version Range: 'constraints' is required") 791 0 791
35  
36 791 my @constraints = ();
37  
38 791 foreach my $constraint (@{$constraints}) {
39  
40 1156 ▶ if (ref($constraint) ne 'URI::VersionRange::Constraint') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $constraint ne "URI::VersionRange::Constraint") 6 1150 1156
41 6 $constraint = URI::VersionRange::Constraint->from_string($constraint);
42 }
43  
44 1156 push @constraints, $constraint;
45  
46 }
47  
48 791 $scheme = lc $scheme;
49  
50 791 my $scheme_class = URI::VersionRange::Version->load($scheme);
51  
52 791 my $self = {scheme => $scheme, constraints => \@constraints, scheme_class => $scheme_class};
53  
54 791 return bless $self, $class;
55  
56 }
57  
58 2609 ▶sub scheme { shift->{scheme} }
Subroutine scheme: called CC=1 SCAR=0.0
Pod documented
59 1609 ▶sub constraints { shift->{constraints} }
Subroutine constraints: called CC=1 SCAR=0.0
Pod documented
60  
61 1 ▶sub encode_vers { __PACKAGE__->new(@_)->to_string }
Subroutine encode_vers: called CC=1 SCAR=0.0
Pod documented
62 23 ▶sub decode_vers { __PACKAGE__->from_string(shift) }
Subroutine decode_vers: called CC=1 SCAR=0.0
Pod documented
63  
64 737 ▶sub from_native {
Subroutine from_native: called CC=3 SCAR=11.0
Pod documented
65  
66 737 my ($class, %params) = @_;
67  
68 737 ▶ my $scheme = delete $params{scheme} or Carp::croak "Invalid Version Range: 'scheme' is required";
Branch: 1 branch
BranchTrueFalseTotal
my $scheme = delete $params{"scheme"} or Carp::croak("Invalid Version Range: 'scheme' is required") 737 0 737
69 737 ▶ my $range = delete $params{range} or Carp::croak "Invalid Version Range: 'range' is required";
Branch: 1 branch
BranchTrueFalseTotal
my $range = delete $params{"range"} or Carp::croak("Invalid Version Range: 'range' is required") 736 1 737
70  
71 736 my $vers = native_range_to_vers(lc $scheme, $range);
72 736 return $class->from_string($vers);
73  
74 }
75  
76 791 ▶sub from_string {
Subroutine from_string: called CC=5 SCAR=16.1
Pod documented
77  
78 791 my ($class, $string) = @_;
79  
80 791 ▶ if ($string !~ /$VERS_REGEXP/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($string =~ /$VERS_REGEXP/) 1 790 791
81 1 Carp::croak 'Malformed Version Range string';
82 }
83  
84 790 ▶ if ($string =~ /^vers\:(none|all)\// && $string !~ /^vers\:(none|all)\/\*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ m[^vers\:(none|all)/] and not $string =~ m[^vers\:(none|all)/\*$]) 2 788 790
Condition100%3/3
$string =~ m[^vers\:(none|all)/] and not $string =~ m[^vers\:(none|all)/\*$]
!ll&&!rl&&r
78262
MC/DC100%2/2
$string =~ m[^vers\:(none|all)/] and not $string =~ m[^vers\:(none|all)/\*$]
$string =~ m[^vers\:(none|all)/] not $string =~ m[^vers\:(none|all)/\*$]
Truth table100%3/3
A and not(B)
A= $string =~ m[^vers\:(none|all)/]B= not $string =~ m[^vers\:(none|all)/\*$]
ABresult
0X0
100
111
85 2 Carp::croak 'Malformed Version Range string';
86 }
87  
88 788 my %params = ();
89  
90 # - Remove all spaces and tabs.
91 # - Start from left, and split once on colon ":".
92 # - The left hand side is the URI-scheme that must be lowercase.
93 # Tools must validate that the URI-scheme value is vers.
94 # - The right hand side is the specifier.
95  
96 788 $string =~ s/(\s|\t)+//g;
97  
98 788 my @s1 = split(':', $string);
99  
100 # - Split the specifier from left once on a slash "/".
101 # - The left hand side is the <versioning-scheme> that must be lowercase. Tools
102 # should validate that the <versioning-scheme> is a known scheme.
103 # - The right hand side is a list of one or more constraints. Tools must validate
104 # that this constraints string is not empty ignoring spaces.
105  
106 788 my @s2 = split('/', $s1[1]);
107 788 $params{scheme} = lc $s2[0];
108  
109 # - If the constraints string is equal to "", the ``<version-constraint>``
110 # is "". Parsing is done and no further processing is needed for this vers.
111 # A tool should report an error if there are extra characters beyond "*".
112 # - Strip leading and trailing pipes "|" from the constraints string.
113 # - Split the constraints on pipe "|". The result is a list of <version-constraint>.
114 # Consecutive pipes must be treated as one and leading and trailing pipes ignored.
115  
116 788 $s2[1] =~ s/(^\|)|(\|$)//g;
117  
118 788 my @s3 = split(/\|/, $s2[1]);
119 788 $params{constraints} = [];
120  
121 # - For each <version-constraint>:
122 # - Determine if the <version-constraint> starts with one of the two comparators:
123 # - If it starts with ">=", then the comparator is ">=".
124 # - If it starts with "<=", then the comparator is "<=".
125 # - If it starts with "!=", then the comparator is "!=".
126 # - If it starts with "<", then the comparator is "<".
127 # - If it starts with ">", then the comparator is ">".
128 # - Remove the comparator from <version-constraint> string start. The remaining string is the version.
129 # - Otherwise the version is the full <version-constraint> string (which implies an equality comparator of "=")
130 # - Tools should validate and report an error if the version is empty.
131 # - If the version contains a percent "%" character, apply URL quoting rules to unquote this string.
132 # - Append the parsed (comparator, version) to the constraints list.
133  
134 788 foreach (@s3) {
135 1147 push @{$params{constraints}}, URI::VersionRange::Constraint->from_string($_);
136 }
137  
138 788 if (DEBUG) {
139 say STDERR "-- S1: @s1";
140 say STDERR "-- S2: @s2";
141 say STDERR "-- S3: @s3";
142 }
143  
144 788 return $class->new(%params);
145  
146 }
147  
148 1538 ▶sub to_string {
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
149 1538 my $self = shift;
150 1538 my @constraints = sort { version_compare($self->scheme, $a->version, $b->version) } @{$self->constraints};
151 1538 return join '', 'vers:', $self->scheme, '/', join('|', @constraints);
152 }
153  
154 23 ▶sub constraint_contains {
Subroutine constraint_contains: called CC=8 SCAR=21.1
Pod documented
155  
156 23 my ($self, $constraint, $version) = @_;
157  
158 23 ▶ return TRUE if $constraint->comparator eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if $constraint->comparator eq "*" 16 7 23
159  
160 7 my $version_class = $self->{scheme_class};
161  
162 7 my $v1 = $version_class->new($version);
163 7 my $v2 = $version_class->new($constraint->version);
164  
165 7 ▶ return ($v1 == $v2) if ($constraint->comparator eq '=');
Branch: 1 branch
BranchTrueFalseTotal
if $constraint->comparator eq "=" 0 7 7
166 7 ▶ return ($v1 != $v2) if ($constraint->comparator eq '!=');
Branch: 1 branch
BranchTrueFalseTotal
if $constraint->comparator eq "!=" 0 7 7
167 7 ▶ return ($v1 <= $v2) if ($constraint->comparator eq '<=');
Branch: 1 branch
BranchTrueFalseTotal
if $constraint->comparator eq "<=" 1 6 7
168 6 ▶ return ($v1 >= $v2) if ($constraint->comparator eq '>=');
Branch: 1 branch
BranchTrueFalseTotal
if $constraint->comparator eq ">=" 1 5 6
169 5 ▶ return ($v1 < $v2) if ($constraint->comparator eq '<');
Branch: 1 branch
BranchTrueFalseTotal
if $constraint->comparator eq "<" 4 1 5
170 1 ▶ return ($v1 > $v2) if ($constraint->comparator eq '>');
Branch: 1 branch
BranchTrueFalseTotal
if $constraint->comparator eq ">" 1 0 1
171  
172 0 return FALSE;
173  
174 }
175  
176 35 ▶sub contains {
Subroutine contains: called CC=23 SCAR=31.5
Pod documented
177  
178 35 my ($self, $version) = @_;
179  
180 35 my @first = ();
181 35 my @second = ();
182  
183 35 my $version_class = $self->{scheme_class};
184  
185 35 ▶ if (scalar @{$self->constraints} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->constraints;} == 1) 21 14 35
186 21 return $self->constraint_contains($self->constraints->[0], $version);
187 }
188  
189 14 foreach my $constraint (@{$self->constraints}) {
190  
191 # If the "tested version" is equal to the any of the constraint version
192 # where the constraint comparator is for equality (any of "=", "<=", or ">=")
193 # then the "tested version" is in the range. Check is finished.
194  
195 return TRUE
196 38 ▶ if ((first { $constraint->comparator eq $_ } ('=', '<=', '>='))
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &first(sub { $constraint->comparator eq $_; } , "=", "<=", ">=") and $version_class->new($version) == $version_class->new($constraint->version) 2 36 38
Condition100%3/3
&first(sub { $constraint->comparator eq $_; } , "=", "<=", ">=") and $version_class->new($version) == $version_class->new($constraint->version)
!ll&&!rl&&r
13232
MC/DC100%2/2
&first(sub { $constraint->comparator eq $_; } , "=", "<=", ">=") and $version_class->new($version) == $version_class->new($constraint->version)
&first(sub { $constraint->comparator eq $_; } , "=", "<=", ">=") $version_class->new($version) == $version_class->new($constraint->version)
Truth table100%3/3
A and B
A= &first(sub { $constraint->comparator eq $_; } , "=", "<=", ">=")B= $version_class->new($version) == $version_class->new($constraint->version)
ABresult
0X0
100
111
197 && ($version_class->new($version) == $version_class->new($constraint->version)));
198  
199 # If the "tested version" is equal to the any of the constraint version
200 # where the constraint comparator is "=!" then the "tested version" is NOT
201 # in the range. Check is finished.
202  
203 36 ▶ return FALSE
Branch: 1 branch
BranchTrueFalseTotal
if $constraint->comparator eq "!=" and $version_class->new($version) == $version_class->new($constraint->version) 2 34 36
Condition100%3/3
$constraint->comparator eq "!=" and $version_class->new($version) == $version_class->new($constraint->version)
!ll&&!rl&&r
2952
MC/DC100%2/2
$constraint->comparator eq "!=" and $version_class->new($version) == $version_class->new($constraint->version)
$constraint->comparator eq "!=" $version_class->new($version) == $version_class->new($constraint->version)
Truth table100%3/3
A and B
A= $constraint->comparator eq "!="B= $version_class->new($version) == $version_class->new($constraint->version)
ABresult
0X0
100
111
204 if ($constraint->comparator eq '!='
205 && ($version_class->new($version) == $version_class->new($constraint->version)));
206  
207 # Split the constraint list in two sub lists:
208 # a first list where the comparator is "=" or "!="
209 # a second list where the comparator is neither "=" nor "!="
210  
211 34 ▶ push @first, $constraint if ((first { $constraint->comparator eq $_ } ('=', '!=')));
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &first(sub { $constraint->comparator eq $_; } , "=", "!=") 12 22 34
212 34 ▶ push @second, $constraint if (!(first { $constraint->comparator eq $_ } ('=', '!=')));
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless &first(sub { $constraint->comparator eq $_; } , "=", "!=") 22 12 34
213  
214 }
215  
216 10 ▶ return FALSE unless @second;
Branch: 1 branch
BranchTrueFalseTotal
unless @second 0 10 10
217  
218 10 ▶ if (scalar @second == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @second == 1) 2 8 10
219 2 return $self->constraint_contains($second[0], $version);
220 }
221  
222 # Iterate over the current and next contiguous constraints pairs (aka. pairwise)
223 # in the second list.
224  
225 # For each current and next constraint:
226  
227 8 my $is_first_iteration = TRUE;
228  
229 8 my $current_constraint = undef;
230 8 my $next_constraint = undef;
231  
232 8 foreach (_pairwise(@second)) {
233  
234 8 ($current_constraint, $next_constraint) = @{$_};
235  
236 8 DEBUG and say STDERR sprintf '-- Current constraint --> %s', $current_constraint;
237 8 DEBUG and say STDERR sprintf '-- Next constraint --> %s', $next_constraint;
238  
239 # If this is the first iteration and current comparator is "<" or <=" and
240 # the "tested version" is less than the current version then the "tested
241 # version" is IN the range. Check is finished.
242  
243 8 ▶ if ($is_first_iteration) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_first_iteration) 8 0 8
244  
245 return TRUE
246 8 ▶ if ((first { $current_constraint->comparator eq $_ } ('<=', '<'))
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if &first(sub { $current_constraint->comparator eq $_; } , "<=", "<") and $version_class->new($version) < $version_class->new($current_constraint->version) 0 8 8
Condition66%2/3
&first(sub { $current_constraint->comparator eq $_; } , "<=", "<") and $version_class->new($version) < $version_class->new($current_constraint->version)
!ll&&!rl&&r
710
MC/DC0%0/2
&first(sub { $current_constraint->comparator eq $_; } , "<=", "<") and $version_class->new($version) < $version_class->new($current_constraint->version)
&first(sub { $current_constraint->comparator eq $_; } , "<=", "<") $version_class->new($version) < $version_class->new($current_constraint->version)
Truth table66%2/3
A and B
A= &first(sub { $current_constraint->comparator eq $_; } , "<=", "<")B= $version_class->new($version) < $version_class->new($current_constraint->version)
ABresult
0X0
100
111
247 && ($version_class->new($version) < $version_class->new($current_constraint->version)));
248  
249 8 $is_first_iteration = FALSE;
250  
251 }
252  
253 # If current comparator is ">" or >=" and next comparator is "<" or <="
254 # and the "tested version" is greater than the current version and the
255 # "tested version" is less than the next version then the "tested version"
256 # is IN the range. Check is finished.
257  
258 8 ▶ if ( (first { $current_constraint->comparator eq $_ } ('>', '>='))
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 2 branches
BranchTrueFalseTotal
if (&first(sub { $current_constraint->comparator eq $_; } , '>', '>=') and &first(sub { $next_constraint->comparator eq $_; } , '<', '<=') and $version_class->new($version) > $version_class->new($current_constraint->version) and $version_class->new($version) < $version_class->new($next_constraint->version)) { } 3 5 8
elsif (&first(sub { $current_constraint->comparator eq $_; } , '<', '<=') and &first(sub { $next_constraint->comparator; } , '>', '>=')) { } 1 8 9
Condition88%8/9
&first(sub { $current_constraint->comparator eq $_; } , ">", ">=") and &first(sub { $next_constraint->comparator eq $_; } , "<", "<=") and $version_class->new($version) > $version_class->new($current_constraint->version) and $version_class->new($version) < $version_class->new($next_constraint->version)
!ll&&!rl&&r
413
&first(sub { $current_constraint->comparator eq $_; } , ">", ">=") and &first(sub { $next_constraint->comparator eq $_; } , "<", "<=") and $version_class->new($version) > $version_class->new($current_constraint->version)
!ll&&!rl&&r
134
&first(sub { $current_constraint->comparator eq $_; } , ">", ">=") and &first(sub { $next_constraint->comparator eq $_; } , "<", "<=")
!ll&&!rl&&r
107
MC/DC75%3/4
&first(sub { $current_constraint->comparator eq $_; } , ">", ">=") and &first(sub { $next_constraint->comparator eq $_; } , "<", "<=") and $version_class->new($version) > $version_class->new($current_constraint->version) and $version_class->new($version) < $version_class->new($next_constraint->version)
&first(sub { $current_constraint->comparator eq $_; } , ">", ">=") &first(sub { $next_constraint->comparator eq $_; } , "<", "<=") $version_class->new($version) > $version_class->new($current_constraint->version) $version_class->new($version) < $version_class->new($next_constraint->version)
Truth table80%4/5
A and B and C and D
A= &first(sub { $current_constraint->comparator eq $_; } , ">", ">=")B= &first(sub { $next_constraint->comparator eq $_; } , "<", "<=")C= $version_class->new($version) > $version_class->new($current_constraint->version)D= $version_class->new($version) < $version_class->new($next_constraint->version)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
259 14 ▶ && (first { $next_constraint->comparator eq $_ } ('<', '<='))
Subroutine __ANON__: called CC=1 SCAR=0.0
260 && ($version_class->new($version) > $version_class->new($current_constraint->version))
261 && ($version_class->new($version) < $version_class->new($next_constraint->version)))
262 {
263 3 return TRUE;
264 }
265  
266 # If current comparator is "<" or <=" and next comparator is ">" or >="
267 # then these versions are out the range. Continue to the next iteration.
268  
269 10 ▶ elsif ((first { $current_constraint->comparator eq $_ } ('<', '<='))
Subroutine __ANON__: called CC=1 SCAR=0.2
Condition66%2/3
&first(sub { $current_constraint->comparator eq $_; } , "<", "<=") and &first(sub { $next_constraint->comparator; } , ">", ">=")
!ll&&!rl&&r
401
MC/DC50%1/2
&first(sub { $current_constraint->comparator eq $_; } , "<", "<=") and &first(sub { $next_constraint->comparator; } , ">", ">=")
&first(sub { $current_constraint->comparator eq $_; } , "<", "<=") &first(sub { $next_constraint->comparator; } , ">", ">=")
Truth table66%2/3
A and B
A= &first(sub { $current_constraint->comparator eq $_; } , "<", "<=")B= &first(sub { $next_constraint->comparator; } , ">", ">=")
ABresult
0X0
100
111
270 1 ▶ && (first { $next_constraint->comparator } ('>', '>=')))
Subroutine __ANON__: called CC=1 SCAR=0.0
271 {
272 1 next;
273 }
274  
275 }
276  
277 # If this is the last iteration and next comparator is ">" or >=" and the
278 # "tested version" is greater than the next version then the "tested version"
279 # is IN the range. Check is finished.
280  
281 return TRUE
282 5 ▶ if ((first { $next_constraint->comparator eq $_ } ('>', '>='))
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if &first(sub { $next_constraint->comparator eq $_; } , ">", ">=") and $version_class->new($version) > $version_class->new($next_constraint->version) 0 5 5
Condition66%2/3
&first(sub { $next_constraint->comparator eq $_; } , ">", ">=") and $version_class->new($version) > $version_class->new($next_constraint->version)
!ll&&!rl&&r
410
MC/DC0%0/2
&first(sub { $next_constraint->comparator eq $_; } , ">", ">=") and $version_class->new($version) > $version_class->new($next_constraint->version)
&first(sub { $next_constraint->comparator eq $_; } , ">", ">=") $version_class->new($version) > $version_class->new($next_constraint->version)
Truth table66%2/3
A and B
A= &first(sub { $next_constraint->comparator eq $_; } , ">", ">=")B= $version_class->new($version) > $version_class->new($next_constraint->version)
ABresult
0X0
100
111
283 && ($version_class->new($version) > $version_class->new($next_constraint->version)));
284  
285 5 return FALSE;
286  
287 }
288  
289 1 ▶sub to_hash {
Subroutine to_hash: called CC=1 SCAR=0.0
Pod documented
290 1 return {scheme => $_[0]->scheme, constraints => $_[0]->constraints};
291 }
292  
293 1 ▶sub TO_JSON { shift->to_hash }
Subroutine TO_JSON: called CC=1 SCAR=0.0
Pod documented
294  
295 8 ▶sub _pairwise {
Subroutine _pairwise: called CC=3 SCAR=11.0
296  
297 8 my @out = ();
298  
299 8 ▶ for (my $i = 0; $i < scalar @_; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @_) 16 8 24
300 16 ▶ push @out, [$_[$i], $_[$i + 1]] if $_[$i + 1];
Branch: 1 branch
BranchTrueFalseTotal
if $_[$i + 1] 8 8 16
301 }
302  
303 8 return @out;
304  
305 }
306  
307 4 1;
308  
309 __END__
« PackageURL.pm ↑ Summary VersionRange/Constraint.pm »