Perlito5X/Test2/Formatter/TAP.pm untested

filter: Statementstmt 0.0% 0 / 222 Branchbran 0.0% 0 / 84 Conditioncond 0.0% 0 / 59 MC/DCmcdc 0.0% 0 / 37 Subroutinesub 0.0% 0 / 30 Podpod 0.0% 0 / 22 totaltotal 0.0% 0 / 454 CC 42 SCAR 36.0
CC
42
Cov
0%
event_other
110.0
event_ok
72.0
write
42.0
CRAP 36.6
1 package Test2::Formatter::TAP;
2 use strict;
3 use warnings;
4 require PerlIO;
5  
6 our $VERSION = '1.302073';
7  
8 use Test2::Util::HashBase qw{
9 no_numbers handles _encoding
10 };
11  
12 sub OUT_STD() { 0 }
13 sub OUT_ERR() { 1 }
14  
15 use Carp qw/croak/;
16  
17 BEGIN { require Test2::Formatter; our @ISA = qw(Test2::Formatter) }
18  
19 my %CONVERTERS = (
20 'Test2::Event::Ok' => 'event_ok',
21 'Test2::Event::Skip' => 'event_skip',
22 'Test2::Event::Note' => 'event_note',
23 'Test2::Event::Diag' => 'event_diag',
24 'Test2::Event::Bail' => 'event_bail',
25 'Test2::Event::Exception' => 'event_exception',
26 'Test2::Event::Subtest' => 'event_subtest',
27 'Test2::Event::Plan' => 'event_plan',
28 'Test2::Event::TAP::Version' => 'event_version',
29 );
30  
31 # Initial list of converters are safe for direct hash access cause we control them.
32 my %SAFE_TO_ACCESS_HASH = %CONVERTERS;
33  
34 sub register_event {
35 my $class = shift;
36 my ($type, $convert) = @_;
37 croak "Event type is a required argument" unless $type;
38 croak "Event type '$type' already registered" if $CONVERTERS{$type};
39 croak "The second argument to register_event() must be a code reference or method name"
40 unless $convert && (ref($convert) eq 'CODE' || $class->can($convert));
41 $CONVERTERS{$type} = $convert;
42 }
43  
44 _autoflush(\*STDOUT);
45 _autoflush(\*STDERR);
46  
47 sub init {
48 my $self = shift;
49  
50 $self->{+HANDLES} ||= $self->_open_handles;
51 if(my $enc = delete $self->{encoding}) {
52 $self->encoding($enc);
53 }
54 }
55  
56 sub hide_buffered { 1 }
57  
58 sub encoding {
59 my $self = shift;
60  
61 if (@_) {
62 my ($enc) = @_;
63 my $handles = $self->{+HANDLES};
64  
65 # https://rt.perl.org/Public/Bug/Display.html?id=31923
66 # If utf8 is requested we use ':utf8' instead of ':encoding(utf8)' in
67 # order to avoid the thread segfault.
68 if ($enc =~ m/^utf-?8$/i) {
69 binmode($_, ":utf8") for @$handles;
70 }
71 else {
72 binmode($_, ":encoding($enc)") for @$handles;
73 }
74 $self->{+_ENCODING} = $enc;
75 }
76  
77 return $self->{+_ENCODING};
78 }
79  
80 if ($^C) {
81 no warnings 'redefine';
82 *write = sub {};
83 }
84 sub write {
85 my ($self, $e, $num) = @_;
86  
87 my $type = ref($e);
88  
89 my $converter = $CONVERTERS{$type} || 'event_other';
90 my @tap = $self->$converter($e, $self->{+NO_NUMBERS} ? undef : $num) or return;
91  
92 my $handles = $self->{+HANDLES};
93 my $nesting = ($SAFE_TO_ACCESS_HASH{$type} ? $e->{nested} : $e->nested) || 0;
94 my $indent = ' ' x $nesting;
95  
96 # Local is expensive! Only do it if we really need to.
97 local($\, $,) = (undef, '') if $\ || $,;
98 for my $set (@tap) {
99 no warnings 'uninitialized';
100 my ($hid, $msg) = @$set;
101 next unless $msg;
102 my $io = $handles->[$hid] or next;
103  
104 $msg =~ s/^/$indent/mg if $nesting;
105 print $io $msg;
106 }
107 }
108  
109 sub _open_handles {
110 my $self = shift;
111  
112 my %seen;
113 open(my $out, '>&', STDOUT) or die "Can't dup STDOUT: $!";
114 binmode($out, join(":", "", "raw", grep { $_ ne 'unix' and !$seen{$_}++ } PerlIO::get_layers(STDOUT)));
115  
116 %seen = ();
117 open(my $err, '>&', STDERR) or die "Can't dup STDERR: $!";
118 binmode($err, join(":", "", "raw", grep { $_ ne 'unix' and !$seen{$_}++ } PerlIO::get_layers(STDERR)));
119  
120 _autoflush($out);
121 _autoflush($err);
122  
123 return [$out, $err];
124 }
125  
126 sub _autoflush {
127 my($fh) = pop;
128 my $old_fh = select $fh;
129 $| = 1;
130 select $old_fh;
131 }
132  
133 sub event_tap {
134 my $self = shift;
135 my ($e, $num) = @_;
136  
137 my $converter = $CONVERTERS{ref($e)} or return;
138  
139 $num = undef if $self->{+NO_NUMBERS};
140  
141 return $self->$converter($e, $num);
142 }
143  
144 sub event_ok {
145 my $self = shift;
146 my ($e, $num) = @_;
147  
148 # We use direct hash access for performance. OK events are so common we
149 # need this to be fast.
150 my ($name, $todo) = @{$e}{qw/name todo/};
151 my $in_todo = defined($todo);
152  
153 my $out = "";
154 $out .= "not " unless $e->{pass};
155 $out .= "ok";
156 $out .= " $num" if defined($num);
157  
158 # The regex form is ~250ms, the index form is ~50ms
159 my @extra;
160 defined($name) && (
161 (index($name, "\n") != -1 && (($name, @extra) = split(/\n\r?/, $name, -1))),
162 ((index($name, "#" ) != -1 || substr($name, -1) eq '\\') && (($name =~ s|\\|\\\\|g), ($name =~ s|#|\\#|g)))
163 );
164  
165 my $space = @extra ? ' ' x (length($out) + 2) : '';
166  
167 $out .= " - $name" if defined $name;
168 $out .= " # TODO" if $in_todo;
169 $out .= " $todo" if defined($todo) && length($todo);
170  
171 # The primary line of TAP, if the test passed this is all we need.
172 return([OUT_STD, "$out\n"]) unless @extra;
173  
174 return $self->event_ok_multiline($out, $space, @extra);
175 }
176  
177 sub event_ok_multiline {
178 my $self = shift;
179 my ($out, $space, @extra) = @_;
180  
181 return(
182 [OUT_STD, "$out\n"],
183 map {[OUT_STD, "#${space}$_\n"]} @extra,
184 );
185 }
186  
187 sub event_skip {
188 my $self = shift;
189 my ($e, $num) = @_;
190  
191 my $name = $e->name;
192 my $reason = $e->reason;
193 my $todo = $e->todo;
194  
195 my $out = "";
196 $out .= "not " unless $e->{pass};
197 $out .= "ok";
198 $out .= " $num" if defined $num;
199 $out .= " - $name" if $name;
200 if (defined($todo)) {
201 $out .= " # TODO & SKIP"
202 }
203 else {
204 $out .= " # skip";
205 }
206 $out .= " $reason" if defined($reason) && length($reason);
207  
208 return([OUT_STD, "$out\n"]);
209 }
210  
211 sub event_note {
212 my $self = shift;
213 my ($e, $num) = @_;
214  
215 chomp(my $msg = $e->message);
216 $msg =~ s/^/# /;
217 $msg =~ s/\n/\n# /g;
218  
219 return [OUT_STD, "$msg\n"];
220 }
221  
222 sub event_diag {
223 my $self = shift;
224 my ($e, $num) = @_;
225  
226 chomp(my $msg = $e->message);
227 $msg =~ s/^/# /;
228 $msg =~ s/\n/\n# /g;
229  
230 return [OUT_ERR, "$msg\n"];
231 }
232  
233 sub event_bail {
234 my $self = shift;
235 my ($e, $num) = @_;
236  
237 return if $e->nested;
238  
239 return [
240 OUT_STD,
241 "Bail out! " . $e->reason . "\n",
242 ];
243 }
244  
245 sub event_exception {
246 my $self = shift;
247 my ($e, $num) = @_;
248 return [ OUT_ERR, $e->error ];
249 }
250  
251 sub event_subtest {
252 my $self = shift;
253 my ($e, $num) = @_;
254  
255 # A 'subtest' is a subclass of 'ok'. Let the code that renders 'ok' render
256 # this event.
257 my ($ok, @diag) = $self->event_ok($e, $num);
258  
259 # If the subtest is not buffered then the sub-events have already been
260 # rendered, we can go ahead and return.
261 return ($ok, @diag) unless $e->buffered;
262  
263 # In a verbose harness we indent the diagnostics from the 'Ok' event since
264 # they will appear inside the subtest braces. This helps readability. In a
265 # non-verbose harness we do not do this because it is less readable.
266 if ($ENV{HARNESS_IS_VERBOSE}) {
267 # index 0 is the filehandle, index 1 is the message we want to indent.
268 $_->[1] =~ s/^(.*\S.*)$/ $1/mg for @diag;
269 }
270  
271 # Add the trailing ' {' to the 'ok' line of TAP output.
272 $ok->[1] =~ s/\n/ {\n/;
273  
274 # Render the sub-events, we use our own counter for these.
275 my $count = 0;
276 my @subs = map {
277 # Bump the count for any event that should bump it.
278 $count++ if $_->increments_count;
279  
280 # This indents all output lines generated for the sub-events.
281 # index 0 is the filehandle, index 1 is the message we want to indent.
282 map { $_->[1] =~ s/^(.*\S.*)$/ $1/mg; $_ } $self->event_tap($_, $count);
283 } @{$e->subevents};
284  
285 return (
286 $ok, # opening ok - name {
287 @diag, # diagnostics if the subtest failed
288 @subs, # All the inner-event lines
289 [OUT_STD(), "}\n"], # } (closing brace)
290 );
291 }
292  
293 sub event_plan {
294 my $self = shift;
295 my ($e, $num) = @_;
296  
297 my $directive = $e->directive;
298 return if $directive && $directive eq 'NO PLAN';
299  
300 my $reason = $e->reason;
301 $reason =~ s/\n/\n# /g if $reason;
302  
303 my $plan = "1.." . $e->max;
304 if ($directive) {
305 $plan .= " # $directive";
306 $plan .= " $reason" if defined $reason;
307 }
308  
309 return [OUT_STD, "$plan\n"];
310 }
311  
312 sub event_version {
313 my $self = shift;
314 my ($e, $num) = @_;
315  
316 my $version = $e->version;
317  
318 return [OUT_STD, "TAP version $version\n"];
319 }
320  
321 sub event_other {
322 my $self = shift;
323 my ($e, $num) = @_;
324 return if $e->no_display;
325  
326 my @out;
327  
328 if (my ($max, $directive, $reason) = $e->sets_plan) {
329 my $plan = "1..$max";
330 $plan .= " # $directive" if $directive;
331 $plan .= " $reason" if defined $reason;
332 push @out => [OUT_STD, "$plan\n"];
333 }
334  
335 if ($e->increments_count) {
336 my $ok = "";
337 $ok .= "not " if $e->causes_fail;
338 $ok .= "ok";
339 $ok .= " $num" if defined($num);
340 $ok .= " - " . $e->summary if $e->summary;
341  
342 push @out => [OUT_STD, "$ok\n"];
343 }
344 else { # Comment
345 my $handle = ($e->causes_fail || $e->diagnostics) ? OUT_ERR : OUT_STD;
346 my $summary = $e->summary || ref($e);
347 chomp($summary);
348 $summary =~ s/^/# /smg;
349 push @out => [$handle, "$summary\n"];
350 }
351  
352 return @out;
353 }
354  
355 1;
356  
357 __END__