File Coverage

blib/lib/Test2/Formatter/YAMLEnhancedTAP.pm
Criterion Covered Total %
statement 52 52 100.0
branch 14 20 70.0
condition 17 26 65.3
subroutine 7 7 100.0
pod 1 2 50.0
total 91 107 85.0


line stmt bran cond sub pod time code
1             package Test2::Formatter::YAMLEnhancedTAP;
2              
3 1     1   5905 use strict;
  1         2  
  1         44  
4 1     1   5 use warnings;
  1         3  
  1         61  
5 1     1   713 use TAP::Parser::YAMLish::Writer;
  1         3221  
  1         46  
6 1     1   8 use base 'Test2::Formatter::TAP';
  1         4  
  1         520  
7              
8             our $VERSION = '0.0.5';
9              
10             # Private: TAP::Parser::YAMLish::Writer instance to write YAML TAP snippets
11             # it didn't really like YAML::PP in the output for whatever reason
12             my $_yaml_writer = TAP::Parser::YAMLish::Writer->new;
13              
14             sub _yamilify_message {
15 1     1   15 my ($self, $frame, $event, $message) = @_;
16 1         2 my (undef, $filename, $lineno, $caller_class) = @{$frame};
  1         3  
17              
18 1         7 my $yaml = "";
19             # Cleanup comments
20 1         8 $message =~ s/#\s+//gm;
21             # Cleanup extra newlines
22 1         3 chomp($message);
23              
24             # Build the YAML.
25 1         14 $_yaml_writer->write({
26             at => {
27             test_num => 0,
28             filename => $filename,
29             line => $lineno
30             },
31             emitter => $caller_class,
32             message => $message
33             }, \$yaml);
34              
35             # indent two spaces for the TAP parser.
36 1         261 $yaml =~ s/^/ /mg;
37             # add an extra newline for readability
38 1         2 $yaml .= "\n";
39 1         2 return $yaml;
40             }
41              
42             #
43             sub print_optimal_pass {
44 5     5 0 7 my $self = shift;
45 5         19 my $ret = $self->SUPER::print_optimal_pass(@_);
46 5         30 $self->{_optimal_pass_happened} = $ret;
47 5         14 return $ret;
48             }
49              
50             sub write {
51 5     5 1 222060 my ($self, $e, $num, $f) = @_;
52              
53             # The most common case, a pass event with no amnesty and a normal name.
54 5 50       15 return if $self->print_optimal_pass($e, $num);
55              
56 5   33     11 $f ||= $e->facet_data;
57 5         9 my $frame = $f->{trace}{frame};
58              
59 5 50       11 $self->encoding($f->{control}->{encoding}) if $f->{control}->{encoding};
60              
61 5 100       23 my @tap = $self->event_tap($f, $num) or return;
62              
63 4 100       170 $self->{MADE_ASSERTION} = 1 if $f->{assert};
64              
65 4   50     13 my $nesting = $f->{trace}->{nested} || 0;
66 4         6 my $handles = $self->{handles};
67 4         10 my $indent = ' ' x $nesting;
68              
69             # Local is expensive! Only do it if we really need to.
70 4 50 33     17 local ($\, $,) = (undef, '') if $\ || $,;
71              
72 4         6 for my $set (@tap) {
73 4         8 my ($hid, $msg) = @$set;
74 4 50       11 next unless $msg;
75 4 50       14 my $io = $handles->[$hid] or next;
76              
77             print $io "\n"
78             if $ENV{HARNESS_ACTIVE}
79             && $hid == $self->SUPER::OUT_ERR()
80 4 100 66     55 && $self->{_LAST_FH} != $io
      100        
      66        
81             && $msg =~ m/^#\s*Failed( \(TODO\))? test /;
82              
83 4         5 my (undef, $filename, $lineno, $caller_class) = @{$frame};
  4         9  
84              
85 4         9 my $is_comment = $msg =~ m/^#/;
86 4         8 my $is_not_subtest_call = $caller_class ne 'Test::More::subtest';
87 4         6 my $is_failed_msg = $msg =~ m/Looks like you failed/;
88 4         13 my $filename_not_within_t_dir = $filename =~ m/^(t|xt)/;
89              
90 4 100 66     46 $msg = $self->_yamilify_message($frame, $e, $msg)
      100        
      66        
91             if $is_comment
92             && $is_not_subtest_call
93             && !$is_failed_msg
94             && $filename_not_within_t_dir;
95              
96 4 50       10 $msg =~ s/^/$indent/mg if $nesting;
97 4         105 print $io $msg;
98 4         21 $self->{_LAST_FH} = $io;
99             }
100             }
101              
102             1;
103             __END__