blib/lib/SemVer.pm

filter: Statementstmt 100% 79 / 79 Branchbran 100% 28 / 28 Conditioncond 90.0% 9 / 10 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 100% 15 / 15 Podpod 100% 8 / 8 totaltotal 98.6% 144 / 146 SCAR 29.4
CC
19
Cov
99%
vcmp
6.0
parse
4.0
new
4.0
CRAP 19.0
↑ 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.6.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 $STRICT_DOTTED_INTEGER_PART = qr/\.$STRICT_INTEGER_PART/;
25 1 my $STRICT_DOTTED_INTEGER_VERSION =
26 qr/ $STRICT_INTEGER_PART $STRICT_DOTTED_INTEGER_PART{2,} /x;
27 1 my $OPTIONAL_EXTRA_PART = qr/[a-zA-Z][-0-9A-Za-z]*/;
28  
29 sub new {
30 338 ▶ my ($class, $ival) = @_;
Subroutine new: called CC=4 SCAR=13.9
Pod documented
31  
32 # Handle vstring.
33 338 ▶ return $class->SUPER::new($ival) if Scalar::Util::isvstring($ival);
Branch: 1 branch
BranchTrueFalseTotal
if &Scalar::Util::isvstring($ival) 5 333 338
34  
35 # Let version handle cloning.
36 333 ▶ if (eval { $ival->isa('version') }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $ival->isa("version") } }) 231 102 333
37 231 my $self = $class->SUPER::new($ival);
38 231 $self->{extra} = $ival->{extra};
39 231 $self->{dash} = $ival->{dash};
40 231 return $self;
41 }
42  
43 102 my ($val, $dash, $extra) = (
44 $ival =~ /^v?($STRICT_DOTTED_INTEGER_VERSION)(?:(-)($OPTIONAL_EXTRA_PART))?$/
45 );
46 102 ▶ _die qq{Invalid semantic version string format: "$ival"}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 8 94 102
47 unless defined $val;
48  
49 94 my $self = $class->SUPER::new($val);
50 94 $self->{dash} = $dash;
51 94 $self->{extra} = $extra;
52 94 return $self;
53 }
54  
55 1 $VERSION = __PACKAGE__->new($VERSION); # For ourselves.
56  
57 sub declare {
58 270 ▶ my ($class, $ival) = @_;
Subroutine declare: called CC=4 SCAR=13.9
Pod documented
59 return $class->new($ival) if Scalar::Util::isvstring($ival)
60 270 ▶ or eval { $ival->isa('version') };
Branch: 1 branch
BranchTrueFalseTotal
if &Scalar::Util::isvstring($ival) or eval { do { $ival->isa("version") } } 212 58 270
Condition100%3/3
&Scalar::Util::isvstring($ival) or eval { do { $ival->isa("version") } }
l!l&&r!l&&!r
221058
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
61  
62 58 (my $v = $ival) =~ s/(?:(-?)($OPTIONAL_EXTRA_PART))[[:space:]]*$//;
63 58 my $dash = $1;
64 58 my $extra = $2;
65 58 ▶ $v += 0 if $v =~ s/_//g; # ignore underscores.
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ s/_//g 2 56 58
66 58 my $self = $class->SUPER::declare($v);
67 58 $self->{dash} = $dash;
68 58 $self->{extra} = $extra;
69 58 return $self;
70 }
71  
72 sub parse {
73 24 ▶ my ($class, $ival) = @_;
Subroutine parse: called CC=4 SCAR=13.9
Pod documented
74 return $class->new($ival) if Scalar::Util::isvstring($ival)
75 24 ▶ or eval { $ival->isa('version') };
Branch: 1 branch
BranchTrueFalseTotal
if &Scalar::Util::isvstring($ival) or eval { do { $ival->isa("version") } } 1 23 24
Condition66%2/3
&Scalar::Util::isvstring($ival) or eval { do { $ival->isa("version") } }
l!l&&r!l&&!r
1023
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
76  
77 23 (my $v = $ival) =~ s/(?:(-?)($OPTIONAL_EXTRA_PART))[[:space:]]*$//;
78 23 my $dash = $1;
79 23 my $extra = $2;
80 23 ▶ $v += 0 if $v =~ s/_//g; # ignore underscores.
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ s/_//g 1 22 23
81 23 my $self = $class->SUPER::parse($v);
82 23 $self->{dash} = $dash;
83 23 $self->{extra} = $extra;
84 23 return $self;
85 }
86  
87 sub stringify {
88 511 ▶ my $self = shift;
Subroutine stringify: called CC=4 SCAR=13.9
Pod documented
89 511 my $str = $self->SUPER::stringify;
90 # This is purely for SemVers constructed from version objects.
91 511 ▶ $str += 0 if $str =~ s/_//g; # ignore underscores.
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ s/_//g 1 510 511
92 511 ▶ return $str . ($self->{dash} || '') . ($self->{extra} || '');
Condition100%4/4
$self->{'dash'} || ''
l!l
149362
$self->{'extra'} || ''
l!l
163348
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
93 }
94  
95 sub normal {
96 125 ▶ my $self = shift;
Subroutine normal: called CC=2 SCAR=6.9
Pod documented
97 125 (my $norm = $self->SUPER::normal) =~ s/^v//;
98 125 $norm =~ s/_/./g;
99 125 ▶ return $norm . ($self->{extra} ? "-$self->{extra}" : '');
Branch: 1 branch
BranchTrueFalseTotal
$self->{'extra'} ? : 19 106 125
100 }
101  
102 1 ▶sub numify { _die 'Semantic versions cannot be numified'; }
Subroutine numify: called CC=1 SCAR=0.0
Pod documented
103 12 ▶sub is_alpha { !!shift->{extra} }
Subroutine is_alpha: called CC=1 SCAR=0.0
Pod documented
104  
105 sub vcmp {
106 244 ▶ my $left = shift;
Subroutine vcmp: called CC=6 SCAR=17.9
Pod documented
107 244 my $right = ref($left)->declare(shift);
108  
109 # Reverse?
110 244 ▶ ($left, $right) = shift() ? ($right, $left): ($left, $right);
Branch: 1 branch
BranchTrueFalseTotal
shift() ? : 30 214 244
111  
112 # Major and minor win.
113 244 ▶ if (my $ret = $left->SUPER::vcmp($right, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ret = $left->SUPER::vcmp($right, 0)) { } 62 182 244
114 62 return $ret;
115 } else {
116 # They're equal. Check the extra text stuff.
117 182 ▶ if (my $l = $left->{extra}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $l = $left->{'extra'}) { } 74 108 182
118 74 ▶ my $r = $right->{extra} or return -1;
Branch: 1 branch
BranchTrueFalseTotal
my $r = $right->{"extra"} or return -1 8 66 74
119 66 return lc $l cmp lc $r;
120 } else {
121 108 ▶ return $right->{extra} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$right->{'extra'} ? : 8 100 108
122 }
123 }
124 }
125  
126 1 1;
127 __END__
↑ Summary Build »