Natural/Duration/Checks.pm

filter: Statementstmt 99.4% 174 / 175 Branchbran 93.1% 41 / 44 Conditioncond 76.9% 30 / 39 MC/DCmcdc 63.6% 14 / 22 Subroutinesub 100% 19 / 19 Podpod n/a totaltotal 92.9% 278 / 299 CC 43 SCAR 18.0
CC
43
Cov
95%
__ANON__
11.0
_from_count_to_count_extract
9.0
_from_count_to_count
7.0
CRAP 6.1
« Natural/Formatted.pm ↑ Summary Natural/Rewrite.pm »
1 package DateTime::Format::Natural::Duration::Checks;
2  
3 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 27 ▶use boolean qw(true false);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 27 our $VERSION = '0.07';
8  
9 sub _for
10 1258 ▶{
Subroutine _for: called CC=3 SCAR=11.0
11 1258 my ($duration, $date_strings, $present) = @_;
12  
13 1258 ▶ if (@$date_strings == 1
Branch: 1 branch
BranchTrueFalseTotal
if (@$date_strings == 1 and $date_strings->[0] =~ /$duration->{'for'}{'regex'}/) { } 55 1203 1258
Condition100%3/3
@$date_strings == 1 and $date_strings->[0] =~ /$duration->{"for"}{"regex"}/
!ll&&!rl&&r
103916455
MC/DC100%2/2
@$date_strings == 1 and $date_strings->[0] =~ /$duration->{"for"}{"regex"}/
@$date_strings == 1 $date_strings->[0] =~ /$duration->{"for"}{"regex"}/
Truth table100%3/3
A and B
A= @$date_strings == 1B= $date_strings->[0] =~ /$duration->{"for"}{"regex"}/
ABresult
0X0
100
111
14 && $date_strings->[0] =~ $duration->{for}{regex}
15 ) {
16 55 $$present = $duration->{for}{present};
17 55 return true;
18 }
19 else {
20 1203 return false;
21 }
22 }
23  
24 sub _first_to_last
25 1203 ▶{
Subroutine _first_to_last: called CC=4 SCAR=13.9
26 1203 my ($duration, $date_strings, $extract) = @_;
27  
28 1203 my %regexes = %{$duration->{first_to_last}{regexes}};
29  
30 1203 ▶ if (@$date_strings == 2
Branch: 1 branch
BranchTrueFalseTotal
if (@$date_strings == 2 and $date_strings->[0] =~ /^$regexes{'first'}$/ and $date_strings->[1] =~ /^$regexes{'last'}$/) { } 27 1176 1203
Condition83%5/6
@$date_strings == 2 and $date_strings->[0] =~ /^$regexes{"first"}$/ and $date_strings->[1] =~ /^$regexes{"last"}$/
!ll&&!rl&&r
1176027
@$date_strings == 2 and $date_strings->[0] =~ /^$regexes{"first"}$/
!ll&&!rl&&r
164101227
MC/DC66%2/3
@$date_strings == 2 and $date_strings->[0] =~ /^$regexes{"first"}$/ and $date_strings->[1] =~ /^$regexes{"last"}$/
@$date_strings == 2 $date_strings->[0] =~ /^$regexes{"first"}$/ $date_strings->[1] =~ /^$regexes{"last"}$/
Truth table75%3/4
A and B and C
A= @$date_strings == 2B= $date_strings->[0] =~ /^$regexes{"first"}$/C= $date_strings->[1] =~ /^$regexes{"last"}$/
ABCresult
0XX0
10X0
1100
1111
31 && $date_strings->[0] =~ /^$regexes{first}$/
32 && $date_strings->[1] =~ /^$regexes{last}$/
33 ) {
34 27 $$extract = $regexes{extract};
35 27 return true;
36 }
37 else {
38 1176 return false;
39 }
40 }
41  
42 my %anchor_regex = (
43 199 ▶ left => sub { my $regex = shift; qr/(?:^|(?<=\s))$regex/ },
Subroutine __ANON__: called CC=1 SCAR=0.0
44 199 ▶ right => sub { my $regex = shift; qr/$regex(?:(?=\s)|$)/ },
Subroutine __ANON__: called CC=1 SCAR=0.0
45 5770 ▶ both => sub { my $regex = shift; qr/(?:^|(?<=\s))$regex(?:(?=\s)|$)/ },
Subroutine __ANON__: called CC=1 SCAR=0.0
46 27 );
47  
48 my $extract_chunk = sub
49 148 ▶{
Subroutine __ANON__: called CC=4 SCAR=13.9
50 148 my ($string, $base_index, $start_pos, $match) = @_;
51  
52 148 my $start_index = 0;
53  
54 148 ▶ if ($start_pos > 0
Branch: 1 branch
BranchTrueFalseTotal
if ($start_pos > 0 and $string =~ /^(.{0,$start_pos})\s+/) 17 131 148
Condition66%2/3
$start_pos > 0 and $string =~ /^(.{0,$start_pos})\s+/
!ll&&!rl&&r
131017
MC/DC50%1/2
$start_pos > 0 and $string =~ /^(.{0,$start_pos})\s+/
$start_pos > 0 $string =~ /^(.{0,$start_pos})\s+/
Truth table66%2/3
A and B
A= $start_pos > 0B= $string =~ /^(.{0,$start_pos})\s+/
ABresult
0X0
100
111
55 && $string =~ /^(.{0,$start_pos})\s+/
56 ) {
57 17 my $substring = $1;
58 17 ▶ $start_index++ while $substring =~ /\s+/g;
Branch: 1 branch
BranchTrueFalseTotal
$substring =~ /\s+/g and do { ++$start_index } 5 17 22
59 17 $start_index++; # final space
60 }
61 148 my @tokens = split /\s+/, $match;
62 148 my $end_index = $start_index + $#tokens;
63  
64 148 my $expression = join ' ', @tokens;
65  
66 148 return [ [ $base_index + $start_index, $base_index + $end_index ], $expression ];
67 27 };
68  
69 my $has_timespan_sep = sub
70 63 ▶{
Subroutine __ANON__: called CC=4 SCAR=17.2
71 63 my ($tokens, $chunks, $timespan_sep) = @_;
72  
73 63 my ($left_index, $right_index) = ($chunks->[0]->[0][1], $chunks->[1]->[0][0]);
74  
75 63 ▶ if ($tokens->[$left_index + 1] =~ /^$timespan_sep$/i
Branch: 1 branch
BranchTrueFalseTotal
if ($$tokens[$left_index + 1] =~ /^$timespan_sep$/i and $$tokens[$right_index - 1] =~ /^$timespan_sep$/i and $right_index - $left_index == 2) { } 63 0 63
Condition33%2/6
$$tokens[$left_index + 1] =~ /^$timespan_sep$/i and $$tokens[$right_index - 1] =~ /^$timespan_sep$/i and $right_index - $left_index == 2
!ll&&!rl&&r
0063
$$tokens[$left_index + 1] =~ /^$timespan_sep$/i and $$tokens[$right_index - 1] =~ /^$timespan_sep$/i
!ll&&!rl&&r
0063
MC/DC0%0/3
$$tokens[$left_index + 1] =~ /^$timespan_sep$/i and $$tokens[$right_index - 1] =~ /^$timespan_sep$/i and $right_index - $left_index == 2
$$tokens[$left_index + 1] =~ /^$timespan_sep$/i $$tokens[$right_index - 1] =~ /^$timespan_sep$/i $right_index - $left_index == 2
Truth table25%1/4
A and B and C
A= $$tokens[$left_index + 1] =~ /^$timespan_sep$/iB= $$tokens[$right_index - 1] =~ /^$timespan_sep$/iC= $right_index - $left_index == 2
ABCresult
0XX0
10X0
1100
1111
76 && $tokens->[$right_index - 1] =~ /^$timespan_sep$/i
77 && $right_index - $left_index == 2
78 ) {
79 63 return true;
80 }
81 else {
82 0 return false;
83 }
84 27 };
85  
86 # invoked from Extract.pm
87 sub _first_to_last_extract
88 111 ▶{
Subroutine _first_to_last_extract: called CC=6 SCAR=17.9
89 111 my $self = shift;
90 111 my ($date_strings, $indexes, $tokens, $chunks) = @_;
91  
92 111 ▶ return false unless @$date_strings == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @$date_strings == 2 0 111 111
93  
94 111 my $duration = $self->{data}->{duration};
95  
96 111 my %regexes = %{$duration->{first_to_last}{regexes}};
97  
98 111 $regexes{first} = $anchor_regex{left}->($regexes{first});
99 111 $regexes{last} = $anchor_regex{right}->($regexes{last});
100  
101 111 my $timespan_sep = $self->{data}->__timespan('literal');
102  
103 111 my @chunks;
104 111 ▶ if ($date_strings->[0] =~ /(?=($regexes{first})$)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date_strings->[0] =~ /(?=($regexes{"first"})$)/g) 17 94 111
105 17 my $match = $1;
106 17 push @chunks, $extract_chunk->($date_strings->[0], $indexes->[0][0], pos $date_strings->[0], $match);
107 }
108 111 ▶ if ($date_strings->[1] =~ /(?=^($regexes{last}))/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date_strings->[1] =~ /(?=^($regexes{"last"}))/g) 17 94 111
109 17 my $match = $1;
110 17 push @chunks, $extract_chunk->($date_strings->[1], $indexes->[1][0], pos $date_strings->[1], $match);
111 }
112 111 ▶ if (@chunks == 2 && $has_timespan_sep->($tokens, \@chunks, $timespan_sep)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@chunks == 2 and &$has_timespan_sep($tokens, \@chunks, $timespan_sep)) { } 16 95 111
Condition66%2/3
@chunks == 2 and &$has_timespan_sep($tokens, \@chunks, $timespan_sep)
!ll&&!rl&&r
95016
MC/DC50%1/2
@chunks == 2 and &$has_timespan_sep($tokens, \@chunks, $timespan_sep)
@chunks == 2 &$has_timespan_sep($tokens, \@chunks, $timespan_sep)
Truth table66%2/3
A and B
A= @chunks == 2B= &$has_timespan_sep($tokens, \@chunks, $timespan_sep)
ABresult
0X0
100
111
113 16 @$chunks = @chunks;
114 16 return true;
115 }
116 else {
117 95 return false;
118 }
119 }
120  
121 my $duration_matches = sub
122 1107 ▶{
Subroutine __ANON__: called CC=11 SCAR=24.0
123 1107 my ($duration, $date_strings, $entry, $target) = @_;
124  
125 1107 my $data = $duration->{from_count_to_count};
126  
127 1107 my (@matches, %seen);
128 1107 foreach my $ident (@{$data->{order}}) {
129 4428 my $regex = $anchor_regex{both}->($data->{regexes}{$ident});
130 4428 ▶ while ($date_strings->[0] =~ /(?=$regex)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date_strings->[0] =~ /(?=$regex)/g) 2013 4428 6441
131 2013 my $pos = pos $date_strings->[0];
132 2013 ▶ next if $seen{$pos};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$pos} 276 1737 2013
133 1737 push @matches, [ $ident, $pos ];
134 1737 $seen{$pos} = true;
135 }
136 }
137 1107 my @idents = map $_->[0], sort { $a->[1] <=> $b->[1] } @matches;
138  
139 1107 my %categories;
140 1107 foreach my $ident (@{$data->{order}}) {
141 4428 my $category = $data->{categories}{$ident};
142 4428 push @{$categories{$category}}, $ident;
143 }
144  
145 my $get_target = sub
146 997 ▶ {
Subroutine __ANON__: called CC=3 SCAR=11.0
147 997 my ($category, $target) = @_;
148 997 foreach my $ident (@{$categories{$category}}) {
149 1342 my $regex = $anchor_regex{both}->($data->{regexes}{$ident});
150 1342 ▶ if ($date_strings->[1] =~ $regex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date_strings->[1] =~ /$regex/) 968 374 1342
151 968 $$target = $ident;
152 968 return true;
153 }
154 }
155 29 return false;
156 1107 };
157  
158 1107 ▶ if (@idents >= 2
Branch: 2 branches
BranchTrueFalseTotal
if (@idents >= 2 and $data->{'categories'}{$idents[-1]} eq 'day' and $data->{'categories'}{$idents[-2]} eq 'time' and &$get_target($data->{'categories'}{$idents[-2]}, $target)) { } 246 861 1107
elsif (@idents and &$get_target($data->{'categories'}{$idents[-1]}, $target)) { } 722 139 861
Condition88%8/9
@idents >= 2 and $data->{"categories"}{$idents[-1]} eq "day" and $data->{"categories"}{$idents[-2]} eq "time" and &$get_target($data->{"categories"}{$idents[-2]}, $target)
!ll&&!rl&&r
8601246
@idents >= 2 and $data->{"categories"}{$idents[-1]} eq "day" and $data->{"categories"}{$idents[-2]} eq "time"
!ll&&!rl&&r
8600247
@idents >= 2 and $data->{"categories"}{$idents[-1]} eq "day"
!ll&&!rl&&r
366494247
MC/DC75%3/4
@idents >= 2 and $data->{"categories"}{$idents[-1]} eq "day" and $data->{"categories"}{$idents[-2]} eq "time" and &$get_target($data->{"categories"}{$idents[-2]}, $target)
@idents >= 2 $data->{"categories"}{$idents[-1]} eq "day" $data->{"categories"}{$idents[-2]} eq "time" &$get_target($data->{"categories"}{$idents[-2]}, $target)
Truth table80%4/5
A and B and C and D
A= @idents >= 2B= $data->{"categories"}{$idents[-1]} eq "day"C= $data->{"categories"}{$idents[-2]} eq "time"D= &$get_target($data->{"categories"}{$idents[-2]}, $target)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
159 && $data->{categories}{$idents[-1]} eq 'day'
160 && $data->{categories}{$idents[-2]} eq 'time'
161 && $get_target->($data->{categories}{$idents[-2]}, $target)
162 ) {
163 246 $$entry = $idents[-2];
164 246 return true;
165 }
166 ▶ elsif (@idents
Condition100%3/3
@idents and &$get_target($data->{"categories"}{$idents[-1]}, $target)
!ll&&!rl&&r
11128722
MC/DC100%2/2
@idents and &$get_target($data->{"categories"}{$idents[-1]}, $target)
@idents &$get_target($data->{"categories"}{$idents[-1]}, $target)
Truth table100%3/3
A and B
A= @identsB= &$get_target($data->{"categories"}{$idents[-1]}, $target)
ABresult
0X0
100
111
167 && $get_target->($data->{categories}{$idents[-1]}, $target)
168 ) {
169 722 $$entry = $idents[-1];
170 722 return true;
171 }
172 else {
173 139 return false;
174 }
175 27 };
176  
177 sub _from_count_to_count
178 1176 ▶{
Subroutine _from_count_to_count: called CC=7 SCAR=19.5
179 1176 my ($duration, $date_strings, $extract, $adjust, $indexes) = @_;
180  
181 1176 ▶ return false unless @$date_strings == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @$date_strings == 2 164 1012 1176
182  
183 1012 my ($entry, $target);
184 1012 ▶ return false unless $duration_matches->($duration, $date_strings, \$entry, \$target);
Branch: 1 branch
BranchTrueFalseTotal
unless &$duration_matches($duration, $date_strings, \$entry, \$target) 104 908 1012
185  
186 908 my $data = $duration->{from_count_to_count};
187  
188 my $get_data = sub
189 1307 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
190 1307 my ($types, $idents, $type) = @_;
191  
192 1307 my $regex = $data->{regexes}{$idents->[0]};
193 1307 my %regexes = (
194 left => qr/^.+? \s+ $regex$/x,
195 right => qr/^$regex \s+ .+$/x,
196 target => qr/^$data->{regexes}{$idents->[1]}$/,
197 );
198 1307 my %extract = (
199 left => qr/^(.+?) \s+ $regex$/x,
200 right => qr/^$regex \s+ (.+)$/x,
201 );
202 my %adjust = (
203 left => sub
204 617 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
205 617 my ($date_strings, $index, $complete) = @_;
206 617 $date_strings->[$index] = "$complete $date_strings->[$index]";
207 },
208 right => sub
209 282 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
210 282 my ($date_strings, $index, $complete) = @_;
211 282 $date_strings->[$index] .= " $complete";
212 },
213 1307 );
214  
215 1307 return (@regexes{@$types}, $extract{$type}, $adjust{$type});
216 908 };
217  
218 908 my @sets = (
219 [ [ qw( left target) ], [ $entry, $target ], 'left', [0,1] ],
220 [ [ qw(right target) ], [ $entry, $target ], 'right', [0,1] ],
221 );
222  
223 908 my @new;
224 908 foreach my $set (@sets) {
225 1816 push @new, [ [ reverse @{$set->[0]} ], [ reverse @{$set->[1]} ], $set->[2], [ reverse @{$set->[3]} ] ];
226 }
227 908 push @sets, @new;
228  
229 908 foreach my $set (@sets) {
230 1307 my ($regex_types, $idents, $type, $string_indexes) = @$set;
231  
232 1307 my ($regex_from, $regex_to, $extract_regex, $adjust_code) = $get_data->($regex_types, $idents, $type);
233  
234 1307 ▶ if ($date_strings->[0] =~ $regex_from
Branch: 1 branch
BranchTrueFalseTotal
if ($date_strings->[0] =~ /$regex_from/ and $date_strings->[1] =~ /$regex_to/) 899 408 1307
Condition100%3/3
$date_strings->[0] =~ /$regex_from/ and $date_strings->[1] =~ /$regex_to/
!ll&&!rl&&r
37236899
MC/DC100%2/2
$date_strings->[0] =~ /$regex_from/ and $date_strings->[1] =~ /$regex_to/
$date_strings->[0] =~ /$regex_from/ $date_strings->[1] =~ /$regex_to/
Truth table100%3/3
A and B
A= $date_strings->[0] =~ /$regex_from/B= $date_strings->[1] =~ /$regex_to/
ABresult
0X0
100
111
235 && $date_strings->[1] =~ $regex_to
236 ) {
237 899 $$extract = $extract_regex;
238 899 $$adjust = $adjust_code;
239 899 @$indexes = @$string_indexes;
240 899 return true;
241 }
242 }
243  
244 9 return false;
245 }
246  
247 # invoked from Extract.pm
248 sub _from_count_to_count_extract
249 95 ▶{
Subroutine _from_count_to_count_extract: called CC=9 SCAR=22.0
250 95 my $self = shift;
251 95 my ($date_strings, $indexes, $tokens, $chunks) = @_;
252  
253 95 ▶ return false unless @$date_strings == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @$date_strings == 2 0 95 95
254  
255 95 my $duration = $self->{data}->{duration};
256  
257 95 my ($entry, $target);
258 95 ▶ return false unless $duration_matches->($duration, $date_strings, \$entry, \$target);
Branch: 1 branch
BranchTrueFalseTotal
unless &$duration_matches($duration, $date_strings, \$entry, \$target) 35 60 95
259  
260 60 my $data = $duration->{from_count_to_count};
261  
262 my $get_data = sub
263 88 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
264 88 my ($types, $idents) = @_;
265  
266 88 my $category = $data->{categories}{$idents->[0]};
267 88 my $regex = $data->{regexes}{$idents->[0]};
268  
269 my %regexes = (
270 left => qr/$data->{extract}{left}{$category}\s+$regex/,
271 right => qr/$regex\s+$data->{extract}{right}{$category}/,
272 88 target => $data->{regexes}{$idents->[1]},
273 );
274  
275 88 $regexes{entry} = qr/(?:$regexes{left}|$regexes{right})/;
276  
277 88 return @regexes{@$types};
278 60 };
279  
280 60 my $timespan_sep = $self->{data}->__timespan('literal');
281  
282 60 my @sets = (
283 [ [ qw(entry target) ], [ $entry, $target ] ],
284 );
285  
286 60 my @new;
287 60 foreach my $set (@sets) {
288 60 push @new, [ [ reverse @{$set->[0]} ], [ reverse @{$set->[1]} ] ];
289 }
290 60 push @sets, @new;
291  
292 60 foreach my $set (@sets) {
293 88 my ($regex_types, $idents) = @$set;
294  
295 88 my ($regex_from, $regex_to) = $get_data->($regex_types, $idents);
296  
297 88 $regex_from = $anchor_regex{left}->($regex_from);
298 88 $regex_to = $anchor_regex{right}->($regex_to);
299  
300 88 my @chunks;
301 88 ▶ if ($date_strings->[0] =~ /(?=($regex_from)$)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date_strings->[0] =~ /(?=($regex_from)$)/g) 57 31 88
302 57 my $match = $1;
303 57 push @chunks, $extract_chunk->($date_strings->[0], $indexes->[0][0], pos $date_strings->[0], $match);
304 }
305 88 ▶ if ($date_strings->[1] =~ /(?=^($regex_to))/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date_strings->[1] =~ /(?=^($regex_to))/g) 57 31 88
306 57 my $match = $1;
307 57 push @chunks, $extract_chunk->($date_strings->[1], $indexes->[1][0], pos $date_strings->[1], $match);
308 }
309 88 ▶ if (@chunks == 2 && $has_timespan_sep->($tokens, \@chunks, $timespan_sep)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@chunks == 2 and &$has_timespan_sep($tokens, \@chunks, $timespan_sep)) 47 41 88
Condition66%2/3
@chunks == 2 and &$has_timespan_sep($tokens, \@chunks, $timespan_sep)
!ll&&!rl&&r
41047
MC/DC50%1/2
@chunks == 2 and &$has_timespan_sep($tokens, \@chunks, $timespan_sep)
@chunks == 2 &$has_timespan_sep($tokens, \@chunks, $timespan_sep)
Truth table66%2/3
A and B
A= @chunks == 2B= &$has_timespan_sep($tokens, \@chunks, $timespan_sep)
ABresult
0X0
100
111
310 47 @$chunks = @chunks;
311 47 return true;
312 }
313  
314 41 pos $date_strings->[0] = 0;
315 41 pos $date_strings->[1] = 0;
316 }
317  
318 13 return false;
319 }
320  
321 27 1;
322 __END__
« Natural/Formatted.pm ↑ Summary Natural/Rewrite.pm »