blib/lib/Test/Deep/DateTime/RFC3339.pm

filter: Statementstmt 100% 80 / 80 Branchbran 89.2% 25 / 28 Conditioncond 77.7% 7 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 19 / 19 Podpod 16.6% 1 / 6 totaltotal 91.2% 135 / 148 CC 18 SCAR 13.9
CC
18
Cov
96%
init
8.0
descend
7.0
renderExp
3.0
CRAP 4.0
↑ Summary
1 package Test::Deep::DateTime::RFC3339;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use 5.008_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 our $VERSION = '0.04';
7  
8 1 ▶use Test::Deep::Cmp; # isa
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 our @EXPORT = qw(datetime_rfc3339);
12  
13 1 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use DateTime::Duration;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use DateTime::Format::RFC3339;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use DateTime::Format::Duration::DurationString;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use DateTime::Format::Human::Duration;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Safe::Isa '$_isa';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 15 ▶sub datetime_rfc3339 {
Subroutine datetime_rfc3339: called CC=1 SCAR=0.0
Pod documented
23 15 __PACKAGE__->new(@_);
24 }
25  
26 15 ▶sub init {
Subroutine init: called CC=8 SCAR=20.8
Pod undocumented
27 15 my $self = shift;
28  
29 15 $self->{parser} = DateTime::Format::RFC3339->new;
30 15 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 2 13 15
31  
32 13 ▶ my $expected = shift or confess "Expected datetime required for datetime_rfc3339() with arguments";
Branch: 1 branch
BranchTrueFalseTotal
my $expected = shift() or confess("Expected datetime required for datetime_rfc3339() with arguments") 13 0 13
33 13 ▶ my $tolerance = shift || DateTime::Duration->new; # default to an ->is_zero duration
Condition66%2/3
shift() || 'DateTime::Duration'->new
l!l&&r!l&&!r
760
MC/DC0%0/2
shift() || 'DateTime::Duration'->new
shift() 'DateTime::Duration'->new
Truth table66%2/3
A || B
A= shift()B= 'DateTime::Duration'->new
ABresult
1X1
011
000
34  
35 13 ▶ unless ($expected->$_isa("DateTime")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($expected->$Test::Deep::DateTime::RFC3339::_isa("DateTime")) 2 11 13
36 2 ▶ my $parsed = eval { $self->{parser}->parse_datetime($expected) }
Branch: 1 branch
BranchTrueFalseTotal
my $parsed = eval { do { $self->{"parser"}->parse_datetime($expected) } } or confess("Expected datetime isn't a DateTime and can't be parsed as RFC3339: '${expected}', $@") 1 1 2
37 or confess "Expected datetime isn't a DateTime and can't be parsed as RFC3339: '$expected', $@";
38 1 $expected = $parsed;
39 }
40 12 ▶ unless ($tolerance->$_isa("DateTime::Duration")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tolerance->$Test::Deep::DateTime::RFC3339::_isa("DateTime::Duration")) 6 6 12
41 6 my $parser = DateTime::Format::Duration::DurationString->new;
42 6 ▶ my $parsed = eval { $parser->parse($tolerance)->to_duration }
Branch: 1 branch
BranchTrueFalseTotal
my $parsed = eval { do { $parser->parse($tolerance)->to_duration } } or confess("Expected tolerance isn't a DateTime::Duration and can't be parsed: '${tolerance}', $@") 5 1 6
43 or confess "Expected tolerance isn't a DateTime::Duration and can't be parsed: '$tolerance', $@";
44 5 $tolerance = $parsed;
45 }
46  
47 # Do all comparisons and math in UTC
48 11 $expected->set_time_zone('UTC');
49  
50 11 $self->{expected} = $expected;
51 11 $self->{tolerance} = $tolerance;
52  
53 11 return;
54 }
55  
56 10 ▶sub descend {
Subroutine descend: called CC=7 SCAR=19.5
Pod undocumented
57 10 my ($self, $got) = @_;
58 10 my ($expected, $tolerance) = @$self{'expected', 'tolerance'};
59  
60 10 $got = eval { $self->{parser}->parse_datetime($got) };
61  
62 10 ▶ if ($@ or not $got) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $got) 2 8 10
Condition66%2/3
$@ or not $got
l!l&&r!l&&!r
208
MC/DC50%1/2
$@ or not $got
$@ not $got
Truth table66%2/3
A or not(B)
A= $@B= not $got
ABresult
1X1
011
000
63 2 ▶ $self->{diag_message} = sprintf "Can't parse %s as an RFC3339 timestamp: %s",
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 2 0 2
64 (defined $_[1] ? "'$_[1]'" : "an undefined value"), $@;
65 2 return 0;
66 }
67  
68 8 ▶ $got->set_time_zone('UTC')
Branch: 1 branch
BranchTrueFalseTotal
if $expected 7 1 8
69 if $expected;
70  
71 # This lets us receive the DateTime object in renderGot
72 8 $self->data->{got_string} = $self->data->{got};
73 8 $self->data->{got} = $got;
74  
75 8 ▶ return $expected
Branch: 1 branch
BranchTrueFalseTotal
$expected ? : 7 1 8
Condition100%3/3
$got >= $expected - $tolerance and $got <= $expected + $tolerance
!ll&&!rl&&r
124
MC/DC100%2/2
$got >= $expected - $tolerance and $got <= $expected + $tolerance
$got >= $expected - $tolerance $got <= $expected + $tolerance
Truth table100%3/3
A and B
A= $got >= $expected - $toleranceB= $got <= $expected + $tolerance
ABresult
0X0
100
111
76 ? ($got >= $expected - $tolerance and $got <= $expected + $tolerance)
77 : 1; # we parsed!
78 }
79  
80 # reported at top of diagnostic output on failure
81 2 ▶sub diag_message {
Subroutine diag_message: called CC=2 SCAR=7.0
Pod undocumented
82 2 my ($self, $where) = @_;
83 2 my $msg = "Compared $where";
84 $msg .= "\n" . $self->{diag_message}
85 2 ▶ if $self->{diag_message};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"diag_message"} 2 0 2
86 2 return $msg;
87 }
88  
89 # used in diagnostic output on failure to render the expected value
90 4 ▶sub renderExp {
Subroutine renderExp: called CC=3 SCAR=11.0
Pod undocumented
91 4 my $self = shift;
92 4 ▶ return "any RFC3339 timestamp" unless $self->{expected};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"expected"} 1 3 4
93  
94 3 my $expected = $self->_format( $self->{expected} );
95 return $self->{tolerance}->is_zero
96 ? $expected
97 3 ▶ : $expected . " +/- " . DateTime::Format::Human::Duration->new->format_duration($self->{tolerance});
Branch: 1 branch
BranchTrueFalseTotal
$self->{'tolerance'}->is_zero ? : 2 1 3
98 }
99  
100 3 ▶sub renderGot {
Subroutine renderGot: called CC=2 SCAR=6.9
Pod undocumented
101 3 my ($self, $got) = @_;
102 3 ▶ return $got->$_isa("DateTime") ? $self->_format($got) : $got;
Branch: 1 branch
BranchTrueFalseTotal
$got->$Test::Deep::DateTime::RFC3339::_isa('DateTime') ? : 1 2 3
103 }
104  
105 4 ▶sub _format {
Subroutine _format: called CC=1 SCAR=0.0
106 4 my $self = shift;
107 4 return $self->{parser}->format_datetime(@_);
108 }
109  
110 1 1;
111 __END__
↑ Summary