Print/Value/Time.pm

filter: Statementstmt 94.6% 53 / 56 Branchbran 80.0% 24 / 30 Conditioncond 53.3% 8 / 15 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 81.8% 99 / 121 CC 21 SCAR 27.7
CC
21
Cov
84%
print
23.5
_parse_date
3.0
BEGIN
1.0
CRAP 15.9
↑ Summary Print/Value/Quantity.pm »
1 package Wikibase::Datatype::Print::Value::Time;
2  
3 47 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 47 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 47 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 47 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 47 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 47 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 47 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 47 Readonly::Array our @EXPORT_OK => qw(print);
13  
14 47 our $VERSION = 0.21;
15  
16 16 ▶sub print {
Subroutine print: called CC=19 SCAR=31.6
Pod documented
17 16 my ($obj, $opts_hr) = @_;
18  
19 # Default options.
20 16 ▶ if (! defined $opts_hr) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts_hr) 3 13 16
21 3 $opts_hr = {};
22 }
23 16 ▶ if (! exists $opts_hr->{'print_name'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opts_hr->{"print_name"}) 15 1 16
24 15 $opts_hr->{'print_name'} = 1;
25 }
26  
27 16 ▶ if (! $obj->isa('Wikibase::Datatype::Value::Time')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->isa("Wikibase::Datatype::Value::Time")) 1 15 16
28 1 err "Object isn't 'Wikibase::Datatype::Value::Time'.";
29 }
30  
31 15 ▶ if (exists $opts_hr->{'cb'} && ! $opts_hr->{'cb'}->isa('Wikibase::Cache')) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts_hr->{"cb"} and not $opts_hr->{"cb"}->isa("Wikibase::Cache")) 1 14 15
Condition100%3/3
exists $opts_hr->{"cb"} and not $opts_hr->{"cb"}->isa("Wikibase::Cache")
!ll&&!rl&&r
1311
MC/DC100%2/2
exists $opts_hr->{"cb"} and not $opts_hr->{"cb"}->isa("Wikibase::Cache")
exists $opts_hr->{"cb"} not $opts_hr->{"cb"}->isa("Wikibase::Cache")
Truth table100%3/3
A and not(B)
A= exists $opts_hr->{"cb"}B= not $opts_hr->{"cb"}->isa("Wikibase::Cache")
ABresult
0X0
100
111
32 1 err "Option 'cb' must be a instance of Wikibase::Cache.";
33 }
34  
35 # Calendar.
36 14 my $calendar;
37 14 ▶ if ($opts_hr->{'print_name'} && exists $opts_hr->{'cb'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts_hr->{'print_name'} and exists $opts_hr->{'cb'}) { } 1 13 14
Condition66%2/3
$opts_hr->{"print_name"} and exists $opts_hr->{"cb"}
!ll&&!rl&&r
0131
MC/DC50%1/2
$opts_hr->{"print_name"} and exists $opts_hr->{"cb"}
$opts_hr->{"print_name"} exists $opts_hr->{"cb"}
Truth table66%2/3
A and B
A= $opts_hr->{"print_name"}B= exists $opts_hr->{"cb"}
ABresult
0X0
100
111
38 1 ▶ $calendar = $opts_hr->{'cb'}->get('label', $obj->calendarmodel) || $obj->calendarmodel;
Condition33%1/3
$opts_hr->{'cb'}->get('label', $obj->calendarmodel) || $obj->calendarmodel
l!l&&r!l&&!r
100
MC/DC0%0/2
$opts_hr->{'cb'}->get('label', $obj->calendarmodel) || $obj->calendarmodel
$opts_hr->{'cb'}->get('label', $obj->calendarmodel) $obj->calendarmodel
Truth table33%1/3
A || B
A= $opts_hr->{'cb'}->get('label', $obj->calendarmodel)B= $obj->calendarmodel
ABresult
1X1
011
000
39 } else {
40 13 $calendar = $obj->calendarmodel;
41 }
42  
43 # Convert to DateTime.
44 14 my $dt = _parse_date($obj->value);
45  
46 14 my $printed_date;
47  
48 # Day.
49 14 ▶ if ($obj->precision == 11) {
Branch: 5 branches
BranchTrueFalseTotal
if ($obj->precision == 11) { } 10 4 14
elsif ($obj->precision == 10) { } 0 4 4
elsif ($obj->precision == 9) { } 2 2 4
elsif ($obj->precision == 8) { } 2 0 2
elsif ($obj->precision <= 7 and $obj->precision >= 0) { } 0 0 0
50 10 $printed_date = $dt->strftime("%e %B %Y");
51 10 $printed_date =~ s/^\s+//ms;
52  
53 # Month.
54 } elsif ($obj->precision == 10) {
55 0 $printed_date = $dt->strftime("%B %Y");
56  
57 # Year.
58 } elsif ($obj->precision == 9) {
59 2 $printed_date = $dt->strftime("%Y");
60 2 ▶ if ($obj->before || $obj->after) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->before or $obj->after) 1 1 2
Condition66%2/3
$obj->before or $obj->after
l!l&&r!l&&!r
101
MC/DC50%1/2
$obj->before or $obj->after
$obj->before $obj->after
Truth table66%2/3
A or B
A= $obj->beforeB= $obj->after
ABresult
1X1
011
000
61 1 ▶ my $before = $obj->before ? $dt->year - $obj->before : $dt->year;
Branch: 1 branch
BranchTrueFalseTotal
$obj->before ? : 1 0 1
62 1 ▶ my $after = $obj->after ? $dt->year + $obj->after : $dt->year;
Branch: 1 branch
BranchTrueFalseTotal
$obj->after ? : 1 0 1
63 1 $printed_date .= " ($before-$after)";
64 }
65  
66 # Decade.
67 } elsif ($obj->precision == 8) {
68 2 $printed_date = (int($dt->strftime('%Y') / 10) * 10).'s';
69  
70 # TODO Better precision print?
71 # 0 - billion years, 1 - hundred million years, ..., 6 - millenia, 7 - century
72 ▶ } elsif ($obj->precision <= 7 && $obj->precision >= 0) {
Condition0%0/3
$obj->precision <= 7 and $obj->precision >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
$obj->precision <= 7 and $obj->precision >= 0
$obj->precision <= 7 $obj->precision >= 0
Truth table0%0/3
A and B
A= $obj->precision <= 7B= $obj->precision >= 0
ABresult
0X0
100
111
73 0 $printed_date = $dt->strftime("%Y");
74 } else {
75 0 err "Unsupported precision '".$obj->precision."'.";
76 }
77  
78 14 return $printed_date.' ('.$calendar.')';
79 }
80  
81 14 ▶sub _parse_date {
Subroutine _parse_date: called CC=3 SCAR=11.0
82 14 my $date = shift;
83  
84 14 my ($year, $month, $day) = ($date =~ m/^([\+\-]\d+)\-(\d{2})\-(\d{2})T\d{2}:\d{2}:\d{2}Z$/ms);
85 14 ▶ my $dt = DateTime->new(
Branch: 2 branches
BranchTrueFalseTotal
$month != 0 ? : 10 4 14
$day != 0 ? : 10 4 14
86 'year' => int($year),
87 $month != 0 ? ('month' => $month) : (),
88 $day != 0 ? ('day' => $day) : (),
89 );
90  
91 14 return $dt;
92 }
93  
94 47 1;
95  
96 __END__
↑ Summary Print/Value/Quantity.pm »