| 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
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 13 | ▶ | my $expected = shift or confess "Expected datetime required for datetime_rfc3339() with arguments"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 13 | ▶ | my $tolerance = shift || DateTime::Duration->new; # default to an ->is_zero duration | ||||||||||||||||||||||||||
|
Condition66%2/3
shift() || 'DateTime::Duration'->new
MC/DC0%0/2
shift() || 'DateTime::Duration'->new
shift()
'DateTime::Duration'->new
Truth table66%2/3
A || B
A= shift()B= 'DateTime::Duration'->new
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 13 | ▶ | unless ($expected->$_isa("DateTime")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 2 | ▶ | my $parsed = eval { $self->{parser}->parse_datetime($expected) } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 41 | 6 | my $parser = DateTime::Format::Duration::DurationString->new; | |||||||||||||||||||||||||||
| 42 | 6 | ▶ | my $parsed = eval { $parser->parse($tolerance)->to_duration } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$@ or not $got
MC/DC50%1/2
$@ or not $got
$@
not $got
Truth table66%2/3
A or not(B)
A= $@B= not $got
| |||||||||||||||||||||||||||||
| 63 | 2 | ▶ | $self->{diag_message} = sprintf "Can't parse %s as an RFC3339 timestamp: %s", | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | (defined $_[1] ? "'$_[1]'" : "an undefined value"), $@; | ||||||||||||||||||||||||||||
| 65 | 2 | return 0; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 8 | ▶ | $got->set_time_zone('UTC') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$got >= $expected - $tolerance and $got <= $expected + $tolerance
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||