1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
package Test::BDD::Cucumber::Harness::TermColor;
$Test::BDD::Cucumber::Harness::TermColor::VERSION = '0.26';
=head1 NAME
Test::BDD::Cucumber::Harness::TermColor - Prints colorized text to the screen
=head1 VERSION
version 0.26
=head1 DESCRIPTION
A L<Test::BDD::Cucumber::Harness> subclass that prints test output, colorized,
to the terminal.
=head1 METHODS
=cut
use strict;
use warnings;
use Moose;
# Try and make the colors just work on Windows...
BEGIN {
if (
# We're apparently on Windows
$^O =~ /MSWin32/i &&
# We haven't disabled coloured output for Term::ANSIColor
( ! $ENV{'ANSI_COLORS_DISABLED'} ) &&
# Here's a flag you can use if you really really need to turn this fall-
# back behaviour off
(! $ENV{'DISABLE_WIN32_FALLBACK'} )
) {
# Try and load
eval { require Win32::Console::ANSI };
if ( $@ ) {
print "# Install Win32::Console::ANSI to display colors properly\n";
}
}
}
use Term::ANSIColor;
use Test::BDD::Cucumber::Model::Result;
extends 'Test::BDD::Cucumber::Harness';
has 'fh' => ( is => 'rw', isa => 'FileHandle', default => sub { \*STDOUT } );
my $margin = 2;
sub BUILD {
my $self = shift;
my $fh = $self->fh;
if ( $margin > 1 ) {
print $fh "\n" x ( $margin - 1 );
}
}
my $current_feature;
sub feature {
my ( $self, $feature ) = @_;
my $fh = $self->fh;
$current_feature = $feature;
$self->_display({
indent => 0,
color => 'bright_white',
text => $feature->name,
follow_up => [ map { $_->content } @{ $feature->satisfaction || [] } ],
trailing => 1
});
}
sub feature_done {
my $self = shift;
my $fh = $self->fh;
print $fh "\n";
}
sub scenario {
my ( $self, $scenario, $dataset, $longest ) = @_;
my $text =
"Scenario: " . color('bright_blue') . ($scenario->name || '' );
$self->_display({
indent => 2,
color => 'bright_white',
text => $text,
follow_up => [],
trailing => 0,
longest_line => ($longest||0)
});
}
sub scenario_done {
my $self = shift;
my $fh = $self->fh;
print $fh "\n";
}
sub step {}
sub step_done {
my ($self, $context, $result, $highlights ) = @_;
my $color;
my $follow_up = [];
my $status = $result->result;
if ( $status eq 'undefined' || $status eq 'pending' ) {
$color = 'yellow';
} elsif ( $status eq 'passing' ) {
$color = 'green';
} else {
$color = 'red';
$follow_up = [ split(/\n/, $result->{'output'} ) ];
if ( ! $context->is_hook )
{
unshift @{ $follow_up },
'step defined at '
. $context->step->line->document->filename
. ' line '
. $context->step->line->number
. '.';
}
}
my $text;
if ( $context->is_hook ) {
$color eq 'red' or return;
$text = 'In ' . ucfirst( $context->verb ) . ' Hook';
undef $highlights;
} elsif ( $highlights ) {
$text = $context->step->verb_original . ' ' . $context->text;
$highlights = [[ 0, $context->step->verb_original . ' ' ], @$highlights];
} else {
$text = $context->step->verb_original . ' ' . $context->text;
$highlights = [[ 0, $text ]];
}
$self->_display({
indent => 4,
color => $color,
text => $text,
highlights => $highlights,
highlight => 'bright_cyan',
trailing => 0,
follow_up => $follow_up,
longest_line => $context->stash->{'scenario'}->{'longest_step_line'}
});
$self->_note_step_data( $context->step );
}
sub _note_step_data {
my ( $self, $step ) = @_;
return unless $step;
my @step_data = @{ $step->data_as_strings };
return unless @step_data;
my $note = sub {
my ( $text, $extra_indent ) = @_;
$extra_indent ||= 0;
$self->_display({
indent => 6 + $extra_indent,
color => 'bright_cyan',
text => $text
});
};
if ( ref( $step->data ) eq 'ARRAY' ) {
for ( @step_data ) {
$note->( $_ );
}
} else {
$note->('"""');
for ( @step_data ) {
$note->( $_, 2 );
}
$note->('"""');
}
}
sub _display {
my ( $class, $options ) = @_;
my $fh = ref $class ? $class->fh : \*STDOUT;
$options->{'indent'} += $margin;
# Reset it all...
print $fh color 'reset';
# Print the main line
print $fh ' ' x $options->{'indent'};
# Highlight as appropriate
my $color = color $options->{'color'};
if ( $options->{'highlight'} && $options->{'highlights'} ) {
my $reset = color 'reset';
my $base = color $options->{'color'};
my $hl = color $options->{'highlight'};
for ( @{$options->{'highlights'}} ) {
my ($flag, $text) = @$_;
print $fh $reset . ( $flag ? $hl : $base ) . $text . $reset;
}
# Normal output
} else {
print $fh color $options->{'color'};
print $fh $options->{'text'};
}
# Reset and newline
print $fh color 'reset';
print $fh "\n";
# Print follow-up lines...
for my $line ( @{ $options->{'follow_up'} || [] } ) {
print $fh color 'reset';
print $fh ' ' x ( $options->{'indent'} + 2 );
print $fh color $options->{'color'};
print $fh $line;
print $fh color 'reset';
print $fh "\n";
}
print $fh "\n" if $options->{'trailing'};
}
=head1 AUTHOR
Peter Sergeant C<pete@clueball.com>
=head1 LICENSE
Copyright 2011-2014, Peter Sergeant; Licensed under the same terms as Perl
=cut
1;
|