blib/lib/Test/MockTime.pm

filter: Statementstmt 95.0% 58 / 61 Branchbran 81.2% 13 / 16 Conditioncond 45.4% 5 / 11 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 13 / 13 Podpod 57.1% 4 / 7 totaltotal 81.7% 94 / 115 CC 13 SCAR 9.6
CC
13
Cov
86%
_time
4.0
set_relative_time
3.6
set_absolute_time
3.4
CRAP 2.6
↑ Summary
1 package Test::MockTime;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Carp();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Exporter();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 *import = \&Exporter::import;
8 4 our @EXPORT_OK = qw(
9 set_relative_time
10 set_absolute_time
11 set_fixed_time
12 restore_time
13 );
14 4 our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK, );
15 4 our $VERSION = '0.17';
16 4 our $offset = 0;
17 4 our $fixed = undef;
18  
19 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 *CORE::GLOBAL::time = \&Test::MockTime::time;
21 4 *CORE::GLOBAL::localtime = \&Test::MockTime::localtime;
22 4 *CORE::GLOBAL::gmtime = \&Test::MockTime::gmtime;
23 }
24  
25 3 ▶sub set_relative_time {
Subroutine set_relative_time: called CC=3 SCAR=12.7
Pod documented
26 3 my ($relative) = @_;
27 3 ▶ if ( ( $relative eq __PACKAGE__ )
Branch: 1 branch
BranchTrueFalseTotal
if ($relative eq __PACKAGE__ or UNIVERSAL::isa($relative, __PACKAGE__)) 0 3 3
Condition33%1/3
$relative eq __PACKAGE__ or UNIVERSAL::isa($relative, __PACKAGE__)
l!l&&r!l&&!r
003
MC/DC0%0/2
$relative eq __PACKAGE__ or UNIVERSAL::isa($relative, __PACKAGE__)
$relative eq __PACKAGE__ UNIVERSAL::isa($relative, __PACKAGE__)
Truth table33%1/3
A or B
A= $relative eq __PACKAGE__B= UNIVERSAL::isa($relative, __PACKAGE__)
ABresult
1X1
011
000
28 || ( UNIVERSAL::isa( $relative, __PACKAGE__ ) ) )
29 {
30 0 Carp::carp("Test::MockTime::set_relative_time called incorrectly\n");
31 }
32 3 $offset = $_[-1]; # last argument. might have been called in a OO syntax?
33 3 return $offset;
34 }
35  
36 15 ▶sub _time {
Subroutine _time: called CC=4 SCAR=13.9
37 15 my ( $time, $spec ) = @_;
38 15 ▶ if ( $time !~ /\A -? \d+ \z/xms ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($time =~ /\A -? \d+ \z/msx) 8 7 15
39 8 ▶ $spec ||= '%Y-%m-%dT%H:%M:%SZ';
Condition100%2/2
$spec ||= "%Y-%m-%dT%H:%M:%SZ"
l!l
44
MC/DC100%1/1
$spec ||= "%Y-%m-%dT%H:%M:%SZ"
$spec
Truth table100%2/2
A
A= $spec
Aresult
11
00
40 }
41 15 ▶ if ($spec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec) 8 7 15
42 8 require Time::Piece;
43 8 $time = Time::Piece->strptime( $time, $spec )->epoch();
44 }
45 15 return $time;
46 }
47  
48 4 ▶sub set_absolute_time {
Subroutine set_absolute_time: called CC=3 SCAR=12.3
Pod documented
49 4 my ( $time, $spec ) = @_;
50 4 ▶ if ( ( $time eq __PACKAGE__ ) || ( UNIVERSAL::isa( $time, __PACKAGE__ ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($time eq __PACKAGE__ or UNIVERSAL::isa($time, __PACKAGE__)) 0 4 4
Condition33%1/3
$time eq __PACKAGE__ or UNIVERSAL::isa($time, __PACKAGE__)
l!l&&r!l&&!r
004
MC/DC0%0/2
$time eq __PACKAGE__ or UNIVERSAL::isa($time, __PACKAGE__)
$time eq __PACKAGE__ UNIVERSAL::isa($time, __PACKAGE__)
Truth table33%1/3
A or B
A= $time eq __PACKAGE__B= UNIVERSAL::isa($time, __PACKAGE__)
ABresult
1X1
011
000
51 {
52 0 Carp::carp("Test::MockTime::set_absolute_time called incorrectly\n");
53 }
54 4 $time = _time( $time, $spec );
55 4 $offset = $time - CORE::time;
56 4 return $offset;
57 }
58  
59 11 ▶sub set_fixed_time {
Subroutine set_fixed_time: called CC=3 SCAR=12.3
Pod documented
60 11 my ( $time, $spec ) = @_;
61 11 ▶ if ( ( $time eq __PACKAGE__ ) || ( UNIVERSAL::isa( $time, __PACKAGE__ ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($time eq __PACKAGE__ or UNIVERSAL::isa($time, __PACKAGE__)) 0 11 11
Condition33%1/3
$time eq __PACKAGE__ or UNIVERSAL::isa($time, __PACKAGE__)
l!l&&r!l&&!r
0011
MC/DC0%0/2
$time eq __PACKAGE__ or UNIVERSAL::isa($time, __PACKAGE__)
$time eq __PACKAGE__ UNIVERSAL::isa($time, __PACKAGE__)
Truth table33%1/3
A or B
A= $time eq __PACKAGE__B= UNIVERSAL::isa($time, __PACKAGE__)
ABresult
1X1
011
000
62 {
63 0 Carp::carp("Test::MockTime::set_fixed_time called incorrectly\n");
64 }
65 11 $time = _time( $time, $spec );
66 11 $fixed = $time;
67 11 return $fixed;
68 }
69  
70 118 ▶sub time() {
Subroutine time: called CC=2 SCAR=6.9
Pod undocumented
71 118 ▶ if ( defined $fixed ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Test::MockTime::fixed) { } 58 60 118
72 58 return $fixed;
73 }
74 else {
75 60 return ( CORE::time + $offset );
76 }
77 }
78  
79 30 ▶sub localtime (;$) {
Subroutine localtime: called CC=2 SCAR=6.9
Pod undocumented
80 30 my ($time) = @_;
81 30 ▶ if ( !defined $time ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $time) 12 18 30
82 12 $time = Test::MockTime::time();
83 }
84 30 return CORE::localtime($time);
85 }
86  
87 10 ▶sub gmtime (;$) {
Subroutine gmtime: called CC=2 SCAR=6.9
Pod undocumented
88 10 my ($time) = @_;
89 10 ▶ if ( !defined $time ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $time) 6 4 10
90 6 $time = Test::MockTime::time();
91 }
92 10 return CORE::gmtime($time);
93 }
94  
95 1 ▶sub restore {
Subroutine restore: called CC=1 SCAR=0.0
Pod documented
96 1 $offset = 0;
97 1 $fixed = undef;
98 1 return;
99 }
100 4 *restore_time = \&restore;
101  
102 4 1;
103 __END__
↑ Summary