File Coverage

blib/lib/URI/VersionRange/Constraint.pm
Criterion Covered Total %
statement 43 44 97.7
branch 9 10 90.0
condition 1 2 50.0
subroutine 14 15 93.3
pod 7 7 100.0
total 74 78 94.8


line stmt bran cond sub pod time code
1             package URI::VersionRange::Constraint;
2              
3 4     4   28 use feature ':5.10';
  4         8  
  4         541  
4 4     4   42 use strict;
  4         8  
  4         104  
5 4     4   18 use utf8;
  4         8  
  4         23  
6 4     4   147 use warnings;
  4         8  
  4         208  
7              
8 4     4   50 use Carp ();
  4         8  
  4         109  
9 4     4   16 use Exporter qw(import);
  4         7  
  4         229  
10              
11 4     4   765 use overload '""' => 'to_string', fallback => 1;
  4         2068  
  4         32  
12              
13 4     4   2674 use URI::VersionRange::Version;
  4         13  
  4         2583  
14              
15             our $VERSION = '2.25';
16              
17             our %COMPARATOR = (
18             '=' => 'equal',
19             '<' => 'less than',
20             '<=' => 'less than or equal',
21             '>' => 'greater than',
22             '>=' => 'greater than or equal',
23             );
24              
25             sub new {
26              
27 1156     1156 1 4023 my ($class, %params) = @_;
28              
29 1156   50     3233 my $comparator = delete $params{comparator} // '=';
30 1156         2286 my $version = delete $params{version};
31              
32 1156         3159 my $self = {comparator => $comparator, version => $version};
33              
34 1156         5806 return bless $self, $class;
35             }
36              
37 4467     4467 1 18700 sub version { shift->{version} }
38 6954     6954 1 15563 sub comparator { shift->{comparator} }
39              
40             sub from_string {
41              
42 1153     1153 1 2480 my ($class, $string) = @_;
43              
44 1153 50       2533 Carp::croak 'Empty version' unless $string;
45              
46             # - For each :
47             # - Determine if the starts with one of the two comparators:
48             # - If it starts with ">=", then the comparator is ">=".
49             # - If it starts with "<=", then the comparator is "<=".
50             # - If it starts with "!=", then the comparator is "!=".
51             # - If it starts with "<", then the comparator is "<".
52             # - If it starts with ">", then the comparator is ">".
53             # - Remove the comparator from string start. The remaining string is the version.
54             # - Otherwise the version is the full string (which implies an equality comparator of "=")
55             # - Tools should validate and report an error if the version is empty.
56             # - If the version contains a percent "%" character, apply URL quoting rules to unquote this string.
57              
58 1153 100       4208 if ($string =~ /^(>=|<=|!=|<|>)(.*)/) {
59 1013         3076 my ($comparator, $version) = ($1, $2);
60 1013         2341 return $class->new(comparator => $comparator, version => $version);
61             }
62              
63 140 100       444 return $class->new(comparator => '*') if ($string eq '*');
64              
65 122         355 return $class->new(comparator => '=', version => $string);
66              
67             }
68              
69             sub to_string {
70              
71 2277     2277 1 3537 my $self = shift;
72              
73 2277 100       4219 return '*' if $self->comparator eq '*';
74              
75 2272 100       4167 return $self->version if $self->comparator eq '=';
76              
77 2038         5149 return join '', $self->comparator, $self->version;
78              
79             }
80              
81 0     0 1 0 sub to_human_string { sprintf '%s %s', $COMPARATOR{$_[0]->comparator}, $_[0]->version }
82              
83 3     3 1 289 sub TO_JSON { {version => $_[0]->version, comparator => $_[0]->comparator} }
84              
85             1;
86              
87             __END__