Proto/TestRunner.pm

filter: Statementstmt 99.1% 121 / 122 Branchbran 88.8% 32 / 36 Conditioncond 77.7% 7 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 28 / 28 Podpod 92.3% 12 / 13 totaltotal 95.3% 204 / 214 CC 24 SCAR 14.0
CC
24
Cov
96%
run_test
10.1
status
6.0
complete
5.0
CRAP 4.0
« Proto/Formatter.pm ↑ Summary Proto/Role/Value.pm »
1 package Test::Proto::TestRunner;
2 14 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use overload 'bool' => sub { $_[0]->value };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
6 14 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 14 ▶use Object::ID;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use Test::Proto::Common ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 70 ▶sub _zero {
Subroutine _zero: called CC=1 SCAR=0.0
11 53630 ▶ sub { 0; }
Subroutine __ANON__: called CC=1 SCAR=0.0
12 70 }
13  
14 =head1 NAME
15  
16 Test::Proto::TestRunner - Embodies a run through a test
17  
18 =head1 SYNOPSIS
19  
20 my $runner = Test::Proto::TestRunner->new(test_case=>$testCase);
21 my $subRunner $runner->subtest;
22 $subRunner->pass;
23 $runner->done();
24  
25 Note that it is a Moo class.
26  
27 Unless otherwise specified, the return value is itself.
28  
29 =cut
30  
31 10726 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
32 10726 my $self = shift;
33 10726 $self->inform_formatter('new');
34 }
35  
36 =head2 ATTRIBUTES
37  
38 All of these attributes are chainable as setters.
39  
40 =cut
41  
42 =head3 subject
43  
44 Returns the test subject
45  
46 =cut
47  
48 14 has 'subject' => is => 'rw';
49  
50 =head3 test_case
51  
52 Returns the test case or the prototype
53  
54 =cut
55  
56 14 has 'test_case' => is => 'rw';
57  
58 =head3 parent
59  
60 Returns the parent of the test.
61  
62 =cut
63  
64 14 has 'parent' => is => 'rwp',
65 weak_ref => 1;
66  
67 =head3 is_complete
68  
69 Returns C<1> if the test run has finished, C<0> otherwise.
70  
71 =cut
72  
73 14 has 'is_complete' => is => 'rwp',
74 default => _zero;
75  
76 =head3 value
77  
78 Returns C<0> if the test run has failed or exception, C<1> otherwise.
79  
80 =cut
81  
82 14 has 'value' => is => 'rw',
83 default => _zero;
84  
85 =head3 skipped_tags
86  
87 If any test case or prototype has one of the tags in this list, the runner will skip it.
88  
89 =cut
90  
91 has 'skipped_tags' => is => 'rw',
92 992 ▶ default => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
93  
94 =head3 required_tags
95  
96 If this list is not empty, then unless a test case or prototype has a tag in this list, the runner will skip it.
97  
98 =cut
99  
100 has 'required_tags' => is => 'rw',
101 992 ▶ default => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
102  
103 =head3 is_exception
104  
105 Returns C<1> if the test run has run into an exception, C<0> otherwise.
106  
107 =cut
108  
109 14 has 'is_exception' => is => 'rwp',
110 default => _zero;
111  
112 =head3 is_info
113  
114 Returns C<1> if the result is for information purposes, C<0> otherwise.
115  
116 =cut
117  
118 14 has 'is_info' => is => 'rwp',
119 default => _zero;
120  
121 =head3 is_skipped
122  
123 Returns C<1> if the test case was skipped, C<0> otherwise.
124  
125 =cut
126  
127 14 has 'is_skipped' => is => 'rwp',
128 default => _zero;
129  
130 =head3 children
131  
132 Returns an arrayref
133  
134 =cut
135  
136 has 'children' => is => 'rw',
137 10726 ▶ default => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
138  
139 =head3 status_message
140  
141 This is a string which indicates the reason for skipping, exception info, etc.
142  
143 =cut
144  
145 has 'status_message' => is => 'rw',
146 10716 ▶ default => sub { '' };
Subroutine __ANON__: called CC=1 SCAR=0.0
147  
148 =head3 formatter
149  
150 Returns the formatter used.
151  
152 =cut
153  
154 14 has 'formatter' => is => 'rw'; # Test::Proto::Common::Formatter->new;
155  
156 14 around qw(subject test_case parent is_complete skipped_tags required_tags children value is_exception is_info is_skipped children status_message ), \&Test::Proto::Common::chainable;
157  
158 =head2 METHODS
159  
160 =head3 complete
161  
162 $self->complete(0);
163 $self->complete(0, 'Something went wrong');
164  
165 Declares the test run is complete. It is intended that this is only called by the other methods C<done>, C<pass>, C<fail>, C<exception>, C<diag>, C<skip>.
166  
167 =cut
168  
169 10723 ▶sub complete {
Subroutine complete: called CC=5 SCAR=16.2
Pod documented
170 10723 my ( $self, $value, $message ) = @_;
171 10723 ▶ if ( $self->is_complete ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_complete) 1 10722 10723
172 1 ▶ warn "Tried to complete something that was already complete (a " . $self->status . "). (Tried with value=> " . ( defined $value ? $value : '[undefined]' ) . ", message=>" . ( defined $message ? $message : '[undefined]' ) . ")";
Branch: 2 branches
BranchTrueFalseTotal
defined $value ? : 1 0 1
defined $message ? : 0 1 1
173 1 return $self;
174 }
175 10722 $self->value($value);
176 10722 ▶ $self->status_message($message) if defined $message;
Branch: 1 branch
BranchTrueFalseTotal
if defined $message 5188 5534 10722
177 10722 $self->_set_is_complete(1);
178 10722 $self->inform_formatter('done');
179 10722 return $self;
180 }
181  
182 =head3 subtest
183  
184 Creates and returns a child, which is another TestRunner. The child keeps the same formatter, subject, and test_case as the parent. The child is added to the parent's list of events.
185  
186 =cut
187  
188 9734 ▶sub subtest {
Subroutine subtest: called CC=1 SCAR=0.0
Pod documented
189 9734 my $self = shift;
190 9734 my $event = __PACKAGE__->new( {
191 formatter => $self->formatter,
192 subject => $self->subject,
193 test_case => $self->test_case,
194 skipped_tags => $self->skipped_tags,
195 required_tags => $self->required_tags,
196 parent => $self,
197 @_
198 }
199 );
200 9734 $self->add_event($event);
201 9734 return $event;
202 }
203  
204 =head3 add_event
205  
206 Adds an event to the runner.
207  
208 =cut
209  
210 9741 ▶sub add_event {
Subroutine add_event: called CC=3 SCAR=11.1
Pod documented
211 9741 my ( $self, $event ) = @_;
212 9741 ▶ if ( $self->is_complete ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_complete) { } 2 9739 9741
213 2 warn "Tried to add an event to a TestRunner which is already complete";
214 }
215 else {
216 9739 ▶ unless ( defined $event ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $event) 0 9739 9739
217 0 die('tried to add an undefined event');
218 }
219 9739 push @{ $self->children }, $event;
220 }
221 9741 return $self;
222 }
223  
224 =head3 done
225  
226 $self->done;
227 $self->done ('Completed check of widgets');
228  
229 Declares that the test run is complete, and determines if the result is a pass or a fail - if there are any failures, then the result is deemed to be a failure.
230  
231 =cut
232  
233 6362 ▶sub done {
Subroutine done: called CC=3 SCAR=11.0
Pod documented
234 6362 my ( $self, $message ) = @_;
235 6362 ▶ return $self->exception if scalar grep { $_->is_exception } @{ $self->children() };
Branch: 1 branch
BranchTrueFalseTotal
if scalar grep({$_->is_exception;} @{$self->children;}) 10 6352 6362
236 6352 ▶ $self->complete( $self->_count_fails ? 0 : 1, $message );
Branch: 1 branch
BranchTrueFalseTotal
$self->_count_fails ? : 1439 4913 6352
237 6352 return $self;
238 }
239  
240 6352 ▶sub _count_fails {
Subroutine _count_fails: called CC=1 SCAR=0.0
241 6352 my $self = shift;
242 6352 return scalar grep { !$_->value } @{ $self->children() };
243 }
244  
245 # add_(pass|fail|diag|exception) to spec further
246  
247 =head3 pass
248  
249 $self->pass;
250  
251 Declares that the test run is complete, and declares the result to be a pass, irrespective of what the results of the subtests were.
252  
253 =cut
254  
255 1929 ▶sub pass {
Subroutine pass: called CC=1 SCAR=0.0
Pod documented
256 1929 my ( $self, $message ) = @_;
257 1929 $self->complete( 1, $message );
258 1929 return $self;
259 }
260  
261 =head3 fail
262  
263 $self->fail;
264  
265 Declares that the test run is complete, and declares the result to be a failure, irrespective of what the results of the subtests were.
266  
267 =cut
268  
269 627 ▶sub fail {
Subroutine fail: called CC=1 SCAR=0.0
Pod documented
270 627 my ( $self, $message ) = @_;
271 627 $self->complete( 0, $message );
272 627 return $self;
273 }
274  
275 =head3 diag
276  
277 $self->diag;
278  
279 Declares that the test run is complete, and declares that it is not a result but a diagnostic message, irrespective of what the results of the subtests were.
280  
281 =cut
282  
283 1794 ▶sub diag {
Subroutine diag: called CC=1 SCAR=0.0
Pod documented
284 1794 my ( $self, $message ) = @_;
285 1794 $self->_set_is_info(1);
286 1794 $self->complete( 1, $message );
287 1794 return $self;
288 }
289  
290 =head3 skip
291  
292 $self->skip;
293  
294 Declares that the test run is complete, but that it was skipped.
295  
296 =cut
297  
298 4 ▶sub skip {
Subroutine skip: called CC=1 SCAR=0.0
Pod documented
299 4 my ( $self, $message ) = @_;
300 4 $self->_set_is_skipped(1);
301 4 $self->complete( 1, $message );
302 4 return $self;
303 }
304  
305 =head3 exception
306  
307 $self->exception;
308  
309 Declares that the test run is complete, and declares the result to be an exception, irrespective of what the results of the subtests were.
310  
311 =cut
312  
313 17 ▶sub exception {
Subroutine exception: called CC=1 SCAR=0.0
Pod documented
314 17 my ( $self, $message ) = @_;
315 17 $self->_set_is_exception(1);
316 17 $self->complete( 0, $message );
317 17 return $self;
318 }
319  
320 =head3 inform_formatter
321  
322 $self->inform_formatter;
323  
324 Used internally to send events to the formatter. The two events currently permitted are 'new' and 'done'.
325  
326 =cut
327  
328 21448 ▶sub inform_formatter {
Subroutine inform_formatter: called CC=2 SCAR=6.9
Pod documented
329 21448 my ($self) = @_;
330 21448 my $formatter = $self->formatter;
331 21448 ▶ if ( defined $formatter ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $formatter) 218 21230 21448
332 218 $formatter->event(@_);
333 }
334 }
335  
336 =head3 status
337  
338 $self->status;
339  
340 Useful to summarise the status of the TestRunner. Possible values are: FAIL, PASS, INFO, SKIPPED, EXCEPTION, INCOMPLETE.
341  
342 =cut
343  
344 117 ▶sub status {
Subroutine status: called CC=6 SCAR=17.9
Pod documented
345 117 my ($self) = @_;
346 117 ▶ return 'INCOMPLETE' unless $self->is_complete;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_complete 1 116 117
347 116 ▶ return 'EXCEPTION' if $self->is_exception;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_exception 1 115 116
348 115 ▶ return 'SKIPPED' if $self->is_skipped;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_skipped 1 114 115
349 114 ▶ return 'INFO' if $self->is_info;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_info 2 112 114
350 112 ▶ return 'PASS' if $self->value;
Branch: 1 branch
BranchTrueFalseTotal
if $self->value 111 1 112
351 1 return 'FAIL';
352 }
353  
354 =head3 run_test
355  
356 $self->run_test($test, $proto);
357  
358 This method runs a particular test in the object's script, and returns the subtest. It is called by the C<< Test::Proto::Base::run_tests >> and should only be called by subclasses of L<Test::Proto::Base> which override that method.
359  
360 This is documented for information purposes only and is not intended to be used except in the maintainance of C<Test::Proto> itself.
361  
362 =cut
363  
364 4067 ▶sub run_test {
Subroutine run_test: called CC=10 SCAR=23.1
Pod documented
365 4067 my ( $self, $test, $proto ) = @_;
366 4067 my $runner = $self->subtest(
367 test_case => $test,
368 subject => $self->subject
369 );
370 4067 foreach my $tag ( @{ $self->skipped_tags } ) {
371 2 ▶ if ( $test->has_tag($tag) or $proto->has_tag($tag) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test->has_tag($tag) or $proto->has_tag($tag)) 1 1 2
Condition66%2/3
$test->has_tag($tag) or $proto->has_tag($tag)
l!l&&r!l&&!r
011
MC/DC50%1/2
$test->has_tag($tag) or $proto->has_tag($tag)
$test->has_tag($tag) $proto->has_tag($tag)
Truth table66%2/3
A or B
A= $test->has_tag($tag)B= $proto->has_tag($tag)
ABresult
1X1
011
000
372 1 return $runner->skip( 'Skipping tag ' . $tag );
373 }
374 }
375 4066 foreach my $tag ( @{ $self->required_tags } ) {
376 2 ▶ if ( $test->has_tag($tag) or $proto->has_tag($tag) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test->has_tag($tag) or $proto->has_tag($tag)) 1 1 2
Condition66%2/3
$test->has_tag($tag) or $proto->has_tag($tag)
l!l&&r!l&&!r
011
MC/DC50%1/2
$test->has_tag($tag) or $proto->has_tag($tag)
$test->has_tag($tag) $proto->has_tag($tag)
Truth table66%2/3
A or B
A= $test->has_tag($tag)B= $proto->has_tag($tag)
ABresult
1X1
011
000
377 1 $runner->required_tags( [] );
378 1 last;
379 }
380 }
381 4066 ▶ return $runner->skip( 'None of the required tags (' . join( '', @{ $self->required_tags() } ) . ') found' ) if @{ $self->required_tags() } and @{ $runner->required_tags() };
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->required_tags;} and @{$runner->required_tags;} 1 4065 4066
Condition100%3/3
@{$self->required_tags;} and @{$runner->required_tags;}
!ll&&!rl&&r
406411
MC/DC100%2/2
@{$self->required_tags;} and @{$runner->required_tags;}
@{$self->required_tags;} @{$runner->required_tags;}
Truth table100%3/3
A and B
A= @{$self->required_tags;}B= @{$runner->required_tags;}
ABresult
0X0
100
111
382 4065 my $result = $test->code->($runner);
383 4065 ▶ $runner->exception("Test execution did not complete.") unless $runner->is_complete;
Branch: 1 branch
BranchTrueFalseTotal
unless $runner->is_complete 0 4065 4065
384 4065 return $runner;
385 }
386  
387 =head3 object_id, object_uuid
388  
389 Test::Proto::TestRunner implements L<Object::ID>. This is used by formatters.
390  
391 =cut
392  
393 14 1;
394  
« Proto/Formatter.pm ↑ Summary Proto/Role/Value.pm »