File Coverage

blib/lib/Test/Class/Moose/Role/HasTimeReport.pm
Criterion Covered Total %
statement 25 25 100.0
branch 2 2 100.0
condition 2 3 66.6
subroutine 8 8 100.0
pod n/a
total 37 38 97.3


line stmt bran cond sub pod time code
1             package Test::Class::Moose::Role::HasTimeReport;
2              
3             # ABSTRACT: Report timing role
4              
5 30     30   16174 use strict;
  30         95  
  30         980  
6 30     30   137 use warnings;
  30         49  
  30         694  
7 30     30   131 use namespace::autoclean;
  30         109  
  30         163  
8              
9 30     30   1993 use 5.010000;
  30         186  
10              
11             our $VERSION = '0.98';
12              
13 30     30   180 use Moose::Role;
  30         59  
  30         148  
14 30     30   156025 use Benchmark qw(timediff timestr :hireswallclock);
  30         118402  
  30         207  
15 30     30   16024 use Test::Class::Moose::Report::Time;
  30         96  
  30         7867  
16              
17             has '_start_benchmark' => (
18             is => 'ro',
19             isa => 'Benchmark',
20             lazy => 1,
21             default => sub { Benchmark->new },
22             predicate => '_has_start_benchmark',
23             );
24              
25             has '_end_benchmark' => (
26             is => 'ro',
27             isa => 'Benchmark',
28             lazy => 1,
29             default => sub { Benchmark->new },
30             predicate => '_has_end_benchmark',
31             );
32              
33             has 'time' => (
34             is => 'ro',
35             isa => 'Test::Class::Moose::Report::Time',
36             lazy => 1,
37             builder => '_build_time',
38             );
39              
40             # If Time::HiRes is available these will be non-integers
41             has 'start_time' => (
42             is => 'ro',
43             isa => 'Num',
44             lazy => 1,
45             default => sub { $_[0]->_start_benchmark->[0] },
46             );
47              
48             has 'end_time' => (
49             is => 'ro',
50             isa => 'Num',
51             lazy => 1,
52             default => sub { $_[0]->_end_benchmark->[0] },
53             );
54              
55             sub _build_time {
56 236     236   354 my $self = shift;
57              
58             # If we don't have start & end marked we'll return a report with zero time
59             # elapsed.
60 236 100 66     9695 unless ( $self->_has_start_benchmark && $self->_has_end_benchmark ) {
61 3         25 my $benchmark = Benchmark->new;
62 3         73 return Test::Class::Moose::Report::Time->new(
63             timediff => timediff( $benchmark, $benchmark ) );
64             }
65              
66 233         7552 return Test::Class::Moose::Report::Time->new( timediff =>
67             timediff( $self->_end_benchmark, $self->_start_benchmark ) );
68             }
69              
70             1;
71              
72             __END__
73              
74             =pod
75              
76             =encoding UTF-8
77              
78             =head1 NAME
79              
80             Test::Class::Moose::Role::HasTimeReport - Report timing role
81              
82             =head1 VERSION
83              
84             version 0.98
85              
86             =head1 DESCRIPTION
87              
88             Note that everything in here is experimental and subject to change.
89              
90             =head1 REQUIRES
91              
92             None.
93              
94             =head1 PROVIDED
95              
96             =head1 ATTRIBUTES
97              
98             =head2 C<time>
99              
100             Returns a L<Test::Class::Moose::Report::Time> object. This object
101             represents the duration of this class or method. The duration may be "0" if
102             it's an abstract class with no tests run.
103              
104             =head2 C<start_time>
105              
106             Returns the start time for the report as an epoch value.
107              
108             =head2 C<end_time>
109              
110             Returns the end time for the report as an epoch value.
111              
112             =head1 SUPPORT
113              
114             Bugs may be submitted at L<https://github.com/houseabsolute/test-class-moose/issues>.
115              
116             I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
117              
118             =head1 SOURCE
119              
120             The source code repository for Test-Class-Moose can be found at L<https://github.com/houseabsolute/test-class-moose>.
121              
122             =head1 AUTHORS
123              
124             =over 4
125              
126             =item *
127              
128             Curtis "Ovid" Poe <ovid@cpan.org>
129              
130             =item *
131              
132             Dave Rolsky <autarch@urth.org>
133              
134             =back
135              
136             =head1 COPYRIGHT AND LICENSE
137              
138             This software is copyright (c) 2012 - 2019 by Curtis "Ovid" Poe.
139              
140             This is free software; you can redistribute it and/or modify it under
141             the same terms as the Perl 5 programming language system itself.
142              
143             The full text of the license can be found in the
144             F<LICENSE> file included with this distribution.
145              
146             =cut