Perlito5X/Test2/API/Context.pm untested

filter: Statementstmt 0.0% 0 / 197 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 43 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 0.0% 0 / 32 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 390 CC 35 SCAR 32.2
CC
35
Cov
0%
init
56.0
DESTROY
56.0
release
56.0
CRAP 25.2
1 package Test2::API::Context;
2 use strict;
3 use warnings;
4  
5 our $VERSION = '1.302073';
6  
7  
8 use Carp qw/confess croak longmess/;
9 use Scalar::Util qw/weaken blessed/;
10 use Test2::Util qw/get_tid try pkg_to_file get_tid/;
11  
12 use Test2::Util::Trace();
13 use Test2::API();
14  
15 # Preload some key event types
16 my %LOADED = (
17 map {
18 my $pkg = "Test2::Event::$_";
19 my $file = "Test2/Event/$_.pm";
20 require $file unless $INC{$file};
21 ( $pkg => $pkg, $_ => $pkg )
22 } qw/Ok Diag Note Info Plan Bail Exception Waiting Skip Subtest/
23 );
24  
25 use Test2::Util::ExternalMeta qw/meta get_meta set_meta delete_meta/;
26 use Test2::Util::HashBase qw{
27 stack hub trace _on_release _depth _is_canon _is_spawn _aborted
28 errno eval_error child_error thrown
29 };
30  
31 # Private, not package vars
32 # It is safe to cache these.
33 my $ON_RELEASE = Test2::API::_context_release_callbacks_ref();
34 my $CONTEXTS = Test2::API::_contexts_ref();
35  
36 sub init {
37 my $self = shift;
38  
39 confess "The 'trace' attribute is required"
40 unless $self->{+TRACE};
41  
42 confess "The 'hub' attribute is required"
43 unless $self->{+HUB};
44  
45 $self->{+_DEPTH} = 0 unless defined $self->{+_DEPTH};
46  
47 $self->{+ERRNO} = $! unless exists $self->{+ERRNO};
48 $self->{+EVAL_ERROR} = $@ unless exists $self->{+EVAL_ERROR};
49 $self->{+CHILD_ERROR} = $? unless exists $self->{+CHILD_ERROR};
50 }
51  
52 sub snapshot { bless {%{$_[0]}, _is_canon => undef, _is_spawn => undef, _aborted => undef}, __PACKAGE__ }
53  
54 sub restore_error_vars {
55 my $self = shift;
56 ($!, $@, $?) = @$self{+ERRNO, +EVAL_ERROR, +CHILD_ERROR};
57 }
58  
59 sub DESTROY {
60 return unless $_[0]->{+_IS_CANON} || $_[0]->{+_IS_SPAWN};
61 return if $_[0]->{+_ABORTED} && ${$_[0]->{+_ABORTED}};
62 my ($self) = @_;
63  
64 my $hub = $self->{+HUB};
65 my $hid = $hub->{hid};
66  
67 # Do not show the warning if it looks like an exception has been thrown, or
68 # if the context is not local to this process or thread.
69 {
70 # Sometimes $@ is uninitialized, not a problem in this case so do not
71 # show the warning about using eq.
72 no warnings 'uninitialized';
73 if($self->{+EVAL_ERROR} eq $@ && $hub->is_local) {
74 my $frame = $self->{+_IS_SPAWN} || $self->{+TRACE}->frame;
75 warn <<" EOT";
76 A context appears to have been destroyed without first calling release().
77 Based on \$@ it does not look like an exception was thrown (this is not always
78 a reliable test)
79  
80 This is a problem because the global error variables (\$!, \$@, and \$?) will
81 not be restored. In addition some release callbacks will not work properly from
82 inside a DESTROY method.
83  
84 Here are the context creation details, just in case a tool forgot to call
85 release():
86 File: $frame->[1]
87 Line: $frame->[2]
88 Tool: $frame->[3]
89  
90 Cleaning up the CONTEXT stack...
91 EOT
92 }
93 }
94  
95 return if $self->{+_IS_SPAWN};
96  
97 # Remove the key itself to avoid a slow memory leak
98 delete $CONTEXTS->{$hid};
99 $self->{+_IS_CANON} = undef;
100  
101 if (my $cbk = $self->{+_ON_RELEASE}) {
102 $_->($self) for reverse @$cbk;
103 }
104 if (my $hcbk = $hub->{_context_release}) {
105 $_->($self) for reverse @$hcbk;
106 }
107 $_->($self) for reverse @$ON_RELEASE;
108 }
109  
110 # release exists to implement behaviors like die-on-fail. In die-on-fail you
111 # want to die after a failure, but only after diagnostics have been reported.
112 # The ideal time for the die to happen is when the context is released.
113 # Unfortunately die does not work in a DESTROY block.
114 sub release {
115 my ($self) = @_;
116  
117 ($!, $@, $?) = @$self{+ERRNO, +EVAL_ERROR, +CHILD_ERROR} and return if $self->{+THROWN};
118  
119 ($!, $@, $?) = @$self{+ERRNO, +EVAL_ERROR, +CHILD_ERROR} and return $self->{+_IS_SPAWN} = undef
120 if $self->{+_IS_SPAWN};
121  
122 croak "release() should not be called on context that is neither canon nor a child"
123 unless $self->{+_IS_CANON};
124  
125 my $hub = $self->{+HUB};
126 my $hid = $hub->{hid};
127  
128 croak "context thinks it is canon, but it is not"
129 unless $CONTEXTS->{$hid} && $CONTEXTS->{$hid} == $self;
130  
131 # Remove the key itself to avoid a slow memory leak
132 $self->{+_IS_CANON} = undef;
133 delete $CONTEXTS->{$hid};
134  
135 if (my $cbk = $self->{+_ON_RELEASE}) {
136 $_->($self) for reverse @$cbk;
137 }
138 if (my $hcbk = $hub->{_context_release}) {
139 $_->($self) for reverse @$hcbk;
140 }
141 $_->($self) for reverse @$ON_RELEASE;
142  
143 # Do this last so that nothing else changes them.
144 # If one of the hooks dies then these do not get restored, this is
145 # intentional
146 ($!, $@, $?) = @$self{+ERRNO, +EVAL_ERROR, +CHILD_ERROR};
147  
148 return;
149 }
150  
151 sub do_in_context {
152 my $self = shift;
153 my ($sub, @args) = @_;
154  
155 # We need to update the pid/tid and error vars.
156 my $clone = $self->snapshot;
157 @$clone{+ERRNO, +EVAL_ERROR, +CHILD_ERROR} = ($!, $@, $?);
158 $clone->{+TRACE} = $clone->{+TRACE}->snapshot;
159 $clone->{+TRACE}->set_pid($$);
160 $clone->{+TRACE}->set_tid(get_tid());
161  
162 my $hub = $clone->{+HUB};
163 my $hid = $hub->hid;
164  
165 my $old = $CONTEXTS->{$hid};
166  
167 $clone->{+_IS_CANON} = 1;
168 $CONTEXTS->{$hid} = $clone;
169 weaken($CONTEXTS->{$hid});
170 my ($ok, $err) = &try($sub, @args);
171 my ($rok, $rerr) = try { $clone->release };
172 delete $clone->{+_IS_CANON};
173  
174 if ($old) {
175 $CONTEXTS->{$hid} = $old;
176 weaken($CONTEXTS->{$hid});
177 }
178 else {
179 delete $CONTEXTS->{$hid};
180 }
181  
182 die $err unless $ok;
183 die $rerr unless $rok;
184 }
185  
186 sub done_testing {
187 my $self = shift;
188 $self->hub->finalize($self->trace, 1);
189 return;
190 }
191  
192 sub throw {
193 my ($self, $msg) = @_;
194 $self->{+THROWN} = 1;
195 ${$self->{+_ABORTED}}++ if $self->{+_ABORTED};
196 $self->release if $self->{+_IS_CANON} || $self->{+_IS_SPAWN};
197 $self->trace->throw($msg);
198 }
199  
200 sub alert {
201 my ($self, $msg) = @_;
202 $self->trace->alert($msg);
203 }
204  
205 sub send_event {
206 my $self = shift;
207 my $event = shift;
208 my %args = @_;
209  
210 my $pkg = $LOADED{$event} || $self->_parse_event($event);
211  
212 my $e = $pkg->new(
213 trace => $self->{+TRACE}->snapshot,
214 %args,
215 );
216  
217 ${$self->{+_ABORTED}}++ if $self->{+_ABORTED} && defined $e->terminate;
218 $self->{+HUB}->send($e);
219 }
220  
221 sub build_event {
222 my $self = shift;
223 my $event = shift;
224 my %args = @_;
225  
226 my $pkg = $LOADED{$event} || $self->_parse_event($event);
227  
228 $pkg->new(
229 trace => $self->{+TRACE}->snapshot,
230 %args,
231 );
232 }
233  
234 sub ok {
235 my $self = shift;
236 my ($pass, $name, $on_fail) = @_;
237  
238 my $hub = $self->{+HUB};
239  
240 my $e = bless {
241 trace => bless( {%{$self->{+TRACE}}}, 'Test2::Util::Trace'),
242 pass => $pass,
243 name => $name,
244 }, 'Test2::Event::Ok';
245 $e->init;
246  
247 $hub->send($e);
248 return $e if $pass;
249  
250 $self->failure_diag($e);
251  
252 if ($on_fail && @$on_fail) {
253 for my $of (@$on_fail) {
254 if (ref($of) eq 'CODE' || (blessed($of) && $of->can('render'))) {
255 $self->info($of, diagnostics => 1);
256 }
257 else {
258 $self->diag($of);
259 }
260 }
261 }
262  
263 return $e;
264 }
265  
266 sub failure_diag {
267 my $self = shift;
268 my ($e) = @_;
269  
270 # This behavior is inherited from Test::Builder which injected a newline at
271 # the start of the first diagnostics when the harness is active, but not
272 # verbose. This is important to keep the diagnostics from showing up
273 # appended to the existing line, which is hard to read. In a verbose
274 # harness there is no need for this.
275 my $prefix = $ENV{HARNESS_ACTIVE} && !$ENV{HARNESS_IS_VERBOSE} ? "\n" : "";
276  
277 # Figure out the debug info, this is typically the file name and line
278 # number, but can also be a custom message. If no trace object is provided
279 # then we have nothing useful to display.
280 my $name = $e->name;
281 my $trace = $e->trace;
282 my $debug = $trace ? $trace->debug : "[No trace info available]";
283  
284 # Create the initial diagnostics. If the test has a name we put the debug
285 # info on a second line, this behavior is inherited from Test::Builder.
286 my $msg = defined($name)
287 ? qq[${prefix}Failed test '$name'\n$debug.\n]
288 : qq[${prefix}Failed test $debug.\n];
289  
290 $self->diag($msg);
291 }
292  
293 sub skip {
294 my $self = shift;
295 my ($name, $reason, @extra) = @_;
296 $self->send_event(
297 'Skip',
298 name => $name,
299 reason => $reason,
300 pass => 1,
301 @extra,
302 );
303 }
304  
305 sub info {
306 my $self = shift;
307 my ($renderer, %params) = @_;
308 $self->send_event('Info', renderer => $renderer, %params);
309 }
310  
311 sub note {
312 my $self = shift;
313 my ($message) = @_;
314 $self->send_event('Note', message => $message);
315 }
316  
317 sub diag {
318 my $self = shift;
319 my ($message) = @_;
320 my $hub = $self->{+HUB};
321 $self->send_event(
322 'Diag',
323 message => $message,
324 );
325 }
326  
327 sub plan {
328 my ($self, $max, $directive, $reason) = @_;
329 $self->send_event('Plan', max => $max, directive => $directive, reason => $reason);
330 }
331  
332 sub bail {
333 my ($self, $reason) = @_;
334 $self->send_event('Bail', reason => $reason);
335 }
336  
337 sub _parse_event {
338 my $self = shift;
339 my $event = shift;
340  
341 my $pkg;
342 if ($event =~ m/^\+(.*)/) {
343 $pkg = $1;
344 }
345 else {
346 $pkg = "Test2::Event::$event";
347 }
348  
349 unless ($LOADED{$pkg}) {
350 my $file = pkg_to_file($pkg);
351 my ($ok, $err) = try { require $file };
352 $self->throw("Could not load event module '$pkg': $err")
353 unless $ok;
354  
355 $LOADED{$pkg} = $pkg;
356 }
357  
358 confess "'$pkg' is not a subclass of 'Test2::Event'"
359 unless $pkg->isa('Test2::Event');
360  
361 $LOADED{$event} = $pkg;
362  
363 return $pkg;
364 }
365  
366 1;
367  
368 __END__