1package Test2::API;
2use strict;
3use warnings;
4
5use Test2::Util qw/USE_THREADS/;
6
7BEGIN {
8    $ENV{TEST_ACTIVE} ||= 1;
9    $ENV{TEST2_ACTIVE} = 1;
10}
11
12our $VERSION = '1.302133';
13
14
15my $INST;
16my $ENDING = 0;
17sub test2_set_is_end { ($ENDING) = @_ ? @_ : (1) }
18sub test2_get_is_end { $ENDING }
19
20use Test2::API::Instance(\$INST);
21
22# Set the exit status
23END {
24    test2_set_is_end(); # See gh #16
25    $INST->set_exit();
26}
27
28sub CLONE {
29    my $init = test2_init_done();
30    my $load = test2_load_done();
31
32    return if $init && $load;
33
34    require Carp;
35    Carp::croak "Test2 must be fully loaded before you start a new thread!\n";
36}
37
38# See gh #16
39{
40    no warnings;
41    INIT { eval 'END { test2_set_is_end() }; 1' or die $@ }
42}
43
44BEGIN {
45    no warnings 'once';
46    if($] ge '5.014' || $ENV{T2_CHECK_DEPTH} || $Test2::API::DO_DEPTH_CHECK) {
47        *DO_DEPTH_CHECK = sub() { 1 };
48    }
49    else {
50        *DO_DEPTH_CHECK = sub() { 0 };
51    }
52}
53
54use Test2::EventFacet::Trace();
55use Test2::Util::Trace(); # Legacy
56
57use Test2::Hub::Subtest();
58use Test2::Hub::Interceptor();
59use Test2::Hub::Interceptor::Terminator();
60
61use Test2::Event::Ok();
62use Test2::Event::Diag();
63use Test2::Event::Note();
64use Test2::Event::Plan();
65use Test2::Event::Bail();
66use Test2::Event::Exception();
67use Test2::Event::Waiting();
68use Test2::Event::Skip();
69use Test2::Event::Subtest();
70
71use Carp qw/carp croak confess/;
72use Scalar::Util qw/blessed weaken/;
73use Test2::Util qw/get_tid clone_io pkg_to_file/;
74
75our @EXPORT_OK = qw{
76    context release
77    context_do
78    no_context
79    intercept intercept_deep
80    run_subtest
81
82    test2_init_done
83    test2_load_done
84    test2_load
85    test2_start_preload
86    test2_stop_preload
87    test2_in_preload
88
89    test2_set_is_end
90    test2_get_is_end
91
92    test2_pid
93    test2_tid
94    test2_stack
95    test2_no_wait
96    test2_ipc_wait_enable
97    test2_ipc_wait_disable
98    test2_ipc_wait_enabled
99
100    test2_add_uuid_via
101
102    test2_add_callback_context_aquire
103    test2_add_callback_context_acquire
104    test2_add_callback_context_init
105    test2_add_callback_context_release
106    test2_add_callback_exit
107    test2_add_callback_post_load
108    test2_add_callback_pre_subtest
109    test2_list_context_aquire_callbacks
110    test2_list_context_acquire_callbacks
111    test2_list_context_init_callbacks
112    test2_list_context_release_callbacks
113    test2_list_exit_callbacks
114    test2_list_post_load_callbacks
115    test2_list_pre_subtest_callbacks
116
117    test2_ipc
118    test2_has_ipc
119    test2_ipc_disable
120    test2_ipc_disabled
121    test2_ipc_drivers
122    test2_ipc_add_driver
123    test2_ipc_polling
124    test2_ipc_disable_polling
125    test2_ipc_enable_polling
126    test2_ipc_get_pending
127    test2_ipc_set_pending
128    test2_ipc_get_timeout
129    test2_ipc_set_timeout
130    test2_ipc_enable_shm
131
132    test2_formatter
133    test2_formatters
134    test2_formatter_add
135    test2_formatter_set
136
137    test2_stdout
138    test2_stderr
139    test2_reset_io
140};
141BEGIN { require Exporter; our @ISA = qw(Exporter) }
142
143my $STACK       = $INST->stack;
144my $CONTEXTS    = $INST->contexts;
145my $INIT_CBS    = $INST->context_init_callbacks;
146my $ACQUIRE_CBS = $INST->context_acquire_callbacks;
147
148my $STDOUT = clone_io(\*STDOUT);
149my $STDERR = clone_io(\*STDERR);
150sub test2_stdout { $STDOUT ||= clone_io(\*STDOUT) }
151sub test2_stderr { $STDERR ||= clone_io(\*STDERR) }
152
153sub test2_post_preload_reset {
154    test2_reset_io();
155    $INST->post_preload_reset;
156}
157
158sub test2_reset_io {
159    $STDOUT = clone_io(\*STDOUT);
160    $STDERR = clone_io(\*STDERR);
161}
162
163sub test2_init_done { $INST->finalized }
164sub test2_load_done { $INST->loaded }
165
166sub test2_load          { $INST->load }
167sub test2_start_preload { $ENV{T2_IN_PRELOAD} = 1; $INST->start_preload }
168sub test2_stop_preload  { $ENV{T2_IN_PRELOAD} = 0; $INST->stop_preload }
169sub test2_in_preload    { $INST->preload }
170
171sub test2_pid              { $INST->pid }
172sub test2_tid              { $INST->tid }
173sub test2_stack            { $INST->stack }
174sub test2_ipc_wait_enable  { $INST->set_no_wait(0) }
175sub test2_ipc_wait_disable { $INST->set_no_wait(1) }
176sub test2_ipc_wait_enabled { !$INST->no_wait }
177
178sub test2_no_wait {
179    $INST->set_no_wait(@_) if @_;
180    $INST->no_wait;
181}
182
183sub test2_add_callback_context_acquire   { $INST->add_context_acquire_callback(@_) }
184sub test2_add_callback_context_aquire    { $INST->add_context_acquire_callback(@_) }
185sub test2_add_callback_context_init      { $INST->add_context_init_callback(@_) }
186sub test2_add_callback_context_release   { $INST->add_context_release_callback(@_) }
187sub test2_add_callback_exit              { $INST->add_exit_callback(@_) }
188sub test2_add_callback_post_load         { $INST->add_post_load_callback(@_) }
189sub test2_add_callback_pre_subtest       { $INST->add_pre_subtest_callback(@_) }
190sub test2_list_context_aquire_callbacks  { @{$INST->context_acquire_callbacks} }
191sub test2_list_context_acquire_callbacks { @{$INST->context_acquire_callbacks} }
192sub test2_list_context_init_callbacks    { @{$INST->context_init_callbacks} }
193sub test2_list_context_release_callbacks { @{$INST->context_release_callbacks} }
194sub test2_list_exit_callbacks            { @{$INST->exit_callbacks} }
195sub test2_list_post_load_callbacks       { @{$INST->post_load_callbacks} }
196sub test2_list_pre_subtest_callbacks     { @{$INST->pre_subtest_callbacks} }
197
198sub test2_add_uuid_via {
199    $INST->set_add_uuid_via(@_) if @_;
200    $INST->add_uuid_via();
201}
202
203sub test2_ipc                 { $INST->ipc }
204sub test2_has_ipc             { $INST->has_ipc }
205sub test2_ipc_disable         { $INST->ipc_disable }
206sub test2_ipc_disabled        { $INST->ipc_disabled }
207sub test2_ipc_add_driver      { $INST->add_ipc_driver(@_) }
208sub test2_ipc_drivers         { @{$INST->ipc_drivers} }
209sub test2_ipc_polling         { $INST->ipc_polling }
210sub test2_ipc_enable_polling  { $INST->enable_ipc_polling }
211sub test2_ipc_disable_polling { $INST->disable_ipc_polling }
212sub test2_ipc_get_pending     { $INST->get_ipc_pending }
213sub test2_ipc_set_pending     { $INST->set_ipc_pending(@_) }
214sub test2_ipc_set_timeout     { $INST->set_ipc_timeout(@_) }
215sub test2_ipc_get_timeout     { $INST->ipc_timeout() }
216sub test2_ipc_enable_shm      { $INST->ipc_enable_shm }
217
218sub test2_formatter     {
219    if ($ENV{T2_FORMATTER} && $ENV{T2_FORMATTER} =~ m/^(\+)?(.*)$/) {
220        my $formatter = $1 ? $2 : "Test2::Formatter::$2";
221        my $file = pkg_to_file($formatter);
222        require $file;
223        return $formatter;
224    }
225
226    return $INST->formatter;
227}
228
229sub test2_formatters    { @{$INST->formatters} }
230sub test2_formatter_add { $INST->add_formatter(@_) }
231sub test2_formatter_set {
232    my ($formatter) = @_;
233    croak "No formatter specified" unless $formatter;
234    croak "Global Formatter already set" if $INST->formatter_set;
235    $INST->set_formatter($formatter);
236}
237
238# Private, for use in Test2::API::Context
239sub _contexts_ref                  { $INST->contexts }
240sub _context_acquire_callbacks_ref { $INST->context_acquire_callbacks }
241sub _context_init_callbacks_ref    { $INST->context_init_callbacks }
242sub _context_release_callbacks_ref { $INST->context_release_callbacks }
243sub _add_uuid_via_ref              { \($INST->{Test2::API::Instance::ADD_UUID_VIA()}) }
244
245# Private, for use in Test2::IPC
246sub _set_ipc { $INST->set_ipc(@_) }
247
248sub context_do(&;@) {
249    my $code = shift;
250    my @args = @_;
251
252    my $ctx = context(level => 1);
253
254    my $want = wantarray;
255
256    my @out;
257    my $ok = eval {
258        $want          ? @out    = $code->($ctx, @args) :
259        defined($want) ? $out[0] = $code->($ctx, @args) :
260                                   $code->($ctx, @args) ;
261        1;
262    };
263    my $err = $@;
264
265    $ctx->release;
266
267    die $err unless $ok;
268
269    return @out    if $want;
270    return $out[0] if defined $want;
271    return;
272}
273
274sub no_context(&;$) {
275    my ($code, $hid) = @_;
276    $hid ||= $STACK->top->hid;
277
278    my $ctx = $CONTEXTS->{$hid};
279    delete $CONTEXTS->{$hid};
280    my $ok = eval { $code->(); 1 };
281    my $err = $@;
282
283    $CONTEXTS->{$hid} = $ctx;
284    weaken($CONTEXTS->{$hid});
285
286    die $err unless $ok;
287
288    return;
289};
290
291my $UUID_VIA = _add_uuid_via_ref();
292my $CID = 1;
293sub context {
294    # We need to grab these before anything else to ensure they are not
295    # changed.
296    my ($errno, $eval_error, $child_error, $extended_error) = (0 + $!, $@, $?, $^E);
297
298    my %params = (level => 0, wrapped => 0, @_);
299
300    # If something is getting a context then the sync system needs to be
301    # considered loaded...
302    $INST->load unless $INST->{loaded};
303
304    croak "context() called, but return value is ignored"
305        unless defined wantarray;
306
307    my $stack   = $params{stack} || $STACK;
308    my $hub     = $params{hub}   || (@$stack ? $stack->[-1] : $stack->top);
309    my $hid     = $hub->{hid};
310    my $current = $CONTEXTS->{$hid};
311
312    $_->(\%params) for @$ACQUIRE_CBS;
313    map $_->(\%params), @{$hub->{_context_acquire}} if $hub->{_context_acquire};
314
315    # This is for https://github.com/Test-More/test-more/issues/16
316    # and https://rt.perl.org/Public/Bug/Display.html?id=127774
317    my $phase = ${^GLOBAL_PHASE} || 'NA';
318    my $end_phase = $ENDING || $phase eq 'END' || $phase eq 'DESTRUCT';
319
320    my $level = 1 + $params{level};
321    my ($pkg, $file, $line, $sub) = $end_phase ? caller(0) : caller($level);
322    unless ($pkg || $end_phase) {
323        confess "Could not find context at depth $level" unless $params{fudge};
324        ($pkg, $file, $line, $sub) = caller(--$level) while ($level >= 0 && !$pkg);
325    }
326
327    my $depth = $level;
328    $depth++ while DO_DEPTH_CHECK && !$end_phase && (!$current || $depth <= $current->{_depth} + $params{wrapped}) && caller($depth + 1);
329    $depth -= $params{wrapped};
330    my $depth_ok = !DO_DEPTH_CHECK || $end_phase || !$current || $current->{_depth} < $depth;
331
332    if ($current && $params{on_release} && $depth_ok) {
333        $current->{_on_release} ||= [];
334        push @{$current->{_on_release}} => $params{on_release};
335    }
336
337    # I know this is ugly....
338    ($!, $@, $?, $^E) = ($errno, $eval_error, $child_error, $extended_error) and return bless(
339        {
340            %$current,
341            _is_canon   => undef,
342            errno       => $errno,
343            eval_error  => $eval_error,
344            child_error => $child_error,
345            _is_spawn   => [$pkg, $file, $line, $sub],
346        },
347        'Test2::API::Context'
348    ) if $current && $depth_ok;
349
350    # Handle error condition of bad level
351    if ($current) {
352        unless (${$current->{_aborted}}) {
353            _canon_error($current, [$pkg, $file, $line, $sub, $depth])
354                unless $current->{_is_canon};
355
356            _depth_error($current, [$pkg, $file, $line, $sub, $depth])
357                unless $depth_ok;
358        }
359
360        $current->release if $current->{_is_canon};
361
362        delete $CONTEXTS->{$hid};
363    }
364
365    # Directly bless the object here, calling new is a noticeable performance
366    # hit with how often this needs to be called.
367    my $trace = bless(
368        {
369            frame  => [$pkg, $file, $line, $sub],
370            pid    => $$,
371            tid    => get_tid(),
372            cid    => 'C' . $CID++,
373            hid    => $hid,
374            nested => $hub->{nested},
375            buffered => $hub->{buffered},
376
377            $$UUID_VIA ? (
378                huuid => $hub->{uuid},
379                uuid  => ${$UUID_VIA}->('context'),
380            ) : (),
381        },
382        'Test2::EventFacet::Trace'
383    );
384
385    # Directly bless the object here, calling new is a noticeable performance
386    # hit with how often this needs to be called.
387    my $aborted = 0;
388    $current = bless(
389        {
390            _aborted     => \$aborted,
391            stack        => $stack,
392            hub          => $hub,
393            trace        => $trace,
394            _is_canon    => 1,
395            _depth       => $depth,
396            errno        => $errno,
397            eval_error   => $eval_error,
398            child_error  => $child_error,
399            $params{on_release} ? (_on_release => [$params{on_release}]) : (),
400        },
401        'Test2::API::Context'
402    );
403
404    $CONTEXTS->{$hid} = $current;
405    weaken($CONTEXTS->{$hid});
406
407    $_->($current) for @$INIT_CBS;
408    map $_->($current), @{$hub->{_context_init}} if $hub->{_context_init};
409
410    $params{on_init}->($current) if $params{on_init};
411
412    ($!, $@, $?, $^E) = ($errno, $eval_error, $child_error, $extended_error);
413
414    return $current;
415}
416
417sub _depth_error {
418    _existing_error(@_, <<"    EOT");
419context() was called to retrieve an existing context, however the existing
420context was created in a stack frame at the same, or deeper level. This usually
421means that a tool failed to release the context when it was finished.
422    EOT
423}
424
425sub _canon_error {
426    _existing_error(@_, <<"    EOT");
427context() was called to retrieve an existing context, however the existing
428context has an invalid internal state (!_canon_count). This should not normally
429happen unless something is mucking about with internals...
430    EOT
431}
432
433sub _existing_error {
434    my ($ctx, $details, $msg) = @_;
435    my ($pkg, $file, $line, $sub, $depth) = @$details;
436
437    my $oldframe = $ctx->{trace}->frame;
438    my $olddepth = $ctx->{_depth};
439
440    # Older versions of Carp do not export longmess() function, so it needs to be called with package name
441    my $mess = Carp::longmess();
442
443    warn <<"    EOT";
444$msg
445Old context details:
446   File: $oldframe->[1]
447   Line: $oldframe->[2]
448   Tool: $oldframe->[3]
449  Depth: $olddepth
450
451New context details:
452   File: $file
453   Line: $line
454   Tool: $sub
455  Depth: $depth
456
457Trace: $mess
458
459Removing the old context and creating a new one...
460    EOT
461}
462
463sub release($;$) {
464    $_[0]->release;
465    return $_[1];
466}
467
468sub intercept(&) {
469    my $code = shift;
470    my $ctx = context();
471
472    my $events = _intercept($code, deep => 0);
473
474    $ctx->release;
475
476    return $events;
477}
478
479sub intercept_deep(&) {
480    my $code = shift;
481    my $ctx = context();
482
483    my $events = _intercept($code, deep => 1);
484
485    $ctx->release;
486
487    return $events;
488}
489
490sub _intercept {
491    my $code = shift;
492    my %params = @_;
493    my $ctx = context();
494
495    my $ipc;
496    if (my $global_ipc = test2_ipc()) {
497        my $driver = blessed($global_ipc);
498        $ipc = $driver->new;
499    }
500
501    my $hub = Test2::Hub::Interceptor->new(
502        ipc => $ipc,
503        no_ending => 1,
504    );
505
506    my @events;
507    $hub->listen(sub { push @events => $_[1] }, inherit => $params{deep});
508
509    $ctx->stack->top; # Make sure there is a top hub before we begin.
510    $ctx->stack->push($hub);
511
512    my ($ok, $err) = (1, undef);
513    T2_SUBTEST_WRAPPER: {
514        # Do not use 'try' cause it localizes __DIE__
515        $ok = eval { $code->(hub => $hub, context => $ctx->snapshot); 1 };
516        $err = $@;
517
518        # They might have done 'BEGIN { skip_all => "whatever" }'
519        if (!$ok && $err =~ m/Label not found for "last T2_SUBTEST_WRAPPER"/ || (blessed($err) && $err->isa('Test2::Hub::Interceptor::Terminator'))) {
520            $ok  = 1;
521            $err = undef;
522        }
523    }
524
525    $hub->cull;
526    $ctx->stack->pop($hub);
527
528    my $trace = $ctx->trace;
529    $ctx->release;
530
531    die $err unless $ok;
532
533    $hub->finalize($trace, 1)
534        if $ok
535        && !$hub->no_ending
536        && !$hub->ended;
537
538    return \@events;
539}
540
541sub run_subtest {
542    my ($name, $code, $params, @args) = @_;
543
544    $_->($name,$code,@args)
545        for Test2::API::test2_list_pre_subtest_callbacks();
546
547    $params = {buffered => $params} unless ref $params;
548    my $inherit_trace = delete $params->{inherit_trace};
549
550    my $ctx = context();
551
552    my $parent = $ctx->hub;
553
554    # If a parent is buffered then the child must be as well.
555    my $buffered = $params->{buffered} || $parent->{buffered};
556
557    $ctx->note($name) unless $buffered;
558
559    my $stack = $ctx->stack || $STACK;
560    my $hub = $stack->new_hub(
561        class => 'Test2::Hub::Subtest',
562        %$params,
563        buffered => $buffered,
564    );
565
566    my @events;
567    $hub->listen(sub { push @events => $_[1] });
568
569    if ($buffered) {
570        if (my $format = $hub->format) {
571            my $hide = $format->can('hide_buffered') ? $format->hide_buffered : 1;
572            $hub->format(undef) if $hide;
573        }
574    }
575
576    if ($inherit_trace) {
577        my $orig = $code;
578        $code = sub {
579            my $base_trace = $ctx->trace;
580            my $trace = $base_trace->snapshot(nested => 1 + $base_trace->nested);
581            my $st_ctx = Test2::API::Context->new(
582                trace  => $trace,
583                hub    => $hub,
584            );
585            $st_ctx->do_in_context($orig, @args);
586        };
587    }
588
589    my ($ok, $err, $finished);
590    T2_SUBTEST_WRAPPER: {
591        # Do not use 'try' cause it localizes __DIE__
592        $ok = eval { $code->(@args); 1 };
593        $err = $@;
594
595        # They might have done 'BEGIN { skip_all => "whatever" }'
596        if (!$ok && $err =~ m/Label not found for "last T2_SUBTEST_WRAPPER"/ || (blessed($err) && blessed($err) eq 'Test::Builder::Exception')) {
597            $ok  = undef;
598            $err = undef;
599        }
600        else {
601            $finished = 1;
602        }
603    }
604
605    if ($params->{no_fork}) {
606        if ($$ != $ctx->trace->pid) {
607            warn $ok ? "Forked inside subtest, but subtest never finished!\n" : $err;
608            exit 255;
609        }
610
611        if (get_tid() != $ctx->trace->tid) {
612            warn $ok ? "Started new thread inside subtest, but thread never finished!\n" : $err;
613            exit 255;
614        }
615    }
616    elsif (!$parent->is_local && !$parent->ipc) {
617        warn $ok ? "A new process or thread was started inside subtest, but IPC is not enabled!\n" : $err;
618        exit 255;
619    }
620
621    $stack->pop($hub);
622
623    my $trace = $ctx->trace;
624
625    my $bailed = $hub->bailed_out;
626
627    if (!$finished) {
628        if ($bailed && !$buffered) {
629            $ctx->bail($bailed->reason);
630        }
631        elsif ($bailed && $buffered) {
632            $ok = 1;
633        }
634        else {
635            my $code = $hub->exit_code;
636            $ok = !$code;
637            $err = "Subtest ended with exit code $code" if $code;
638        }
639    }
640
641    $hub->finalize($trace->snapshot(huuid => $hub->uuid, hid => $hub->hid, nested => $hub->nested, buffered => $buffered), 1)
642        if $ok
643        && !$hub->no_ending
644        && !$hub->ended;
645
646    my $pass = $ok && $hub->is_passing;
647    my $e = $ctx->build_event(
648        'Subtest',
649        pass         => $pass,
650        name         => $name,
651        subtest_id   => $hub->id,
652        subtest_uuid => $hub->uuid,
653        buffered     => $buffered,
654        subevents    => \@events,
655    );
656
657    my $plan_ok = $hub->check_plan;
658
659    $ctx->hub->send($e);
660
661    $ctx->failure_diag($e) unless $e->pass;
662
663    $ctx->diag("Caught exception in subtest: $err") unless $ok;
664
665    $ctx->diag("Bad subtest plan, expected " . $hub->plan . " but ran " . $hub->count)
666        if defined($plan_ok) && !$plan_ok;
667
668    $ctx->bail($bailed->reason) if $bailed && $buffered;
669
670    $ctx->release;
671    return $pass;
672}
673
674# There is a use-cycle between API and API/Context. Context needs to use some
675# API functions as the package is compiling. Test2::API::context() needs
676# Test2::API::Context to be loaded, but we cannot 'require' the module there as
677# it causes a very noticeable performance impact with how often context() is
678# called.
679require Test2::API::Context;
680
6811;
682
683__END__
684
685=pod
686
687=encoding UTF-8
688
689=head1 NAME
690
691Test2::API - Primary interface for writing Test2 based testing tools.
692
693=head1 ***INTERNALS NOTE***
694
695B<The internals of this package are subject to change at any time!> The public
696methods provided will not change in backwards-incompatible ways (once there is
697a stable release), but the underlying implementation details might.
698B<Do not break encapsulation here!>
699
700Currently the implementation is to create a single instance of the
701L<Test2::API::Instance> Object. All class methods defer to the single
702instance. There is no public access to the singleton, and that is intentional.
703The class methods provided by this package provide the only functionality
704publicly exposed.
705
706This is done primarily to avoid the problems Test::Builder had by exposing its
707singleton. We do not want anyone to replace this singleton, rebless it, or
708directly muck with its internals. If you need to do something and cannot
709because of the restrictions placed here, then please report it as an issue. If
710possible, we will create a way for you to implement your functionality without
711exposing things that should not be exposed.
712
713=head1 DESCRIPTION
714
715This package exports all the functions necessary to write and/or verify testing
716tools. Using these building blocks you can begin writing test tools very
717quickly. You are also provided with tools that help you to test the tools you
718write.
719
720=head1 SYNOPSIS
721
722=head2 WRITING A TOOL
723
724The C<context()> method is your primary interface into the Test2 framework.
725
726    package My::Ok;
727    use Test2::API qw/context/;
728
729    our @EXPORT = qw/my_ok/;
730    use base 'Exporter';
731
732    # Just like ok() from Test::More
733    sub my_ok($;$) {
734        my ($bool, $name) = @_;
735        my $ctx = context(); # Get a context
736        $ctx->ok($bool, $name);
737        $ctx->release; # Release the context
738        return $bool;
739    }
740
741See L<Test2::API::Context> for a list of methods available on the context object.
742
743=head2 TESTING YOUR TOOLS
744
745The C<intercept { ... }> tool lets you temporarily intercept all events
746generated by the test system:
747
748    use Test2::API qw/intercept/;
749
750    use My::Ok qw/my_ok/;
751
752    my $events = intercept {
753        # These events are not displayed
754        my_ok(1, "pass");
755        my_ok(0, "fail");
756    };
757
758    my_ok(@$events == 2, "got 2 events, the pass and the fail");
759    my_ok($events->[0]->pass, "first event passed");
760    my_ok(!$events->[1]->pass, "second event failed");
761
762=head3 DEEP EVENT INTERCEPTION
763
764Normally C<intercept { ... }> only intercepts events sent to the main hub (as
765added by intercept itself). Nested hubs, such as those created by subtests,
766will not be intercepted. This is normally what you will still see the nested
767events by inspecting the subtest event. However there are times where you want
768to verify each event as it is sent, in that case use C<intercept_deep { ... }>.
769
770    my $events = intercept_Deep {
771        buffered_subtest foo => sub {
772            ok(1, "pass");
773        };
774    };
775
776C<$events> in this case will contain 3 items:
777
778=over 4
779
780=item The event from C<ok(1, "pass")>
781
782=item The plan event for the subtest
783
784=item The subtest event itself, with the first 2 events nested inside it as children.
785
786=back
787
788This lets you see the order in which the events were sent, unlike
789C<intercept { ... }> which only lets you see events as the main hub sees them.
790
791=head2 OTHER API FUNCTIONS
792
793    use Test2::API qw{
794        test2_init_done
795        test2_stack
796        test2_set_is_end
797        test2_get_is_end
798        test2_ipc
799        test2_formatter_set
800        test2_formatter
801    };
802
803    my $init  = test2_init_done();
804    my $stack = test2_stack();
805    my $ipc   = test2_ipc();
806
807    test2_formatter_set($FORMATTER)
808    my $formatter = test2_formatter();
809
810    ... And others ...
811
812=head1 MAIN API EXPORTS
813
814All exports are optional. You must specify subs to import.
815
816    use Test2::API qw/context intercept run_subtest/;
817
818This is the list of exports that are most commonly needed. If you are simply
819writing a tool, then this is probably all you need. If you need something and
820you cannot find it here, then you can also look at L</OTHER API EXPORTS>.
821
822These exports lack the 'test2_' prefix because of how important/common they
823are. Exports in the L</OTHER API EXPORTS> section have the 'test2_' prefix to
824ensure they stand out.
825
826=head2 context(...)
827
828Usage:
829
830=over 4
831
832=item $ctx = context()
833
834=item $ctx = context(%params)
835
836=back
837
838The C<context()> function will always return the current context. If
839there is already a context active, it will be returned. If there is not an
840active context, one will be generated. When a context is generated it will
841default to using the file and line number where the currently running sub was
842called from.
843
844Please see L<Test2::API::Context/"CRITICAL DETAILS"> for important rules about
845what you can and cannot do with a context once it is obtained.
846
847B<Note> This function will throw an exception if you ignore the context object
848it returns.
849
850B<Note> On perls 5.14+ a depth check is used to insure there are no context
851leaks. This cannot be safely done on older perls due to
852L<https://rt.perl.org/Public/Bug/Display.html?id=127774>
853You can forcefully enable it either by setting C<$ENV{T2_CHECK_DEPTH} = 1> or
854C<$Test2::API::DO_DEPTH_CHECK = 1> B<BEFORE> loading L<Test2::API>.
855
856=head3 OPTIONAL PARAMETERS
857
858All parameters to C<context> are optional.
859
860=over 4
861
862=item level => $int
863
864If you must obtain a context in a sub deeper than your entry point you can use
865this to tell it how many EXTRA stack frames to look back. If this option is not
866provided the default of C<0> is used.
867
868    sub third_party_tool {
869        my $sub = shift;
870        ... # Does not obtain a context
871        $sub->();
872        ...
873    }
874
875    third_party_tool(sub {
876        my $ctx = context(level => 1);
877        ...
878        $ctx->release;
879    });
880
881=item wrapped => $int
882
883Use this if you need to write your own tool that wraps a call to C<context()>
884with the intent that it should return a context object.
885
886    sub my_context {
887        my %params = ( wrapped => 0, @_ );
888        $params{wrapped}++;
889        my $ctx = context(%params);
890        ...
891        return $ctx;
892    }
893
894    sub my_tool {
895        my $ctx = my_context();
896        ...
897        $ctx->release;
898    }
899
900If you do not do this, then tools you call that also check for a context will
901notice that the context they grabbed was created at the same stack depth, which
902will trigger protective measures that warn you and destroy the existing
903context.
904
905=item stack => $stack
906
907Normally C<context()> looks at the global hub stack. If you are maintaining
908your own L<Test2::API::Stack> instance you may pass it in to be used
909instead of the global one.
910
911=item hub => $hub
912
913Use this parameter if you want to obtain the context for a specific hub instead
914of whatever one happens to be at the top of the stack.
915
916=item on_init => sub { ... }
917
918This lets you provide a callback sub that will be called B<ONLY> if your call
919to C<context()> generated a new context. The callback B<WILL NOT> be called if
920C<context()> is returning an existing context. The only argument passed into
921the callback will be the context object itself.
922
923    sub foo {
924        my $ctx = context(on_init => sub { 'will run' });
925
926        my $inner = sub {
927            # This callback is not run since we are getting the existing
928            # context from our parent sub.
929            my $ctx = context(on_init => sub { 'will NOT run' });
930            $ctx->release;
931        }
932        $inner->();
933
934        $ctx->release;
935    }
936
937=item on_release => sub { ... }
938
939This lets you provide a callback sub that will be called when the context
940instance is released. This callback will be added to the returned context even
941if an existing context is returned. If multiple calls to context add callbacks,
942then all will be called in reverse order when the context is finally released.
943
944    sub foo {
945        my $ctx = context(on_release => sub { 'will run second' });
946
947        my $inner = sub {
948            my $ctx = context(on_release => sub { 'will run first' });
949
950            # Neither callback runs on this release
951            $ctx->release;
952        }
953        $inner->();
954
955        # Both callbacks run here.
956        $ctx->release;
957    }
958
959=back
960
961=head2 release($;$)
962
963Usage:
964
965=over 4
966
967=item release $ctx;
968
969=item release $ctx, ...;
970
971=back
972
973This is intended as a shortcut that lets you release your context and return a
974value in one statement. This function will get your context, and an optional
975return value. It will release your context, then return your value. Scalar
976context is always assumed.
977
978    sub tool {
979        my $ctx = context();
980        ...
981
982        return release $ctx, 1;
983    }
984
985This tool is most useful when you want to return the value you get from calling
986a function that needs to see the current context:
987
988    my $ctx = context();
989    my $out = some_tool(...);
990    $ctx->release;
991    return $out;
992
993We can combine the last 3 lines of the above like so:
994
995    my $ctx = context();
996    release $ctx, some_tool(...);
997
998=head2 context_do(&;@)
999
1000Usage:
1001
1002    sub my_tool {
1003        context_do {
1004            my $ctx = shift;
1005
1006            my (@args) = @_;
1007
1008            $ctx->ok(1, "pass");
1009
1010            ...
1011
1012            # No need to call $ctx->release, done for you on scope exit.
1013        } @_;
1014    }
1015
1016Using this inside your test tool takes care of a lot of boilerplate for you. It
1017will ensure a context is acquired. It will capture and rethrow any exception. It
1018will insure the context is released when you are done. It preserves the
1019subroutine call context (array, scalar, void).
1020
1021This is the safest way to write a test tool. The only two downsides to this are a
1022slight performance decrease, and some extra indentation in your source. If the
1023indentation is a problem for you then you can take a peek at the next section.
1024
1025=head2 no_context(&;$)
1026
1027Usage:
1028
1029=over 4
1030
1031=item no_context { ... };
1032
1033=item no_context { ... } $hid;
1034
1035    sub my_tool(&) {
1036        my $code = shift;
1037        my $ctx = context();
1038        ...
1039
1040        no_context {
1041            # Things in here will not see our current context, they get a new
1042            # one.
1043
1044            $code->();
1045        };
1046
1047        ...
1048        $ctx->release;
1049    };
1050
1051=back
1052
1053This tool will hide a context for the provided block of code. This means any
1054tools run inside the block will get a completely new context if they acquire
1055one. The new context will be inherited by tools nested below the one that
1056acquired it.
1057
1058This will normally hide the current context for the top hub. If you need to
1059hide the context for a different hub you can pass in the optional C<$hid>
1060parameter.
1061
1062=head2 intercept(&)
1063
1064Usage:
1065
1066    my $events = intercept {
1067        ok(1, "pass");
1068        ok(0, "fail");
1069        ...
1070    };
1071
1072This function takes a codeblock as its only argument, and it has a prototype.
1073It will execute the codeblock, intercepting any generated events in the
1074process. It will return an array reference with all the generated event
1075objects. All events should be subclasses of L<Test2::Event>.
1076
1077This is a very low-level subtest tool. This is useful for writing tools which
1078produce subtests. This is not intended for people simply writing tests.
1079
1080=head2 run_subtest(...)
1081
1082Usage:
1083
1084    run_subtest($NAME, \&CODE, $BUFFERED, @ARGS)
1085
1086    # or
1087
1088    run_subtest($NAME, \&CODE, \%PARAMS, @ARGS)
1089
1090This will run the provided codeblock with the args in C<@args>. This codeblock
1091will be run as a subtest. A subtest is an isolated test state that is condensed
1092into a single L<Test2::Event::Subtest> event, which contains all events
1093generated inside the subtest.
1094
1095=head3 ARGUMENTS:
1096
1097=over 4
1098
1099=item $NAME
1100
1101The name of the subtest.
1102
1103=item \&CODE
1104
1105The code to run inside the subtest.
1106
1107=item $BUFFERED or \%PARAMS
1108
1109If this is a simple scalar then it will be treated as a boolean for the
1110'buffered' setting. If this is a hash reference then it will be used as a
1111parameters hash. The param hash will be used for hub construction (with the
1112specified keys removed).
1113
1114Keys that are removed and used by run_subtest:
1115
1116=over 4
1117
1118=item 'buffered' => $bool
1119
1120Toggle buffered status.
1121
1122=item 'inherit_trace' => $bool
1123
1124Normally the subtest hub is pushed and the sub is allowed to generate its own
1125root context for the hub. When this setting is turned on a root context will be
1126created for the hub that shares the same trace as the current context.
1127
1128Set this to true if your tool is producing subtests without user-specified
1129subs.
1130
1131=item 'no_fork' => $bool
1132
1133Defaults to off. Normally forking inside a subtest will actually fork the
1134subtest, resulting in 2 final subtest events. This parameter will turn off that
1135behavior, only the original process/thread will return a final subtest event.
1136
1137=back
1138
1139=item @ARGS
1140
1141Any extra arguments you want passed into the subtest code.
1142
1143=back
1144
1145=head3 BUFFERED VS UNBUFFERED (OR STREAMED)
1146
1147Normally all events inside and outside a subtest are sent to the formatter
1148immediately by the hub. Sometimes it is desirable to hold off sending events
1149within a subtest until the subtest is complete. This usually depends on the
1150formatter being used.
1151
1152=over 4
1153
1154=item Things not effected by this flag
1155
1156In both cases events are generated and stored in an array. This array is
1157eventually used to populate the C<subevents> attribute on the
1158L<Test2::Event::Subtest> event that is generated at the end of the subtest.
1159This flag has no effect on this part, it always happens.
1160
1161At the end of the subtest, the final L<Test2::Event::Subtest> event is sent to
1162the formatter.
1163
1164=item Things that are effected by this flag
1165
1166The C<buffered> attribute of the L<Test2::Event::Subtest> event will be set to
1167the value of this flag. This means any formatter, listener, etc which looks at
1168the event will know if it was buffered.
1169
1170=item Things that are formatter dependant
1171
1172Events within a buffered subtest may or may not be sent to the formatter as
1173they happen. If a formatter fails to specify then the default is to B<NOT SEND>
1174the events as they are generated, instead the formatter can pull them from the
1175C<subevents> attribute.
1176
1177A formatter can specify by implementing the C<hide_buffered()> method. If this
1178method returns true then events generated inside a buffered subtest will not be
1179sent independently of the final subtest event.
1180
1181=back
1182
1183An example of how this is used is the L<Test2::Formatter::TAP> formatter. For
1184unbuffered subtests the events are rendered as they are generated. At the end
1185of the subtest, the final subtest event is rendered, but the C<subevents>
1186attribute is ignored. For buffered subtests the opposite occurs, the events are
1187NOT rendered as they are generated, instead the C<subevents> attribute is used
1188to render them all at once. This is useful when running subtests tests in
1189parallel, since without it the output from subtests would be interleaved
1190together.
1191
1192=head1 OTHER API EXPORTS
1193
1194Exports in this section are not commonly needed. These all have the 'test2_'
1195prefix to help ensure they stand out. You should look at the L</MAIN API
1196EXPORTS> section before looking here. This section is one where "Great power
1197comes with great responsibility". It is possible to break things badly if you
1198are not careful with these.
1199
1200All exports are optional. You need to list which ones you want at import time:
1201
1202    use Test2::API qw/test2_init_done .../;
1203
1204=head2 STATUS AND INITIALIZATION STATE
1205
1206These provide access to internal state and object instances.
1207
1208=over 4
1209
1210=item $bool = test2_init_done()
1211
1212This will return true if the stack and IPC instances have already been
1213initialized. It will return false if they have not. Init happens as late as
1214possible. It happens as soon as a tool requests the IPC instance, the
1215formatter, or the stack.
1216
1217=item $bool = test2_load_done()
1218
1219This will simply return the boolean value of the loaded flag. If Test2 has
1220finished loading this will be true, otherwise false. Loading is considered
1221complete the first time a tool requests a context.
1222
1223=item test2_set_is_end()
1224
1225=item test2_set_is_end($bool)
1226
1227This is used to toggle Test2's belief that the END phase has already started.
1228With no arguments this will set it to true. With arguments it will set it to
1229the first argument's value.
1230
1231This is used to prevent the use of C<caller()> in END blocks which can cause
1232segfaults. This is only necessary in some persistent environments that may have
1233multiple END phases.
1234
1235=item $bool = test2_get_is_end()
1236
1237Check if Test2 believes it is the END phase.
1238
1239=item $stack = test2_stack()
1240
1241This will return the global L<Test2::API::Stack> instance. If this has not
1242yet been initialized it will be initialized now.
1243
1244=item test2_ipc_disable
1245
1246Disable IPC.
1247
1248=item $bool = test2_ipc_diabled
1249
1250Check if IPC is disabled.
1251
1252=item test2_ipc_wait_enable()
1253
1254=item test2_ipc_wait_disable()
1255
1256=item $bool = test2_ipc_wait_enabled()
1257
1258These can be used to turn IPC waiting on and off, or check the current value of
1259the flag.
1260
1261Waiting is turned on by default. Waiting will cause the parent process/thread
1262to wait until all child processes and threads are finished before exiting. You
1263will almost never want to turn this off.
1264
1265=item $bool = test2_no_wait()
1266
1267=item test2_no_wait($bool)
1268
1269B<DISCOURAGED>: This is a confusing interface, it is better to use
1270C<test2_ipc_wait_enable()>, C<test2_ipc_wait_disable()> and
1271C<test2_ipc_wait_enabled()>.
1272
1273This can be used to get/set the no_wait status. Waiting is turned on by
1274default. Waiting will cause the parent process/thread to wait until all child
1275processes and threads are finished before exiting. You will almost never want
1276to turn this off.
1277
1278=item $fh = test2_stdout()
1279
1280=item $fh = test2_stderr()
1281
1282These functions return the filehandles that test output should be written to.
1283They are primarily useful when writing a custom formatter and code that turns
1284events into actual output (TAP, etc.)  They will return a dupe of the original
1285filehandles that formatted output can be sent to regardless of whatever state
1286the currently running test may have left STDOUT and STDERR in.
1287
1288=item test2_reset_io()
1289
1290Re-dupe the internal filehandles returned by C<test2_stdout()> and
1291C<test2_stderr()> from the current STDOUT and STDERR.  You shouldn't need to do
1292this except in very peculiar situations (for example, you're testing a new
1293formatter and you need control over where the formatter is sending its output.)
1294
1295=back
1296
1297=head2 BEHAVIOR HOOKS
1298
1299These are hooks that allow you to add custom behavior to actions taken by Test2
1300and tools built on top of it.
1301
1302=over 4
1303
1304=item test2_add_callback_exit(sub { ... })
1305
1306This can be used to add a callback that is called after all testing is done. This
1307is too late to add additional results, the main use of this callback is to set the
1308exit code.
1309
1310    test2_add_callback_exit(
1311        sub {
1312            my ($context, $exit, \$new_exit) = @_;
1313            ...
1314        }
1315    );
1316
1317The C<$context> passed in will be an instance of L<Test2::API::Context>. The
1318C<$exit> argument will be the original exit code before anything modified it.
1319C<$$new_exit> is a reference to the new exit code. You may modify this to
1320change the exit code. Please note that C<$$new_exit> may already be different
1321from C<$exit>
1322
1323=item test2_add_callback_post_load(sub { ... })
1324
1325Add a callback that will be called when Test2 is finished loading. This
1326means the callback will be run once, the first time a context is obtained.
1327If Test2 has already finished loading then the callback will be run immediately.
1328
1329=item test2_add_callback_context_acquire(sub { ... })
1330
1331Add a callback that will be called every time someone tries to acquire a
1332context. This will be called on EVERY call to C<context()>. It gets a single
1333argument, a reference to the hash of parameters being used the construct the
1334context. This is your chance to change the parameters by directly altering the
1335hash.
1336
1337    test2_add_callback_context_acquire(sub {
1338        my $params = shift;
1339        $params->{level}++;
1340    });
1341
1342This is a very scary API function. Please do not use this unless you need to.
1343This is here for L<Test::Builder> and backwards compatibility. This has you
1344directly manipulate the hash instead of returning a new one for performance
1345reasons.
1346
1347=item test2_add_callback_context_init(sub { ... })
1348
1349Add a callback that will be called every time a new context is created. The
1350callback will receive the newly created context as its only argument.
1351
1352=item test2_add_callback_context_release(sub { ... })
1353
1354Add a callback that will be called every time a context is released. The
1355callback will receive the released context as its only argument.
1356
1357=item test2_add_callback_pre_subtest(sub { ... })
1358
1359Add a callback that will be called every time a subtest is going to be
1360run. The callback will receive the subtest name, coderef, and any
1361arguments.
1362
1363=item @list = test2_list_context_acquire_callbacks()
1364
1365Return all the context acquire callback references.
1366
1367=item @list = test2_list_context_init_callbacks()
1368
1369Returns all the context init callback references.
1370
1371=item @list = test2_list_context_release_callbacks()
1372
1373Returns all the context release callback references.
1374
1375=item @list = test2_list_exit_callbacks()
1376
1377Returns all the exit callback references.
1378
1379=item @list = test2_list_post_load_callbacks()
1380
1381Returns all the post load callback references.
1382
1383=item @list = test2_list_pre_subtest_callbacks()
1384
1385Returns all the pre-subtest callback references.
1386
1387=item test2_add_uuid_via(sub { ... })
1388
1389=item $sub = test2_add_uuid_via()
1390
1391This allows you to provide a UUID generator. If provided UUIDs will be attached
1392to all events, hubs, and contexts. This is useful for storing, tracking, and
1393linking these objects.
1394
1395The sub you provide should always return a unique identifier. Most things will
1396expect a proper UUID string, however nothing in Test2::API enforces this.
1397
1398The sub will receive exactly 1 argument, the type of thing being tagged
1399'context', 'hub', or 'event'. In the future additional things may be tagged, in
1400which case new strings will be passed in. These are purely informative, you can
1401(and usually should) ignore them.
1402
1403=back
1404
1405=head2 IPC AND CONCURRENCY
1406
1407These let you access, or specify, the IPC system internals.
1408
1409=over 4
1410
1411=item $bool = test2_has_ipc()
1412
1413Check if IPC is enabled.
1414
1415=item $ipc = test2_ipc()
1416
1417This will return the global L<Test2::IPC::Driver> instance. If this has not yet
1418been initialized it will be initialized now.
1419
1420=item test2_ipc_add_driver($DRIVER)
1421
1422Add an IPC driver to the list. This will add the driver to the start of the
1423list.
1424
1425=item @drivers = test2_ipc_drivers()
1426
1427Get the list of IPC drivers.
1428
1429=item $bool = test2_ipc_polling()
1430
1431Check if polling is enabled.
1432
1433=item test2_ipc_enable_polling()
1434
1435Turn on polling. This will cull events from other processes and threads every
1436time a context is created.
1437
1438=item test2_ipc_disable_polling()
1439
1440Turn off IPC polling.
1441
1442=item test2_ipc_enable_shm()
1443
1444Turn on IPC SHM. Only some IPC drivers use this, and most will turn it on
1445themselves.
1446
1447=item test2_ipc_set_pending($uniq_val)
1448
1449Tell other processes and events that an event is pending. C<$uniq_val> should
1450be a unique value no other thread/process will generate.
1451
1452B<Note:> After calling this C<test2_ipc_get_pending()> will return 1. This is
1453intentional, and not avoidable.
1454
1455=item $pending = test2_ipc_get_pending()
1456
1457This returns -1 if there is no way to check (assume yes)
1458
1459This returns 0 if there are (most likely) no pending events.
1460
1461This returns 1 if there are (likely) pending events. Upon return it will reset,
1462nothing else will be able to see that there were pending events.
1463
1464=item $timeout = test2_ipc_get_timeout()
1465
1466=item test2_ipc_set_timeout($timeout)
1467
1468Get/Set the timeout value for the IPC system. This timeout is how long the IPC
1469system will wait for child processes and threads to finish before aborting.
1470
1471The default value is C<30> seconds.
1472
1473=back
1474
1475=head2 MANAGING FORMATTERS
1476
1477These let you access, or specify, the formatters that can/should be used.
1478
1479=over 4
1480
1481=item $formatter = test2_formatter
1482
1483This will return the global formatter class. This is not an instance. By
1484default the formatter is set to L<Test2::Formatter::TAP>.
1485
1486You can override this default using the C<T2_FORMATTER> environment variable.
1487
1488Normally 'Test2::Formatter::' is prefixed to the value in the
1489environment variable:
1490
1491    $ T2_FORMATTER='TAP' perl test.t     # Use the Test2::Formatter::TAP formatter
1492    $ T2_FORMATTER='Foo' perl test.t     # Use the Test2::Formatter::Foo formatter
1493
1494If you want to specify a full module name you use the '+' prefix:
1495
1496    $ T2_FORMATTER='+Foo::Bar' perl test.t     # Use the Foo::Bar formatter
1497
1498=item test2_formatter_set($class_or_instance)
1499
1500Set the global formatter class. This can only be set once. B<Note:> This will
1501override anything specified in the 'T2_FORMATTER' environment variable.
1502
1503=item @formatters = test2_formatters()
1504
1505Get a list of all loaded formatters.
1506
1507=item test2_formatter_add($class_or_instance)
1508
1509Add a formatter to the list. Last formatter added is used at initialization. If
1510this is called after initialization a warning will be issued.
1511
1512=back
1513
1514=head1 OTHER EXAMPLES
1515
1516See the C</Examples/> directory included in this distribution.
1517
1518=head1 SEE ALSO
1519
1520L<Test2::API::Context> - Detailed documentation of the context object.
1521
1522L<Test2::IPC> - The IPC system used for threading/fork support.
1523
1524L<Test2::Formatter> - Formatters such as TAP live here.
1525
1526L<Test2::Event> - Events live in this namespace.
1527
1528L<Test2::Hub> - All events eventually funnel through a hub. Custom hubs are how
1529C<intercept()> and C<run_subtest()> are implemented.
1530
1531=head1 MAGIC
1532
1533This package has an END block. This END block is responsible for setting the
1534exit code based on the test results. This end block also calls the callbacks that
1535can be added to this package.
1536
1537=head1 SOURCE
1538
1539The source code repository for Test2 can be found at
1540F<http://github.com/Test-More/test-more/>.
1541
1542=head1 MAINTAINERS
1543
1544=over 4
1545
1546=item Chad Granum E<lt>exodist@cpan.orgE<gt>
1547
1548=back
1549
1550=head1 AUTHORS
1551
1552=over 4
1553
1554=item Chad Granum E<lt>exodist@cpan.orgE<gt>
1555
1556=back
1557
1558=head1 COPYRIGHT
1559
1560Copyright 2018 Chad Granum E<lt>exodist@cpan.orgE<gt>.
1561
1562This program is free software; you can redistribute it and/or
1563modify it under the same terms as Perl itself.
1564
1565See F<http://dev.perl.org/licenses/>
1566
1567=cut
1568