File Coverage

blib/lib/Test/Mini/Logger.pm
Criterion Covered Total %
statement 45 47 95.7
branch n/a
condition 2 2 100.0
subroutine 19 20 95.0
pod 17 17 100.0
total 83 86 96.5


line stmt bran cond sub pod time code
1             # Output Logger Base Class.
2             #
3             # Whether you're using a tool that expects output in a certain format, or you
4             # just long for the familiar look and feel of another testing framework, this
5             # is what you're looking for.
6             package Test::Mini::Logger;
7 4     4   22912 use strict;
  4         9  
  4         138  
8 4     4   22 use warnings;
  4         7  
  4         126  
9              
10 4     4   3039 use Time::HiRes;
  4         5771  
  4         26  
11              
12             # Constructor.
13             #
14             # @param [Hash] %args Initial state for the new instance.
15             # @option %args verbose (0) Logger verbosity.
16             # @option %args buffer [IO] (STDOUT) Output buffer.
17             sub new {
18 28     28 1 414 my ($class, %args) = @_;
19 28         230 return bless {
20             verbose => 0,
21             buffer => *STDOUT{IO},
22             %args,
23             count => {},
24             times => {},
25             }, $class;
26             }
27              
28             # @group Attribute Accessors
29              
30             # @return Logger verbosity.
31             sub verbose {
32 0     0 1 0 my ($self) = @_;
33 0         0 return $self->{verbose};
34             }
35              
36             # @return [IO] Output buffer.
37             sub buffer {
38 89     89 1 257 my ($self) = @_;
39 89         13857 return $self->{buffer};
40             }
41              
42             # @group Output Functions
43              
44             # Write output to the {#buffer}.
45             # Lines will be output without added newlines.
46             #
47             # @param @msg The message(s) to be printed; will be handled as per +print+.
48             sub print {
49 89     89 1 187 my ($self, @msg) = @_;
50 89         96 print { $self->buffer() } @msg;
  89         221  
51             }
52              
53             # Write output to the {#buffer}.
54             # Lines will be output with appended newlines.
55             #
56             # @param @msg The message(s) to be printed; newlines will be appended to each
57             # message, before being passed to {#print}.
58             sub say {
59 84     84 1 275 my ($self, @msg) = @_;
60 84         351 $self->print(join("\n", @msg), "\n");
61             }
62              
63             # @group Callbacks
64              
65             # Called before the test suite is run.
66             #
67             # @param [Hash] %args Options the test suite was run with.
68             # @option %args [String] filter Test name filter.
69             # @option %args [String] seed Randomness seed.
70             sub begin_test_suite {
71 12     12 1 127 my ($self, %args) = @_;
72 12         97 $self->{times}->{$self} = -Time::HiRes::time();
73             }
74              
75             # Called before each test case is run.
76             #
77             # @param [Class] $tc The test case being run.
78             # @param [Array] @tests A list of tests to be run.
79             sub begin_test_case {
80 9     9 1 33 my ($self, $tc, @tests) = @_;
81 9         48 $self->{times}->{$tc} = -Time::HiRes::time();
82             }
83              
84             # Called before each test is run.
85             #
86             # @param [Class] $tc The test case owning the test method.
87             # @param [String] $test The name of the test method being run.
88             sub begin_test {
89 12     12 1 48 my ($self, $tc, $test) = @_;
90 12         149 $self->{times}->{"$tc#$test"} = -Time::HiRes::time();
91             }
92              
93             # Called after each test is run.
94             # Increments the test and assertion counts, and finalizes the test's timing.
95             #
96             # @param [Class] $tc The test case owning the test method.
97             # @param [String] $test The name of the test method just run.
98             # @param [Integer] $assertions The number of assertions called.
99             sub finish_test {
100 64     64 1 704430 my ($self, $tc, $test, $assertions) = @_;
101 64         163 $self->{count}->{test}++;
102 64         129 $self->{count}->{assert} += $assertions;
103 64         431 $self->{times}->{"$tc#$test"} += Time::HiRes::time();
104             }
105              
106             # Called after each test case is run.
107             # Increments the test case count, and finalizes the test case's timing.
108             #
109             # @param [Class] $tc The test case just run.
110             # @param [Array] @tests A list of tests run.
111             sub finish_test_case {
112 12     12 1 56 my ($self, $tc, @tests) = @_;
113 12         32 $self->{count}->{test_case}++;
114 12         64 $self->{times}->{$tc} += Time::HiRes::time();
115             }
116              
117             # Called after each test suite is run.
118             # Finalizes the test suite timing.
119             #
120             # @param [Integer] $exit_code Status the tests finished with.
121             sub finish_test_suite {
122 9     9 1 17 my ($self, $exit_code) = @_;
123 9         48 $self->{times}->{$self} += Time::HiRes::time();
124             }
125              
126             # Called when a test passes.
127             # Increments the pass count.
128             #
129             # @param [Class] $tc The test case owning the test method.
130             # @param [String] $test The name of the passing test.
131             sub pass {
132 4     4 1 14 my ($self, $tc, $test) = @_;
133 4         17 $self->{count}->{pass}++;
134             }
135              
136             # Called when a test is skipped.
137             # Increments the skip count.
138             #
139             # @param [Class] $tc The test case owning the test method.
140             # @param [String] $test The name of the skipped test.
141             # @param [Test::Mini::Exception::Skip] $e The exception object.
142             sub skip {
143 2     2 1 11 my ($self, $tc, $test, $e) = @_;
144 2         10 $self->{count}->{skip}++;
145             }
146              
147             # Called when a test fails.
148             # Increments the failure count.
149             #
150             # @param [Class] $tc The test case owning the test method.
151             # @param [String] $test The name of the failed test.
152             # @param [Test::Mini::Exception::Assert] $e The exception object.
153             sub fail {
154 2     2 1 9 my ($self, $tc, $test, $e) = @_;
155 2         10 $self->{count}->{fail}++;
156             }
157              
158             # Called when a test dies with an error.
159             # Increments the error count.
160             #
161             # @param [Class] $tc The test case owning the test method.
162             # @param [String] $test The name of the test with an error.
163             # @param [Test::Mini::Exception] $e The exception object.
164             sub error {
165 4     4 1 14 my ($self, $tc, $test, $e) = @_;
166 4         16 $self->{count}->{error}++;
167             }
168              
169             # @group Statistics
170              
171             # Accessor for counters.
172             #
173             # @overload count()
174             # @return [Hash] The count hash.
175             #
176             # @overload count($key)
177             # @param $key A key in the count hash.
178             # @return [Number] The value for the given key.
179             sub count {
180 7     7 1 15 my ($self, $key) = @_;
181 7   100     64 return ($key ? $self->{count}->{$key} : $self->{count}) || 0;
182             }
183              
184             # Accessor for the timing data.
185             #
186             # @param $key The key to look up timings for. Typical values are:
187             # +$self+ :: Time for test suite
188             # "TestCase" :: Time for the test case
189             # "TestCase#test" :: Time for the given test
190             # Times for units that have not finished should not be relied upon.
191             # @return [Number] The time taken by the given argument, in seconds.
192             sub time {
193 6     6 1 17 my ($self, $key) = @_;
194 6         31 return $self->{times}->{$key};
195             }
196              
197             1;