| 1 | package Test::Recent; | ||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Test::Builder::Tester; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use DateTime; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Time::Duration::Parse qw(parse_duration); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use DateTime::Format::ISO8601; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use vars qw(@EXPORT_OK $VERSION $OverridedNowForTesting $RelativeTo); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 3 | $VERSION = "2.50"; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 3 | my $tester = Test::Builder->new(); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # utility regex | ||||||||||||||||||||||||||||
| 22 | 3 | my $YMD = qr/[0-9]{4}-[0-9]{2}-[0-9]{2}/x; | |||||||||||||||||||||||||||
| 23 | 3 | my $HMS = qr/[0-9]{2}:[0-9]{2}:[0-9]{2}/x; | |||||||||||||||||||||||||||
| 24 | 3 | my $SUBSEC = qr/[0-9]+/x; | |||||||||||||||||||||||||||
| 25 | 3 | my $TZ = qr/[+-][0-9]{2}/x; | |||||||||||||||||||||||||||
| 26 | 3 | my $EPOCH = qr/\A \d+ (?:\.\d+)? \z/x; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 3 | $Test::Recent::future_duration = DateTime::Duration->new( seconds => 0 ); | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # convert anything that's passed to us into a DateTime object | ||||||||||||||||||||||||||||
| 31 | 57 | ▶ | sub _datetime($) { | ||||||||||||||||||||||||||
|
Subroutine
_datetime: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 32 | 57 | my $str = shift; | |||||||||||||||||||||||||||
| 33 | 57 | ▶ | return unless defined $str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 57 | ▶ | return $str if blessed $str && $str->isa("DateTime"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($str) and $str->isa("DateTime")
MC/DC50%1/2
builtin::blessed($str) and $str->isa("DateTime")
builtin::blessed($str)
$str->isa("DateTime")
Truth table66%2/3
A and B
A= builtin::blessed($str)B= $str->isa("DateTime")
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | ### | ||||||||||||||||||||||||||||
| 37 | # is this epoch seconds? | ||||||||||||||||||||||||||||
| 38 | ### | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 26 | ▶ | if ($str =~ $EPOCH) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 5 | return DateTime->from_epoch( epoch => $str ); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | ### | ||||||||||||||||||||||||||||
| 45 | # munge common extra formats into ISO8601 | ||||||||||||||||||||||||||||
| 46 | ### | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # postgres | ||||||||||||||||||||||||||||
| 49 | 21 | $str =~ s<\A ($YMD) [ ] ($HMS) [.] $SUBSEC ($TZ) \z><$1T$2$3>x; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 21 | return eval { DateTime::Format::ISO8601->parse_datetime( $str ) }; ## no critic (RequireCheckingReturnValueOfEval) | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # work out what the time is now | ||||||||||||||||||||||||||||
| 55 | 53 | ▶ | sub _now() { | ||||||||||||||||||||||||||
|
Subroutine
_now: called CC=5 SCAR=16.4
| |||||||||||||||||||||||||||||
| 56 | # people can override time! | ||||||||||||||||||||||||||||
| 57 | 53 | my $now = $RelativeTo; | |||||||||||||||||||||||||||
| 58 | 53 | ▶ | if (defined $now) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 14 | $now = _datetime($now); | |||||||||||||||||||||||||||
| 60 | 14 | ▶ | unless (defined $now) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | croak "\$Test::Recent::RelativeTo isn't parsable by Test::Recent"; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # historically we allowed $OverridedNowForTesting to be used to override | ||||||||||||||||||||||||||||
| 66 | # the sense of time. If some muppet is still using this, let them | ||||||||||||||||||||||||||||
| 67 | 53 | ▶ | $now = $OverridedNowForTesting unless defined $now; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 53 | ▶ | $now = DateTime->now() unless defined $now; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 53 | return $now; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 44 | ▶ | sub occured_within_ago($$) { | ||||||||||||||||||||||||||
|
Subroutine
occured_within_ago: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 44 | my $value = shift; | |||||||||||||||||||||||||||
| 76 | 44 | ▶ | return unless defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 43 | my $time = _datetime($value); | |||||||||||||||||||||||||||
| 79 | 43 | ▶ | return unless defined $time; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # forget the nanoseconds in the time passed to us. This is necessary | ||||||||||||||||||||||||||||
| 82 | # because DateTime->now() doesn't return nanoseconds, so if we don't | ||||||||||||||||||||||||||||
| 83 | # forget nanoseconds what is passed in might actually be mistaken | ||||||||||||||||||||||||||||
| 84 | # for something in the future | ||||||||||||||||||||||||||||
| 85 | 42 | $time = $time->clone->set_nanosecond(0); | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 42 | my $durations = shift; | |||||||||||||||||||||||||||
| 88 | 42 | my ($past_duration, $future_duration); | |||||||||||||||||||||||||||
| 89 | 42 | ▶ | if (ref $durations eq "ARRAY") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 4 | ($past_duration, $future_duration) = @{ $durations }; | |||||||||||||||||||||||||||
| 91 | } else { | ||||||||||||||||||||||||||||
| 92 | 38 | ($past_duration, $future_duration) | |||||||||||||||||||||||||||
| 93 | = ($durations, $Test::Recent::future_duration); | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 42 | foreach my $duration ($past_duration, $future_duration) { | |||||||||||||||||||||||||||
| 97 | 84 | ▶ | unless (blessed $duration && $duration->isa("DateTime::Duration")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($duration) and $duration->isa("DateTime::Duration")
MC/DC50%1/2
builtin::blessed($duration) and $duration->isa("DateTime::Duration")
builtin::blessed($duration)
$duration->isa("DateTime::Duration")
Truth table66%2/3
A and B
A= builtin::blessed($duration)B= $duration->isa("DateTime::Duration")
| |||||||||||||||||||||||||||||
| 98 | 39 | $duration = DateTime::Duration->new( | |||||||||||||||||||||||||||
| 99 | seconds => parse_duration($duration) | ||||||||||||||||||||||||||||
| 100 | ); | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 42 | my $now = _now; | |||||||||||||||||||||||||||
| 105 | 42 | my $ago = $now - $past_duration; | |||||||||||||||||||||||||||
| 106 | 42 | my $ahead = $now + $future_duration; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 42 | ▶ | return if $ahead < $time; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 32 | ▶ | return if $time < $ago; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 26 | return 1; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | 3 | push @EXPORT_OK, "occured_within_ago"; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 11 | ▶ | sub recent ($;$$) { | ||||||||||||||||||||||||||
|
Subroutine
recent: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 115 | 11 | my $time = shift; | |||||||||||||||||||||||||||
| 116 | 11 | ▶ | my $desc = pop || "recent time"; | ||||||||||||||||||||||||||
|
Condition50%1/2
pop() || 'recent time'
MC/DC0%0/1
pop() || 'recent time'
pop()
Truth table50%1/2
A
A= pop()
| |||||||||||||||||||||||||||||
| 117 | 11 | ▶ | my $duration = shift || "10s"; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || '10s'
MC/DC100%1/1
shift() || '10s'
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # work out when now is and "freeze it" | ||||||||||||||||||||||||||||
| 120 | 11 | local $RelativeTo = _now; ## no critic (ProhibitMixedCaseVars) | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 11 | my $ok = occured_within_ago($time, $duration); | |||||||||||||||||||||||||||
| 123 | 11 | $tester->ok($ok, $desc); | |||||||||||||||||||||||||||
| 124 | 11 | ▶ | return 1 if $ok; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 4 | $tester->diag("$time not recent to $RelativeTo"); | |||||||||||||||||||||||||||
| 126 | 4 | return; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 3 | push @EXPORT_OK, "recent"; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 3 | 1; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | __END__ | ||||||||||||||||||||||||||||