blib/lib/Test/Recent.pm

filter: Statementstmt 98.8% 84 / 85 Branchbran 89.2% 25 / 28 Conditioncond 70.0% 7 / 10 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 14 / 14 Podpod 100% 2 / 2 totaltotal 93.1% 135 / 145 CC 20 SCAR 15.7
CC
20
Cov
94%
occured_within_ago
9.0
_now
5.2
_datetime
5.0
CRAP 4.8
↑ Summary
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
BranchTrueFalseTotal
unless defined $str 0 57 57
34 57 ▶ return $str if blessed $str && $str->isa("DateTime");
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($str) and $str->isa("DateTime") 31 26 57
Condition66%2/3
builtin::blessed($str) and $str->isa("DateTime")
!ll&&!rl&&r
26031
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")
ABresult
0X0
100
111
35  
36 ###
37 # is this epoch seconds?
38 ###
39  
40 26 ▶ if ($str =~ $EPOCH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /$EPOCH/x) 5 21 26
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
BranchTrueFalseTotal
if (defined $now) 14 39 53
59 14 $now = _datetime($now);
60 14 ▶ unless (defined $now) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $now) 0 14 14
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
BranchTrueFalseTotal
unless defined $now 39 14 53
68  
69 53 ▶ $now = DateTime->now() unless defined $now;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $now 0 53 53
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
BranchTrueFalseTotal
unless defined $value 1 43 44
77  
78 43 my $time = _datetime($value);
79 43 ▶ return unless defined $time;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time 1 42 43
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
BranchTrueFalseTotal
if (ref $durations eq 'ARRAY') { } 4 38 42
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
BranchTrueFalseTotal
unless (builtin::blessed($duration) and $duration->isa("DateTime::Duration")) 39 45 84
Condition66%2/3
builtin::blessed($duration) and $duration->isa("DateTime::Duration")
!ll&&!rl&&r
39045
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")
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if $ahead < $time 10 32 42
109 32 ▶ return if $time < $ago;
Branch: 1 branch
BranchTrueFalseTotal
if $time < $ago 6 26 32
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'
l!l
110
MC/DC0%0/1
pop() || 'recent time'
pop()
Truth table50%1/2
A
A= pop()
Aresult
11
00
117 11 ▶ my $duration = shift || "10s";
Condition100%2/2
shift() || '10s'
l!l
83
MC/DC100%1/1
shift() || '10s'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
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
BranchTrueFalseTotal
if $ok 7 4 11
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__
↑ Summary