File Coverage

blib/lib/TAP/Formatter/Console/Session.pm
Criterion Covered Total %
statement 76 87 87.3
branch 28 34 82.3
condition 8 17 47.0
subroutine 16 17 94.1
pod n/a
total 128 155 82.5


line stmt bran cond sub pod time code
1             package TAP::Formatter::Console::Session;
2              
3 3     85   17 use strict;
  3         5  
  3         97  
4 3     11   14 use warnings;
  3         5  
  3         119  
5              
6 3     3   13 use base 'TAP::Formatter::Session';
  3         5  
  3         1086  
7              
8             my @ACCESSOR;
9              
10             BEGIN {
11 3     3   10 my @CLOSURE_BINDING = qw( header result clear_for_close close_test );
12              
13 3         6 for my $method (@CLOSURE_BINDING) {
14 3     3   18 no strict 'refs';
  3         4  
  3         208  
15             *$method = sub {
16 82     82   70 my $self = shift;
17 82   66     250 return ( $self->{_closures} ||= $self->_closures )->{$method}
18             ->(@_);
19 12         2317 };
20             }
21             }
22              
23             =head1 NAME
24              
25             TAP::Formatter::Console::Session - Harness output delegate for default console output
26              
27             =head1 VERSION
28              
29             Version 3.38
30              
31             =cut
32              
33             our $VERSION = '3.38';
34              
35             =head1 DESCRIPTION
36              
37             This provides console orientated output formatting for TAP::Harness.
38              
39             =cut
40              
41             sub _get_output_result {
42 14     14   14 my $self = shift;
43              
44             my @color_map = (
45 19 100   19   32 { test => sub { $_->is_test && !$_->is_ok },
46             colors => ['red'],
47             },
48 17 100   17   26 { test => sub { $_->is_test && $_->has_skip },
49             colors => [
50             'white',
51             'on_blue'
52             ],
53             },
54 17 100   17   27 { test => sub { $_->is_test && $_->has_todo },
55 14         173 colors => ['yellow'],
56             },
57             );
58              
59 14         31 my $formatter = $self->formatter;
60 14         27 my $parser = $self->parser;
61              
62             return $formatter->_colorizer
63             ? sub {
64 19     19   13 my $result = shift;
65 19         21 for my $col (@color_map) {
66 53         33 local $_ = $result;
67 53 100       63 if ( $col->{test}->() ) {
68 2         5 $formatter->_set_colors( @{ $col->{colors} } );
  2         9  
69 2         23 last;
70             }
71             }
72 19         40 $formatter->_output( $self->_format_for_output($result) );
73 19         220 $formatter->_set_colors('reset');
74             }
75             : sub {
76 3     3   10 $formatter->_output( $self->_format_for_output(shift) );
77 14 100       48 };
78             }
79              
80             sub _closures {
81 14     14   13 my $self = shift;
82              
83 14         26 my $parser = $self->parser;
84 14         24 my $formatter = $self->formatter;
85 14         31 my $pretty = $formatter->_format_name( $self->name );
86 14         22 my $show_count = $self->show_count;
87              
88 14         33 my $really_quiet = $formatter->really_quiet;
89 14         42 my $quiet = $formatter->quiet;
90 14         22 my $verbose = $formatter->verbose;
91 14         39 my $directives = $formatter->directives;
92 14         39 my $failures = $formatter->failures;
93 14         42 my $comments = $formatter->comments;
94              
95 14         22 my $output_result = $self->_get_output_result;
96              
97 14         20 my $output = '_output';
98 14         18 my $plan = '';
99 14         13 my $newline_printed = 0;
100              
101 14         11 my $last_status_printed = 0;
102              
103             return {
104             header => sub {
105 14 100   14   55 $formatter->_output($pretty)
106             unless $really_quiet;
107             },
108              
109             result => sub {
110 54     54   40 my $result = shift;
111              
112 54 50       99 if ( $result->is_bailout ) {
113 0         0 $formatter->_failure_output(
114             "Bailout called. Further testing stopped: "
115             . $result->explanation
116             . "\n" );
117             }
118              
119 54 100       88 return if $really_quiet;
120              
121 39         61 my $is_test = $result->is_test;
122              
123             # These are used in close_test - but only if $really_quiet
124             # is false - so it's safe to only set them here unless that
125             # relationship changes.
126              
127 39 100       54 if ( !$plan ) {
128 10   50     20 my $planned = $parser->tests_planned || '?';
129 10         19 $plan = "/$planned ";
130             }
131 39         90 $output = $formatter->_get_output_method($parser);
132              
133 39 50 33     71 if ( $show_count and $is_test ) {
134 0         0 my $number = $result->number;
135 0         0 my $now = CORE::time;
136              
137             # Print status roughly once per second.
138             # We will always get the first number as a side effect of
139             # $last_status_printed starting with the value 0, which $now
140             # will never be. (Unless someone sets their clock to 1970)
141 0 0       0 if ( $last_status_printed != $now ) {
142 0         0 $formatter->$output("\r$pretty$number$plan");
143 0         0 $last_status_printed = $now;
144             }
145             }
146              
147 39 100 33     188 if (!$quiet
      66        
148             && ( $verbose
149             || ( $is_test && $failures && !$result->is_ok )
150             || ( $comments && $result->is_comment )
151             || ( $directives && $result->has_directive ) )
152             )
153             {
154 22 100       29 unless ($newline_printed) {
155 8         17 $formatter->_output("\n");
156 8         75 $newline_printed = 1;
157             }
158 22         36 $output_result->($result);
159 22         223 $formatter->_output("\n");
160             }
161             },
162              
163             clear_for_close => sub {
164 0     0   0 my $spaces
165             = ' ' x length( '.' . $pretty . $plan . $parser->tests_run );
166 0         0 $formatter->$output("\r$spaces");
167             },
168              
169             close_test => sub {
170 14 50 33 14   34 if ( $show_count && !$really_quiet ) {
171 0         0 $self->clear_for_close;
172 0         0 $formatter->$output("\r$pretty");
173             }
174              
175             # Avoid circular references
176 14         33 $self->parser(undef);
177 14         17 $self->{_closures} = {};
178              
179 14 100       191 return if $really_quiet;
180              
181 11 50       25 if ( my $skip_all = $parser->skip_all ) {
    100          
182 0         0 $formatter->_output("skipped: $skip_all\n");
183             }
184             elsif ( $parser->has_problems ) {
185 5         21 $self->_output_test_failure($parser);
186             }
187             else {
188 6         29 my $time_report = $self->time_report($formatter, $parser);
189 6         20 $formatter->_output( $self->_make_ok_line($time_report) );
190             }
191             },
192 14         231 };
193             }
194              
195             =head2 C<< clear_for_close >>
196              
197             =head2 C<< close_test >>
198              
199             =head2 C<< header >>
200              
201             =head2 C<< result >>
202              
203             =cut
204              
205             1;