Natural/Formatted.pm

filter: Statementstmt 92.8% 117 / 126 Branchbran 88.2% 30 / 34 Conditioncond 60.6% 20 / 33 MC/DCmcdc 31.5% 6 / 19 Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 82.5% 184 / 223 CC 30 SCAR 23.8
CC
30
Cov
87%
_parse_formatted_ymd
20.4
_parse_formatted_md
5.1
__ANON__
3.1
CRAP 10.8
« Natural/Expand.pm ↑ Summary Natural/Duration/Checks.pm »
1 package DateTime::Format::Natural::Formatted;
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.12';
8  
9 sub _parse_formatted_ymd
10 271 ▶{
Subroutine _parse_formatted_ymd: called CC=18 SCAR=30.1
11 271 my $self = shift;
12 271 my ($date_string, $count) = @_;
13  
14 271 my $date = $self->_split_formatted($date_string);
15  
16 271 my $date_sep = quotemeta((keys %$count)[0]);
17 271 my @date_chunks = split /$date_sep/, $date;
18  
19 271 ▶ if ($date_chunks[1] =~ /^[a-zA-Z]+$/
Branch: 1 branch
BranchTrueFalseTotal
if ($date_chunks[1] =~ /^[a-zA-Z]+$/ and my($month_abbrev) = grep({$date_chunks[1] =~ /^$_$/i;} keys %{$$self{"data"}{"months_abbrev"};})) 7 264 271
Condition66%2/3
$date_chunks[1] =~ /^[a-zA-Z]+$/ and my($month_abbrev) = grep({$date_chunks[1] =~ /^$_$/i;} keys %{$$self{"data"}{"months_abbrev"};})
!ll&&!rl&&r
26407
MC/DC50%1/2
$date_chunks[1] =~ /^[a-zA-Z]+$/ and my($month_abbrev) = grep({$date_chunks[1] =~ /^$_$/i;} keys %{$$self{"data"}{"months_abbrev"};})
$date_chunks[1] =~ /^[a-zA-Z]+$/ my($month_abbrev) = grep({$date_chunks[1] =~ /^$_$/i;} keys %{$$self{"data"}{"months_abbrev"};})
Truth table66%2/3
A and B
A= $date_chunks[1] =~ /^[a-zA-Z]+$/B= my($month_abbrev) = grep({$date_chunks[1] =~ /^$_$/i;} keys %{$$self{"data"}{"months_abbrev"};})
ABresult
0X0
100
111
20 84 and my ($month_abbrev) = grep { $date_chunks[1] =~ /^${_}$/i } keys %{$self->{data}->{months_abbrev}}
21 ) {
22 7 my ($months_abbrev, $months) = map $self->{data}->{$_}, qw(months_abbrev months);
23 7 $date_chunks[1] = sprintf '%02d', $months->{$months_abbrev->{$month_abbrev}};
24 }
25  
26 271 my $i = 0;
27 271 my %chunks_length = map { length $_ => $i++ } @date_chunks;
28  
29 271 my $format = lc $self->{Format};
30 271 my $format_sep;
31  
32 271 my $lax = false;
33  
34 271 ▶ if (exists $chunks_length{4}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $chunks_length{'4'}) { } 253 18 271
elsif ($date_sep =~ /^\\[-.]$/ and not $format =~ /$date_sep/) { } 12 6 18
35 $format = join $date_sep,
36 253 ▶ ($chunks_length{4} == 0
Branch: 2 branches
BranchTrueFalseTotal
$chunks_length{'4'} == 0 ? : 172 81 253
$format =~ /^m/ ? : 6 75 81
37 ? qw(yyyy mm dd)
38 : ($format =~ /^m/
39 ? qw(mm dd yyyy)
40 : qw(dd mm yyyy)
41 )
42 );
43 253 $lax = true;
44 }
45 ▶ elsif ($date_sep =~ /^\\[-.]$/ && $format !~ /$date_sep/) {
Condition66%2/3
$date_sep =~ /^\\[-.]$/ and not $format =~ /$date_sep/
!ll&&!rl&&r
6012
MC/DC50%1/2
$date_sep =~ /^\\[-.]$/ and not $format =~ /$date_sep/
$date_sep =~ /^\\[-.]$/ not $format =~ /$date_sep/
Truth table66%2/3
A and not(B)
A= $date_sep =~ /^\\[-.]$/B= not $format =~ /$date_sep/
ABresult
0X0
100
111
46 12 $format = join $date_sep, qw(dd mm yy);
47 12 $lax = true;
48 }
49 else {
50 6 $format_sep = do { local $_ = $format;
51 6 tr/a-zA-Z//d;
52 6 tr/a-zA-Z//cs;
53 6 quotemeta; };
54 }
55 271 ▶ $format_sep ||= $date_sep;
Condition66%2/3
$format_sep ||= $date_sep
l!l&&r!l&&!r
62650
MC/DC0%0/2
$format_sep ||= $date_sep
$format_sep $date_sep
Truth table66%2/3
A ||= B
A= $format_sepB= $date_sep
ABresult
1X1
011
000
56  
57 271 ▶ if (!$lax && $format_sep ne $date_sep) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $lax and $format_sep ne $date_sep) 0 271 271
Condition66%2/3
not $lax and $format_sep ne $date_sep
!ll&&!rl&&r
26560
MC/DC0%0/2
not $lax and $format_sep ne $date_sep
not $lax $format_sep ne $date_sep
Truth table66%2/3
not(A) and B
A= not $laxB= $format_sep ne $date_sep
ABresult
0X0
100
111
58 0 $self->_set_failure;
59 0 $self->_set_error("(mismatch between format and date separator)");
60 0 return $self->_get_datetime_object;
61 }
62  
63 271 my @format_order = split /$format_sep/, $format;
64  
65 271 my ($d, $m, $y) = do {
66 271 my %f = map { substr($_, 0, 1) => true } @format_order;
67 271 ($f{d}, $f{m}, $f{y});
68 };
69 271 ▶ unless (@format_order == 3 and $d && $m && $y) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@format_order == 3 and ($d and $m and $y)) 0 271 271
Condition33%3/9
@format_order == 3 and ($d and $m and $y)
!ll&&!rl&&r
00271
$d and $m and $y
!ll&&!rl&&r
00271
$d and $m
!ll&&!rl&&r
00271
MC/DC0%0/4
@format_order == 3 and ($d and $m and $y)
@format_order == 3 $d $m $y
Truth table20%1/5
A and B and C and D
A= @format_order == 3B= $dC= $mD= $y
ABCDresult
0XXX0
10XX0
110X0
11100
11111
70 0 $self->_set_failure;
71 0 $self->_set_error("('format' parameter invalid)");
72 0 return $self->_get_datetime_object;
73 }
74  
75 271 $i = 0;
76 271 my %format_index = map { substr($_, 0, 1) => $i++ } @format_order;
77  
78 my $century = $self->{datetime}
79 271 ▶ ? int($self->{datetime}->year / 100)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'datetime'} ? : 271 0 271
80 : substr((localtime)[5] + 1900, 0, 2);
81  
82 271 my ($day, $month, $year) = map $date_chunks[$format_index{$_}], qw(d m y);
83  
84 271 ▶ if (length $year == 2) { $year = "$century$year" };
Branch: 1 branch
BranchTrueFalseTotal
if (length $year == 2) 18 253 271
85  
86 271 ▶ unless ($self->_check_date($year, $month, $day)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_check_date($year, $month, $day)) 1 270 271
87 1 $self->_set_failure;
88 1 $self->_set_error("(invalid date)");
89 1 return $self->_get_datetime_object;
90 }
91  
92 $self->_set(
93 270 year => $year,
94 month => $month,
95 day => $day,
96 );
97 270 $self->{datetime}->truncate(to => 'day');
98 270 $self->_set_truncated;
99 270 $self->_set_valid_exp;
100  
101 270 $self->_process_tokens;
102  
103 270 return undef;
104 }
105  
106 sub _parse_formatted_md
107 193 ▶{
Subroutine _parse_formatted_md: called CC=5 SCAR=16.2
108 193 my $self = shift;
109 193 my ($date_string) = @_;
110  
111 193 my $date = $self->_split_formatted($date_string);
112  
113 193 my @date_chunks = split /\//, $date;
114  
115 my $format = $self->{Format} =~ m{^[dm]{1,2}/[dm]{1,2}$}i
116 193 ▶ ? do { local $_ = lc $self->{Format}; tr/dm//s; $_ }
Branch: 1 branch
BranchTrueFalseTotal
$self->{'Format'} =~ m[^[dm]{1,2}/[dm]{1,2}$]i ? : 192 1 193
117 : undef;
118  
119 193 ▶ unless (defined $format && $format =~ m{^(?:(?:m/d)|(?:d/m))$}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $format and $format =~ m[^(?:(?:m/d)|(?:d/m))$]) 1 192 193
Condition66%2/3
defined $format and $format =~ m[^(?:(?:m/d)|(?:d/m))$]
!ll&&!rl&&r
10192
MC/DC50%1/2
defined $format and $format =~ m[^(?:(?:m/d)|(?:d/m))$]
defined $format $format =~ m[^(?:(?:m/d)|(?:d/m))$]
Truth table66%2/3
A and B
A= defined $formatB= $format =~ m[^(?:(?:m/d)|(?:d/m))$]
ABresult
0X0
100
111
120 1 $self->_set_failure;
121 1 $self->_set_error("('format' parameter invalid)");
122 1 return $self->_get_datetime_object;
123 }
124  
125 192 my $i = 0;
126 192 my %format_index = map { $_ => $i++ } split /\//, $format;
127  
128 192 my ($day, $month) = map $date_chunks[$format_index{$_}], qw(d m);
129  
130 192 ▶ unless ($self->_check_date($self->{datetime}->year, $month, $day)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_check_date($self->{"datetime"}->year, $month, $day)) 0 192 192
131 0 $self->_set_failure;
132 0 $self->_set_error("(invalid date)");
133 0 return $self->_get_datetime_object;
134 }
135  
136 $self->_set(
137 192 month => $month,
138 day => $day,
139 );
140 192 $self->{datetime}->truncate(to => 'day');
141 192 $self->_set_truncated;
142 192 $self->_set_valid_exp;
143  
144 192 $self->_process_tokens;
145  
146 192 return undef;
147 }
148  
149 sub _split_formatted
150 464 ▶{
Subroutine _split_formatted: called CC=3 SCAR=11.0
151 464 my $self = shift;
152 464 my ($date_string) = @_;
153  
154 464 my $date;
155 464 ▶ if ($date_string =~ /^\S+\b \s+ \b\S+/x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date_string =~ /^\S+\b \s+ \b\S+/x) { } 321 143 464
156 321 ($date, @{$self->{tokens}}) = split /\s+/, $date_string;
157 321 $self->{count}{tokens} = 1 + @{$self->{tokens}};
158 }
159 else {
160 143 $self->{count}{tokens} = 1;
161 }
162  
163 464 ▶ return defined $date ? $date : $date_string;
Branch: 1 branch
BranchTrueFalseTotal
defined $date ? : 321 143 464
164 }
165  
166 sub _process_tokens
167 462 ▶{
Subroutine _process_tokens: called CC=2 SCAR=6.9
168 462 my $self = shift;
169  
170 462 ▶ if (@{$self->{tokens}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"tokens"};}) 321 141 462
171 321 $self->{count}{tokens}--;
172 321 $self->_unset_truncated;
173 321 $self->_unset_valid_exp;
174 321 $self->_process;
175 }
176 }
177  
178 sub _count_separators
179 515051 ▶{
Subroutine _count_separators: called CC=3 SCAR=11.0
180 515051 my $self = shift;
181 515051 my ($formatted) = @_;
182  
183 515051 my %count;
184 515051 ▶ if (defined $formatted) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $formatted) 10664 504387 515051
185 10664 my @count = $formatted =~ m![-./]!g;
186 10664 $count{$_}++ foreach @count;
187 }
188  
189 515051 return %count;
190 }
191  
192 sub _check_formatted
193 527956 ▶{
Subroutine _check_formatted: called CC=1 SCAR=0.0
194 527956 my $self = shift;
195 527956 my ($check, $count) = @_;
196  
197 my %checks = (
198 ymd => sub
199 515051 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
200 515051 my ($count) = @_;
201 515051 ▶ return scalar keys %$count == 1 && $count->{(keys %$count)[0]} == 2;
Condition100%3/3
scalar keys %$count == 1 && $$count{(keys %$count)[0]} == 2
!ll&&!rl&&r
50438724110423
MC/DC100%2/2
scalar keys %$count == 1 && $$count{(keys %$count)[0]} == 2
scalar keys %$count == 1 $$count{(keys %$count)[0]} == 2
Truth table100%3/3
A && B
A= scalar keys %$count == 1B= $$count{(keys %$count)[0]} == 2
ABresult
0X0
100
111
202 },
203 md => sub
204 12905 ▶ {
Subroutine __ANON__: called CC=3 SCAR=11.4
205 12905 my ($count) = @_;
206 12905 ▶ return scalar keys %$count == 1 && $count->{(keys %$count)[0]} == 1 && (keys %$count)[0] eq '/';
Condition66%4/6
scalar keys %$count == 1 && $$count{(keys %$count)[0]} == 1 && (keys %$count)[0] eq '/'
!ll&&!rl&&r
126640241
scalar keys %$count == 1 and $$count{(keys %$count)[0]} == 1
!ll&&!rl&&r
126640241
MC/DC33%1/3
scalar keys %$count == 1 && $$count{(keys %$count)[0]} == 1 && (keys %$count)[0] eq '/'
scalar keys %$count == 1 $$count{(keys %$count)[0]} == 1 (keys %$count)[0] eq '/'
Truth table50%2/4
A and B && C
A= scalar keys %$count == 1B= $$count{(keys %$count)[0]} == 1C= (keys %$count)[0] eq '/'
ABCresult
0XX0
10X0
1100
1111
207 },
208 527956 );
209  
210 527956 return $checks{$check}->($count);
211 }
212  
213 27 1;
214 __END__
« Natural/Expand.pm ↑ Summary Natural/Duration/Checks.pm »