lib/Test/BDD/Cucumber/Harness/TermColor.pm

filter: Statementstmt 94.4% 118 / 125 Branchbran 76.6% 23 / 30 Conditioncond 60.7% 17 / 28 MC/DCmcdc 33.3% 5 / 15 Subroutinesub 100% 21 / 21 Podpod 100% 6 / 6 totaltotal 84.4% 190 / 225 CC 32 SCAR 17.7
CC
32
Cov
86%
BEGIN
14.9
_display
9.0
step_done
8.1
CRAP 5.9
« blib/lib/Test/BDD/Cucumber/Parser.pm ↑ Summary blib/lib/Test/BDD/Cucumber/Model/TagSpec.pm »
1 11 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Test::BDD::Cucumber::Harness::TermColor 0.87;
5  
6 =head1 NAME
7  
8 Test::BDD::Cucumber::Harness::TermColor - Prints colorized text to the screen
9  
10 =head1 VERSION
11  
12 version 0.87
13  
14 =head1 DESCRIPTION
15  
16 A L<Test::BDD::Cucumber::Harness> subclass that prints test output, colorized,
17 to the terminal.
18  
19 =head1 CONFIGURABLE ENV
20  
21 =head2 ANSI_COLORS_DISABLED
22  
23 You can use L<Term::ANSIColor>'s C<ANSI_COLORS_DISABLED> to turn off colors
24 in the output.
25  
26 =cut
27  
28 11 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 11 ▶use Types::Standard qw( Str HashRef FileHandle );
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 11 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # Try and make the colors just work on Windows...
34 11 ▶BEGIN {
Subroutine BEGIN: called CC=5 SCAR=27.0
35 11 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /MSWin32/iu and not $ENV{"ANSI_COLORS_DISABLED"} and not $ENV{"DISABLE_WIN32_FALLBACK"}) 0 11 11
Condition33%2/6
$^O =~ /MSWin32/iu and not $ENV{"ANSI_COLORS_DISABLED"} and not $ENV{"DISABLE_WIN32_FALLBACK"}
!ll&&!rl&&r
1100
$^O =~ /MSWin32/iu and not $ENV{"ANSI_COLORS_DISABLED"}
!ll&&!rl&&r
1100
MC/DC0%0/3
$^O =~ /MSWin32/iu and not $ENV{"ANSI_COLORS_DISABLED"} and not $ENV{"DISABLE_WIN32_FALLBACK"}
$^O =~ /MSWin32/iu not $ENV{"ANSI_COLORS_DISABLED"} not $ENV{"DISABLE_WIN32_FALLBACK"}
Truth table25%1/4
A and not(B) and not(C)
A= $^O =~ /MSWin32/iuB= not $ENV{"ANSI_COLORS_DISABLED"}C= not $ENV{"DISABLE_WIN32_FALLBACK"}
ABCresult
0XX0
10X0
1100
1111
36 # We're apparently on Windows
37 $^O =~ /MSWin32/i &&
38  
39 # We haven't disabled coloured output for Term::ANSIColor
40 ( !$ENV{'ANSI_COLORS_DISABLED'} ) &&
41  
42 # Here's a flag you can use if you really really need to turn this fall-
43 # back behaviour off
44 ( !$ENV{'DISABLE_WIN32_FALLBACK'} )
45 )
46 {
47 # Try and load
48 0 eval { require Win32::Console::ANSI };
49 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
50 0 print "# Install Win32::Console::ANSI to display colors properly\n";
51 }
52 }
53 }
54  
55 11 ▶use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 11 ▶use Test::BDD::Cucumber::Model::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 11 extends 'Test::BDD::Cucumber::Harness';
59  
60 =head1 CONFIGURABLE ATTRIBUTES
61  
62 =head2 fh
63  
64 A filehandle to write output to; defaults to C<STDOUT>
65  
66 =cut
67  
68 10 ▶has 'fh' => ( is => 'rw', isa => FileHandle, default => sub { \*STDOUT } );
Subroutine __ANON__: called CC=1 SCAR=0.0
69  
70 =head2 theme
71  
72 Name of the theme to use for colours. Defaults to `dark`. Themes are defined
73 in the private attribute C<_themes>, and currently include `light` and `dark`
74  
75 =cut
76  
77 has theme => (
78 'is' => 'ro',
79 isa => Str,
80 lazy => 1,
81 10 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
82 10 my $theme = 'dark';
83 10 Getopt::Long::Configure('pass_through');
84 10 GetOptions( "c|theme=s" => \$theme );
85 10 return ($theme);
86 }
87 11 );
88  
89 has _themes => (
90 is => 'ro',
91 isa => HashRef[HashRef],
92 lazy => 1,
93 10 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
94 {
95 10 dark => {
96 'feature' => 'bright_white',
97 'scenario' => 'bright_white',
98 'scenario_name' => 'bright_blue',
99 'pending' => 'yellow',
100 'passing' => 'green',
101 'failed' => 'red',
102 'step_data' => 'bright_cyan',
103 },
104 light => {
105 'feature' => 'reset',
106 'scenario' => 'black',
107 'scenario_name' => 'blue',
108 'pending' => 'yellow',
109 'passing' => 'green',
110 'failed' => 'red',
111 'step_data' => 'magenta',
112 },
113 };
114 }
115 11 );
116  
117 853 ▶sub _colors {
Subroutine _colors: called CC=2 SCAR=7.2
118 853 my $self = shift;
119 853 ▶ return $self->_themes->{ $self->theme }
Condition50%1/2
$self->_themes->{$self->theme} || die('Unknown color theme [' . $self->theme . ']')
l!l
8530
MC/DC0%0/1
$self->_themes->{$self->theme} || die('Unknown color theme [' . $self->theme . ']')
$self->_themes->{$self->theme}
Truth table50%1/2
A
A= $self->_themes->{$self->theme}
Aresult
11
00
120 || die( 'Unknown color theme [' . $self->theme . ']' );
121 }
122  
123 11 my $margin = 2;
124 11 my $current_feature;
125  
126 10 ▶sub feature {
Subroutine feature: called CC=3 SCAR=11.2
Pod documented
127 10 my ( $self, $feature ) = @_;
128 10 my $fh = $self->fh;
129  
130 10 $current_feature = $feature;
131 $self->_display(
132 {
133 indent => 0,
134 color => $self->_colors->{'feature'},
135 text => $feature->keyword_original . ': ' . ( $feature->name || '' ),
136 follow_up =>
137 10 ▶ [ map { $_->content } @{ $feature->satisfaction || [] } ],
Condition50%2/4
$feature->name || ''
l!l
100
$feature->satisfaction || []
l!l
100
MC/DC0%0/1
$feature->name || ''
$feature->name
MC/DC0%0/1
$feature->satisfaction || []
$feature->satisfaction
Truth table50%1/2
A
A= $feature->name
Aresult
11
00
Truth table50%1/2
A
A= $feature->satisfaction
Aresult
11
00
138 trailing => 1
139 }
140 );
141 }
142  
143 10 ▶sub feature_done {
Subroutine feature_done: called CC=1 SCAR=0.0
Pod documented
144 10 my $self = shift;
145 10 my $fh = $self->fh;
146 10 print $fh "\n";
147 }
148  
149 69 ▶sub scenario {
Subroutine scenario: called CC=4 SCAR=15.6
Pod documented
150 69 my ( $self, $scenario, $dataset, $longest ) = @_;
151 my $text =
152 $scenario->keyword_original . ': '
153 69 ▶ . color( $self->_colors->{'scenario_name'} )
Condition50%1/2
$scenario->name || ''
l!l
690
MC/DC0%0/1
$scenario->name || ''
$scenario->name
Truth table50%1/2
A
A= $scenario->name
Aresult
11
00
154 . ( $scenario->name || '' );
155  
156 $self->_display(
157 {
158 indent => 2,
159 color => $self->_colors->{'scenario'},
160 text => $text,
161 follow_up =>
162 0 ▶ [ map { $_->content } @{ $scenario->description || [] } ],
Condition50%2/4
$scenario->description || []
l!l
690
$longest || 0
l!l
069
MC/DC0%0/1
$scenario->description || []
$scenario->description
MC/DC0%0/1
$longest || 0
$longest
Truth table50%1/2
A
A= $scenario->description
Aresult
11
00
Truth table50%1/2
A
A= $longest
Aresult
11
00
163 trailing => 0,
164 longest_line => ( $longest || 0 )
165 }
166 );
167 }
168  
169 69 ▶sub scenario_done {
Subroutine scenario_done: called CC=1 SCAR=0.0
Pod documented
170 69 my $self = shift;
171 69 my $fh = $self->fh;
172 69 print $fh "\n";
173 }
174  
175 356 ▶sub step { }
Subroutine step: called CC=1 SCAR=0.0
Pod documented
176  
177 356 ▶sub step_done {
Subroutine step_done: called CC=8 SCAR=20.9
Pod documented
178 356 my ( $self, $context, $result, $highlights ) = @_;
179  
180 356 my $color;
181 356 my $follow_up = [];
182 356 my $status = $result->result;
183 356 my $failed = 0;
184  
185 356 ▶ if ( $status eq 'undefined' || $status eq 'pending' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($status eq 'undefined' or $status eq 'pending') { } 9 347 356
elsif ($status eq 'passing') { } 346 1 347
Condition100%3/3
$status eq "undefined" or $status eq "pending"
l!l&&r!l&&!r
36347
MC/DC100%2/2
$status eq "undefined" or $status eq "pending"
$status eq "undefined" $status eq "pending"
Truth table100%3/3
A or B
A= $status eq "undefined"B= $status eq "pending"
ABresult
1X1
011
000
186 9 $color = $self->_colors->{'pending'};
187 } elsif ( $status eq 'passing' ) {
188 346 $color = $self->_colors->{'passing'};
189 } else {
190 1 $failed = 1;
191 1 $color = $self->_colors->{'failed'};
192 1 $follow_up = [ split( /\n/, $result->{'output'} ) ];
193  
194 1 ▶ if ( !$context->is_hook ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($context->is_hook) 1 0 1
195 1 unshift @{$follow_up},
196 'step defined at '
197 . $context->step->line->document->filename
198 . ' line '
199 . $context->step->line->number . '.';
200 }
201 }
202  
203 356 my $text;
204  
205 356 ▶ if ( $context->is_hook ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($context->is_hook) { } 66 290 356
elsif ($highlights) { } 281 9 290
206 66 ▶ $failed or return;
Branch: 1 branch
BranchTrueFalseTotal
$failed or return 0 66 66
207 0 $text = 'In ' . ucfirst( $context->verb ) . ' Hook';
208 0 undef $highlights;
209 } elsif ($highlights) {
210 281 $text = $context->step->verb_original . ' ' . $context->text;
211 281 $highlights =
212 [ [ 0, $context->step->verb_original . ' ' ], @$highlights ];
213 } else {
214 9 $text = $context->step->verb_original . ' ' . $context->text;
215 9 $highlights = [ [ 0, $text ] ];
216 }
217  
218 $self->_display(
219 {
220 indent => 4,
221 color => $color,
222 text => $text,
223 highlights => $highlights,
224 highlight => $self->_colors->{'step_data'},
225 trailing => 0,
226 follow_up => $follow_up,
227 290 longest_line => $context->stash->{'scenario'}->{'longest_step_line'}
228 }
229 );
230  
231 290 $self->_note_step_data( $context->step );
232 }
233  
234 290 ▶sub _note_step_data {
Subroutine _note_step_data: called CC=6 SCAR=17.9
235 290 my ( $self, $step ) = @_;
236 290 ▶ return unless $step;
Branch: 1 branch
BranchTrueFalseTotal
unless $step 0 290 290
237 290 my @step_data = @{ $step->data_as_strings };
238 290 ▶ return unless @step_data;
Branch: 1 branch
BranchTrueFalseTotal
unless @step_data 277 13 290
239  
240 59 ▶ my $note = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
241 59 my ( $text, $extra_indent ) = @_;
242 59 ▶ $extra_indent ||= 0;
Condition100%2/2
$extra_indent ||= 0
l!l
2534
MC/DC100%1/1
$extra_indent ||= 0
$extra_indent
Truth table100%2/2
A
A= $extra_indent
Aresult
11
00
243  
244 $self->_display(
245 {
246 indent => 6 + $extra_indent,
247 59 color => $self->_colors->{'step_data'},
248 text => $text
249 }
250 );
251 13 };
252  
253 13 ▶ if ( ref( $step->data ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $step->data eq 'ARRAY') { } 4 9 13
254 4 for (@step_data) {
255 16 $note->($_);
256 }
257 } else {
258 9 $note->('"""');
259 9 for (@step_data) {
260 25 $note->( $_, 2 );
261 }
262 9 $note->('"""');
263 }
264 }
265  
266 428 ▶sub _display {
Subroutine _display: called CC=9 SCAR=22.0
267 428 my ( $class, $options ) = @_;
268 428 ▶ my $fh = ref $class ? $class->fh : \*STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 428 0 428
269 428 $options->{'indent'} += $margin;
270  
271 # Reset it all...
272 428 print $fh color 'reset';
273  
274 # Print the main line
275 428 print $fh ' ' x $options->{'indent'};
276  
277 # Highlight as appropriate
278 428 my $color = color $options->{'color'};
279 428 ▶ if ( $options->{'highlight'} && $options->{'highlights'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'highlight'} and $options->{'highlights'}) { } 290 138 428
Condition66%2/3
$options->{"highlight"} and $options->{"highlights"}
!ll&&!rl&&r
1380290
MC/DC50%1/2
$options->{"highlight"} and $options->{"highlights"}
$options->{"highlight"} $options->{"highlights"}
Truth table66%2/3
A and B
A= $options->{"highlight"}B= $options->{"highlights"}
ABresult
0X0
100
111
280 290 my $reset = color 'reset';
281 290 my $base = color $options->{'color'};
282 290 my $hl = color $options->{'highlight'};
283  
284 290 for ( @{ $options->{'highlights'} } ) {
285 1066 my ( $flag, $text ) = @$_;
286 1066 ▶ print $fh $reset . ( $flag ? $hl : $base ) . $text . $reset;
Branch: 1 branch
BranchTrueFalseTotal
$flag ? : 277 789 1066
287 }
288  
289 # Normal output
290 } else {
291 138 print $fh color $options->{'color'};
292 138 print $fh $options->{'text'};
293 }
294  
295 # Reset and newline
296 428 print $fh color 'reset';
297 428 print $fh "\n";
298  
299 # Print follow-up lines...
300 428 ▶ for my $line ( @{ $options->{'follow_up'} || [] } ) {
Condition100%2/2
$options->{'follow_up'} || []
l!l
36959
MC/DC100%1/1
$options->{'follow_up'} || []
$options->{'follow_up'}
Truth table100%2/2
A
A= $options->{'follow_up'}
Aresult
11
00
301 35 print $fh color 'reset';
302 35 print $fh ' ' x ( $options->{'indent'} + 4 );
303 35 print $fh color $options->{'color'};
304 35 print $fh $line;
305 35 print $fh color 'reset';
306 35 print $fh "\n";
307 }
308  
309 428 ▶ print $fh "\n" if $options->{'trailing'};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"trailing"} 10 418 428
310 }
311  
312 =head1 AUTHOR
313  
314 Peter Sergeant C<pete@clueball.com>
315  
316 =head1 LICENSE
317  
318 Copyright 2019-2023, Erik Huelsmann
319 Copyright 2011-2019, Peter Sergeant; Licensed under the same terms as Perl
320  
321 =cut
322  
323 11 1;
« blib/lib/Test/BDD/Cucumber/Parser.pm ↑ Summary blib/lib/Test/BDD/Cucumber/Model/TagSpec.pm »