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 29     29   15848 use strict;
  29         109  
  29         910  
6 29     29   129 use warnings;
  29         62  
  29         677  
7 29     29   136 use namespace::autoclean;
  29         121  
  29         160  
8              
9 29     29   1686 use 5.010000;
  29         250  
10              
11             our $VERSION = '0.97';
12              
13 29     29   177 use Moose::Role;
  29         56  
  29         247  
14 29     29   138976 use Benchmark qw(timediff timestr :hireswallclock);
  29         116167  
  29         203  
15 29     29   15423 use Test::Class::Moose::Report::Time;
  29         105  
  29         7832  
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   339 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     7353 unless ( $self->_has_start_benchmark && $self->_has_end_benchmark ) {
61 3         20 my $benchmark = Benchmark->new;
62 3         66 return Test::Class::Moose::Report::Time->new(
63             timediff => timediff( $benchmark, $benchmark ) );
64             }
65              
66 233         6020 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.97
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