<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Test2::Manual::Anatomy::Context;
use strict;
use warnings;

our $VERSION = '0.000162';

1;

__END__

=head1 NAME

Test2::Manual::Anatomy::Context - Internals documentation for the Context
objects.

=head1 DESCRIPTION

This document explains how the L&lt;Test2::API::Context&gt; object works.

=head1 WHAT IS THE CONTEXT OBJECT?

The context object is one of the key components of Test2, and makes many
features possible that would otherwise be impossible. Every test tool starts by
getting a context, and ends by releasing the context. A test tool does all its
work between getting and releasing the context. The context instance is the
primary interface for sending events to the Test2 stack. Finally the context
system is responsible for tracking what file and line number a tool operates
on, which is critical for debugging.

=head2 PRIMARY INTERFACE FOR TEST TOOLS

Nearly every Test2 based tool should start by calling C&lt;$ctx =
Test2::API::context()&gt; in order to get a context object, and should end by
calling C&lt;&lt; $ctx-&gt;release() &gt;&gt;. Once a tool has its context object it can call
methods on the object to send events or have other effects. Nearly everything a
test tool needs to do should be done through the context object.

=head2 TRACK FILE AND LINE NUMBERS FOR ERROR REPORTING

When you call C&lt;Test2::API::Context&gt; a new context object will be returned. If
there is already a context object in effect (from a different point in the
stack) you will get a clone of the existing one. If there is not already a
current context then a completely new one will be generated. When a new context
is generated Test2 will determine the file name and line number for your test
code, these will be used when reporting any failures.

Typically the file and line number will be determined using C&lt;caller()&gt; to look
at your tools caller. The C&lt;$Test::Builder::Level&gt; will be respected if
detected, but is discouraged in favor of just using context objects at every
level.

When calling C&lt;Test2::API::Context()&gt; you can specify the
C&lt;&lt; level =&gt; $count &gt;&gt; arguments if you need to look at a deeper caller.

=head2 PRESERVE $?, $!, $^E AND $@

When you call C&lt;Test2::API::context()&gt; the current values of C&lt;$?&gt;, C&lt;$!&gt;,
C&lt;$^E&gt;, and C&lt;$@&gt; are stored in the context object itself. Whenever the context
is released the original values of these variables will be restored. This
protects the variables from any side effects caused by testing tools.

=head2 FINALIZE THE API STATE

L&lt;Test2::API&gt; works via a hidden singleton instance of L&lt;Test2::API::Instance&gt;.
The singleton has some state that is not set in stone until the last possible
minute. The last possible minute happens to be the first time a context is
acquired. State includes IPC instance, Formatter class, Root PID, etc.

=head2 FIND/CREATE THE CURRENT/ROOT HUB

L&lt;Test2&gt; has a stack of hubs, the stack can be accessed via
L&lt;Test2::API::test2_stack&gt;. When you get a context it will find the current
hub, if there is no current hub then the root one will be initialized.

=head2 PROVIDE HOOKS

There are hooks that run when contexts are created, found, and released. See
L&lt;Test2::API&gt; for details on these hooks and how to use them.

=head1 SEE ALSO

L&lt;Test2::Manual&gt; - Primary index of the manual.

=head1 SOURCE

The source code repository for Test2-Manual can be found at
F&lt;https://github.com/Test-More/Test2-Suite/&gt;.

=head1 MAINTAINERS

=over 4

=item Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;

=back

=head1 AUTHORS

=over 4

=item Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;

=back

=head1 COPYRIGHT

Copyright 2018 Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;.

This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

See F&lt;http://dev.perl.org/licenses/&gt;

=cut
</pre></body></html>