File Coverage

blib/lib/Test2/Tools/ComboObject.pm
Criterion Covered Total %
statement 75 75 100.0
branch 10 10 100.0
condition 2 2 100.0
subroutine 14 14 100.0
pod 6 8 75.0
total 107 109 98.1


line stmt bran cond sub pod time code
1 1     1   173612 use warnings;
  1         3  
  1         59  
2 1     1   46 use 5.020;
  1         4  
3 1     1   705 use experimental qw( postderef signatures );
  1         4884  
  1         8  
4              
5             package Test2::Tools::ComboObject 0.01 {
6              
7             # ABSTRACT: Combine checks and diagnostics into a single test as an object
8              
9              
10 1     1   279 use Exporter 'import';
  1         2  
  1         86  
11 1     1   6 use Test2::API ();
  1         2  
  1         101  
12             use Class::Tiny qw/ context /, {
13             name => "combo object test",
14             status => 1,
15 19         240 _log => sub { [] },
16 1         11 _count => 0,
17             _done => 0,
18             _extra => 0,
19 1     1   586 };
  1         2574  
20              
21 22     22 0 2982 sub BUILD ($self, $) {
  22         52  
  22         38  
22 22         1240 my %args = ( level => 3 + $self->_extra );
23 22         248 $self->context( Test2::API::context( %args ) );
24             }
25              
26 22     22 0 1606 sub DEMOLISH ($self, $) {
  22         42  
  22         35  
27 22         68 $self->finish;
28             }
29              
30              
31             our @EXPORT = qw( combo );
32              
33             sub combo :prototype(;$) {
34 21   100 21 1 444105 my $name = shift // 'combo object test';
35 21         192 return __PACKAGE__->new( name => $name, _extra => 1 );
36             }
37              
38              
39 41     41 1 295 sub finish ($self) {
  41         68  
  41         61  
40 41 100       937 return $self->status if $self->_done;
41              
42 19         523 $self->_done(1);
43              
44 19 100       583 unless ( $self->_count ) {
45 1         28 push $self->_log->@*, "Test::ComboTest object had no checks";
46 1         21 $self->status(0);
47             }
48              
49 19 100       499 if ( $self->status ) {
50 7 100       236 if ( $self->_log->@* ) {
51 3         92 $self->context->pass( $self->name );
52 3         435 $self->context->note( $_ ) for $self->_log->@*;
53 3         2073 $self->context->release;
54             } else {
55 4         93 $self->context->pass_and_release( $self->name );
56             }
57             } else {
58 12         281 $self->context->fail_and_release( $self->name, $self->_log->@* );
59             }
60              
61 19         5607 return $self->status;
62             }
63              
64              
65 27     27 1 593 sub log ( $self, @messages ) {
  27         120  
  27         81  
  27         81  
66 27         694 push $self->_log->@*, @messages;
67 27         116 return $self;
68             }
69              
70              
71 8     8 1 875 sub pass ( $self, @messages ) {
  8         23  
  8         15  
  8         14  
72 8         32 $self->log(@messages);
73 8         171 $self->_count( $self->_count + 1 );
74 8         87 return $self;
75             }
76              
77              
78 7     7 1 673 sub fail ( $self, @messages ) {
  7         15  
  7         15  
  7         12  
79 7         168 $self->status(0);
80 7         57 $self->log(@messages);
81 7         150 $self->_count( $self->_count + 1 );
82 7         123 return $self;
83             }
84              
85              
86 10     10 1 1165 sub ok ( $self, $status, @messages ) {
  10         20  
  10         22  
  10         21  
  10         18  
87 10 100       202 $self->status(0) unless $status;
88 10         76 $self->log(@messages);
89 10         218 $self->_count( $self->_count + 1 );
90 10         116 return $self;
91             }
92              
93             }
94              
95             1;
96              
97             __END__