| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package TAPx::Harness::Color; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 3 |  |  | 3 |  | 3976 | use strict; | 
|  | 3 |  |  | 0 |  | 13 |  | 
|  | 3 |  |  |  |  | 2055 |  | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 4 | 3 |  |  | 3 |  | 19 | use warnings; | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 160 |  | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 3 |  |  | 3 |  | 16 | use TAPx::Parser; | 
|  | 3 |  |  |  |  | 5 |  | 
|  | 3 |  |  |  |  | 7317 |  | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 7 | 3 |  |  | 3 |  | 29 | use TAPx::Harness; | 
|  | 3 |  |  |  |  | 5 |  | 
|  | 3 |  |  |  |  | 138 |  | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 3 |  |  | 3 |  | 17 | use vars qw($VERSION @ISA); | 
|  | 3 |  |  |  |  | 9 |  | 
|  | 3 |  |  |  |  | 1494 |  | 
| 10 |  |  |  |  |  |  | @ISA = 'TAPx::Harness'; | 
| 11 |  |  |  |  |  |  |  | 
| 12 | 3 |  |  | 3 |  | 292 | use constant IS_WIN32 => ( $^O =~ /^(MS)?Win32$/ ); | 
|  | 3 |  |  |  |  | 9 |  | 
|  | 3 |  |  |  |  | 1015 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | my $NO_COLOR; | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | BEGIN { | 
| 17 | 3 |  |  | 3 |  | 2094 | $NO_COLOR = 0; | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 3 | 50 |  |  |  | 18 | if (IS_WIN32) { | 
| 20 | 1 |  |  |  |  | 163 | eval 'use Win32::Console'; | 
| 21 | 1 | 50 |  |  |  | 9 | if ($@) { | 
|  |  | 0 |  |  |  |  |  | 
| 22 | 1 |  |  |  |  | 3 | $NO_COLOR = $@; | 
| 23 |  |  |  |  |  |  | } | 
| 24 |  |  |  |  |  |  | else { | 
| 25 | 0 |  |  |  |  | 0 | my $console = Win32::Console->new( STD_OUTPUT_HANDLE() ); | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | # eval here because we might not know about these variables | 
| 28 | 0 |  |  |  |  | 0 | my $fg = eval '$FG_LIGHTGRAY'; | 
| 29 | 0 |  |  |  |  | 0 | my $bg = eval '$BG_BLACK'; | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | *_set_color = sub { | 
| 32 | 0 |  |  |  |  | 0 | my $self  = shift; | 
| 33 | 0 |  |  |  |  | 0 | my $color = shift; | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 0 |  |  |  |  | 0 | my $var; | 
| 36 | 0 | 0 |  |  |  | 0 | if ( $color eq 'reset' ) { | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
| 37 | 0 |  |  |  |  | 0 | $fg = eval '$FG_LIGHTGRAY'; | 
| 38 | 0 |  |  |  |  | 0 | $bg = eval '$BG_BLACK'; | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  | elsif ( $color =~ /^on_(.+)$/ ) { | 
| 41 | 0 |  |  |  |  | 0 | $bg = eval '$BG_' . uc($1); | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  | else { | 
| 44 | 0 |  |  |  |  | 0 | $fg = eval '$FG_' . uc($color); | 
| 45 |  |  |  |  |  |  | } | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | # In case of colors that aren't defined | 
| 48 | 0 | 0 | 0 |  |  | 0 | $self->_set_color('reset') | 
|  |  | 0 |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | unless defined $bg && defined $fg; | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 0 |  |  |  |  | 0 | $console->Attr( $bg | $fg ); | 
| 52 | 0 |  |  |  |  | 0 | }; | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | # Not sure if we'll have buffering problems using print instead | 
| 55 |  |  |  |  |  |  | # of $console->Write(). Don't want to override output unnecessarily | 
| 56 |  |  |  |  |  |  | # though and it /seems/ to work OK. | 
| 57 |  |  |  |  |  |  | # | 
| 58 |  |  |  |  |  |  | # *output = sub { | 
| 59 |  |  |  |  |  |  | #     my $self = shift; | 
| 60 |  |  |  |  |  |  | #     $console->Write($_) for @_; | 
| 61 |  |  |  |  |  |  | #     #print @_; | 
| 62 |  |  |  |  |  |  | # }; | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  | else { | 
| 66 | 3 |  |  | 3 |  | 1013 | eval 'use Term::ANSIColor'; | 
|  | 2 |  |  |  |  | 7013 |  | 
|  | 2 |  |  |  |  | 38587 |  | 
|  | 2 |  |  |  |  | 186 |  | 
| 67 | 3 | 100 |  |  |  | 28 | if ($@) { | 
| 68 | 0 |  |  |  |  | 0 | $NO_COLOR = $@; | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  | else { | 
| 71 |  |  |  |  |  |  | *_set_color = sub { | 
| 72 | 0 |  |  | 0 |  | 0 | my $self  = shift; | 
| 73 | 0 |  |  |  |  | 0 | my $color = shift; | 
| 74 | 0 |  |  |  |  | 0 | $self->output( color($color) ); | 
| 75 | 3 |  |  |  |  | 874 | }; | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 | 2 | 50 |  |  |  | 11831 | if ($NO_COLOR) { | 
|  |  | 0 |  |  |  |  |  | 
| 80 | 0 |  |  |  |  | 0 | *_set_color = sub { }; | 
|  | 0 |  |  |  |  | 0 |  | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | =head1 NAME | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | TAPx::Harness::Color - Run Perl test scripts with color | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | =head1 VERSION | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | Version 0.50_07 | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | =cut | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | $VERSION = '0.50_07'; | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | Note that this harness is I.  You may not like the colors I've | 
| 99 |  |  |  |  |  |  | chosen and I haven't yet provided an easy way to override them. | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | This test harness is the same as C, but test results are output | 
| 102 |  |  |  |  |  |  | in color.  Passing tests are printed in green.  Failing tests are in red. | 
| 103 |  |  |  |  |  |  | Skipped tests are blue on a white background and TODO tests are printed in | 
| 104 |  |  |  |  |  |  | white. | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | If C cannot be found or if running under Windows, tests will | 
| 107 |  |  |  |  |  |  | be run without color. | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | use TAPx::Harness::Color; | 
| 112 |  |  |  |  |  |  | my $harness = TAPx::Harness::Color->new( \%args ); | 
| 113 |  |  |  |  |  |  | $harness->runtests(@tests); | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | =head1 METHODS | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | =head2 Class methods | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | =head3 C | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | my %args = ( | 
| 122 |  |  |  |  |  |  | verbose => 1, | 
| 123 |  |  |  |  |  |  | lib     => [ 'lib', 'blib/lib' ], | 
| 124 |  |  |  |  |  |  | shuffle => 0, | 
| 125 |  |  |  |  |  |  | ) | 
| 126 |  |  |  |  |  |  | my $harness = TAPx::Harness::Color->new( \%args ); | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | The constructor returns a new C object.  If | 
| 129 |  |  |  |  |  |  | C is not installed, returns a C object.  See | 
| 130 |  |  |  |  |  |  | C for more details. | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | =cut | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | sub new { | 
| 135 | 11 |  |  | 11 | 1 | 8165 | my $class = shift; | 
|  |  |  |  |  | 1 |  |  | 
| 136 | 11 | 50 |  |  |  | 32 | if ($NO_COLOR) { | 
| 137 | 0 |  |  |  |  | 0 | warn "Cannot run tests in color: $NO_COLOR"; | 
| 138 | 0 |  |  |  |  | 0 | return TAPx::Harness->new(@_); | 
| 139 |  |  |  |  |  |  | } | 
| 140 | 11 |  |  |  |  | 57 | return $class->SUPER::new(@_); | 
| 141 |  |  |  |  |  |  | } | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | ############################################################################## | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | =head3 C | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | $harness->failure_output(@list_of_strings_to_output); | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | Overrides L C to output failure information in | 
| 150 |  |  |  |  |  |  | red. | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | =cut | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | sub failure_output { | 
| 155 | 1 |  |  | 1 | 1 | 753 | my $self = shift; | 
| 156 | 1 |  |  |  |  | 5 | $self->_set_colors('red'); | 
| 157 | 1 |  |  |  |  | 7 | my $out = join( '', @_ ); | 
| 158 | 1 |  |  |  |  | 32 | my $has_newline = chomp $out; | 
| 159 | 0 |  |  |  |  |  | $self->output($out); | 
| 160 | 0 |  |  |  |  |  | $self->_set_colors('reset'); | 
| 161 | 0 | 50 |  |  |  |  | $self->output($/) | 
| 162 |  |  |  |  |  |  | if $has_newline; | 
| 163 |  |  |  |  |  |  | } | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | # Set terminal color | 
| 166 |  |  |  |  |  |  | sub _set_colors { | 
| 167 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 168 | 0 |  |  |  |  |  | for my $color (@_) { | 
| 169 | 0 |  |  |  |  |  | $self->_set_color($color); | 
| 170 |  |  |  |  |  |  | } | 
| 171 |  |  |  |  |  |  | } | 
| 172 |  |  |  |  |  |  |  | 
| 173 |  |  |  |  |  |  | sub _process { | 
| 174 | 0 |  |  | 0 |  |  | my ( $self, $parser, $result ) = @_; | 
| 175 | 0 |  |  |  |  |  | $self->_set_colors('reset'); | 
| 176 | 0 | 0 |  |  |  |  | return unless $self->_should_display( $parser, $result ); | 
| 177 |  |  |  |  |  |  |  | 
| 178 | 0 | 0 |  |  |  |  | if ( $result->is_test ) { | 
| 179 | 0 | 0 |  |  |  |  | if ( !$result->is_ok ) {    # even if it's TODO | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
| 180 | 0 |  |  |  |  |  | $self->_set_colors('red'); | 
| 181 |  |  |  |  |  |  | } | 
| 182 |  |  |  |  |  |  | elsif ( $result->has_skip ) { | 
| 183 | 0 |  |  |  |  |  | $self->_set_colors( 'white', 'on_blue' ); | 
| 184 |  |  |  |  |  |  |  | 
| 185 |  |  |  |  |  |  | } | 
| 186 |  |  |  |  |  |  | elsif ( $result->has_todo ) { | 
| 187 | 0 |  |  |  |  |  | $self->_set_colors('white'); | 
| 188 |  |  |  |  |  |  | } | 
| 189 |  |  |  |  |  |  | } | 
| 190 | 0 |  |  |  |  |  | $self->output( $result->as_string ); | 
| 191 | 0 |  |  |  |  |  | $self->_set_colors('reset'); | 
| 192 | 0 |  |  |  |  |  | $self->output("\n"); | 
| 193 |  |  |  |  |  |  | } | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | 1; |