blib/lib/CPAN/Changes/Parser.pm

filter: Statementstmt 98.8% 166 / 168 Branchbran 89.7% 70 / 78 Conditioncond 92.1% 35 / 38 MC/DCmcdc 65.0% 13 / 20 Subroutinesub 100% 16 / 16 Podpod 100% 2 / 2 totaltotal 93.7% 302 / 322 CC 54 SCAR 29.6
CC
54
Cov
95%
_parse
32.0
_split_date
16.1
parse_file
4.1
CRAP 19.3
↑ Summary blib/lib/Test/CPAN/Changes.pm »
1 package CPAN::Changes::Parser;
2 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 30 our $VERSION = '0.500002';
6 30 $VERSION =~ tr/_//d;
7  
8 30 ▶use Module::Runtime qw(use_module);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 30 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 30 ▶use Encode qw(decode FB_CROAK LEAVE_SRC);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 30 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 has _changelog_class => (
15 is => 'ro',
16 default => 'CPAN::Changes',
17 40 ▶ coerce => sub { use_module($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
18 30 );
19 has _release_class => (
20 is => 'ro',
21 default => 'CPAN::Changes::Release',
22 40 ▶ coerce => sub { use_module($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 30 );
24 has _entry_class => (
25 is => 'ro',
26 default => 'CPAN::Changes::Entry',
27 40 ▶ coerce => sub { use_module($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 30 );
29 30 has version_like => (
30 is => 'ro',
31 );
32 30 has version_prefix => (
33 is => 'ro',
34 );
35  
36 sub parse_string {
37 37 ▶ my ($self, $string) = @_;
Subroutine parse_string: called CC=1 SCAR=0.0
Pod documented
38 37 $self->_transform($self->_parse($string));
39 }
40  
41 sub parse_file {
42 28 ▶ my ($self, $file, $layers) = @_;
Subroutine parse_file: called CC=4 SCAR=14.1
Pod documented
43 28 ▶ my $mode = defined $layers ? "<$layers" : '<:raw';
Branch: 1 branch
BranchTrueFalseTotal
defined $layers ? : 0 28 28
44 28 ▶ open my $fh, $mode, $file or croak "Can't open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $mode, $file or croak("Can't open ${file}: $!") 0 28 28
45 28 my $content = do { local $/; <$fh> };
46 28 ▶ if (!defined $layers) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $layers) 28 0 28
47 # if it's valid UTF-8, decode that. otherwise, assume latin 1 and leave it.
48 28 eval { $content = decode('UTF-8', $content, FB_CROAK | LEAVE_SRC) };
49 }
50 28 $self->parse_string($content);
51 }
52  
53 sub _transform {
54 37 ▶ my ($self, $data) = @_;
Subroutine _transform: called CC=4 SCAR=13.9
55  
56 37 my $release_class = $self->_release_class;
57 37 my $entry_class = $self->_entry_class;
58  
59 $self->_changelog_class->new(
60 (defined $data->{preamble} ? (preamble => $data->{preamble}) : ()),
61 releases => [
62 map {
63 213 my $r = $_;
64 $release_class->new(
65 1065 ▶ (map { defined $r->{$_} ? ($_ => $r->{$_}) : () }
Branch: 2 branches
BranchTrueFalseTotal
defined $r->{$_} ? : 896 169 1065
$_->{'entries'} ? : 213 0 213
66 qw(version line date raw_date note)),
67 ($_->{entries} ? (
68 entries => [
69 213 map { _trans_entry($entry_class, $_) } @{$_->{entries}},
70 ],
71 ) : () ),
72 )
73 37 ▶ } reverse @{$data->{releases}},
Branch: 1 branch
BranchTrueFalseTotal
defined $data->{'preamble'} ? : 15 22 37
74 ],
75 );
76 }
77  
78 sub _trans_entry {
79 1285 ▶ my ($entry_class, $entry) = @_;
Subroutine _trans_entry: called CC=2 SCAR=6.9
80  
81 $entry_class->new(
82 line => $entry->{line},
83 text => $entry->{text},
84 $entry->{entries} ? (
85 entries => [
86 1285 ▶ map { _trans_entry($entry_class, $_) } @{$entry->{entries}},
Branch: 1 branch
BranchTrueFalseTotal
$entry->{'entries'} ? : 86 1199 1285
87 ],
88 ) : (),
89 );
90 }
91  
92 30 our $VERSION_REGEX = qr{
93 (?:
94 v [0-9]+ (?: (?:\.[0-9]+ )+ (?:_[0-9]+)? )?
95 |
96 (?:[0-9]+)? (?:\.[0-9]+){2,} (?:_[0-9]+)?
97 |
98 [0-9]* \.[0-9]+ (?: _[0-9]+ )?
99 |
100 [0-9]+ (?: _[0-9]+ )?
101 )
102 (?: -TRIAL )?
103 }x;
104  
105 sub _parse {
106 129 ▶ my ($self, $string) = @_;
Subroutine _parse: called CC=32 SCAR=34.7
107  
108 129 my $version_prefix = qr/version|revision/i;
109 129 ▶ if (defined(my $vp = $self->version_prefix)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $vp = $self->version_prefix)) 0 129 129
110 0 $version_prefix = qr/$version_prefix|$vp/
111 }
112 129 my $version_token = qr/$VERSION_REGEX(?:-TRIAL)?/;
113 129 ▶ if (defined(my $vt = $self->version_like)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $vt = $self->version_like)) 101 28 129
114 101 $version_token = qr/$version_token|$vt/
115 }
116  
117 129 my $raw_preamble = '';
118 129 my @releases;
119 my @indents;
120 129 my $line_number = -1;
121 129 ▶ while ($string =~ /((.*?)(?:\r\n?|\n|\z))/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /((.*?)(?:\r\n?|\n|\z))/g) 29704 0 29704
122 29704 my ($full_line, $line) = ($1, $2);
123 last
124 29704 ▶ if !length $full_line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $full_line 129 29575 29704
125 29575 $line_number++;
126  
127 29575 ▶ if ( $line =~ /^(?:$version_prefix\s+)?($version_token)(?:[:;.-]?\s+(.*))?$/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^(?:$version_prefix\s+)?($version_token)(?:[:;.-]?\s+(.*))?$/i) { } 2573 27002 29575
elsif (not @indents) { } 7561 19441 27002
128 2573 my $version = $1;
129 2573 my $note = $2;
130 2573 my $date;
131 my $raw_date;
132 2573 ▶ if (defined $note) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $note) 2394 179 2573
133 2394 ($date, $raw_date, $note) = _split_date($note);
134 }
135  
136 2573 ▶ my $release = {
Branch: 3 branches
BranchTrueFalseTotal
defined $date ? : 1405 1168 2573
defined $raw_date ? : 1405 1168 2573
defined $note ? : 1292 1281 2573
137 version => $version,
138 (defined $date ? (date => $date) : ()),
139 (defined $raw_date ? (raw_date => $raw_date) : ()),
140 (defined $note ? (note => $note) : ()),
141 raw => $full_line,
142 entries => [],
143 line => $line_number+1,
144 };
145 2573 push @releases, $release;
146 2573 @indents = ($release);
147 2573 next;
148 }
149 elsif (!@indents) {
150 7561 $raw_preamble .= $full_line,
151 next;
152 }
153  
154 19441 ▶ if ( $line =~ /^[-_*+~#=\s]*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^[-_*+~#=\s]*$/) 3461 15980 19441
155 3461 ▶ $indents[-1]{done}++
Branch: 1 branch
BranchTrueFalseTotal
if @indents > 1 3072 389 3461
156 if @indents > 1;
157  
158 3461 ▶ if (@indents) {
Branch: 1 branch
BranchTrueFalseTotal
if (@indents) { } 3461 0 3461
159 3461 $indents[-1]{raw} .= $full_line;
160 }
161 else {
162 0 $releases[-1]{raw} .= $full_line;
163 }
164 3461 next;
165 }
166  
167 15980 $line =~ s/\s+$//;
168 15980 $line =~ s/^(\s*)//;
169 15980 my $indent = 1 + length _expand_tab($1);
170 15980 my $change;
171 my $done;
172 15980 my $nest;
173 15980 my $style = '';
174 15980 ▶ if ( $line =~ /^\[\s*([^\[\]]*)\]$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\[\s*([^\[\]]*)\]$/) { } 115 15865 15980
elsif ($line =~ /^([-*+=#]+)\s+(.*)/) { } 9102 6763 15865
175 115 $done = 1;
176 115 $nest = 1;
177 115 $change = $1;
178 115 $style = '[]';
179 115 $change =~ s/\s+$//;
180 }
181 elsif ( $line =~ /^([-*+=#]+)\s+(.*)/ ) {
182 9102 $style = $1;
183 9102 $change = $2;
184 }
185 else {
186 6763 $change = $line;
187 6763 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $indents[-1]{"text"} and not $indents[-1]{"done"} and $indent > $#indents || $indent == $#indents && (length $indents[-1]{"style"} || $indent == 1)) 5613 1150 6763
Condition100%15/15
defined $indents[-1]{"text"} and not $indents[-1]{"done"} and $indent > $#indents || $indent == $#indents && (length $indents[-1]{"style"} || $indent == 1)
!ll&&!rl&&r
7685857138
defined $indents[-1]{"text"} and not $indents[-1]{"done"}
!ll&&!rl&&r
2545145995
$indent > $#indents or $indent == $#indents and length $indents[-1]{"style"} || $indent == 1
l!l&&r!l&&!r
43751061514
$indent == $#indents and length $indents[-1]{"style"} || $indent == 1
!ll&&!rl&&r
421472106
length $indents[-1]{"style"} or $indent == 1
l!l&&r!l&&!r
1132106340
MC/DC50%3/6
defined $indents[-1]{"text"} and not $indents[-1]{"done"} and $indent > $#indents || $indent == $#indents && (length $indents[-1]{"style"} || $indent == 1)
defined $indents[-1]{"text"} not $indents[-1]{"done"} $indent > $#indents $indent == $#indents length $indents[-1]{"style"} $indent == 1
Truth table57%4/7
A and not(B) and C or D and E or F
A= defined $indents[-1]{"text"}B= not $indents[-1]{"done"}C= $indent > $#indentsD= $indent == $#indentsE= length $indents[-1]{"style"}F= $indent == 1
ABCDEFresult
0XXXXX0
10XXXX0
1100XX0
1101000
111XXX1
11011X1
1101011
188 defined $indents[-1]{text}
189 && !$indents[-1]{done}
190 && (
191 $indent > $#indents
192 || (
193 $indent == $#indents
194 && (
195 length $indents[-1]{style}
196 || $indent == 1
197 )
198 )
199 )
200 ) {
201 5613 $indents[-1]{raw} .= $full_line;
202 5613 $indents[-1]{text} .= " $change";
203 5613 next;
204 }
205 }
206  
207 10367 my $group;
208 my $nested;
209  
210 10367 ▶ if ( !$nest && $indents[$indent]{nested} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $nest and $indents[$indent]{'nested'}) { } 113 10254 10367
elsif (not $nest and $indents[$indent]{'nest'}) { } 102 10152 10254
Condition100%3/3
not $nest and $indents[$indent]{"nested"}
!ll&&!rl&&r
11510139113
MC/DC100%2/2
not $nest and $indents[$indent]{"nested"}
not $nest $indents[$indent]{"nested"}
Truth table100%3/3
not(A) and B
A= not $nestB= $indents[$indent]{"nested"}
ABresult
0X0
100
111
211 113 $nested = $group = $indents[$indent]{nested};
212 }
213 ▶ elsif ( !$nest && $indents[$indent]{nest} ) {
Condition100%3/3
not $nest and $indents[$indent]{"nest"}
!ll&&!rl&&r
11510037102
MC/DC100%2/2
not $nest and $indents[$indent]{"nest"}
not $nest $indents[$indent]{"nest"}
Truth table100%3/3
not(A) and B
A= not $nestB= $indents[$indent]{"nest"}
ABresult
0X0
100
111
214 102 $nested = $group = $indents[$indent];
215 }
216 else {
217 10152 ($group) = grep {defined} reverse @indents[ 0 .. $indent - 1 ];
218 }
219  
220 10367 my $entry = {
221 text => $change,
222 line => $line_number+1,
223 done => $done,
224 nest => $nest,
225 nested => $nested,
226 style => $style,
227 raw => $full_line,
228 };
229 10367 ▶ push @{ $group->{entries} ||= [] }, $entry;
Condition100%2/2
$group->{"entries"} ||= []
l!l
9691676
MC/DC100%1/1
$group->{"entries"} ||= []
$group->{"entries"}
Truth table100%2/2
A
A= $group->{"entries"}
Aresult
11
00
230  
231 10367 ▶ if ( $indent <= $#indents ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indent <= $#indents) 10303 64 10367
232 10303 $#indents = $indent;
233 }
234  
235 10367 $indents[$indent] = $entry;
236 }
237 129 my $preamble;
238 129 ▶ if (length $raw_preamble) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $raw_preamble) 87 42 129
239 87 $preamble = $raw_preamble;
240 87 $preamble =~ s/\A\s*\n//;
241 87 $preamble =~ s/\s+\z//;
242 87 $preamble =~ s/\r\n?/\n/g;
243 }
244  
245 129 my @entries = @releases;
246 129 ▶ while ( my $entry = shift @entries ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = shift @entries) 12940 129 13069
247 12940 ▶ push @entries, @{ $entry->{entries} } if $entry->{entries};
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"entries"} 3249 9691 12940
248 12940 delete @{$entry}{qw(done nest nested)};
249 }
250 return {
251 129 ▶ ( defined $preamble ? (preamble => $preamble) : () ),
Branch: 1 branch
BranchTrueFalseTotal
defined $preamble ? : 87 42 129
252 raw_preamble => $raw_preamble,
253 releases => \@releases,
254 };
255 }
256  
257 30 my @months = qw(
258 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
259 );
260 30 my %months = map {; lc $months[$_] => $_ } 0 .. $#months;
261 30 our ($_SHORT_MONTH) = map qr{$_}i, join '|', map quotemeta, @months;
262 30 our ($_SHORT_DAY) = map qr{$_}i, join '|', map quotemeta, qw(
263 Sun Mon Tue Wed Thu Fri Sat
264 );
265 30 our ($_UNKNOWN_DATE) = map qr{$_}i, join '|', map quotemeta, (
266 'Unknown Release Date',
267 'Unknown',
268 'Not Released',
269 'Development Release',
270 'Development',
271 'Developer Release',
272 );
273  
274 30 our $_LOCALTIME_DATE = qr{
275 (?:
276 (?:$_SHORT_DAY\s+)?
277 ($_SHORT_MONTH)\s+
278 |
279 ($_SHORT_MONTH)\s+
280 (?:$_SHORT_DAY\s+)
281 )
282 (\d{1,2})\s+ # date
283 (?: ([\d:]+)\s+ )? # time
284 (?: ([A-Z]+)\s+ )? # timezone
285 (\d{4}) # year
286 }x;
287  
288 30 our $_RFC_2822_DATE = qr{
289 $_SHORT_DAY,\s+
290 (\d{1,2})\s+
291 ($_SHORT_MONTH)\s+
292 (\d{4})\s+
293 (\d\d:\d\d:\d\d)\s+
294 ([+-])(\d{2})(\d{2})
295 }x;
296  
297 30 our $_DZIL_DATE = qr{
298 (\d{4}-\d\d-\d\d)\s+
299 (\d\d:\d\d(?::\d\d)?)(\s+[A-Za-z]+/[A-Za-z_-]+)
300 }x;
301  
302 30 our $_ISO_8601_DATE = qr{
303 \d\d\d\d # Year
304 (?:
305 [-/]\d\d # -Month
306 (?:
307 [-/]\d\d # -Day
308 (?:
309 [T\s]
310 \d\d:\d\d # Hour:Minute
311 (?:
312 :\d\d # :Second
313 (?: \.\d+ )? # .Fractional_Second
314 )?
315 (?:
316 Z # UTC
317 |
318 [+-]\d\d:\d\d # Hour:Minute TZ offset
319 (?: :\d\d )? # :Second TZ offset
320 )?
321 )?
322 )?
323 )?
324 }x;
325  
326 sub _split_date {
327 2394 ▶ my $note = shift;
Subroutine _split_date: called CC=16 SCAR=27.8
328 2394 my $date;
329 my $parsed_date;
330 # munge date formats, save the remainder as note
331 2394 ▶ if (defined $note && length $note) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $note and length $note) 2394 0 2394
Condition33%1/3
defined $note and length $note
!ll&&!rl&&r
002394
MC/DC0%0/2
defined $note and length $note
defined $note length $note
Truth table33%1/3
A and B
A= defined $noteB= length $note
ABresult
0X0
100
111
332 2394 $note =~ s/^[^\w\s]*\s+//;
333 2394 $note =~ s/\s+$//;
334  
335 # explicitly unknown dates
336 2394 ▶ if ( $note =~ s{^($_UNKNOWN_DATE)}{} ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($note =~ s/^($CPAN::Changes::Parser::_UNKNOWN_DATE)//) { } 16 2378 2394
elsif ($note =~ s/^($CPAN::Changes::Parser::_LOCALTIME_DATE)//) { } 508 1870 2378
elsif ($note =~ s/^($CPAN::Changes::Parser::_RFC_2822_DATE)//) { } 2 1868 1870
elsif ($note =~ s/^($CPAN::Changes::Parser::_DZIL_DATE)//) { } 7 1861 1868
elsif ($note =~ s/^($CPAN::Changes::Parser::_ISO_8601_DATE)//) { } 872 989 1861
337 16 $parsed_date = $date = $1;
338 }
339  
340 # handle localtime-like timestamps
341 elsif ( $note =~ s{^($_LOCALTIME_DATE)}{} ) {
342 508 $date = $1;
343 508 ▶ $parsed_date = sprintf( '%d-%02d-%02d', $7, 1+$months{lc($2 || $3)}, $4 );
Condition66%2/3
$2 || $3
l!l&&r!l&&!r
50710
MC/DC0%0/2
$2 || $3
$2 $3
Truth table66%2/3
A || B
A= $2B= $3
ABresult
1X1
011
000
344 508 ▶ if ($5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($5) 501 7 508
345 # unfortunately ignores TZ data ($6)
346 501 $parsed_date .= sprintf( 'T%sZ', $5 );
347 }
348 }
349  
350 # RFC 2822
351 elsif ( $note =~ s{^($_RFC_2822_DATE)}{} ) {
352 2 $date = $1;
353 $parsed_date = sprintf( '%d-%02d-%02dT%s%s%02d:%02d',
354 2 $4, 1+$months{lc $3}, $2, $5, $6, $7, $8 );
355 }
356  
357 # handle dist-zilla style, again ingoring TZ data
358 elsif ( $note =~ s{^($_DZIL_DATE)}{} ) {
359 7 $date = $1;
360 7 $parsed_date = sprintf( '%sT%sZ', $2, $3 );
361 7 $note = $4 . $note;
362 }
363  
364 # start with W3CDTF, ignore rest
365 elsif ( $note =~ s{^($_ISO_8601_DATE)}{} ) {
366 872 $parsed_date = $date = $1;
367 872 $parsed_date =~ s{ }{T};
368 872 $parsed_date =~ s{/}{-}g;
369  
370 # Add UTC TZ if date ends at H:M, H:M:S or H:M:S.FS
371 872 ▶ $parsed_date .= 'Z'
Branch: 1 branch
BranchTrueFalseTotal
if length $parsed_date == 16 or length $parsed_date == 19 or $parsed_date =~ /\.\d+$/ 265 536 801
Condition100%6/6
length $parsed_date == 16 or length $parsed_date == 19 or $parsed_date =~ /\.\d+$/
l!l&&r!l&&!r
3351536
length $parsed_date == 16 or length $parsed_date == 19
l!l&&r!l&&!r
71264537
MC/DC100%3/3
length $parsed_date == 16 or length $parsed_date == 19 or $parsed_date =~ /\.\d+$/
length $parsed_date == 16 length $parsed_date == 19 $parsed_date =~ /\.\d+$/
Truth table100%4/4
A or B or C
A= length $parsed_date == 16B= length $parsed_date == 19C= $parsed_date =~ /\.\d+$/
ABCresult
1XX1
01X1
0011
0000
372 if length($parsed_date) == 16
373 || length($parsed_date) == 19
374 || $parsed_date =~ m{\.\d+$};
375 }
376  
377 2394 $note =~ s/^\s+//;
378 }
379  
380 2394 ▶ defined $_ && !length $_ && undef $_ for ($parsed_date, $date, $note);
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and not length $_ and undef $_ 1102 6080 7182
Condition100%3/3
defined $_ and not length $_
!ll&&!rl&&r
197841021102
MC/DC100%2/2
defined $_ and not length $_
defined $_ not length $_
Truth table100%3/3
A and not(B)
A= defined $_B= not length $_
ABresult
0X0
100
111
381  
382 2394 return ($parsed_date, $date, $note);
383 }
384  
385 sub _expand_tab {
386 15980 ▶ my $string = "$_[0]";
Subroutine _expand_tab: called CC=1 SCAR=0.0
387 15980 $string =~ s/([^\t]*)\t/$1 . (" " x (8 - (length $1) % 8))/eg;
388 15980 return $string;
389 }
390  
391 30 1;
392 __END__
↑ Summary blib/lib/Test/CPAN/Changes.pm »