blib/lib/Test2/API.pm

filter: Statementstmt 88.6% 384 / 433 Branchbran 75.6% 127 / 168 Conditioncond 61.2% 90 / 147 MC/DCmcdc 35.0% 28 / 80 Subroutinesub 89.4% 102 / 114 Podpod 83.8% 57 / 68 totaltotal 78.0% 788 / 1010 CC 133 SCAR 31.7
CC
133
Cov
80%
context
67.5
run_subtest
51.7
CLONE
12.0
CRAP 23.7
« blib/lib/Test2/Tools/Spec.pm ↑ Summary blib/lib/Test2/Util.pm »
1 package Test2::API;
2 455 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 455 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 455 ▶use Time::HiRes qw/time/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 455 ▶use Test2::Util qw/USE_THREADS/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 455 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
9 455 ▶ $ENV{TEST_ACTIVE} ||= 1;
Condition100%2/2
$ENV{"TEST_ACTIVE"} ||= 1
l!l
21434
MC/DC100%1/1
$ENV{"TEST_ACTIVE"} ||= 1
$ENV{"TEST_ACTIVE"}
Truth table100%2/2
A
A= $ENV{"TEST_ACTIVE"}
Aresult
11
00
10 455 $ENV{TEST2_ACTIVE} = 1;
11 }
12  
13 455 our $VERSION = '1.302226';
14  
15  
16 455 my $INST;
17 455 my $ENDING = 0;
18 237 ▶sub test2_unset_is_end { $ENDING = 0 }
Subroutine test2_unset_is_end: called CC=1 SCAR=0.0
Pod undocumented
19 3 ▶sub test2_get_is_end { $ENDING }
Subroutine test2_get_is_end: called CC=1 SCAR=0.0
Pod documented
20  
21 1348 ▶sub test2_set_is_end {
Subroutine test2_set_is_end: called CC=10 SCAR=23.4
Pod documented
22 1348 my $before = $ENDING;
23 1348 ▶ ($ENDING) = @_ ? @_ : (1);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 1348 1348
24  
25 # Only send the event in a transition from false to true
26 1348 ▶ return if $before;
Branch: 1 branch
BranchTrueFalseTotal
if $before 892 456 1348
27 456 ▶ return unless $ENDING;
Branch: 1 branch
BranchTrueFalseTotal
unless $ENDING 0 456 456
28  
29 456 ▶ return unless $INST;
Branch: 1 branch
BranchTrueFalseTotal
unless $INST 0 456 456
30 456 ▶ my $stack = $INST->stack or return;
Branch: 1 branch
BranchTrueFalseTotal
my $stack = $INST->stack or return 456 0 456
31 456 ▶ my $root = $stack->root or return;
Branch: 1 branch
BranchTrueFalseTotal
my $root = $stack->root or return 454 2 456
32  
33 454 ▶ return unless $root->count;
Branch: 1 branch
BranchTrueFalseTotal
unless $root->count 73 381 454
34  
35 381 ▶ return unless $$ == $INST->pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $$ == $INST->pid 24 357 381
36 357 ▶ return unless get_tid() == $INST->tid;
Branch: 1 branch
BranchTrueFalseTotal
unless &get_tid() == $INST->tid 0 357 357
37  
38 357 my $trace = Test2::EventFacet::Trace->new(
39 frame => [__PACKAGE__, __FILE__, __LINE__, __PACKAGE__ . '::test2_set_is_end'],
40 );
41 357 my $ctx = Test2::API::Context->new(
42 trace => $trace,
43 hub => $root,
44 );
45  
46 357 $ctx->send_ev2(control => { phase => 'END', details => 'Transition to END phase' });
47  
48 357 1;
49 }
50  
51 455 ▶use Test2::API::Instance(\$INST);
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 # Set the exit status
54 455 ▶END {
Subroutine END: called CC=1 SCAR=0.0
55 455 test2_set_is_end(); # See gh #16
56 455 $INST->set_exit();
57 }
58  
59 0 ▶sub CLONE {
Subroutine CLONE: not called CC=3 SCAR=24.8
60 0 my $init = test2_init_done();
61 0 my $load = test2_load_done();
62  
63 0 ▶ return if $init && $load;
Branch: 1 branch
BranchTrueFalseTotal
if $init and $load 0 0 0
Condition0%0/3
$init and $load
!ll&&!rl&&r
000
MC/DC0%0/2
$init and $load
$init $load
Truth table0%0/3
A and B
A= $initB= $load
ABresult
0X0
100
111
64  
65 0 require Carp;
66 0 Carp::croak "Test2 must be fully loaded before you start a new thread!\n";
67 }
68  
69 # See gh #16
70 {
71 455 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 418 ▶ INIT { eval 'END { test2_set_is_end() }; 1' or die $@ }
Subroutine INIT: called CC=2 SCAR=7.2
Subroutine END: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
eval "END { test2_set_is_end() }; 1" or die $@ 418 0 418
73 }
74  
75 455 ▶BEGIN {
Subroutine BEGIN: called CC=4 SCAR=16.1
76 455 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
77 455 ▶ if("$]" >= 5.014 || $ENV{T2_CHECK_DEPTH} || $Test2::API::DO_DEPTH_CHECK) {
Branch: 1 branch
BranchTrueFalseTotal
if ("$]" >= 5.014 or $ENV{'T2_CHECK_DEPTH'} or $Test2::API::DO_DEPTH_CHECK) { } 455 0 455
Condition33%2/6
"$]" >= "5.014" or $ENV{"T2_CHECK_DEPTH"} or $Test2::API::DO_DEPTH_CHECK
l!l&&r!l&&!r
45500
"$]" >= "5.014" or $ENV{"T2_CHECK_DEPTH"}
l!l&&r!l&&!r
45500
MC/DC0%0/3
"$]" >= "5.014" or $ENV{"T2_CHECK_DEPTH"} or $Test2::API::DO_DEPTH_CHECK
"$]" >= "5.014" $ENV{"T2_CHECK_DEPTH"} $Test2::API::DO_DEPTH_CHECK
Truth table25%1/4
A or B or C
A= "$]" >= "5.014"B= $ENV{"T2_CHECK_DEPTH"}C= $Test2::API::DO_DEPTH_CHECK
ABCresult
1XX1
01X1
0011
0000
78 455 *DO_DEPTH_CHECK = sub() { 1 };
79 }
80 else {
81 0 *DO_DEPTH_CHECK = sub() { 0 };
82 }
83 }
84  
85 455 ▶use Test2::EventFacet::Trace();
Subroutine BEGIN: called CC=1 SCAR=0.0
86 455 ▶use Test2::Util::Trace(); # Legacy
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 455 ▶use Test2::Hub::Subtest();
Subroutine BEGIN: called CC=1 SCAR=0.0
89 455 ▶use Test2::Hub::Interceptor();
Subroutine BEGIN: called CC=1 SCAR=0.0
90 455 ▶use Test2::Hub::Interceptor::Terminator();
Subroutine BEGIN: called CC=1 SCAR=0.0
91  
92 455 ▶use Test2::Event::Ok();
Subroutine BEGIN: called CC=1 SCAR=0.0
93 455 ▶use Test2::Event::Diag();
Subroutine BEGIN: called CC=1 SCAR=0.0
94 455 ▶use Test2::Event::Note();
Subroutine BEGIN: called CC=1 SCAR=0.0
95 455 ▶use Test2::Event::Plan();
Subroutine BEGIN: called CC=1 SCAR=0.0
96 455 ▶use Test2::Event::Bail();
Subroutine BEGIN: called CC=1 SCAR=0.0
97 455 ▶use Test2::Event::Exception();
Subroutine BEGIN: called CC=1 SCAR=0.0
98 455 ▶use Test2::Event::Waiting();
Subroutine BEGIN: called CC=1 SCAR=0.0
99 455 ▶use Test2::Event::Skip();
Subroutine BEGIN: called CC=1 SCAR=0.0
100 455 ▶use Test2::Event::Subtest();
Subroutine BEGIN: called CC=1 SCAR=0.0
101  
102 455 ▶use Carp qw/carp croak confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
103 455 ▶use Scalar::Util qw/blessed weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
104 455 ▶use Test2::Util qw/get_tid clone_io pkg_to_file gen_uid/;
Subroutine BEGIN: called CC=1 SCAR=0.0
105  
106 455 our @EXPORT_OK = qw{
107 context release
108 context_do
109 no_context
110 intercept intercept_deep
111 run_subtest
112  
113 test2_init_done
114 test2_load_done
115 test2_load
116 test2_start_preload
117 test2_stop_preload
118 test2_in_preload
119 test2_is_testing_done
120  
121 test2_set_is_end
122 test2_unset_is_end
123 test2_get_is_end
124  
125 test2_pid
126 test2_tid
127 test2_stack
128 test2_no_wait
129 test2_ipc_wait_enable
130 test2_ipc_wait_disable
131 test2_ipc_wait_enabled
132  
133 test2_add_uuid_via
134  
135 test2_add_callback_testing_done
136  
137 test2_add_callback_context_aquire
138 test2_add_callback_context_acquire
139 test2_add_callback_context_init
140 test2_add_callback_context_release
141 test2_add_callback_exit
142 test2_add_callback_post_load
143 test2_add_callback_pre_subtest
144 test2_list_context_aquire_callbacks
145 test2_list_context_acquire_callbacks
146 test2_list_context_init_callbacks
147 test2_list_context_release_callbacks
148 test2_list_exit_callbacks
149 test2_list_post_load_callbacks
150 test2_list_pre_subtest_callbacks
151  
152 test2_ipc
153 test2_has_ipc
154 test2_ipc_disable
155 test2_ipc_disabled
156 test2_ipc_drivers
157 test2_ipc_add_driver
158 test2_ipc_polling
159 test2_ipc_disable_polling
160 test2_ipc_enable_polling
161 test2_ipc_get_pending
162 test2_ipc_set_pending
163 test2_ipc_get_timeout
164 test2_ipc_set_timeout
165  
166 test2_formatter
167 test2_formatters
168 test2_formatter_add
169 test2_formatter_set
170  
171 test2_stdout
172 test2_stderr
173 test2_reset_io
174  
175 test2_enable_trace_stamps
176 test2_disable_trace_stamps
177 test2_trace_stamps_enabled
178  
179 test2_add_pending_diag
180 test2_get_pending_diags
181 test2_clear_pending_diags
182 };
183 455 ▶BEGIN { require Exporter; our @ISA = qw(Exporter) }
Subroutine BEGIN: called CC=1 SCAR=0.0
184  
185 455 my @PENDING_DIAGS;
186  
187 5 ▶sub test2_add_pending_diag { push @PENDING_DIAGS => @_ }
Subroutine test2_add_pending_diag: called CC=1 SCAR=0.0
Pod documented
188 0 ▶sub test2_get_pending_diags { @PENDING_DIAGS }
Subroutine test2_get_pending_diags: not called CC=1 SCAR=6.9
Pod documented
189 13019 ▶sub test2_clear_pending_diags { my @out = @PENDING_DIAGS; @PENDING_DIAGS = (); return @out }
Subroutine test2_clear_pending_diags: called CC=1 SCAR=0.0
Pod documented
190  
191 455 my $STACK = $INST->stack;
192 455 my $CONTEXTS = $INST->contexts;
193 455 my $INIT_CBS = $INST->context_init_callbacks;
194 455 my $ACQUIRE_CBS = $INST->context_acquire_callbacks;
195  
196 455 my $STDOUT = clone_io(\*STDOUT);
197 455 my $STDERR = clone_io(\*STDERR);
198 576 ▶sub test2_stdout { $STDOUT ||= clone_io(\*STDOUT) }
Subroutine test2_stdout: called CC=2 SCAR=9.2
Pod documented
Condition33%1/3
$STDOUT ||= clone_io(\*STDOUT)
l!l&&r!l&&!r
57600
MC/DC0%0/2
$STDOUT ||= clone_io(\*STDOUT)
$STDOUT clone_io(\*STDOUT)
Truth table33%1/3
A ||= B
A= $STDOUTB= clone_io(\*STDOUT)
ABresult
1X1
011
000
199 576 ▶sub test2_stderr { $STDERR ||= clone_io(\*STDERR) }
Subroutine test2_stderr: called CC=2 SCAR=9.2
Pod documented
Condition33%1/3
$STDERR ||= clone_io(\*STDERR)
l!l&&r!l&&!r
57600
MC/DC0%0/2
$STDERR ||= clone_io(\*STDERR)
$STDERR clone_io(\*STDERR)
Truth table33%1/3
A ||= B
A= $STDERRB= clone_io(\*STDERR)
ABresult
1X1
011
000
200  
201 0 ▶sub test2_post_preload_reset {
Subroutine test2_post_preload_reset: not called CC=1 SCAR=6.9
Pod undocumented
202 0 test2_reset_io();
203 0 $INST->post_preload_reset;
204 }
205  
206 0 ▶sub test2_reset_io {
Subroutine test2_reset_io: not called CC=1 SCAR=6.9
Pod documented
207 0 $STDOUT = clone_io(\*STDOUT);
208 0 $STDERR = clone_io(\*STDERR);
209 }
210  
211 331 ▶sub test2_init_done { $INST->finalized }
Subroutine test2_init_done: called CC=1 SCAR=0.0
Pod documented
212 184 ▶sub test2_load_done { $INST->loaded }
Subroutine test2_load_done: called CC=1 SCAR=0.0
Pod documented
213  
214 434 ▶sub test2_load { $INST->load }
Subroutine test2_load: called CC=1 SCAR=0.0
Pod undocumented
215 1 ▶sub test2_start_preload { $ENV{T2_IN_PRELOAD} = 1; $INST->start_preload }
Subroutine test2_start_preload: called CC=1 SCAR=0.0
Pod undocumented
216 1 ▶sub test2_stop_preload { $ENV{T2_IN_PRELOAD} = 0; $INST->stop_preload }
Subroutine test2_stop_preload: called CC=1 SCAR=0.0
Pod undocumented
217 1426 ▶sub test2_in_preload { $INST->preload }
Subroutine test2_in_preload: called CC=1 SCAR=0.0
Pod undocumented
218  
219 1 ▶sub test2_pid { $INST->pid }
Subroutine test2_pid: called CC=1 SCAR=0.0
Pod undocumented
220 1 ▶sub test2_tid { $INST->tid }
Subroutine test2_tid: called CC=1 SCAR=0.0
Pod undocumented
221 2404 ▶sub test2_stack { $INST->stack }
Subroutine test2_stack: called CC=1 SCAR=0.0
Pod documented
222 2 ▶sub test2_ipc_wait_enable { $INST->set_no_wait(0) }
Subroutine test2_ipc_wait_enable: called CC=1 SCAR=0.0
Pod documented
223 1 ▶sub test2_ipc_wait_disable { $INST->set_no_wait(1) }
Subroutine test2_ipc_wait_disable: called CC=1 SCAR=0.0
Pod documented
224 3 ▶sub test2_ipc_wait_enabled { !$INST->no_wait }
Subroutine test2_ipc_wait_enabled: called CC=1 SCAR=0.0
Pod documented
225  
226 1 ▶sub test2_enable_trace_stamps { $INST->test2_enable_trace_stamps }
Subroutine test2_enable_trace_stamps: called CC=1 SCAR=0.0
Pod documented
227 1 ▶sub test2_disable_trace_stamps { $INST->test2_disable_trace_stamps }
Subroutine test2_disable_trace_stamps: called CC=1 SCAR=0.0
Pod documented
228 0 ▶sub test2_trace_stamps_enabled { $INST->test2_trace_stamps_enabled }
Subroutine test2_trace_stamps_enabled: not called CC=1 SCAR=6.9
Pod documented
229  
230 2 ▶sub test2_is_testing_done {
Subroutine test2_is_testing_done: called CC=6 SCAR=18.8
Pod documented
231 # No instance? VERY DONE!
232 2 ▶ return 1 unless $INST;
Branch: 1 branch
BranchTrueFalseTotal
unless $INST 0 2 2
233  
234 # No stack? tests must be done, it is created pretty early
235 2 ▶ my $stack = $INST->stack or return 1;
Branch: 1 branch
BranchTrueFalseTotal
my $stack = $INST->stack or return 1 2 0 2
236  
237 # Nothing on the stack, no root hub yet, likely have not started testing
238 2 ▶ return 0 unless @$stack;
Branch: 1 branch
BranchTrueFalseTotal
unless @$stack 0 2 2
239  
240 # Stack has a slot for the root hub (see above) but it is undefined, likely
241 # garbage collected, test is done
242 2 ▶ my $root_hub = $stack->[0] or return 1;
Branch: 1 branch
BranchTrueFalseTotal
my $root_hub = $stack->[0] or return 1 2 0 2
243  
244 # If the root hub is ended than testing is done.
245 2 ▶ return 1 if $root_hub->ended;
Branch: 1 branch
BranchTrueFalseTotal
if $root_hub->ended 1 1 2
246  
247 # Looks like we are still testing!
248 1 return 0;
249 }
250  
251 5 ▶sub test2_no_wait {
Subroutine test2_no_wait: called CC=2 SCAR=6.9
Pod documented
252 5 ▶ $INST->set_no_wait(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 3 5
253 5 $INST->no_wait;
254 }
255  
256 34 ▶sub test2_add_callback_testing_done {
Subroutine test2_add_callback_testing_done: called CC=1 SCAR=0.0
Pod documented
257 34 my $cb = shift;
258  
259 34 ▶ test2_add_callback_post_load(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
260 34 my $stack = test2_stack();
261 34 $stack->top; # Ensure we have a hub
262 34 my ($hub) = Test2::API::test2_stack->all;
263  
264 34 $hub->set_active(1);
265  
266 34 $hub->follow_up($cb);
267 34 });
268  
269 34 return;
270 }
271  
272 3 ▶sub test2_add_callback_context_acquire { $INST->add_context_acquire_callback(@_) }
Subroutine test2_add_callback_context_acquire: called CC=1 SCAR=0.0
Pod documented
273 183 ▶sub test2_add_callback_context_aquire { $INST->add_context_acquire_callback(@_) }
Subroutine test2_add_callback_context_aquire: called CC=1 SCAR=0.0
Pod undocumented
274 3 ▶sub test2_add_callback_context_init { $INST->add_context_init_callback(@_) }
Subroutine test2_add_callback_context_init: called CC=1 SCAR=0.0
Pod documented
275 5 ▶sub test2_add_callback_context_release { $INST->add_context_release_callback(@_) }
Subroutine test2_add_callback_context_release: called CC=1 SCAR=0.0
Pod documented
276 679 ▶sub test2_add_callback_exit { $INST->add_exit_callback(@_) }
Subroutine test2_add_callback_exit: called CC=1 SCAR=0.0
Pod documented
277 388 ▶sub test2_add_callback_post_load { $INST->add_post_load_callback(@_) }
Subroutine test2_add_callback_post_load: called CC=1 SCAR=0.0
Pod documented
278 2 ▶sub test2_add_callback_pre_subtest { $INST->add_pre_subtest_callback(@_) }
Subroutine test2_add_callback_pre_subtest: called CC=1 SCAR=0.0
Pod documented
279 0 ▶sub test2_list_context_aquire_callbacks { @{$INST->context_acquire_callbacks} }
Subroutine test2_list_context_aquire_callbacks: not called CC=1 SCAR=6.9
Pod undocumented
280 2 ▶sub test2_list_context_acquire_callbacks { @{$INST->context_acquire_callbacks} }
Subroutine test2_list_context_acquire_callbacks: called CC=1 SCAR=0.0
Pod documented
281 2 ▶sub test2_list_context_init_callbacks { @{$INST->context_init_callbacks} }
Subroutine test2_list_context_init_callbacks: called CC=1 SCAR=0.0
Pod documented
282 2 ▶sub test2_list_context_release_callbacks { @{$INST->context_release_callbacks} }
Subroutine test2_list_context_release_callbacks: called CC=1 SCAR=0.0
Pod documented
283 6 ▶sub test2_list_exit_callbacks { @{$INST->exit_callbacks} }
Subroutine test2_list_exit_callbacks: called CC=1 SCAR=0.0
Pod documented
284 2 ▶sub test2_list_post_load_callbacks { @{$INST->post_load_callbacks} }
Subroutine test2_list_post_load_callbacks: called CC=1 SCAR=0.0
Pod documented
285 510 ▶sub test2_list_pre_subtest_callbacks { @{$INST->pre_subtest_callbacks} }
Subroutine test2_list_pre_subtest_callbacks: called CC=1 SCAR=0.0
Pod documented
286  
287 2 ▶sub test2_add_uuid_via {
Subroutine test2_add_uuid_via: called CC=2 SCAR=7.2
Pod documented
288 2 ▶ $INST->set_add_uuid_via(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 0 2
289 2 $INST->add_uuid_via();
290 }
291  
292 1493 ▶sub test2_ipc { $INST->ipc }
Subroutine test2_ipc: called CC=1 SCAR=0.0
Pod documented
293 330 ▶sub test2_has_ipc { $INST->has_ipc }
Subroutine test2_has_ipc: called CC=1 SCAR=0.0
Pod documented
294 2 ▶sub test2_ipc_disable { $INST->ipc_disable }
Subroutine test2_ipc_disable: called CC=1 SCAR=0.0
Pod documented
295 3 ▶sub test2_ipc_disabled { $INST->ipc_disabled }
Subroutine test2_ipc_disabled: called CC=1 SCAR=0.0
Pod documented
296 5 ▶sub test2_ipc_add_driver { $INST->add_ipc_driver(@_) }
Subroutine test2_ipc_add_driver: called CC=1 SCAR=0.0
Pod documented
297 6 ▶sub test2_ipc_drivers { @{$INST->ipc_drivers} }
Subroutine test2_ipc_drivers: called CC=1 SCAR=0.0
Pod documented
298 3 ▶sub test2_ipc_polling { $INST->ipc_polling }
Subroutine test2_ipc_polling: called CC=1 SCAR=0.0
Pod documented
299 1 ▶sub test2_ipc_enable_polling { $INST->enable_ipc_polling }
Subroutine test2_ipc_enable_polling: called CC=1 SCAR=0.0
Pod documented
300 1 ▶sub test2_ipc_disable_polling { $INST->disable_ipc_polling }
Subroutine test2_ipc_disable_polling: called CC=1 SCAR=0.0
Pod documented
301 0 ▶sub test2_ipc_get_pending { $INST->get_ipc_pending }
Subroutine test2_ipc_get_pending: not called CC=1 SCAR=6.9
Pod documented
302 225 ▶sub test2_ipc_set_pending { $INST->set_ipc_pending(@_) }
Subroutine test2_ipc_set_pending: called CC=1 SCAR=0.0
Pod documented
303 2 ▶sub test2_ipc_set_timeout { $INST->set_ipc_timeout(@_) }
Subroutine test2_ipc_set_timeout: called CC=1 SCAR=0.0
Pod documented
304 4 ▶sub test2_ipc_get_timeout { $INST->ipc_timeout() }
Subroutine test2_ipc_get_timeout: called CC=1 SCAR=0.0
Pod documented
305 0 ▶sub test2_ipc_enable_shm { 0 }
Subroutine test2_ipc_enable_shm: not called CC=1 SCAR=6.9
Pod documented
306  
307 476 ▶sub test2_formatter {
Subroutine test2_formatter: called CC=4 SCAR=14.0
Pod documented
308 476 ▶ if ($ENV{T2_FORMATTER} && $ENV{T2_FORMATTER} =~ m/^(\+)?(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"T2_FORMATTER"} and $ENV{"T2_FORMATTER"} =~ /^(\+)?(.*)$/) 18 458 476
Condition66%2/3
$ENV{"T2_FORMATTER"} and $ENV{"T2_FORMATTER"} =~ /^(\+)?(.*)$/
!ll&&!rl&&r
458018
MC/DC50%1/2
$ENV{"T2_FORMATTER"} and $ENV{"T2_FORMATTER"} =~ /^(\+)?(.*)$/
$ENV{"T2_FORMATTER"} $ENV{"T2_FORMATTER"} =~ /^(\+)?(.*)$/
Truth table66%2/3
A and B
A= $ENV{"T2_FORMATTER"}B= $ENV{"T2_FORMATTER"} =~ /^(\+)?(.*)$/
ABresult
0X0
100
111
309 18 ▶ my $formatter = $1 ? $2 : "Test2::Formatter::$2";
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 0 18 18
310 18 my $file = pkg_to_file($formatter);
311 18 require $file;
312 18 return $formatter;
313 }
314  
315 458 return $INST->formatter;
316 }
317  
318 0 ▶sub test2_formatters { @{$INST->formatters} }
Subroutine test2_formatters: not called CC=1 SCAR=6.9
Pod documented
319 183 ▶sub test2_formatter_add { $INST->add_formatter(@_) }
Subroutine test2_formatter_add: called CC=1 SCAR=0.0
Pod documented
320 2 ▶sub test2_formatter_set {
Subroutine test2_formatter_set: called CC=3 SCAR=11.4
Pod documented
321 2 my ($formatter) = @_;
322 2 ▶ croak "No formatter specified" unless $formatter;
Branch: 1 branch
BranchTrueFalseTotal
unless $formatter 1 1 2
323 1 ▶ croak "Global Formatter already set" if $INST->formatter_set;
Branch: 1 branch
BranchTrueFalseTotal
if $INST->formatter_set 1 0 1
324 0 $INST->set_formatter($formatter);
325 }
326  
327 # Private, for use in Test2::API::Context
328 455 ▶sub _contexts_ref { $INST->contexts }
Subroutine _contexts_ref: called CC=1 SCAR=0.0
329 1 ▶sub _context_acquire_callbacks_ref { $INST->context_acquire_callbacks }
Subroutine _context_acquire_callbacks_ref: called CC=1 SCAR=0.0
330 1 ▶sub _context_init_callbacks_ref { $INST->context_init_callbacks }
Subroutine _context_init_callbacks_ref: called CC=1 SCAR=0.0
331 456 ▶sub _context_release_callbacks_ref { $INST->context_release_callbacks }
Subroutine _context_release_callbacks_ref: called CC=1 SCAR=0.0
332 978 ▶sub _add_uuid_via_ref { \($INST->{Test2::API::Instance::ADD_UUID_VIA()}) }
Subroutine _add_uuid_via_ref: called CC=1 SCAR=0.0
333  
334 # Private, for use in Test2::IPC
335 0 ▶sub _set_ipc { $INST->set_ipc(@_) }
Subroutine _set_ipc: not called CC=1 SCAR=6.9
336  
337 5 ▶sub context_do(&;@) {
Subroutine context_do: called CC=6 SCAR=17.9
Pod documented
338 5 my $code = shift;
339 5 my @args = @_;
340  
341 5 my $ctx = context(level => 1);
342  
343 5 my $want = wantarray;
344  
345 5 my @out;
346 5 my $ok = eval {
347 5 ▶ $want ? @out = $code->($ctx, @args) :
Branch: 2 branches
BranchTrueFalseTotal
$want ? : 1 4 5
defined $want ? : 2 2 4
348 defined($want) ? $out[0] = $code->($ctx, @args) :
349 $code->($ctx, @args) ;
350 3 1;
351 };
352 5 my $err = $@;
353  
354 5 $ctx->release;
355  
356 5 ▶ die $err unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 2 3 5
357  
358 3 ▶ return @out if $want;
Branch: 1 branch
BranchTrueFalseTotal
if $want 1 2 3
359 2 ▶ return $out[0] if defined $want;
Branch: 1 branch
BranchTrueFalseTotal
if defined $want 1 1 2
360 1 return;
361 }
362  
363 3 ▶sub no_context(&;$) {
Subroutine no_context: called CC=3 SCAR=11.0
Pod documented
364 3 my ($code, $hid) = @_;
365 3 ▶ $hid ||= $STACK->top->hid;
Condition66%2/3
$hid ||= $STACK->top->hid
l!l&&r!l&&!r
210
MC/DC0%0/2
$hid ||= $STACK->top->hid
$hid $STACK->top->hid
Truth table66%2/3
A ||= B
A= $hidB= $STACK->top->hid
ABresult
1X1
011
000
366  
367 3 my $ctx = $CONTEXTS->{$hid};
368 3 delete $CONTEXTS->{$hid};
369 3 my $ok = eval { $code->(); 1 };
370 3 my $err = $@;
371  
372 3 $CONTEXTS->{$hid} = $ctx;
373 3 weaken($CONTEXTS->{$hid});
374  
375 3 ▶ die $err unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 3 3
376  
377 3 return;
378 };
379  
380 455 my $UUID_VIA = _add_uuid_via_ref();
381 18929 ▶sub context {
Subroutine context: called CC=49 SCAR=42.1
Pod documented
382 # We need to grab these before anything else to ensure they are not
383 # changed.
384 18929 my ($errno, $eval_error, $child_error, $extended_error) = (0 + $!, $@, $?, $^E);
385  
386 18929 my %params = (level => 0, wrapped => 0, @_);
387  
388 # If something is getting a context then the sync system needs to be
389 # considered loaded...
390 18929 ▶ $INST->load unless $INST->{loaded};
Branch: 1 branch
BranchTrueFalseTotal
unless $INST->{"loaded"} 295 18634 18929
391  
392 18929 ▶ croak "context() called, but return value is ignored"
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 1 18928 18929
393 unless defined wantarray;
394  
395 18928 ▶ my $stack = $params{stack} || $STACK;
Condition66%2/3
$params{'stack'} || $STACK
l!l&&r!l&&!r
1109378350
MC/DC0%0/2
$params{'stack'} || $STACK
$params{'stack'} $STACK
Truth table66%2/3
A || B
A= $params{'stack'}B= $STACK
ABresult
1X1
011
000
396 18928 ▶ my $hub = $params{hub} || (@$stack ? $stack->[-1] : $stack->top);
Branch: 1 branch
BranchTrueFalseTotal
@$stack ? : 17612 285 17897
Condition66%2/3
$params{'hub'} || (@$stack ? $stack->[-1] : $stack->top)
l!l&&r!l&&!r
1031178970
MC/DC0%0/2
$params{'hub'} || (@$stack ? $stack->[-1] : $stack->top)
$params{'hub'} (@$stack ? $stack->[-1] : $stack->top)
Truth table66%2/3
A || B
A= $params{'hub'}B= (@$stack ? $stack->[-1] : $stack->top)
ABresult
1X1
011
000
397  
398 # Catch an edge case where we try to get context after the root hub has
399 # been garbage collected resulting in a stack that has a single undef
400 # hub
401 18928 ▶ if (!($hub && $hub->{hid}) && !exists($params{hub}) && @$stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $hub && $hub->{"hid"} || exists $params{"hub"} and @$stack) 0 18928 18928
Condition33%3/9
not $hub && $hub->{"hid"} || exists $params{"hub"} and @$stack
!ll&&!rl&&r
1892800
$hub && $hub->{'hid'} || exists $params{'hub'}
l!l&&r!l&&!r
1892800
$hub and $hub->{"hid"}
!ll&&!rl&&r
0018928
MC/DC0%0/4
not $hub && $hub->{"hid"} || exists $params{"hub"} and @$stack
$hub $hub->{"hid"} exists $params{'hub'} @$stack
Truth table14%1/7
not(A and B || C) and D
A= $hubB= $hub->{"hid"}C= exists $params{'hub'}D= @$stack
ABCDresult
11XX0
0X1X0
101X0
0X000
10000
0X011
10011
402 0 my $msg;
403  
404 0 ▶ if ($hub && !$hub->{hid}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hub and not $hub->{'hid'}) { } 0 0 0
Condition0%0/3
$hub and not $hub->{"hid"}
!ll&&!rl&&r
000
MC/DC0%0/2
$hub and not $hub->{"hid"}
$hub not $hub->{"hid"}
Truth table0%0/3
A and not(B)
A= $hubB= not $hub->{"hid"}
ABresult
0X0
100
111
405 0 $msg = Carp::longmess("$hub has no hid! (did you attempt a testing event after done_testing?). You may be relying on a tool or plugin that was based off an old Test2 that did not require hids.");
406 }
407 else {
408 0 $msg = Carp::longmess("Attempt to get Test2 context after testing has completed (did you attempt a testing event after done_testing?)");
409 }
410  
411 # The error message is usually masked by the global destruction, so we have to print to STDER
412 0 print STDERR $msg;
413  
414 # Make sure this is a failure, we are probably already in END, so set $? to change the exit code
415 0 $? = 1;
416  
417 # Now we actually die to interrupt the program flow and avoid undefined his warnings
418 0 die $msg;
419 }
420  
421 18928 my $hid = $hub->{hid};
422 18928 my $current = $CONTEXTS->{$hid};
423  
424 18928 $_->(\%params) for @$ACQUIRE_CBS;
425 18928 ▶ map $_->(\%params), @{$hub->{_context_acquire}} if $hub->{_context_acquire};
Branch: 1 branch
BranchTrueFalseTotal
if $hub->{"_context_acquire"} 88 18840 18928
426  
427 # This is for https://github.com/Test-More/test-more/issues/16
428 # and https://rt.perl.org/Public/Bug/Display.html?id=127774
429 18928 ▶ my $phase = ${^GLOBAL_PHASE} || 'NA';
Condition50%1/2
${^GLOBAL_PHASE} || 'NA'
l!l
189280
MC/DC0%0/1
${^GLOBAL_PHASE} || 'NA'
${^GLOBAL_PHASE}
Truth table50%1/2
A
A= ${^GLOBAL_PHASE}
Aresult
11
00
430 18928 ▶ my $end_phase = $ENDING || $phase eq 'END' || $phase eq 'DESTRUCT';
Condition66%4/6
$ENDING || $phase eq 'END' || $phase eq 'DESTRUCT'
l!l&&r!l&&!r
68018860
$ENDING or $phase eq "END"
l!l&&r!l&&!r
68018860
MC/DC33%1/3
$ENDING || $phase eq 'END' || $phase eq 'DESTRUCT'
$ENDING $phase eq "END" $phase eq 'DESTRUCT'
Truth table50%2/4
A or B || C
A= $ENDINGB= $phase eq "END"C= $phase eq 'DESTRUCT'
ABCresult
1XX1
01X1
0011
0000
431  
432 18928 my $level = 1 + $params{level};
433 18928 ▶ my ($pkg, $file, $line, $sub, @other) = $end_phase ? caller(0) : caller($level);
Branch: 1 branch
BranchTrueFalseTotal
$end_phase ? : 68 18860 18928
434 18928 ▶ unless ($pkg || $end_phase) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pkg or $end_phase) 538 18390 18928
Condition66%2/3
$pkg or $end_phase
l!l&&r!l&&!r
183900538
MC/DC50%1/2
$pkg or $end_phase
$pkg $end_phase
Truth table66%2/3
A or B
A= $pkgB= $end_phase
ABresult
1X1
011
000
435 538 ▶ confess "Could not find context at depth $level" unless $params{fudge};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"fudge"} 1 537 538
436 537 ▶ ($pkg, $file, $line, $sub, @other) = caller(--$level) while ($level >= 0 && !$pkg);
Branch: 1 branch
BranchTrueFalseTotal
$level >= 0 and not $pkg and do { () = caller --$level } 636 537 1173
Condition66%2/3
$level >= 0 and not $pkg
!ll&&!rl&&r
0537636
MC/DC50%1/2
$level >= 0 and not $pkg
$level >= 0 not $pkg
Truth table66%2/3
A and not(B)
A= $level >= 0B= not $pkg
ABresult
0X0
100
111
437 }
438  
439 18927 my $depth = $level;
440 18927 ▶ $depth++ while DO_DEPTH_CHECK && !$end_phase && (!$current || $depth <= $current->{_depth} + $params{wrapped}) && caller($depth + 1);
Branch: 1 branch
BranchTrueFalseTotal
not $end_phase and !$current || $depth <= $current->{"_depth"} + $params{"wrapped"} and caller $depth + 1 and do { ++$depth } 53397 18927 72324
Condition100%9/9
not $end_phase and !$current || $depth <= $current->{"_depth"} + $params{"wrapped"} and caller $depth + 1
!ll&&!rl&&r
58651306253397
not $end_phase and !$current || $depth <= $current->{"_depth"} + $params{"wrapped"}
!ll&&!rl&&r
68579766459
not $current or $depth <= $current->{"_depth"} + $params{"wrapped"}
l!l&&r!l&&!r
54736117235797
MC/DC100%4/4
not $end_phase and !$current || $depth <= $current->{"_depth"} + $params{"wrapped"} and caller $depth + 1
not $end_phase not $current $depth <= $current->{"_depth"} + $params{"wrapped"} caller $depth + 1
Truth table100%6/6
not(A) and not(B) or C and D
A= not $end_phaseB= not $currentC= $depth <= $current->{"_depth"} + $params{"wrapped"}D= caller $depth + 1
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
441 18927 $depth -= $params{wrapped};
442 18927 ▶ my $depth_ok = !DO_DEPTH_CHECK || $end_phase || !$current || $current->{_depth} < $depth;
Condition100%6/6
$end_phase || !$current || $current->{'_depth'} < $depth
l!l&&r!l&&!r
1312757973
$end_phase or not $current
l!l&&r!l&&!r
68130595800
MC/DC100%3/3
$end_phase || !$current || $current->{'_depth'} < $depth
$end_phase not $current $current->{'_depth'} < $depth
Truth table100%4/4
A or not(B) || C
A= $end_phaseB= not $currentC= $current->{'_depth'} < $depth
ABCresult
1XX1
01X1
0011
0000
443  
444 18927 ▶ if ($current && $params{on_release} && $depth_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current and $params{"on_release"} and $depth_ok) 1 18926 18927
Condition83%5/6
$current and $params{"on_release"} and $depth_ok
!ll&&!rl&&r
1892601
$current and $params{"on_release"}
!ll&&!rl&&r
1309958271
MC/DC66%2/3
$current and $params{"on_release"} and $depth_ok
$current $params{"on_release"} $depth_ok
Truth table75%3/4
A and B and C
A= $currentB= $params{"on_release"}C= $depth_ok
ABCresult
0XX0
10X0
1100
1111
445 1 ▶ $current->{_on_release} ||= [];
Condition50%1/2
$current->{"_on_release"} ||= []
l!l
10
MC/DC0%0/1
$current->{"_on_release"} ||= []
$current->{"_on_release"}
Truth table50%1/2
A
A= $current->{"_on_release"}
Aresult
11
00
446 1 push @{$current->{_on_release}} => $params{on_release};
447 }
448  
449 # I know this is ugly....
450 ($!, $@, $?, $^E) = ($errno, $eval_error, $child_error, $extended_error) and return bless(
451 {
452 %$current,
453 _is_canon => undef,
454 errno => $errno,
455 eval_error => $eval_error,
456 child_error => $child_error,
457 _is_spawn => [$pkg, $file, $line, $sub],
458 18927 ▶ _start_fail_count => $hub->{failed} || 0,
Branch: 1 branch
BranchTrueFalseTotal
if $current and $depth_ok 5825 13102 18927
Condition80%8/10
$current && $depth_ok && ((($!, $@, $?, $^E) = ) && (return bless({%$current, '_is_canon', undef, 'errno', $errno, 'eval_error', $eval_error, 'child_error', $child_error, '_is_spawn', [$pkg, $file, $line, $sub], '_start_fail_count', $hub->{'failed'} || 0}, 'Test2::API::Context')))
!ll&&!rl&&r
1310258250
$current and $depth_ok
!ll&&!rl&&r
1309935825
($!, $@, $?, $^E) = and return bless({%$current, "_is_canon", undef, "errno", $errno, "eval_error", $eval_error, "child_error", $child_error, "_is_spawn", [$pkg, $file, $line, $sub], "_start_fail_count", $hub->{"failed"} || 0}, "Test2::API::Context")
!ll
05825
$hub->{'failed'} || 0
l!l
7945031
MC/DC0%0/3
$current && $depth_ok && ((($!, $@, $?, $^E) = ) && (return bless({%$current, '_is_canon', undef, 'errno', $errno, 'eval_error', $eval_error, 'child_error', $child_error, '_is_spawn', [$pkg, $file, $line, $sub], '_start_fail_count', $hub->{'failed'} || 0}, 'Test2::API::Context')))
$current $depth_ok ($!, $@, $?, $^E) =
MC/DC100%1/1
$hub->{'failed'} || 0
$hub->{'failed'}
Truth table50%2/4
A and B && C
A= $currentB= $depth_okC= ($!, $@, $?, $^E) =
ABCresult
0XX0
10X0
1100
1111
Truth table100%2/2
A
A= $hub->{'failed'}
Aresult
11
00
459 },
460 'Test2::API::Context'
461 ) if $current && $depth_ok;
462  
463 # Handle error condition of bad level
464 13102 ▶ if ($current) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current) 3 13099 13102
465 3 ▶ unless (${$current->{_aborted}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (${$current->{"_aborted"};}) 2 1 3
466 _canon_error($current, [$pkg, $file, $line, $sub, $depth])
467 2 ▶ unless $current->{_is_canon};
Branch: 1 branch
BranchTrueFalseTotal
unless $current->{"_is_canon"} 0 2 2
468  
469 2 ▶ _depth_error($current, [$pkg, $file, $line, $sub, $depth])
Branch: 1 branch
BranchTrueFalseTotal
unless $depth_ok 2 0 2
470 unless $depth_ok;
471 }
472  
473 3 ▶ $current->release if $current->{_is_canon};
Branch: 1 branch
BranchTrueFalseTotal
if $current->{"_is_canon"} 3 0 3
474  
475 3 delete $CONTEXTS->{$hid};
476 }
477  
478 # Directly bless the object here, calling new is a noticeable performance
479 # hit with how often this needs to be called.
480 my $trace = bless(
481 {
482 frame => [$pkg, $file, $line, $sub],
483 pid => $$,
484 tid => get_tid(),
485 cid => gen_uid(),
486 hid => $hid,
487 nested => $hub->{nested},
488 buffered => $hub->{buffered},
489  
490 full_caller => [$pkg, $file, $line, $sub, @other],
491  
492 $INST->{trace_stamps} ? (stamp => time()) : (),
493  
494 $$UUID_VIA ? (
495 huuid => $hub->{uuid},
496 13102 ▶ uuid => ${$UUID_VIA}->('context'),
Branch: 2 branches
BranchTrueFalseTotal
$INST->{'trace_stamps'} ? : 3 13099 13102
$$UUID_VIA ? : 69 13033 13102
497 ) : (),
498 },
499 'Test2::EventFacet::Trace'
500 );
501  
502 # Directly bless the object here, calling new is a noticeable performance
503 # hit with how often this needs to be called.
504 13102 my $aborted = 0;
505 $current = bless(
506 {
507 _aborted => \$aborted,
508 stack => $stack,
509 hub => $hub,
510 trace => $trace,
511 _is_canon => 1,
512 _depth => $depth,
513 errno => $errno,
514 eval_error => $eval_error,
515 child_error => $child_error,
516 _start_fail_count => $hub->{failed} || 0,
517 13102 ▶ $params{on_release} ? (_on_release => [$params{on_release}]) : (),
Branch: 1 branch
BranchTrueFalseTotal
$params{'on_release'} ? : 2 13100 13102
Condition100%2/2
$hub->{'failed'} || 0
l!l
78612316
MC/DC100%1/1
$hub->{'failed'} || 0
$hub->{'failed'}
Truth table100%2/2
A
A= $hub->{'failed'}
Aresult
11
00
518 },
519 'Test2::API::Context'
520 );
521  
522 13102 $CONTEXTS->{$hid} = $current;
523 13102 weaken($CONTEXTS->{$hid});
524  
525 13102 $_->($current) for @$INIT_CBS;
526 13102 ▶ map $_->($current), @{$hub->{_context_init}} if $hub->{_context_init};
Branch: 1 branch
BranchTrueFalseTotal
if $hub->{"_context_init"} 49 13053 13102
527  
528 13102 ▶ $params{on_init}->($current) if $params{on_init};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"on_init"} 2 13100 13102
529  
530 13102 ($!, $@, $?, $^E) = ($errno, $eval_error, $child_error, $extended_error);
531  
532 13102 return $current;
533 }
534  
535 2 ▶sub _depth_error {
Subroutine _depth_error: called CC=1 SCAR=0.0
536 2 _existing_error(@_, <<" EOT");
537 context() was called to retrieve an existing context, however the existing
538 context was created in a stack frame at the same, or deeper level. This usually
539 means that a tool failed to release the context when it was finished.
540 EOT
541 }
542  
543 0 ▶sub _canon_error {
Subroutine _canon_error: not called CC=1 SCAR=6.9
544 0 _existing_error(@_, <<" EOT");
545 context() was called to retrieve an existing context, however the existing
546 context has an invalid internal state (!_canon_count). This should not normally
547 happen unless something is mucking about with internals...
548 EOT
549 }
550  
551 2 ▶sub _existing_error {
Subroutine _existing_error: called CC=1 SCAR=0.0
552 2 my ($ctx, $details, $msg) = @_;
553 2 my ($pkg, $file, $line, $sub, $depth) = @$details;
554  
555 2 my $oldframe = $ctx->{trace}->frame;
556 2 my $olddepth = $ctx->{_depth};
557  
558 # Older versions of Carp do not export longmess() function, so it needs to be called with package name
559 2 my $mess = Carp::longmess();
560  
561 2 warn <<" EOT";
562 $msg
563 Old context details:
564 File: $oldframe->[1]
565 Line: $oldframe->[2]
566 Tool: $oldframe->[3]
567 Depth: $olddepth
568  
569 New context details:
570 File: $file
571 Line: $line
572 Tool: $sub
573 Depth: $depth
574  
575 Trace: $mess
576  
577 Removing the old context and creating a new one...
578 EOT
579 }
580  
581 3255 ▶sub release($;$) {
Subroutine release: called CC=1 SCAR=0.0
Pod documented
582 3255 $_[0]->release;
583 3255 return $_[1];
584 }
585  
586 407 ▶sub intercept(&) {
Subroutine intercept: called CC=1 SCAR=0.0
Pod documented
587 407 my $code = shift;
588 407 my $ctx = context();
589  
590 407 my $events = _intercept($code, deep => 0);
591  
592 401 $ctx->release;
593  
594 401 return $events;
595 }
596  
597 2 ▶sub intercept_deep(&) {
Subroutine intercept_deep: called CC=1 SCAR=0.0
Pod undocumented
598 2 my $code = shift;
599 2 my $ctx = context();
600  
601 2 my $events = _intercept($code, deep => 1);
602  
603 2 $ctx->release;
604  
605 2 return $events;
606 }
607  
608 409 ▶sub _intercept {
Subroutine _intercept: called CC=10 SCAR=23.4
609 409 my $code = shift;
610 409 my %params = @_;
611 409 my $ctx = context();
612  
613 409 my $ipc;
614 409 ▶ if (my $global_ipc = test2_ipc()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $global_ipc = test2_ipc()) 129 280 409
615 129 my $driver = blessed($global_ipc);
616 129 $ipc = $driver->new;
617 }
618  
619 409 my $hub = Test2::Hub::Interceptor->new(
620 ipc => $ipc,
621 no_ending => 1,
622 );
623  
624 409 my @events;
625 409 ▶ $hub->listen(sub { push @events => $_[1] }, inherit => $params{deep});
Subroutine __ANON__: called CC=1 SCAR=0.0
626  
627 409 $ctx->stack->top; # Make sure there is a top hub before we begin.
628 409 $ctx->stack->push($hub);
629  
630 409 my $trace = $ctx->trace;
631 409 my $state = {};
632 409 $hub->clean_inherited(trace => $trace, state => $state);
633  
634 409 my ($ok, $err) = (1, undef);
635 T2_SUBTEST_WRAPPER: {
636 # Do not use 'try' cause it localizes __DIE__
637 409 $ok = eval { $code->(hub => $hub, context => $ctx->snapshot); 1 };
638 388 $err = $@;
639  
640 # They might have done 'BEGIN { skip_all => "whatever" }'
641 388 ▶ if (!$ok && $err =~ m/Label not found for "last T2_SUBTEST_WRAPPER"/ || (blessed($err) && $err->isa('Test2::Hub::Interceptor::Terminator'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ok and $err =~ /Label not found for "last T2_SUBTEST_WRAPPER"/ or builtin::blessed($err) and $err->isa("Test2::Hub::Interceptor::Terminator")) 0 388 388
Condition44%4/9
not $ok and $err =~ /Label not found for "last T2_SUBTEST_WRAPPER"/ or builtin::blessed($err) and $err->isa("Test2::Hub::Interceptor::Terminator")
l!l&&r!l&&!r
00388
not $ok and $err =~ /Label not found for "last T2_SUBTEST_WRAPPER"/
!ll&&!rl&&r
38710
builtin::blessed($err) and $err->isa("Test2::Hub::Interceptor::Terminator")
!ll&&!rl&&r
38800
MC/DC0%0/4
not $ok and $err =~ /Label not found for "last T2_SUBTEST_WRAPPER"/ or builtin::blessed($err) and $err->isa("Test2::Hub::Interceptor::Terminator")
not $ok $err =~ /Label not found for "last T2_SUBTEST_WRAPPER"/ builtin::blessed($err) $err->isa("Test2::Hub::Interceptor::Terminator")
Truth table28%2/7
not(A) and B or C and D
A= not $okB= $err =~ /Label not found for "last T2_SUBTEST_WRAPPER"/C= builtin::blessed($err)D= $err->isa("Test2::Hub::Interceptor::Terminator")
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
642 0 $ok = 1;
643 0 $err = undef;
644 }
645 }
646  
647 404 $hub->cull;
648 404 $ctx->stack->pop($hub);
649  
650 404 $hub->restore_inherited(trace => $trace, state => $state);
651  
652 404 $ctx->release;
653  
654 404 ▶ die $err unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 1 403 404
655  
656 403 ▶ $hub->finalize($trace, 1)
Branch: 1 branch
BranchTrueFalseTotal
if $ok and not $hub->no_ending and not $hub->ended 2 401 403
Condition83%5/6
$ok and not $hub->no_ending and not $hub->ended
!ll&&!rl&&r
40012
$ok and not $hub->no_ending
!ll&&!rl&&r
04003
MC/DC66%2/3
$ok and not $hub->no_ending and not $hub->ended
$ok not $hub->no_ending not $hub->ended
Truth table75%3/4
A and not(B) and not(C)
A= $okB= not $hub->no_endingC= not $hub->ended
ABCresult
0XX0
10X0
1100
1111
657 if $ok
658 && !$hub->no_ending
659 && !$hub->ended;
660  
661 403 require Test2::API::InterceptResult;
662 403 return Test2::API::InterceptResult->new_from_ref(\@events);
663 }
664  
665 366 ▶sub run_subtest {
Subroutine run_subtest: called CC=39 SCAR=39.4
Pod documented
666 366 my ($name, $code, $params, @args) = @_;
667  
668 $_->($name,$code,@args)
669 366 for Test2::API::test2_list_pre_subtest_callbacks();
670  
671 366 ▶ $params = {buffered => $params} unless ref $params;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $params 106 260 366
672 366 my $inherit_trace = delete $params->{inherit_trace};
673  
674 366 my $ctx = context();
675  
676 366 my $parent = $ctx->hub;
677  
678 # If a parent is buffered then the child must be as well.
679 366 ▶ my $buffered = $params->{buffered} || $parent->{buffered};
Condition100%3/3
$params->{'buffered'} || $parent->{'buffered'}
l!l&&r!l&&!r
339126
MC/DC100%2/2
$params->{'buffered'} || $parent->{'buffered'}
$params->{'buffered'} $parent->{'buffered'}
Truth table100%3/3
A || B
A= $params->{'buffered'}B= $parent->{'buffered'}
ABresult
1X1
011
000
680  
681 366 ▶ $ctx->note($name) unless $buffered;
Branch: 1 branch
BranchTrueFalseTotal
unless $buffered 26 340 366
682  
683 366 ▶ my $stack = $ctx->stack || $STACK;
Condition33%1/3
$ctx->stack || $STACK
l!l&&r!l&&!r
36600
MC/DC0%0/2
$ctx->stack || $STACK
$ctx->stack $STACK
Truth table33%1/3
A || B
A= $ctx->stackB= $STACK
ABresult
1X1
011
000
684 366 my $hub = $stack->new_hub(
685 class => 'Test2::Hub::Subtest',
686 %$params,
687 buffered => $buffered,
688 );
689  
690 366 my @events;
691 366 ▶ $hub->listen(sub { push @events => $_[1] });
Subroutine __ANON__: called CC=1 SCAR=0.0
692  
693 366 ▶ if ($buffered) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buffered) 340 26 366
694 340 ▶ if (my $format = $hub->format) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $format = $hub->format) 286 54 340
695 286 ▶ my $hide = $format->can('hide_buffered') ? $format->hide_buffered : 1;
Branch: 1 branch
BranchTrueFalseTotal
$format->can('hide_buffered') ? : 285 1 286
696 286 ▶ $hub->format(undef) if $hide;
Branch: 1 branch
BranchTrueFalseTotal
if $hide 284 2 286
697 }
698 }
699  
700 366 ▶ if ($inherit_trace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inherit_trace) 5 361 366
701 5 my $orig = $code;
702 5 ▶ $code = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
703 5 my $base_trace = $ctx->trace;
704 5 my $trace = $base_trace->snapshot(nested => 1 + $base_trace->nested);
705 5 my $st_ctx = Test2::API::Context->new(
706 trace => $trace,
707 hub => $hub,
708 );
709 5 $st_ctx->do_in_context($orig, @args);
710 5 };
711 }
712  
713 366 my $start_stamp = time;
714  
715 366 my ($ok, $err, $finished);
716 T2_SUBTEST_WRAPPER: {
717 # Do not use 'try' cause it localizes __DIE__
718 366 $ok = eval { $code->(@args); 1 };
719 342 $err = $@;
720  
721 # They might have done 'BEGIN { skip_all => "whatever" }'
722 342 ▶ if (!$ok && $err =~ m/Label not found for "last T2_SUBTEST_WRAPPER"/ || (blessed($err) && blessed($err) eq 'Test::Builder::Exception')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ok and $err =~ /Label not found for "last T2_SUBTEST_WRAPPER"/ or builtin::blessed($err) and builtin::blessed($err) eq 'Test::Builder::Exception') { } 0 342 342
Condition44%4/9
not $ok and $err =~ /Label not found for "last T2_SUBTEST_WRAPPER"/ or builtin::blessed($err) and builtin::blessed($err) eq "Test::Builder::Exception"
l!l&&r!l&&!r
00342
not $ok and $err =~ /Label not found for "last T2_SUBTEST_WRAPPER"/
!ll&&!rl&&r
34110
builtin::blessed($err) and builtin::blessed($err) eq "Test::Builder::Exception"
!ll&&!rl&&r
34200
MC/DC0%0/4
not $ok and $err =~ /Label not found for "last T2_SUBTEST_WRAPPER"/ or builtin::blessed($err) and builtin::blessed($err) eq "Test::Builder::Exception"
not $ok $err =~ /Label not found for "last T2_SUBTEST_WRAPPER"/ builtin::blessed($err) builtin::blessed($err) eq "Test::Builder::Exception"
Truth table28%2/7
not(A) and B or C and D
A= not $okB= $err =~ /Label not found for "last T2_SUBTEST_WRAPPER"/C= builtin::blessed($err)D= builtin::blessed($err) eq "Test::Builder::Exception"
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
723 0 $ok = undef;
724 0 $err = undef;
725 }
726 else {
727 342 $finished = 1;
728 }
729 }
730  
731 348 my $stop_stamp = time;
732  
733 348 ▶ if ($params->{no_fork}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($params->{'no_fork'}) { } 2 346 348
elsif (not $parent->is_local || $parent->ipc) { } 0 346 346
734 2 ▶ if ($$ != $ctx->trace->pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$ != $ctx->trace->pid) 1 1 2
735 1 ▶ warn $ok ? "Forked inside subtest, but subtest never finished!\n" : $err;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 0 1 1
736 1 exit 255;
737 }
738  
739 1 ▶ if (get_tid() != $ctx->trace->tid) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 != $ctx->trace->tid) 0 1 1
740 0 ▶ warn $ok ? "Started new thread inside subtest, but thread never finished!\n" : $err;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 0 0 0
741 0 exit 255;
742 }
743 }
744 ▶ elsif (!$parent->is_local && !$parent->ipc) {
Condition33%1/3
$parent->is_local || $parent->ipc
l!l&&r!l&&!r
34600
MC/DC0%0/2
$parent->is_local || $parent->ipc
$parent->is_local $parent->ipc
Truth table33%1/3
A || B
A= $parent->is_localB= $parent->ipc
ABresult
1X1
011
000
745 0 ▶ warn $ok ? "A new process or thread was started inside subtest, but IPC is not enabled!\n" : $err;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 0 0 0
746 0 exit 255;
747 }
748  
749 347 $stack->pop($hub);
750  
751 347 my $trace = $ctx->trace;
752  
753 347 my $bailed = $hub->bailed_out;
754  
755 347 ▶ if (!$finished) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($finished) 6 341 347
756 6 ▶ if ($bailed && !$buffered) {
Branch: 2 branches
BranchTrueFalseTotal
if ($bailed and not $buffered) { } 1 5 6
elsif ($bailed and $buffered) { } 3 2 5
Condition100%3/3
$bailed and not $buffered
!ll&&!rl&&r
231
MC/DC100%2/2
$bailed and not $buffered
$bailed not $buffered
Truth table100%3/3
A and not(B)
A= $bailedB= not $buffered
ABresult
0X0
100
111
757 1 $ctx->bail($bailed->reason);
758 }
759 ▶ elsif ($bailed && $buffered) {
Condition66%2/3
$bailed and $buffered
!ll&&!rl&&r
203
MC/DC50%1/2
$bailed and $buffered
$bailed $buffered
Truth table66%2/3
A and B
A= $bailedB= $buffered
ABresult
0X0
100
111
760 3 $ok = 1;
761 }
762 else {
763 2 my $code = $hub->exit_code;
764 2 $ok = !$code;
765 2 ▶ $err = "Subtest ended with exit code $code" if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 0 2 2
766 }
767 }
768  
769 346 ▶ $hub->finalize($trace->snapshot(huuid => $hub->uuid, hid => $hub->hid, nested => $hub->nested, buffered => $buffered), 1)
Branch: 1 branch
BranchTrueFalseTotal
if $ok and not $hub->no_ending and not $hub->ended 344 2 346
Condition50%3/6
$ok and not $hub->no_ending and not $hub->ended
!ll&&!rl&&r
02344
$ok and not $hub->no_ending
!ll&&!rl&&r
00346
MC/DC33%1/3
$ok and not $hub->no_ending and not $hub->ended
$ok not $hub->no_ending not $hub->ended
Truth table50%2/4
A and not(B) and not(C)
A= $okB= not $hub->no_endingC= not $hub->ended
ABCresult
0XX0
10X0
1100
1111
770 if $ok
771 && !$hub->no_ending
772 && !$hub->ended;
773  
774 346 ▶ my $pass = $ok && $hub->is_passing;
Condition66%2/3
$ok && $hub->is_passing
!ll&&!rl&&r
016330
MC/DC50%1/2
$ok && $hub->is_passing
$ok $hub->is_passing
Truth table66%2/3
A && B
A= $okB= $hub->is_passing
ABresult
0X0
100
111
775 346 my $e = $ctx->build_event(
776 'Subtest',
777 pass => $pass,
778 name => $name,
779 subtest_id => $hub->id,
780 subtest_uuid => $hub->uuid,
781 buffered => $buffered,
782 subevents => \@events,
783 start_stamp => $start_stamp,
784 stop_stamp => $stop_stamp,
785 );
786  
787 346 my $plan_ok = $hub->check_plan;
788  
789 346 $ctx->hub->send($e);
790  
791 346 ▶ $ctx->failure_diag($e) unless $e->pass;
Branch: 1 branch
BranchTrueFalseTotal
unless $e->pass 16 330 346
792  
793 346 ▶ $ctx->diag("Caught exception in subtest: $err") unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 346 346
794  
795 346 ▶ $ctx->diag("Bad subtest plan, expected " . $hub->plan . " but ran " . $hub->count)
Branch: 1 branch
BranchTrueFalseTotal
if defined $plan_ok and not $plan_ok 2 344 346
Condition100%3/3
defined $plan_ok and not $plan_ok
!ll&&!rl&&r
103342
MC/DC100%2/2
defined $plan_ok and not $plan_ok
defined $plan_ok not $plan_ok
Truth table100%3/3
A and not(B)
A= defined $plan_okB= not $plan_ok
ABresult
0X0
100
111
796 if defined($plan_ok) && !$plan_ok;
797  
798 346 ▶ $ctx->bail($bailed->reason) if $bailed && $buffered;
Branch: 1 branch
BranchTrueFalseTotal
if $bailed and $buffered 3 343 346
Condition66%2/3
$bailed and $buffered
!ll&&!rl&&r
34303
MC/DC50%1/2
$bailed and $buffered
$bailed $buffered
Truth table66%2/3
A and B
A= $bailedB= $buffered
ABresult
0X0
100
111
799  
800 343 $ctx->release;
801 343 return $pass;
802 }
803  
804 # There is a use-cycle between API and API/Context. Context needs to use some
805 # API functions as the package is compiling. Test2::API::context() needs
806 # Test2::API::Context to be loaded, but we cannot 'require' the module there as
807 # it causes a very noticeable performance impact with how often context() is
808 # called.
809 455 require Test2::API::Context;
810  
811 # If the env var was set to load plugins, load them now, this is the earliest
812 # safe point to do so.
813 455 ▶if (my $plugins = $ENV{TEST2_ENABLE_PLUGINS}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $plugins = $ENV{"TEST2_ENABLE_PLUGINS"}) 0 455 455
814 0 for my $p (split /\s*,\s*/, $plugins) {
815 0 ▶ $p = "Test2::Plugin::$p" unless $p =~ s/^\+//;
Branch: 1 branch
BranchTrueFalseTotal
unless $p =~ s/^\+// 0 0 0
816 0 my $mod = "$p.pm";
817 0 $mod =~ s{::}{/}g;
818  
819 0 ▶ if ($ENV{HARNESS_IS_VERBOSE} || !$ENV{HARNESS_ACTIVE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"HARNESS_IS_VERBOSE"} or not $ENV{"HARNESS_ACTIVE"}) 0 0 0
Condition0%0/3
$ENV{"HARNESS_IS_VERBOSE"} or not $ENV{"HARNESS_ACTIVE"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{"HARNESS_IS_VERBOSE"} or not $ENV{"HARNESS_ACTIVE"}
$ENV{"HARNESS_IS_VERBOSE"} not $ENV{"HARNESS_ACTIVE"}
Truth table0%0/3
A or not(B)
A= $ENV{"HARNESS_IS_VERBOSE"}B= not $ENV{"HARNESS_ACTIVE"}
ABresult
1X1
011
000
820 # If the harness is verbose then just display the message for all to
821 # see. It is nice info and they already asked for noisy output.
822  
823 0 ▶ test2_add_callback_post_load(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
824 0 test2_stack()->top; # Ensure we have at least 1 hub.
825 0 my ($hub) = test2_stack()->all;
826 0 $hub->send(
827 Test2::Event::Note->new(
828 trace => Test2::Util::Trace->new(frame => [__PACKAGE__, __FILE__, __LINE__, $p]),
829 message => "Loaded plugin '$p' as specified in the TEST2_ENABLE_PLUGINS env var.",
830 ),
831 );
832 0 });
833 }
834  
835 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { package main; require $mod; $p->import; 1 } } or die "Could not load plugin '${p}', which was specified in the TEST2_ENABLE_PLUGINS env var ($plugins): $@" 0 0 0
836 package main;
837 0 require $mod;
838 0 $p->import;
839 0 1
840 } or die "Could not load plugin '$p', which was specified in the TEST2_ENABLE_PLUGINS env var ($plugins): $@";
841 }
842 }
843  
844 455 1;
845  
846 __END__
« blib/lib/Test2/Tools/Spec.pm ↑ Summary blib/lib/Test2/Util.pm »