lib/Gentoo/PerlMod/Version.pm

filter: Statementstmt 95.0% 116 / 122 Branchbran 85.7% 36 / 42 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 18 / 18 Podpod 100% 1 / 1 totaltotal 93.5% 174 / 186 SCAR 33.1
CC
27
Cov
93%
gentooize_version
9.0
_lax_cleaning_2
8.8
_lax_cleaning_1
5.0
CRAP 27.3
« blib/script/gentoo-perlmod-version.pl ↑ Summary lib/Gentoo/PerlMod/Version/Error.pm »
1 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Gentoo::PerlMod::Version;
6  
7 4 our $VERSION = '0.8.0';
8  
9 # ABSTRACT: Convert arbitrary Perl Modules' versions into normalized Gentoo versions.
10  
11 4 our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
12  
13 4 ▶use Sub::Exporter::Progressive -setup => { exports => [qw( gentooize_version )] };
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use version 0.77;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 sub gentooize_version {
17 68 ▶ my ( $perlver, $config ) = @_;
Subroutine gentooize_version: called CC=9 SCAR=22.0
Pod documented
18 68 ▶ $config ||= {};
Condition100%2/2
$config ||= {}
l!l
1949
MC/DC100%1/1
$config ||= {}
$config
Truth table100%2/2
A
A= $config
Aresult
11
00
19 68 ▶ if ( not defined $perlver ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $perlver) 1 67 68
20 1 return _err_perlver_undefined($config);
21 }
22 67 ▶ $config->{lax} = 0 unless defined $config->{lax};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config->{"lax"} 49 18 67
23 67 ▶ if ( _env_hasopt('always_lax') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_env_hasopt("always_lax")) 0 67 67
24 0 $config->{lax} = _env_getopt('always_lax');
25 }
26  
27 67 ▶ if ( $perlver =~ /\Av?[\d.]+\z/msx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($perlver =~ /\Av?[\d.]+\z/msx) 46 21 67
28 46 return _lax_cleaning_0($perlver);
29 }
30  
31 21 ▶ if ( $perlver =~ /\Av?[\d._]+(-TRIAL)?\z/msx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($perlver =~ /\Av?[\d._]+(-TRIAL)?\z/msx) 13 8 21
32 13 ▶ if ( $config->{lax} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"lax"} > 0) 11 2 13
33 11 return _lax_cleaning_1($perlver);
34 }
35 2 return _err_matches_trial_regex_nonlax( $perlver, $config );
36 }
37  
38 8 ▶ if ( 2 == $config->{lax} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (2 == $config->{"lax"}) 6 2 8
39 6 return _lax_cleaning_2($perlver);
40 }
41 2 return _err_not_decimal_or_trial( $perlver, $config );
42 }
43  
44 ###
45 #
46 # character to code translation
47 #
48 ###
49  
50 ## no critic ( ProhibitMagicNumbers )
51 my $char_map = {
52 40 ( map { $_ => $_ } 0 .. 9 ), # 0..9
53 104 ( map { chr( $_ + 65 ) => $_ + 10 } 0 .. 25 ), # A-Z
54 4 ( map { chr( $_ + 97 ) => $_ + 10 } 0 .. 25 ), # a-z
55 };
56  
57 # _code_for('z') -> $number
58 #
59  
60 sub _code_for {
61 25 ▶ my $char = shift;
Subroutine _code_for: called CC=2 SCAR=6.9
62 25 ▶ if ( not exists $char_map->{$char} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $char_map->{$char}) 1 24 25
63 1 my $char_ord = ord $char;
64 1 return _err_bad_char( $char, $char_ord );
65 }
66 24 return $char_map->{$char};
67 }
68  
69 ###
70 #
71 # Pair to number transformation.
72 #
73 # _enc_pair( 'x','y' ) -> $number
74 #
75 ##
76  
77 sub _enc_pair {
78 14 ▶ my (@tokens) = @_;
Subroutine _enc_pair: called CC=3 SCAR=11.2
79 14 ▶ if ( not @tokens ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@tokens) 0 14 14
80 0 return q{};
81 }
82 14 ▶ if ( @tokens < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tokens < 2) 4 10 14
83 4 return _code_for( shift @tokens );
84 }
85 10 return ( _code_for( $tokens[0] ) * 36 ) + ( _code_for( $tokens[1] ) );
86 }
87  
88 ###
89 #
90 # String to dotted-decimal conversion
91 #
92 # $dotstring = _ascii_to_int("HELLOWORLD");
93 #
94 ###
95 sub _ascii_to_int {
96 6 ▶ my $string = shift;
Subroutine _ascii_to_int: called CC=2 SCAR=6.9
97 6 my @chars = split //msx, $string;
98 6 my @output;
99  
100 6 ▶ while (@chars) {
Branch: 1 branch
BranchTrueFalseTotal
if (@chars) 14 6 20
101 14 push @output, _enc_pair( splice @chars, 0, 2, () );
102 }
103  
104 6 return join q{.}, @output;
105 }
106  
107 #
108 # Handler for gentooize_version( ... { lax => 0 } )
109 #
110 sub _lax_cleaning_0 {
111 46 ▶ my $version = shift;
Subroutine _lax_cleaning_0: called CC=1 SCAR=0.0
112 46 return _expand_numeric($version);
113 }
114  
115 #
116 # Handler for gentooize_version( ... { lax => 1 } )
117 #
118  
119 sub _lax_cleaning_1 {
120 17 ▶ my $version = shift;
Subroutine _lax_cleaning_1: called CC=5 SCAR=16.1
121 17 my $isdev = 0;
122 17 my $prereleasever = undef;
123  
124 17 ▶ if ( $version =~ s/-TRIAL\z//msx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version =~ s/-TRIAL\z//msx) 5 12 17
125 5 $isdev = 1;
126 }
127 17 ▶ if ( $version =~ s/_(.*)\z/$1/msx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version =~ s/_(.*)\z/$1/msx) 6 11 17
128 6 $prereleasever = "$1";
129 6 $isdev = 1;
130 6 ▶ if ( $prereleasever =~ /_/msx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prereleasever =~ /_/msx) 1 5 6
131 1 return _err_lax_multi_underscore($version);
132 }
133 }
134 16 $version = _expand_numeric($version);
135 16 ▶ if ($isdev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isdev) 10 6 16
136 10 $version .= '_rc';
137 }
138 16 return $version;
139 }
140  
141 #
142 # Handler for gentooize_version( ... { lax => 2 } )
143 #
144  
145 sub _lax_cleaning_2 {
146 6 ▶ my $version = shift;
Subroutine _lax_cleaning_2: called CC=8 SCAR=21.7
147 6 my $istrial = 0;
148  
149 6 my $has_v = 0;
150  
151 6 ▶ if ( $version =~ s/-TRIAL\z//msx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version =~ s/-TRIAL\z//msx) 0 6 6
152 0 $istrial = 1;
153 }
154 6 ▶ if ( $version =~ s/\Av//msx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version =~ s/\Av//msx) 0 6 6
155 0 $has_v = 1;
156 }
157  
158 6 my @parts = split /([._])/msx, $version;
159 6 my @out;
160 6 for (@parts) {
161 30 ▶ if (/\A[_.]\z/msx) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\A[_.]\z/msx) 12 18 30
162 12 push @out, $_;
163 12 next;
164 }
165 18 ▶ if (/\A\d\z/msx) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\A\d\z/msx) 12 6 18
166 12 push @out, $_;
167 12 next;
168 }
169 6 push @out, _ascii_to_int($_);
170 }
171  
172 6 my $version_out = join q{}, @out;
173 6 ▶ if ($istrial) {
Branch: 1 branch
BranchTrueFalseTotal
if ($istrial) 0 6 6
174 0 $version_out .= '-TRIAL';
175 }
176 6 ▶ if ($has_v) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_v) 0 6 6
177 0 $version_out = 'v' . $version_out;
178 }
179 6 return _lax_cleaning_1($version_out);
180 }
181  
182 #
183 # Expands dotted decimal to a float, and then chunks the float.
184 #
185 # my $clean = _expand_numeric( $dirty );
186 #
187 sub _expand_numeric {
188 62 ▶ my $perlver = shift;
Subroutine _expand_numeric: called CC=2 SCAR=6.9
189  
190 62 my $ver = version->parse($perlver)->normal;
191  
192 62 $ver =~ s/\Av//msx; # strip leading v
193  
194 62 my @tokens = split /[.]/msx, $ver;
195 62 my @out;
196  
197 62 for (@tokens) {
198 203 s/\A0+([1-9])/$1/msx; # strip leading 0's
199 203 push @out, $_;
200 }
201  
202 62 return join q{.}, @out;
203 }
204  
205 BEGIN {
206 4 ▶ for my $err (qw( perlver_undefined matches_trial_regex_nonlax not_decimal_or_trial bad_char lax_multi_underscore )) {
Subroutine BEGIN: called CC=3 SCAR=11.0
207 my $code = sub {
208 7 ▶ require Gentoo::PerlMod::Version::Error;
Subroutine __ANON__: called CC=1 SCAR=0.0
209 7 my $sub = Gentoo::PerlMod::Version::Error->can($err);
210 7 goto $sub;
211 20 };
212 ## no critic ( ProhibitNoStrict )
213 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
214 20 *{ __PACKAGE__ . '::_err_' . $err } = $code;
215 }
216 4 for my $env (qw( opts hasopt getopt )) {
217 my $code = sub {
218 67 ▶ require Gentoo::PerlMod::Version::Env;
Subroutine __ANON__: called CC=1 SCAR=0.0
219 67 my $sub = Gentoo::PerlMod::Version::Env->can($env);
220 67 goto $sub;
221 12 };
222 ## no critic ( ProhibitNoStrict )
223  
224 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
225 12 *{ __PACKAGE__ . '::_env_' . $env } = $code;
226 }
227  
228 }
229  
230 4 1;
231  
232 __END__
« blib/script/gentoo-perlmod-version.pl ↑ Summary lib/Gentoo/PerlMod/Version/Error.pm »